Skip to content

Latest commit

 

History

History
121 lines (90 loc) · 3.88 KB

pe.operators.md

File metadata and controls

121 lines (90 loc) · 3.88 KB

API Reference: pe.operators

Classes

  • class pe.operators.SymbolTable (op, args)

    The SymbolTable class is only to assist with the programmatic construction of a grammar. As such, it has a very narrow usage pattern. Setting a value on an attribute associates the value (an expression of operators) with the attribute name. Regular attribute access always returns a Nonterminal operator with the attribute name, whether or not an expression is associated with the name. The SymbolTable object can then be passed when initializing a Grammar object as if a standard dictionary had been passed. In otherwords, both g1 and g2 below are equivalent:

    from pe import Grammar
    from pe.operators import SymbolTable, Optional, Capture, Sequence
    
    g1 = Grammar(definitions={
      'Float': Capture(
          Sequence(Nonterminal('INTEGER'),
                   Optional(Nonterminal('FRACTION')),
                   Optional(Nonterminal('EXPONENT')))),
      'INTEGER': ...,
      'FRACTION': ...,
      'EXPONENT': ...,
    })
    
    V = SymbolTable()
    V.Float = Capture(
        Sequence(V.INTEGER,
                 Optional(V.FRACTION),
                 Optional(V.EXPONENT)))
    V.INTEGER = ...
    V.FRACTION = ...
    V.EXPONENT = ...
    g2 = Grammar(definitions=V)

Operator Functions

Aside from the Regex and Debug operators below, the rest are described by the specification.

  • pe.operators.Dot()

  • pe.operators.Literal (string)

  • pe.operators.Class (chars)

  • pe.operators.Regex (pattern, flags=0)

    Describe a regular expression as a pe operator. The operator behaves as other terminals, namely that it has an empty value type. When parsing, if the operator leads to a match, only the matching substring of the regular expression is considered; capturing groups are ignored.

  • pe.operators.Sequence (*expressions)

  • pe.operators.Choice (*expressions)

  • pe.operators.Optional (expression)

  • pe.operators.Star (expression)

  • pe.operators.Plus (expression)

  • pe.operators.Repeat (expression, count=-1, min=0, max=-1)

  • pe.operators.Nonterminal (name)

  • pe.operators.And (expression)

  • pe.operators.Not (expression)

  • pe.operators.Capture (expression)

  • pe.operators.Bind (expression, name)

  • pe.operators.Rule (expression, action, name='<anonymous>')

  • pe.operators.Debug (expression)

    Match expression and return its results unchanged, but print a message to indicate that the operation has occurred. When all other operators are wrapped in Debug operators, the concrete syntax tree of a parse is displayed, at the cost of performance. This operator is not generally meant to be used manually and it has no representation in the grammar notation, but rather it is inserted semi-automatically by compiling a grammar with the pe.DEBUG flag.

  • pe.operators.AutoIgnore (expression)

    Interleave the "ignore" pattern around and between sequence items. The ignore pattern is passed to the parser and defaults to pe.patterns.DEFAULT_IGNORE.