Package lepl :: Package matchers :: Module operators :: Class OperatorMixin
[hide private]
[frames] | no frames]

Class OperatorMixin

source code


Define the operators used to combine elements in a grammar specification.
Instance Methods [hide private]
 
__init__(self, name, namespace)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
__add__(self, other)
self + other - Join strings, merge lists.
source code
 
__radd__(self, other)
other + self - Join strings, merge lists.
source code
 
__and__(self, other)
self & other - Append results.
source code
 
__rand__(self, other)
other & self - Append results.
source code
 
__div__(self, other)
For 2.6
source code
 
__rdiv__(self, other)
For 2.6
source code
 
__truediv__(self, other)
self / other - Append results, with optional separating space.
source code
 
__rtruediv__(self, other)
other / self - Append results, with optional separating space.
source code
 
__floordiv__(self, other)
self // other - Append results, with required separating space.
source code
 
__rfloordiv__(self, other)
other // self - Append results, with required separating space.
source code
 
__or__(self, other)
self | other - Try alternative matchers.
source code
 
__ror__(self, other)
other | self - Try alternative matchers.
source code
 
__mod__(self, other)
self % other - Take first match (committed choice).
source code
 
__rmod__(self, other)
other % self - Take first match (committed choice).
source code
 
__invert__(self)
~self - Discard the result.
source code
 
__getitem__(self, indices)
self[start:stop:algorithm, separator, ...] - Repetition and lists.
source code
 
__gt__(self, function)
self > function - Process or label the results.
source code
 
__ge__(self, function)
self >= function - Process or label the results.
source code
 
__rshift__(self, function)
self >> function - Process or label the results (map).
source code
 
__pow__(self, function)
self ** function - Process the results (**kargs).
source code
 
__xor__(self, message)
self ^ message
source code
 
__check(self, name, other, is_match)
Provide some diagnostics if the syntax is completely mixed up.
source code

Inherited from support.context.NamespaceMixin (private): _lookup

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, name, namespace)
(Constructor)

source code 
x.__init__(...) initializes x; see help(type(x)) for signature
Overrides: object.__init__
(inherited documentation)

__add__(self, other)
(Addition operator)

source code 

self + other - Join strings, merge lists.

Combine adjacent matchers in sequence, merging the result with "+" (so strings are joined, lists merged).

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__radd__(self, other)
(Right-side addition operator)

source code 

other + self - Join strings, merge lists.

Combine adjacent matchers in sequence, merging the result with "+" (so strings are joined, lists merged).

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__and__(self, other)
(And operator)

source code 

self & other - Append results.

Combine adjacent matchers in sequence. This is equivalent to And().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__rand__(self, other)

source code 

other & self - Append results.

Combine adjacent matchers in sequence. This is equivalent to And().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__truediv__(self, other)

source code 

self / other - Append results, with optional separating space.

Combine adjacent matchers in sequence, with an optional space between them. The space is included in the results.

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__rtruediv__(self, other)

source code 

other / self - Append results, with optional separating space.

Combine adjacent matchers in sequence, with an optional space between them. The space is included in the results.

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__floordiv__(self, other)

source code 

self // other - Append results, with required separating space.

Combine adjacent matchers in sequence, with a space between them. The space is included in the results.

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__rfloordiv__(self, other)

source code 

other // self - Append results, with required separating space.

Combine adjacent matchers in sequence, with a space between them. The space is included in the results.

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__or__(self, other)
(Or operator)

source code 

self | other - Try alternative matchers.

This introduces backtracking. Matches are tried from left to right and successful results returned (one on each "recall"). This is equivalent to Or().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__ror__(self, other)

source code 

other | self - Try alternative matchers.

This introduces backtracking. Matches are tried from left to right and successful results returned (one on each "recall"). This is equivalent to Or().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__mod__(self, other)

source code 

self % other - Take first match (committed choice).

Matches are tried from left to right and the first successful result is returned. This is equivalent to First().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__rmod__(self, other)

source code 

other % self - Take first match (committed choice).

Matches are tried from left to right and the first successful result is returned. This is equivalent to First().

Parameters:
  • other - Another matcher or a string that will be converted to a literal match.

__invert__(self)

source code 

~self - Discard the result.

This generates a matcher that behaves as the original, but returns an empty list. This is equivalent to Drop().

