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).
|
|
__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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inherited from object:
__delattr__,
__format__,
__getattribute__,
__hash__,
__new__,
__reduce__,
__reduce_ex__,
__setattr__,
__sizeof__,
__subclasshook__
|
|
Inherited from object:
__class__
|
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(x)
- Overrides:
object.__repr__
- (inherited documentation)
|
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.
|