Skip to content

Latest commit

 

History

History
173 lines (114 loc) · 4.99 KB

pe.actions.md

File metadata and controls

173 lines (114 loc) · 4.99 KB

API Reference: pe.actions

Classes

  • class pe.actions.Action()

    Base class for all semantic actions. Not meant to be used directly.

    Each action used when parsing is an instance of a subclass of Action. If a user provides a grammar with an action that is not such an instance, it must be a callable and it will be wrapped in a Call instance.

    Custom actions may be created by subclassing Action and defining a call method as follows:

    class MyAction(Action):
        def __call__(self,
                     s: str,
                     pos: int,
                     end: int,
                     value: Value,
                     args: Tuple,
                     kwargs: Dict) -> Tuple[Tuple, Dict]:
            ...
            return myargs, mykwargs

    The s parameter is for the string being parsed, while pos and end are for the starting and ending positions of the match in s.

    The value parameter is a flag indicating the [value type] of the expression. Currently this is only used internally for the Bind action.

    The args and kwargs parameters are for the tuple of emitted values and dictionary of bound values, respectively.

    The return values myargs and mykwargs are the tuple of emitted values and dictionary of bound values returned by the expression. Normally myargs will have one element and mykwargs is empty, but both are returned to accommodate custom action types.

    These parameter names will be reused below to describe the behavior of the actions.

  • class pe.actions.Call (func)

    Call func with the emitted and bound values as follows:

    func(*args, **kwargs)

    This class is useful for functions that take zero or more arguments and possibly keyword arguments. For example, when constructing a datetime object from integer components.

  • class pe.actions.Pack (func)

    Call func with the emitted values packed as a tuple and the bound values as follows:

    func(args, **kwargs)

    This class is useful for functions that take exactly one iterable argument, possibly with keyword arguments, such as list or dict.

  • class pe.actions.Pair (func)

    Call func with the paired off emitted values and the bound values as follows:

    func(zip(args[::2], args[1::2]), **kwargs)

    This class is useful for functions that take exactly one iterable argument, possibly with keyword arguments, such as list or dict.

  • class pe.actions.Bind (name)

    Bind the determined emitted value to name as follows:

    mykwargs[name] = determine(args)

    The determine() function is only used internally, but it is described by the specification here.

    Binding is useful for functions that obligatorily take keyword arguments and occasionally for mapping arguments to functions where the argument order is different than the parsing order.

  • class pe.actions.Capture (func)

    Call func with the substring as its only argument as follows:

    func(s[pos:end])

    This action always takes the full substring between pos and end and ignores any values emitted or bound by subexpressions. This contrasts with Join, which combines only emitted string values and includes bound values.

  • class pe.actions.Join (func, sep='')

    Call func with the string formed by joining all emitted values with sep. This assumes that all emitted values are strings.

    func(sep.join(args), **kwargs)

    This action is useful for filtering out substrings, such as for escaped newlines in multiline strings as in Python or TOML. Also, unlike Capture, this action passes any bound values to func.

  • class pe.actions.Constant (value)

    Return value.

    Occasionally the presence of a match is enough to emit a value but the content of the match is irrelevant. This action always emits value if the expression succeeds.

  • class pe.actions.Getter (i)

    Return the ith emitted value.

    All other emitted and bound values are discarded.

  • class pe.actions.Fail (message)

    Raise a ParseError immediately.