Note that Lookahead() overrides this method to have different semantics (negative lookahead).

__getitem__(self, indices)
(Indexing operator)

source code 

self[start:stop:algorithm, separator, ...] - Repetition and lists.

This is a complex statement that modifies the current matcher so that it matches several times. A separator may be specified (eg for comma-separated lists) and the results may be combined with "+" (so repeated matching of characters would give a word).

start:stop:algorithm

This controls the number of matches made and the order in which different numbers of matches are returned.

[start]
Repeat exactly start times
[start:stop]
Repeat start to stop times (starting with as many matches as possible, and then decreasing as necessary).
[start:stop:algorithm]

Direction selects the algorithm for searching.

'b' (BREADTH_FIRST)
A breadth first search is used, which tends to give shorter matches before longer ones. This tries all possible matches for the sub-matcher first (before repeating calls to consume more of the stream). If the sub-matcher does not backtrack then this guarantees that the number of matches returned will not decrease (ie will monotonically increase) on backtracking.
'd' (DEPTH_FIRST)
A depth first search is used, which tends to give longer matches before shorter ones. This tries to repeats matches with the sub-matcher, consuming as much of the stream as possible, before backtracking to find alternative matchers. If the sub-matcher does not backtrack then this guarantees that the number of matches returned will not increase (ie will monotonically decrease) on backtracking.
'g' (GREEDY)
An exhaustive search is used, which finds all results (by breadth first search) and orders them by length before returning them ordered from longest to shortest. This guarantees that the number of matches returned will not increase (ie will monotonically decrease) on backtracking, but can consume a lot of resources.
'n' (NON_GREEDY)
As for 'g' (GREEDY), but results are ordered shortest to longest. This guarantees that the number of matches returned will not decrease (ie will monotonically increase) on backtracking, but can consume a lot of resources,

Values may be omitted; the defaults are: start = 0, stop = infinity, algorithm = 'd' (DEPTH_FIRST).

separator
If given, this must appear between repeated values. Matched separators are returned as part of the result (unless, of course, they are implemented with a matcher that returns nothing). If separator is a string it is converted to a literal match.
...
If ... (an ellipsis) is given then the results are joined together with "+".

Examples

Any()[0:3,...] will match 3 or less characters, joining them together so that the result is a single string.

Word()[:,','] will match a comma-separated list of words.

value[:] or value[0:] or value[0::'d'] is a "greedy" match that, if value does not backtrack, is equivalent to the "*" in a regular expression. value[::'n'] is the "non-greedy" equivalent (preferring as short a match as possible) and value[::'g'] is greedy even when value does provide alternative matches on backtracking.

__gt__(self, function)
(Greater-than operator)

source code 

self > function - Process or label the results.

Create a named pair or apply a function to the results. This is equivalent to Apply().

Parameters:
  • function - This can be a string or a function.

    If a string is given each result is replaced by a (name, value) pair, where name is the string and value is the result.

    If a function is given it is called with the results as an argument. The return value is used within a list as the new result. This is equivalent to Apply() with raw=False.

__ge__(self, function)
(Greater-than-or-equals operator)

source code 

self >= function - Process or label the results.

Apply a function to the results. This is equivalent to Apply(raw=True).

Parameters:
  • function - This is called with the results as an argument. The return value is used as the new result. This is equivalent to Apply() with raw=True.

__rshift__(self, function)

source code 

self >> function - Process or label the results (map).

Create a named pair or apply a function to each result in turn. This is equivalent to Map(). It is similar to self >= function, except that the function is applied to each result in turn.

Parameters:
  • function - This can be a string or a function.

    If a string is given each result is replaced by a (name, value) pair, where name is the string and value is the result.

    If a function is given it is called with each result in turn. The return values are used as the new result.

__pow__(self, function)

source code 

self ** function - Process the results (**kargs).

Apply a function to keyword arguments This is equivalent to KApply().

Parameters:
  • function - A function that is called with the keyword arguments described below. The return value is used as the new result.

    Keyword arguments:

    stream_in
    The stream passed to the matcher.
    stream_out
    The stream returned from the matcher.
    results
    A list of the results returned.

__xor__(self, message)
(Exclusive-Or operator)

source code 

self ^ message

Raise a SytaxError.

Parameters:
  • message - The message for the SyntaxError.