expression's AST using this builder instance:: |
expression's AST using this builder instance:: |
|
|
>>> def pe(expr): |
>>> def pe(expr): |
... print parse_expr(expr, builder) |
... return parse_expr(expr, builder) |
|
|
|
|
Names and Constants |
Names and Constants |
NameError: fubar |
NameError: fubar |
|
|
|
|
|
Namespaces and Binding |
|
====================== |
|
|
|
An ``ExprBuilder`` object's ``bindings`` attribute is a list of dictionaries, |
|
mapping names to the desired outputs:: |
|
|
|
>>> builder.bindings |
|
[{}, {'a': Local('a'), 'c': Local('c'), 'b': Local('b'), 'e': Local('e'), |
|
'd': Local('d'), 'g': Local('g'), 'f': Local('f')}, |
|
{...}, {...}, {...}] |
|
|
|
You can add more bindings temporarily with the ``.push()`` method, then |
|
remove them with ``.pop()``:: |
|
|
|
>>> builder.push({'q': pe('42')}) |
|
|
|
>>> builder.Name('q') |
|
Const(42) |
|
|
|
>>> builder.pop() |
|
{'q': Const(42)} |
|
|
|
>>> builder.Name('q') |
|
Traceback (most recent call last): |
|
... |
|
NameError: q |
|
|
|
If you omit the argument to ``.push()``, it just adds an empty namespace to |
|
the bindings:: |
|
|
|
>>> builder.push() |
|
>>> builder.bindings |
|
[{}, {}, {'a': Local('a'), 'c': Local('c'), 'b': Local('b'), |
|
'e': Local('e'), 'd': Local('d'), 'g': Local('g'), |
|
'f': Local('f')}, {...}, {...}, {...}] |
|
|
|
Which you can then modify using ``.bind()``:: |
|
|
|
>>> builder.bind({'x': pe('99')}) |
|
>>> builder.bindings |
|
[{'x': Const(99)}, {}, {'a': Local('a'), 'c': Local('c'), 'b': Local('b'), |
|
'e': Local('e'), 'd': Local('d'), 'g': Local('g'), |
|
'f': Local('f')}, {...}, {...}, {...}] |
|
|
|
And finally remove with ``.pop()``:: |
|
|
|
>>> builder.pop() |
|
{'x': Const(99)} |
|
|
|
>>> builder.bindings |
|
[{}, {'a': Local('a'), 'c': Local('c'), 'b': Local('b'), 'e': Local('e'), |
|
'd': Local('d'), 'g': Local('g'), 'f': Local('f')}, |
|
{...}, {...}, {...}] |
|
|
|
|
Operators |
Operators |
========= |
========= |
|
|