The {@code AstNode} hierarchy sits atop the older {@link Node} class,which was designed for code generation. The {@code Node} class is aflexible, weakly-typed class suitable for creating and rewriting code trees, but using it requires you to remember the exact ordering of the child nodes, which are kept in a linked list. The {@code AstNode}hierarchy is a strongly-typed facade with named accessors for children and common properties, but under the hood it's still using a linked list of child nodes. It isn't a very good idea to use the child list directly unless you know exactly what you're doing.
Note that {@code AstNode} records additional information, includingthe node's position, length, and parent node. Also, some {@code AstNode}subclasses record some of their child nodes in instance members, since they are not needed for code generation. In a nutshell, only the code generator should be mixing and matching {@code AstNode} and {@code Node}objects.All offset fields in all subclasses of AstNode are relative to their parent. For things like paren, bracket and keyword positions, the position is relative to the current node. The node start position is relative to the parent node.
During the actual parsing, node positions are absolute; adding the node to its parent fixes up the offsets to be relative. By the time you see the AST (e.g. using the {@code Visitor} interface), the offsets are relative.
{@code AstNode} objects have property lists accessible via the{@link #getProp} and {@link #putProp} methods. The property lists areinteger-keyed with arbitrary {@code Object} values. For the most part theparser generating the AST avoids using properties, preferring fields for elements that are always set. Property lists are intended for user-defined annotations to the tree. The Rhino code generator acts as a client and uses node properties extensively. You are welcome to use the property-list API for anything your client needs.
This hierarchy does not have separate branches for expressions and statements, as the distinction in JavaScript is not as clear-cut as in Java or C++.
|
|