Package lepl :: Package matchers :: Module derived
[hide private]
[frames] | no frames]

Module derived

source code

Matchers that are defined in terms of other matchers (ie the majority).
Functions [hide private]
 
Repeat(matcher, start=0, stop=None, limit=None, algorithm=DEPTH_FIRST, separator=None, add_=False, reduce=None)
This is called by the [] operator.
source code
 
Apply(matcher, function, raw=False, args=False)
Apply an arbitrary function to the results of the matcher (>, >=).
source code
 
args(function)
A decorator that has the same effect as ApplyArgs for functions/methods.
source code
 
KApply(matcher, function, raw=False)
Apply an arbitrary function to named arguments (**).
source code
 
AnyBut(exclude=None)
Match any character except those specified (or, if a matcher is used as the exclude, if the matcher fails).
source code
 
Optional(matcher)
Match zero or one instances of a matcher ([0:1]).
source code
 
Star(matcher)
Match zero or more instances of a matcher ([0:])
source code
 
ZeroOrMore(matcher)
Match zero or more instances of a matcher ([0:])
source code
 
Plus(matcher)
Match one or more instances of a matcher ([1:])
source code
 
OneOrMore(matcher)
Match one or more instances of a matcher ([1:])
source code
 
Map(matcher, function)
Apply an arbitrary function to each of the tokens in the result of the matcher (>>).
source code
 
Reduce(matcher, zero, join=__add__)
Combine the results from the matcher using reduce(join, results, zero).
source code
 
add(_stream, matcher)
The transformation used in Add - we carefully use "+" in as generic a manner as possible.
source code
 
Add(matcher)
Join tokens in the result using the "+" operator (+).
source code
 
Join(*matchers)
Combine many matchers together with Add(And(...)).
source code
 
Drop(matcher)
Do the match, but return nothing (~).
source code
 
Substitute(matcher, value)
Replace each return value with that given.
source code
 
Name(matcher, name)
Name the result of matching (> name)
source code
 
Identity(matcher)
Functions identically to the matcher given as an argument.
source code
 
Newline()
Match newline (Unix) or carriage return newline (Windows)
source code
 
Space(space=' \t')
Match a single space (by default space or tab).
source code
 
Whitespace(space=whitespace)
Match a single space (by default from string.whitespace, which includes newlines).
source code
 
Digit()
Match any single digit.
source code
 
Letter()
Match any ASCII letter (A-Z, a-z).
source code
 
Upper()
Match any ASCII uppercase letter (A-Z).
source code
 
Lower()
Match any ASCII lowercase letter (A-Z).
source code
 
Printable()
Match any printable character (string.printable).
source code
 
Punctuation()
Match any punctuation character (string.punctuation).
source code
 
UnsignedInteger()
Match a simple sequence of digits.
source code
 
SignedInteger()
Match a sequence of digits with an optional initial sign.
source code
 
Integer()
The default integer is a signed one.
source code
 
UnsignedReal(decimal='.')
Match a sequence of digits that may include a decimal point.
source code
 
SignedReal(decimal='.')
Match a signed sequence of digits that may include a decimal point.
source code
 
UnsignedEReal(decimal='.', exponent='eE')
Match an UnsignedReal followed by an optional exponent (e+02 etc).
source code
 
SignedEReal(decimal='.', exponent='eE')
Match a SignedReal followed by an optional exponent (e+02 etc).
source code
 
Real(decimal='.', exponent='eE')
The default float is signed with exponents.
source code
 
UnsignedFloat(decimal='.')
Match a sequence of digits that must include a decimal point.
source code
 
SignedFloat(decimal='.')
Match a signed sequence of digits that must include a decimal point.
source code
 
UnsignedEFloat(decimal='.', exponent='eE')
As UnsignedEReal, but must contain a decimal or exponent.
source code
 
SignedEFloat(decimal='.', exponent='eE')
As SignedEReal, but must contain a decimal or exponent.
source code
 
Float(decimal='.', exponent='eE')
The default float matcher accepts signed values with optional exponents.
source code
 
