Jalopy 1.0b10

de.hunsicker.jalopy.language
Class TreeWalker

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

public abstract class TreeWalker
extends 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(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;
     }
 }
 

Version:
$Revision: 1.2 $
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(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

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(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(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(AST node)
Iterates over all children of the given node.
Parameters:
node - node to iterate over.

walkNode

protected void walkNode(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.

Jalopy 1.0b10

Submit a bug or feature.

For further information and documentation, visit the official Jalopy website.
This page generated: November 14 2002