Package lepl
[hide private]
[frames] | no frames]

Package lepl

source code

Lepl is a parser library written in Python.

This is the API documentation; the module index is at the bottom of this page. There is also a manual which gives a higher level overview.

The home page for this package is the Lepl website.

Example

A simple example of how to use Lepl:

from lepl import *

# For a simpler result these could be replaced with 'list', giving
# an AST as a set of nested lists
# (ie replace '> Term' etc with '> list' below).

class Term(List): pass
class Factor(List): pass
class Expression(List): pass

def build():

    # Here we define the grammar

    # A delayed value is defined later (see penultimate line in block)
    expr   = Delayed()
    number = Digit()[1:,...]                        >> int

    # Allow spaces between items
    with DroppedSpace():
        term    = number | '(' & expr & ')'         > Term
        muldiv  = Any('*/')
        factor  = term & (muldiv & term)[:]         > Factor
        addsub  = Any('+-')
        expr   += factor & (addsub & factor)[:]     > Expression
        line    = Trace(expr) & Eos()

    return line.get_parse()

if __name__ == '__main__':
    parser = build()
    # parser returns a list of tokens, but line
    # returns a single value, so take the first entry
    print(parser('1 + 2 * (3 + 4 - 5)')[0])

Running this gives the result:

Expression
 +- Factor
 |   `- Term
 |       `- 1
 +- '+'
 `- Factor
     +- Term
     |   `- 2
     +- '*'
     `- Term
         +- '('
         +- Expression
         |   +- Factor
         |   |   `- Term
         |   |       `- 3
         |   +- '+'
         |   +- Factor
         |   |   `- Term
         |   |       `- 4
         |   +- '-'
         |   `- Factor
         |       `- Term
         |           `- 5
         `- ')'

Version: 5.1.3

Submodules [hide private]

Variables [hide private]
  SmartSeparator2
  Configuration
  ConfigBuilder
  GeneratorManager
  RecordDeepest
  TraceStack
  And
  Or
  First
  Difference
  Limit
  Any
  Delayed
  Literal
  Empty
  Lookahead
  Regexp
  PostMatch
  Columns
  Iterate
  Trace
  Apply
  args
  KApply
  Join
  AnyBut
  Optional
  Star
  ZeroOrMore
  Map
  Add
  Drop
  Repeat
  Plus
  OneOrMore
  Substitute
  Name
  Eof
  Eos
  Identity
  Newline
  Space
  Whitespace
  Digit
  Letter
  Upper
  Lower
  Printable
  Punctuation
  UnsignedInteger
  SignedInteger
  Integer
  UnsignedFloat
  SignedFloat
  UnsignedEFloat
  SignedEFloat
  Float
  UnsignedReal
  SignedReal
  UnsignedEReal
  SignedEReal
  Real
  Word
  DropEmpty
  Literals
  String
  SingleLineString
  SkipString
  SkipTo
  Error
  make_error
  raise_error
  RMemo
  LMemo
  MemoException
  Override
  Separator
  SmartSeparator1
  GREEDY
  NON_GREEDY
  DEPTH_FIRST
  BREADTH_FIRST
  DroppedSpace
  REDUCE
  function_matcher
  function_matcher_factory
  sequence_matcher
  sequence_matcher_factory
  trampoline_matcher
  trampoline_matcher_factory
  PostCondition
  Transform
  Assert
  TraceVariables
  Token
  LexerError
  RuntimeLexerError
  Block
  Line
  LineStart
  LineEnd
  constant_indent
  explicit
  to_right
  ContinuedLineFactory
  Extend
  NO_BLOCKS
  DEFAULT_POLICY
  RegexpError
  NfaRegexp
  DfaRegexp
  UnicodeAlphabet
  s_debug
  s_deepest
  s_delta
  s_empty
  s_eq
  s_factory
  s_fmt
  s_global_kargs
  s_id
  s_join
  s_kargs
  s_key
  s_len
  s_line
  s_max
  s_next
  s_stream
  FullFirstMatchException
  DEFAULT_STREAM_FACTORY
  List
  sexpr_fold
  sexpr_throw
  sexpr_flatten
  sexpr_to_tree
  Node
  make_dict
  join_with
  node_throw
  print_timing