A class for capturing the AST as the parser traverses the parse tree.
More...
|
def | __init__ (self, parser) |
| Class constructor. More...
|
|
def | copy (self) |
| Make a copy suitable for adding callback functions and doing translations. More...
|
|
def | add_callback (self, name, callback) |
| Add a callback function to the named AST node. More...
|
|
def | down (self, name) |
| Saves an AST record as the parser traverses down through a node with an assigned callback function. More...
|
|
def | up (self, name, phrase_index, phrase_length) |
| Saves an AST record as the parser traverses up through a node with an assigned callback function. More...
|
|
def | save_state (self) |
| Saves the state of the AST. More...
|
|
def | restore_state (self, state) |
| Restores the AST to a previously saved state. More...
|
|
def | clear (self) |
| Clear the AST for reuse by the parser. More...
|
|
def | translate (self, data=None) |
| Do a depth-first traversal of the AST nodes, calling user-supplied callback functions 1) if a record for the node has been created and 2) if the user has attached a callback function to the node with Ast.add_callback(). More...
|
|
A class for capturing the AST as the parser traverses the parse tree.
Definition at line 69 of file ast.py.
◆ __init__()
def apg_py.lib.ast.Ast.__init__ |
( |
|
self, |
|
|
|
parser |
|
) |
| |
Class constructor.
- Parameters
-
parser | the parser object to attach this AST object to |
Definition at line 72 of file ast.py.
◆ add_callback()
def apg_py.lib.ast.Ast.add_callback |
( |
|
self, |
|
|
|
name, |
|
|
|
callback |
|
) |
| |
Add a callback function to the named AST node.
- Parameters
-
name | The name of the node to add the callback to. |
callback | The callback function to add to the node. The function should have the prototype:
fn(state, input, index, length, data)
- state - SEM_PRE for down, SEM_POST for up (see identifiers.py)
- input - the parser's input string as a tuple of integers/character codes
- index - the index of the first character of the matched phrase
- length - the number of characters in the matched phrase
- data - the user-supplied data (see Ast.translate()
|
Definition at line 109 of file ast.py.
◆ clear()
def apg_py.lib.ast.Ast.clear |
( |
|
self | ) |
|
Clear the AST for reuse by the parser.
Definition at line 188 of file ast.py.
◆ copy()
def apg_py.lib.ast.Ast.copy |
( |
|
self | ) |
|
Make a copy suitable for adding callback functions and doing translations.
This copy will not interact with the parser. The primary purpose of this is so the class ApgExp (exp.py) can internally do an AST translation of the parsed results and also return a copy of the AST for the user to do a separate, independent translation of the pattern-matched results.
Definition at line 86 of file ast.py.
◆ down()
def apg_py.lib.ast.Ast.down |
( |
|
self, |
|
|
|
name |
|
) |
| |
Saves an AST record as the parser traverses down through a node with an assigned callback function.
- Parameters
-
name | The rule or UDT name of the node. |
Definition at line 125 of file ast.py.
◆ restore_state()
def apg_py.lib.ast.Ast.restore_state |
( |
|
self, |
|
|
|
state |
|
) |
| |
Restores the AST to a previously saved state.
Should be called by the RNM operators, for example, if the branch below fails.
- Parameters
-
Definition at line 178 of file ast.py.
◆ save_state()
def apg_py.lib.ast.Ast.save_state |
( |
|
self | ) |
|
Saves the state of the AST.
Should be called by the RNM operators so that the state can be restored should the branch below fail. The state is a list saving off the length of the list of records and the index stack.
Definition at line 170 of file ast.py.
◆ translate()
def apg_py.lib.ast.Ast.translate |
( |
|
self, |
|
|
|
data = None |
|
) |
| |
Do a depth-first traversal of the AST nodes, calling user-supplied callback functions 1) if a record for the node has been created and 2) if the user has attached a callback function to the node with Ast.add_callback().
- Parameters
-
data | User-supplied data which is made available to the callback functions but is otherwise ignored by the AST. |
Definition at line 193 of file ast.py.
◆ up()
def apg_py.lib.ast.Ast.up |
( |
|
self, |
|
|
|
name, |
|
|
|
phrase_index, |
|
|
|
phrase_length |
|
) |
| |
Saves an AST record as the parser traverses up through a node with an assigned callback function.
Completes the matching "down" record with the information that was not available during the downward traversal of the node.
- Parameters
-
name | The rule or UDT name of the node. |
phrase_index | Index of the first input character of the matched phrase. |
phrase_length | The number of input characters matched. |
Definition at line 144 of file ast.py.
◆ indexStack
apg_py.lib.ast.Ast.indexStack |
◆ input
◆ nodes
◆ parser
apg_py.lib.ast.Ast.parser |
◆ records
apg_py.lib.ast.Ast.records |
The documentation for this class was generated from the following file:
Python APG, Version 1.0, is licensed under the
2-Clause BSD License,
an
Open Source Initiative Approved License.