public class AstWalker extends java.lang.Object implements AstWalkListener
AstSymbolResolver
is updated to refer
to that source node, as well as to a companion copy node. The
companion copy node is retrieved using the source node's ID, from a map
which is provided at construction.
By convention, rules which modify AST nodes must operate only on copy AST nodes. Most rules which test state without making modifications operate on source AST nodes.
Instances of this class are used by the pattern engine to apply a pipeline of rulesets to a hierarchy of AST nodes.
PatternEngine
,
AstSymbolResolver
,
RuleSet
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Long,Aast> |
copies
Map of AST copy nodes, indexed by source AST IDs
|
private AstSymbolResolver |
resolver
Object used resolve AST-related expression symbols
|
private RuleSet |
ruleSet
The set of rules associated with this AST walker
|
Constructor and Description |
---|
AstWalker(RuleSet ruleSet,
java.util.Map<java.lang.Long,Aast> copies)
Constructor which accepts a ruleset and a map of AST copy nodes,
which are initially identical copies of source AST nodes.
|
Modifier and Type | Method and Description |
---|---|
void |
ascent(Aast ast)
Called whenever a transition from a child node to its parent takes
place during an AST walk.
|
private void |
configureResolver(Aast ast)
Configure the AST symbol resolver by storing in it the specified AST
as its current source AST and the associated copy AST as its current
copy AST.
|
void |
descent(Aast ast)
Called whenever a transition from a parent node to its first child
takes place during an AST walk.
|
void |
nextChild(Aast ast,
int index)
Called whenever a transition from a child node to its next right
sibling takes place during an AST walk.
|
java.util.Iterator<Aast> |
walk(AstSymbolResolver resolver,
Aast ast)
Fully walk the specified AST from root to every leaf, applying our
ruleset's walk-rules to each source-copy node combination encountered
in the walk.
|
java.util.Iterator<Aast> |
walk(AstSymbolResolver resolver,
Aast ast,
int maxLevels)
Walk the specified AST from root to a maximum node depth of
maxLevels , applying our ruleset's walk-rules to each
source-copy node combination encountered in the walk. |
java.util.Iterator<Aast> |
walk(AstSymbolResolver resolver,
java.util.Iterator<Aast> iter)
Traverse the specified iterator, applying our ruleset's walk-rules to
each source-copy node combination encountered in the walk.
|
private RuleSet ruleSet
private java.util.Map<java.lang.Long,Aast> copies
private AstSymbolResolver resolver
public AstWalker(RuleSet ruleSet, java.util.Map<java.lang.Long,Aast> copies)
ruleSet
- Ruleset which will be applied against the AST walked by this
walker.copies
- Map of writable AST (copy) nodes, indexed by the IDs of the
source AST nodes which they mirror.public java.util.Iterator<Aast> walk(AstSymbolResolver resolver, Aast ast)
resolver
- Symbol resolver used to resolve variables and user functions
for the logical expressions within the rules contained in the
ruleset.ast
- Root node of the AST hierarchy to be walked. Note that this
need not be the absolute root of the entire tree, but can be
any node in the tree. May not be null
.AstSymbolResolver.view
public java.util.Iterator<Aast> walk(AstSymbolResolver resolver, Aast ast, int maxLevels)
maxLevels
, applying our ruleset's walk-rules to each
source-copy node combination encountered in the walk.resolver
- Symbol resolver used to resolve variables and user functions
for the logical expressions within the rules contained in the
ruleset.ast
- Root node of the AST hierarchy to be walked. Note that this
need not be the absolute root of the entire tree, but can be
any node in the tree. May not be null
.maxLevels
- Maximum number of levels or generations to visit relative to
the root defined by ast
. If the tree contains
more levels than maxLevels
, the levels beyond a
relative depth of maxLevels
will be ignored. Set
to 0
to disable this restriction.AstSymbolResolver.view
public java.util.Iterator<Aast> walk(AstSymbolResolver resolver, java.util.Iterator<Aast> iter)
null
copy node is guaranteed to be provided to the resolver at each stop.
However, the copy node is not guaranteed to have a valid
state, nor to be located in the same relative location within its
containing hierarchy as its companion source node, nor even reside in
the tree at all. This is because previous rules may have changed its
state or removed it entirely from the tree.
As the tree is traversed, the ruleset's ascent and descent rules, if any, are applied each time the walk ascends to a parent node level or descends to a child node level, respectively. The respective rules are applied once per level traversed, even if an ascent moves from a deeply nested node back up to a sibling node of an ancestor several levels up. Note that the AST in the resolver's scope at the time descent rules are applied will always be the first child of the new level (descents always occur only one level at a time). However, on ascent, the AST in the resolver's scope will always be the next AST to be visited, which will
resolver
- Symbol resolver used to resolve variables and user functions
for the logical expressions within the rules contained in the
ruleset.iter
- Iterator which defines which AST nodes will be visited during
the traversal.AstSymbolResolver.view
public void ascent(Aast ast)
This method first configures the AST symbol resolver by storing in it the source and copy AST nodes which represent the parent node in the child to parent transition. It then applies the ascent rules, if any, configured for the current ruleset.
ascent
in interface AstWalkListener
ast
- Source AST node which represents the parent in the child to
parent transition of the current AST walk.public void descent(Aast ast)
This method first configures the AST symbol resolver by storing in it the source and copy AST nodes which represent the parent node in the parent to child transition. It then applies the descent rules, if any, configured for the current ruleset.
descent
in interface AstWalkListener
ast
- Source AST node which represents the parent in the parent to
child transition of the current AST walk.public void nextChild(Aast ast, int index)
This method is not invoked after the last child is visited,
as this is the purpose of the ascent(com.goldencode.ast.Aast)
method.
This method first configures the AST symbol resolver by storing in it the source and copy AST nodes which represent the parent node of the children between which the lateral transition is taking place. It then applies the next-child rules, if any, configured for the current ruleset.
nextChild
in interface AstWalkListener
ast
- Source AST node which represents the parent of the children
between which the lateral transition is taking place in the
current AST walk.index
- The 0-based index of the child which is about to be processed.private void configureResolver(Aast ast)
ast
- Source tree AST node to be set into the symbol resolver.