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: 4.3.1

Submodules [hide private]

Variables [hide private]
  SmartSeparator2
  Configuration
  ConfigBuilder
  GeneratorManager
  RecordDeepest
  TraceResults
  And
  Or
  First
  Any
  Delayed
  Literal
  Empty
  Lookahead
  PostMatch
  Regexp
  Trace
  Commit
  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
  SignedEFloat
  Float
  Word
  DropEmpty
  Literals
  String
  SingleLineString
  SkipString
  SkipTo
  Columns
  Error
  make_error
  raise_error
  RMemo
  LMemo
  MemoException
  Override
  Separator
  SmartSeparator1
  GREEDY
  NON_GREEDY
  DEPTH_FIRST
  BREADTH_FIRST
  DroppedSpace
  function_matcher
  function_matcher_factory
  sequence_matcher
  sequence_matcher_factory
  trampoline_matcher
  trampoline_matcher_factory
  PostCondition
  TraceVariables
  Token
  LexerError
  RuntimeLexerError
  Indent
  LineAwareEol
  LineAwareSol
  BIndent
  Line
  Block
  BLine
  ContinuedLineFactory
  ContinuedBLineFactory
  Extend
  SOL
  EOL
  rightmost
  constant_indent
  RegexpError
  NfaRegexp
  DfaRegexp
  UnicodeAlphabet
  FullFirstMatchException
  DEFAULT_STREAM_FACTORY
  List
  sexpr_fold
  sexpr_throw
  Node
  make_dict
  join_with
  node_throw