public abstract class TreeWalker
extends java.lang.Object
To implement a class that walks over all nodes simply dereive from TreeWalker and
implement the callback method visit(de.hunsicker.antlr.collections.AST)
.
class MyWalker extends TreeWalker { // called for every node public void visit(AST node) { System.out.println("node visited: " + node); } }
If you want to control which nodes will be actually visited, overwrite walkNode(de.hunsicker.antlr.collections.AST)
too.
// visits only IMPORT nodes and quits after the last IMPORT node found protected void walkNode(AST node) { switch (node.getType()) { case JavaTokenTypes.ROOT: // skip to next child walkNode(node.getFirstChild()); break; case JavaTokenTypes.PACKAGE_DEF: // skip to next child walkNode(node.getNextSibling()); break; case JavaTokenTypes.IMPORT: // only visit root node, DON'T walk over it's children visit(node); // continue with the next node walkNode(node.getNextSibling()); break; case JavaTokenTypes.CLASS_DEF: // quit after the first non-IMPORT node return; } }
Modifier and Type | Field and Description |
---|---|
protected boolean |
stop
Indicates a stop.
|
Modifier | Constructor and Description |
---|---|
protected |
TreeWalker()
Creates a new TreeWalker object.
|
Modifier and Type | Method and Description |
---|---|
void |
reset()
Resets the walker.
|
protected void |
stop()
Stops the walking.
|
abstract void |
visit(de.hunsicker.antlr.collections.AST node)
Callback method that can be called for a node found.
|
void |
walk(de.hunsicker.antlr.collections.AST tree)
Starts the walking with the root node of the tree or node portion.
|
protected void |
walkChildren(de.hunsicker.antlr.collections.AST node)
Iterates over all children of the given node.
|
protected void |
walkNode(de.hunsicker.antlr.collections.AST node)
Walks over the given node.
|
public void reset()
public abstract void visit(de.hunsicker.antlr.collections.AST node)
In the default implementation, this method will be called for every node of the tree.
node
- a node of the tree.public void walk(de.hunsicker.antlr.collections.AST tree)
tree
- the tree to walk over.protected void stop()
reset()
protected void walkChildren(de.hunsicker.antlr.collections.AST node)
node
- node to iterate over.protected void walkNode(de.hunsicker.antlr.collections.AST node)
visit(node); walkChildren();to visit the current node and continue walking over all children of the node.
node
- a node of the tree.
Submit a bug or feature.
For further information and documentation, visit the official Jalopy website.
This page generated: March 30 2013