|
Jalopy 1.0b10 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--de.hunsicker.jalopy.language.TreeWalker
Helper class to make implementing tree walkers easy.
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; } }
Field Summary | |
protected boolean |
stop
Indicates a stop. |
Constructor Summary | |
protected |
TreeWalker()
Creates a new TreeWalker object. |
Method Summary | |
void |
reset()
Resets the walker. |
protected void |
stop()
Stops the walking. |
abstract void |
visit(AST node)
Callback method that can be called for a node found. |
void |
walk(AST tree)
Starts the walking with the root node of the tree or node portion. |
protected void |
walkChildren(AST node)
Iterates over all children of the given node. |
protected void |
walkNode(AST node)
Walks over the given node. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected boolean stop
Constructor Detail |
protected TreeWalker()
Method Detail |
public void reset()
public abstract void visit(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(AST tree)
tree
- the tree to walk over.protected void stop()
reset()
protected void walkChildren(AST node)
node
- node to iterate over.protected void walkNode(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.
|
Jalopy 1.0b10 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |