de.hunsicker.jalopy.language
Class TreeWalker

java.lang.Object
  |
  +--de.hunsicker.jalopy.language.TreeWalker
Direct Known Subclasses:
CodeInspector

public abstract class TreeWalker
extends java.lang.Object

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(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(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;
     }
 }
 

Version:
$Revision: 1.2.2.1 $
Author:
Marco Hunsicker

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(antlr.collections.AST node)
          Callback method that can be called for a node found.
 void walk(antlr.collections.AST tree)
          Starts the walking with the root node of the tree or node portion.
protected  void walkChildren(antlr.collections.AST node)
          Iterates over all children of the given node.
protected  void walkNode(antlr.collections.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

stop

protected boolean stop
Indicates a stop.

Constructor Detail

TreeWalker

protected TreeWalker()
Creates a new TreeWalker object.

Method Detail

reset

public void reset()
Resets the walker.


visit

public abstract void visit(antlr.collections.AST node)
Callback method that can be called for a node found. Overwrite to perform whatever action you want take place for a node.

In the default implementation, this method will be called for every node of the tree.

Parameters:
node - a node of the tree.

walk

public void walk(antlr.collections.AST tree)
Starts the walking with the root node of the tree or node portion.

Parameters:
tree - the tree to walk over.

stop

protected void stop()
Stops the walking. You have to reset the walker prior reusing it.

See Also:
reset()

walkChildren

protected void walkChildren(antlr.collections.AST node)
Iterates over all children of the given node.

Parameters:
node - node to iterate over.

walkNode

protected void walkNode(antlr.collections.AST node)
Walks over the given node. Links in children. Called for every single node of the tree. The default implemention simply calls
 visit(node);
 walkChildren();
 
to visit the current node and continue walking over all children of the node.

Parameters:
node - a node of the tree.


Copyright © 1997-2005 Jalopy. All Rights Reserved.