Word(chars=NfaRegexp('[^%s]' % whitespace), body=None)
Match a sequence of non-space characters, joining them together.
source code
 
DropEmpty(matcher)
Drop results if they are empty (ie if they are False in Python).
source code
 
Literals(*matchers)
A series of literals, joined with Or.
source code
 
String(quote='"', escape='\\', empty='', join=__add__)
Match a string with quotes that can be escaped.
source code
 
SingleLineString(quote='"', escape='\\', exclude='\n', empty='', join=__add__)
Like String, but will not match across multiple lines.
source code
 
SkipString(quote='"', escape='\\', ignore='\n', empty='', join=__add__)
Like String, matching across multiple lines, but will silently drop newlines.
source code
 
SkipTo(matcher, include=True)
Consume everything up to (and including, if include is True, as it is by default) the matcher.
source code
Variables [hide private]
  Eos = Eof
Match the end of a stream.
  _FLOAT_WARN = '''WARNING: The definition of the Float matchers...
Function Details [hide private]

Repeat(matcher, start=0, stop=None, limit=None, algorithm=DEPTH_FIRST, separator=None, add_=False, reduce=None)

source code 

This is called by the [] operator. It repeats the given matcher between start and stop number of times (inclusive).

If limit is given it is an upper limit on the number of different results returned on backtracking.

algorithm selects the repeat algorithm to use.

If separator is given then each repetition is separated by that matcher.

If add_ is true then the results are joined with Add (once all results are obtained).

If reduce is given it should be a pair (zero, join) where join(results, next) is used to accumulate results and zero is the initial value of results. This is implemented via Reduce.

reduce and add_ cannot be given together.

Apply(matcher, function, raw=False, args=False)

source code 

Apply an arbitrary function to the results of the matcher (>, >=).

Apply can be used via the standard operators by placing > (or >= to set raw=True, or * to set args=True) to the right of a matcher.

If the function is a TransformationWrapper it is used directly. Otherwise a TransformationWrapper is constructed via the raw and args parameters, as described below.

Note: The creation of named pairs (when a string argument is used) behaves more like a mapping than a single function invocation. If several values are present, several pairs will be created.

Note: There is an asymmetry in the default values of raw and args. If the identity function is used with the default settings then a list of results is passed as a single argument (args=False). That is then returned (by the identity) as a list, which is wrapped in an additional list (raw=False), giving an extra level of grouping. This is necessary because Python's list() is an identity for lists, but we want it to add an extra level of grouping so that nested S-expressions are easy to generate.

See also Map.

Parameters:
  • matcher - The matcher whose results will be modified.
  • function - The modification to apply.

    If a Transformation, this is used directly.

    If a string is given, named pairs will be created (and raw and args ignored).

    Otherwise the function should expect a list of results (unless args=True in which case the list is supplied as *args) and can return any value (unless raw=True in which case it should return a list).

  • raw - If True the results are used directly. Otherwise they are wrapped in a list. The default is False --- a list is added. This is set to true if the target function is an ApplyRaw instance.
  • args - If True, the results are passed to the function as separate arguments (Python's '*args' behaviour). The default is False --- the results are passed inside a list). This is set to true if the target function is an ApplyArgs instance.

KApply(matcher, function, raw=False)

source code 

Apply an arbitrary function to named arguments (**). The function should typically expect and return a list. It can be used indirectly by placing **= to the right of the matcher.

The function will be applied with the following keyword arguments:

stream_out
The stream returned from the matcher.
results
A list of the results returned.
Parameters:
  • matcher - The matcher whose results will be modified.
  • function - The modification to apply.
  • raw - If false (the default), the final return value from the function will be placed in a list and returned in a pair together with the new stream returned from the matcher (ie the function returns a single new result).

    If true, the final return value from the function is used directly and so should match the ([results], stream) type expected by other matchers.

AnyBut(exclude=None)

source code 

Match any character except those specified (or, if a matcher is used as the exclude, if the matcher fails).

The argument should be a list of tokens (or a string of suitable characters) to exclude, or a matcher. If omitted all tokens are accepted.

Map(matcher, function)

source code 

Apply an arbitrary function to each of the tokens in the result of the matcher (>>). If the function is a name, named pairs are created instead. It can be used indirectly by placing >> to the right of the matcher.

See also Apply.

Reduce(matcher, zero, join=__add__)

source code 
Combine the results from the matcher using reduce(join, results, zero). Unlike Add this will return a value (zero) when there are no matches.

Add(matcher)

source code 
Join tokens in the result using the "+" operator (+). This joins strings and merges lists. Unlike Reduce this will have no effect of there are no matches.

Join(*matchers)

source code 
Combine many matchers together with Add(And(...)). It can be used indirectly by placing + between matchers.

Drop(matcher)

source code 
Do the match, but return nothing (~). The ~ prefix is equivalent.

Name(matcher, name)

source code 

Name the result of matching (> name)

This replaces each value in the match with a tuple whose first value is the given name and whose second value is the matched token. The Node class recognises this form and associates such values with named attributes.

UnsignedReal(decimal='.')

source code 
Match a sequence of digits that may include a decimal point. This will match both integer and float values.

SignedReal(decimal='.')

source code 
Match a signed sequence of digits that may include a decimal point. This will match both integer and float values.

UnsignedEReal(decimal='.', exponent='eE')

source code 
Match an UnsignedReal followed by an optional exponent (e+02 etc). This will match both integer and float values.

SignedEReal(decimal='.', exponent='eE')

source code 
Match a SignedReal followed by an optional exponent (e+02 etc). This will match both integer and float values.

Real(decimal='.', exponent='eE')

source code 
The default float is signed with exponents.

UnsignedFloat(decimal='.')

source code 
Match a sequence of digits that must include a decimal point. This will match real values that are not integers.
Decorators:
  • @warn_on_use(_FLOAT_WARN)

SignedFloat(decimal='.')

source code 
Match a signed sequence of digits that must include a decimal point. This will match real values that are not integers.
Decorators:
  • @warn_on_use(_FLOAT_WARN)

UnsignedEFloat(decimal='.', exponent='eE')

source code 
As UnsignedEReal, but must contain a decimal or exponent. This will match real values that are not integers.
Decorators:
  • @warn_on_use(_FLOAT_WARN)

SignedEFloat(decimal='.', exponent='eE')

source code 
As SignedEReal, but must contain a decimal or exponent. This will match real values that are not integers.
Decorators:
  • @warn_on_use(_FLOAT_WARN)

Float(decimal='.', exponent='eE')

source code 
The default float matcher accepts signed values with optional exponents. This will not match integers - see Real for that.
Decorators:
  • @warn_on_use(_FLOAT_WARN)

Word(chars=NfaRegexp('[^%s]' % whitespace), body=None)

source code 

Match a sequence of non-space characters, joining them together.

chars and body, if given as strings, define possible characters to use for the first and rest of the characters in the word, respectively. If body is not given, then chars is used for the entire word. They can also specify matchers, which typically should match only a single character.

So Word(Upper(), Lower()) would match names that being with an upper case letter, for example, while Word(AnyBut(Space())) (the default) matches any sequence of non-space characters.

DropEmpty(matcher)

source code 

Drop results if they are empty (ie if they are False in Python).

This will drop empty strings and lists. It will also drop Node instances if they are empty (since the length is then zero).

String(quote='"', escape='\\', empty='', join=__add__)

source code 

Match a string with quotes that can be escaped. This will match across newlines (see SingleLineString for an alternative).

More generally, a string is a grouping of results. Setting empty and join correctly will allow this matcher to work with a variety of types.

SkipTo(matcher, include=True)

source code 
Consume everything up to (and including, if include is True, as it is by default) the matcher. Returns all the skipped data, joined.

Variables Details [hide private]

Eos

Match the end of a stream. Returns nothing.
Value:
Eof

_FLOAT_WARN

Value:
'''WARNING: The definition of the Float matchers changed in Lepl 4.4.0
(you may want to use Real instead).'''