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

Module complex

source code

Complex matchers that are rearely used.
Functions [hide private]
 
PostMatch(matcher, condition, not_=False, equals=True, stream_factory=None)
Apply the condition to each result from the matcher.
source code
 
_Columns(indices, *matchers) source code
 
Columns(*columns, **kargs)
Match data in a set of columns.
source code
 
Iterate(matcher)
This isn't complex to implement, but conceptually is rather odd.
source code
Function Details [hide private]

PostMatch(matcher, condition, not_=False, equals=True, stream_factory=None)

source code 

Apply the condition to each result from the matcher. It should return either an exact match (equals=True) or simply not fail (equals=False). If not_ is set, the test is inverted.

matcher is coerced to Literal(), condition to DfaRegexp()

factory is used to generate a stream from the result. If not set the default factory is used.

Decorators:
  • @trampoline_matcher_factory(matcher= to(Literal), condition= to(DfaRegexp))

_Columns(indices, *matchers)

source code 
Decorators:
  • @trampoline_matcher_factory()

Columns(*columns, **kargs)

source code 

Match data in a set of columns.

This is a fairly complex matcher. It allows matchers to be associated with a range of indices (measured from the current point in the stream) and only succeeds if all matchers succeed. The results are returned in a list, in the same order as the matchers are specified.

A range if indices is given as a tuple (start, stop) which works like an array index. So (0, 4) selects the first four characters (like [0:4]). Alternatively, a number of characters can be given, in which case they start where the previous column finished (or at zero for the first).

The matcher for each column will see the (selected) input data as a separate stream. If a matcher should consume the entire column then it should check for Eos.

Finally, the skip parameter controls how data to "the right" of the columns is handled. If unset, the data are discarded (this functions as an additional, final, column that currently drops data). Data to "the left" are simply discarded.

Note: This does not support backtracking over the columns.

Iterate(matcher)

source code 

This isn't complex to implement, but conceptually is rather odd. It takes a single matcher and returns a result for each match as it consumes the input.

This means parse_all() is needed to retrieve the entire result (and there is no backtracking).

In practice this means that if you have a matcher whose top level is a repeating element (for example, lines in a file) then you can treat the entire parser as a lazy iterator over the input. The obvious application is with .config.low_memory() as this allows for large output to be generated without consuming a large amount of memory.

Decorators:
  • @trampoline_matcher_factory()