Package lepl :: Package stream :: Module core :: Class StreamHelper
[hide private]
[frames] | no frames]

Class StreamHelper

source code


The interface that all helpers should implement.
Instance Methods [hide private]
 
__init__(self, id=None, factory=None, max=None, global_kargs=None, cache_level=None) source code
 
__repr__(self)
Simplify for comparison in tests
source code
 
__eq__(self, other) source code
 
__hash__(self) source code
 
key(self, state, other)
Generate an object that can be hashed (implements __hash__ and __eq__).
source code
 
kargs(self, state, prefix='', kargs=None)
Generate a dictionary of values that describe the stream.
source code
 
fmt(self, state, template, prefix='', kargs=None)
fmt a message using the expensive kargs function.
source code
 
debug(self, state)
Generate an inexpensive debug message.
source code
 
next(self, state, count=1)
Return (value, stream) where value is the next value (or values if count > 1) from the stream and stream is advanced to the next character.
source code
 
join(self, state, *values)
Join sequences of values into a single sequence.
source code
 
empty(self, state)
Return true if no more data available.
source code
 
line(self, state, empty_ok)
Return (values, stream) where values correspond to something like "the rest of the line" from the current point and stream is advanced to the point after the line ends.
source code
 
len(self, state)
Return the remaining length of the stream.
source code
 
stream(self, state, value, id_=None, max=None)
Return a new stream that encapsulates the value given, starting at state.
source code
 
deepest(self)
Return a stream that represents the deepest match.
source code
 
delta(self, state)
Return the offset, lineno and char of the current point, relative to the entire stream, as a tuple.
source code
 
eq(self, state1, state2)
Are the two states equal?
source code
 
new_max(self, state)
Return (old max, new stream), where new stream uses a new max.
source code
 
cacheable(self)
Is this stream cacheable?
source code
Method Details [hide private]

key(self, state, other)

source code 
Generate an object that can be hashed (implements __hash__ and __eq__). See HashKey.

kargs(self, state, prefix='', kargs=None)

source code 

Generate a dictionary of values that describe the stream. These may be extended by subclasses. They are provided to syntax_error_kargs, for example.

prefix modifies the property names

kargs allows values to be provided. These are not overwritten, so if there is a name clash the provided value remains.

Note: Calculating this can be expensive; use only for error messages, not debug messages (that may be discarded).

The following names will be defined (at a minimum).

For these value the "global" prefix indicates the underlying stream when, for example, tokens are used (other values will be relative to the token). If tokens etc are not in use then global and non-global values will agree. - data: a line representing the data, highlighting the current offset - global_data: as data, but for the entire sequence - text: as data, but without a "[...]" at the end - global_text: as text, but for the entire sequence - type: the type of the sequence - global_type: the type of the entire sequence - global_offset: a 0-based index into the underlying sequence

These values are always local: - offset: a 0-based index into the sequence - rest: the data following the current point - repr: the current value, or <EOS> - str: the current value, or an empty string

These values are always global: - filename: a filename, if available, or the type - lineno: a 1-based line number for the current offset - char: a 1-based character count within the line for the current offset - location: a summary of the current location

next(self, state, count=1)

source code 

Return (value, stream) where value is the next value (or values if count > 1) from the stream and stream is advanced to the next character. Note that value is always a sequence (so if the stream is a list of integers, and `count`=1, then it will be a unitary list, for example).

Should raise StopIteration when no more data are available.

line(self, state, empty_ok)

source code 

Return (values, stream) where values correspond to something like "the rest of the line" from the current point and stream is advanced to the point after the line ends.

If empty_ok is true and we are at the end of a line, return an empty line, otherwise advance (and maybe raise a StopIteration).

len(self, state)

source code 
Return the remaining length of the stream. Streams of unknown length (iterables) should raise a TypeError.

stream(self, state, value, id_=None, max=None)

source code 

Return a new stream that encapsulates the value given, starting at state. IMPORTANT: the stream used is the one that corresponds to the start of the value.

For example:
(line, next_stream) = s_line(stream, False) token_stream = s_stream(stream, line) # uses stream, not next_stream

This is used when processing Tokens, for example, or columns (where fragments in the correct column area are parsed separately).

deepest(self)

source code 
Return a stream that represents the deepest match. The stream may be incomplete in some sense (it may not be possible to use it for parsing more data), but it will have usable fmt and kargs methods.

new_max(self, state)

source code 
Return (old max, new stream), where new stream uses a new max. This is used when we want to read from the stream without affecting the max (eg when looking ahead to generate tokens).