Package lepl :: Package support :: Module node :: Class Node
[hide private]
[frames] | no frames]

Class Node

source code


A base class for AST nodes.

It is designed to be applied to a list of results, via >.

Nodes support both simple list--like behaviour:

>>> abc = Node('a', 'b', 'c')
>>> abc[1]
'b'
>>> abc[1:]
['b', 'c']
>>> abc[:-1]
['a', 'b']

and dict--like behaviour through attributes:

>>> fb = Node(('foo', 23), ('bar', 'baz'))
>>> fb.foo
[23]
>>> fb.bar
['baz']

Both mixed together:

>>> fb = Node(('foo', 23), ('bar', 'baz'), 43, 'zap', ('foo', 'again'))
>>> fb[:]
[23, 'baz', 43, 'zap', 'again']
>>> fb.foo
[23, 'again']

Note how ('name', value) pairs have a special meaning in the constructor. This is supported by the creation of "named pairs":

>>> letter = Letter() > 'letter'
>>> digit = Digit() > 'digit'
>>> example = (letter | digit)[:] > Node
>>> n = example.parse('abc123d45e')[0]
>>> n.letter
['a', 'b', 'c', 'd', 'e']
>>> n.digit
['1', '2', '3', '4', '5']

However, a named pair with a Node as a value is coerced into a subclass of Node with the given name (this keeps Nodes connected into a single tree and so simplifies traversal).

Instance Methods [hide private]
 
__init__(self, *args)
Expects a single list of arguments, as will be received if invoked with the > operator.
source code
 
__add_named_child(self, name, value)
Add a value associated with a name (either a named pair or the class of a Node subclass).
source code
 
__add_anon_child(self, value)
Add a nameless value.
source code
 
__add_attribute(self, name, value)
Attributes are associated with lists of (named) values.
source code
 
__dir__(self)
The names of all the attributes constructed from the results.
source code
 
__getitem__(self, index) source code
 
__iter__(self) source code
 
__str__(self)
str(x)
source code
 
__repr__(self)
repr(x)
source code
 
__len__(self) source code
 
__bool__(self) source code
 
__nonzero__(self) source code
 
_recursively_eq(self, other)
This compares two nodes by recursively comparing their contents.
source code
 
_constructor_args(self)
Regenerate the constructor arguments (returns (args, kargs)).
source code

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

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args)
(Constructor)

source code 
Expects a single list of arguments, as will be received if invoked with the > operator.
Overrides: object.__init__

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)

_recursively_eq(self, other)

source code 
This compares two nodes by recursively comparing their contents. It may be useful for testing, for example, but care should be taken to avoid its use on cycles of objects.

_constructor_args(self)

source code 
Regenerate the constructor arguments (returns (args, kargs)).
Overrides: graph.ConstructorGraphNode._constructor_args