Public Member Functions | |
def | __init__ |
def | getTokenType |
Using the map of token names to token types, return the type. | |
def | create |
Create a tree or node from the indicated tree pattern that closely follows ANTLR tree grammar tree element syntax:. | |
def | index |
Walk the entire tree and make a node name to nodes mapping. | |
def | find |
Return a list of matching token. | |
def | visit |
Visit every node in tree matching what, invoking the visitor. | |
def | parse |
Given a pattern like (ASSIGN lhs:ID rhs:. | |
def | equals |
Compare t1 and t2; return true if token types/text, structure match exactly. | |
Public Attributes | |
adaptor | |
tokenNameToTypeMap | |
Private Member Functions | |
def | _index |
Do the work for index. | |
def | _findTokenType |
Return a List of tree nodes with token type ttype. | |
def | _findPattern |
Return a List of subtrees matching pattern. | |
def | _visitType |
Do the recursive work for visit. | |
def | _visitPattern |
For all subtrees that match the pattern, execute the visit action. | |
def | _parse |
Do the work for parse. | |
def | _equals |
Must know about the names of tokens so you have to pass in a map or array of token names (from which this class can build the map). I.e., Token DECL means nothing unless the class can translate it to a token type.
In order to create nodes and navigate, this class needs a TreeAdaptor.
This class can build a token type -> node index for repeated use or for iterating over the various nodes with a particular type.
This class works in conjunction with the TreeAdaptor rather than moving all this functionality into the adaptor. An adaptor helps build and navigate trees using methods. This class helps you do it with string patterns like "(A B C)". You can create a tree from that pattern or match subtrees against it.
Definition at line 325 of file treewizard.py.
def antlr3.treewizard.TreeWizard.__init__ | ( | self, | ||
adaptor = None , |
||||
tokenNames = None , |
||||
typeMap = None | ||||
) |
Definition at line 327 of file treewizard.py.
def antlr3.treewizard.TreeWizard.getTokenType | ( | self, | ||
tokenName | ||||
) |
Using the map of token names to token types, return the type.
Definition at line 341 of file treewizard.py.
def antlr3.treewizard.TreeWizard.create | ( | self, | ||
pattern | ||||
) |
Create a tree or node from the indicated tree pattern that closely follows ANTLR tree grammar tree element syntax:.
(root child1 ... child2).
You can also just pass in a node: ID
Any node can have a text argument: ID[foo] (notice there are no quotes around foo--it's clear it's a string).
nil is a special name meaning "give me a nil node". Useful for making lists: (nil A B C) is a list of A B C.
Definition at line 364 of file treewizard.py.
def antlr3.treewizard.TreeWizard.index | ( | self, | ||
tree | ||||
) |
Walk the entire tree and make a node name to nodes mapping.
For now, use recursion but later nonrecursive version may be more efficient. Returns a dict int -> list where the list is of your AST node type. The int is the token type of the node.
Definition at line 378 of file treewizard.py.
def antlr3.treewizard.TreeWizard._index | ( | self, | ||
t, | ||||
m | ||||
) | [private] |
def antlr3.treewizard.TreeWizard.find | ( | self, | ||
tree, | ||||
what | ||||
) |
Return a list of matching token.
what may either be an integer specifzing the token type to find or a string with a pattern that must be matched.
Definition at line 410 of file treewizard.py.
def antlr3.treewizard.TreeWizard._findTokenType | ( | self, | ||
t, | ||||
ttype | ||||
) | [private] |
def antlr3.treewizard.TreeWizard._findPattern | ( | self, | ||
t, | ||||
pattern | ||||
) | [private] |
def antlr3.treewizard.TreeWizard.visit | ( | self, | ||
tree, | ||||
what, | ||||
visitor | ||||
) |
Visit every node in tree matching what, invoking the visitor.
If what is a string, it is parsed as a pattern and only matching subtrees will be visited. The implementation uses the root node of the pattern in combination with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. Patterns with wildcard roots are also not allowed.
If what is an integer, it is used as a token type and visit will match all nodes of that type (this is faster than the pattern match). The labels arg of the visitor action method is never set (it's None) since using a token type rather than a pattern doesn't let us set a label.
Definition at line 478 of file treewizard.py.
def antlr3.treewizard.TreeWizard._visitType | ( | self, | ||
t, | ||||
parent, | ||||
childIndex, | ||||
ttype, | ||||
visitor | ||||
) | [private] |
def antlr3.treewizard.TreeWizard._visitPattern | ( | self, | ||
tree, | ||||
pattern, | ||||
visitor | ||||
) | [private] |
For all subtrees that match the pattern, execute the visit action.
Definition at line 509 of file treewizard.py.
def antlr3.treewizard.TreeWizard.parse | ( | self, | ||
t, | ||||
pattern, | ||||
labels = None | ||||
) |
Given a pattern like (ASSIGN lhs:ID rhs:.
) with optional labels on the various nodes and '.' (dot) as the node/subtree wildcard, return true if the pattern matches and fill the labels Map with the labels pointing at the appropriate nodes. Return false if the pattern is malformed or the tree does not match.
If a node specifies a text arg in pattern, then that must match for that node in t.
Definition at line 542 of file treewizard.py.
def antlr3.treewizard.TreeWizard._parse | ( | self, | ||
t1, | ||||
tpattern, | ||||
labels | ||||
) | [private] |
Do the work for parse.
Check to see if the tpattern fits the structure and token types in t1. Check text if the pattern has text arguments on nodes. Fill labels map with pointers to nodes in tree matched against nodes in pattern with labels.
Definition at line 558 of file treewizard.py.
def antlr3.treewizard.TreeWizard.equals | ( | self, | ||
t1, | ||||
t2, | ||||
adaptor = None | ||||
) |
Compare t1 and t2; return true if token types/text, structure match exactly.
The trees are examined in their entirety so that (A B) does not match (A B C) nor (A (B C)).
Definition at line 600 of file treewizard.py.
def antlr3.treewizard.TreeWizard._equals | ( | self, | ||
t1, | ||||
t2, | ||||
adaptor | ||||
) | [private] |
Definition at line 608 of file treewizard.py.
Definition at line 328 of file treewizard.py.
Definition at line 330 of file treewizard.py.