A NodeVisitor enables operations to be performed on a parse tree.
The Visitor design pattern is discussed in detail in Design Patterns
(ISBN 0-201-63361-2) by Gamma, Helm, Johnson and Vlissides.
The traditional operations performed on parse trees are type checking
and code generation. The Visitor allows those operations to all be
encapsulated into one place instead of having that functionality spread
out into every Node subclass. It also makes it easy to target multiple
languages by allowing any kind of code generation Visitor to be
designed.
When using a Visitor to traverse a parse tree, the code responsible
for moving the traversal into children nodes can either be placed in
the nodes or in the Visitor implementation. The NodeVisitor places that
responsibility onto NodeVisitor implementations because it is much
more flexible. As a result, all Nodes have the same simple implementation
for their "accept" method, but do not inherit it.
Every visit method in this interface returns an Object. The definition
of that returned Object is left up to the specific implementation of the
NodeVisitor. Most NodeVisitors can simply return null, but those that
are modifying a parse tree or are using one to create another can use the
returned Object to pass around newly created Nodes.
author: Brian S O'Neill version: 30 , 5/31/01 See Also: Node.accept(NodeVisitor) |