Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
Public Member Functions | Public Attributes | List of all members
apg_py.lib.ast.Ast Class Reference

A class for capturing the AST as the parser traverses the parse tree. More...

Public Member Functions

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...
 

Public Attributes

 parser
 
 input
 
 indexStack
 
 records
 
 nodes
 

Detailed Description

A class for capturing the AST as the parser traverses the parse tree.

Definition at line 69 of file ast.py.

Constructor & Destructor Documentation

◆ __init__()

def apg_py.lib.ast.Ast.__init__ (   self,
  parser 
)

Class constructor.

Parameters
parserthe parser object to attach this AST object to

Definition at line 72 of file ast.py.

Member Function Documentation

◆ add_callback()

def apg_py.lib.ast.Ast.add_callback (   self,
  name,
  callback 
)

Add a callback function to the named AST node.

Parameters
nameThe name of the node to add the callback to.
callbackThe 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
nameThe 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
statethe return value of a previous call to Ast.save_state()

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
dataUser-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
nameThe rule or UDT name of the node.
phrase_indexIndex of the first input character of the matched phrase.
phrase_lengthThe number of input characters matched.

Definition at line 144 of file ast.py.

Member Data Documentation

◆ indexStack

apg_py.lib.ast.Ast.indexStack

Definition at line 78 of file ast.py.

◆ input

apg_py.lib.ast.Ast.input

Definition at line 77 of file ast.py.

◆ nodes

apg_py.lib.ast.Ast.nodes

Definition at line 80 of file ast.py.

◆ parser

apg_py.lib.ast.Ast.parser

Definition at line 75 of file ast.py.

◆ records

apg_py.lib.ast.Ast.records

Definition at line 79 of file ast.py.


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.