public abstract class AstNode extends Node implements java.lang.Comparable<AstNode>
The AstNode hierarchy sits atop the older Node class, which
was designed for code generation. The Node class is a flexible,
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 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.
AstNode records additional information, including the
node's position, length, and parent node. Also, some 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 AstNode and 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 Visitor interface), the offsets are relative.
AstNode objects have property lists accessible via the
Node.getProp(int) and Node.putProp(int, java.lang.Object) methods. The property lists are
integer-keyed with arbitrary Object values. For the most part the
parser 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++.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
AstNode.DebugPrintVisitor |
static class |
AstNode.PositionComparator |
Node.NodeIterator| Modifier and Type | Field and Description |
|---|---|
protected int |
length |
protected AstNode |
parent |
protected int |
position |
ATTRIBUTE_FLAG, BOTH, CASEARRAY_PROP, CATCH_SCOPE_PROP, CONTROL_BLOCK_PROP, DECR_FLAG, DESCENDANTS_FLAG, DESTRUCTURING_ARRAY_LENGTH, DESTRUCTURING_NAMES, DESTRUCTURING_PARAMS, DESTRUCTURING_SHORTHAND, DIRECTCALL_PROP, END_DROPS_OFF, END_RETURNS, END_RETURNS_VALUE, END_UNREACHED, END_YIELDS, EXPRESSION_CLOSURE_PROP, first, FUNCTION_PROP, GENERATOR_END_PROP, INCRDECR_PROP, ISNUMBER_PROP, JSDOC_PROP, LABEL_ID_PROP, last, LAST_PROP, LEFT, lineno, LOCAL_BLOCK_PROP, LOCAL_PROP, MEMBER_TYPE_PROP, NAME_PROP, next, NON_SPECIALCALL, OBJECT_IDS_PROP, PARENTHESIZED_PROP, POST_FLAG, PROPERTY_FLAG, propListHead, REGEXP_PROP, RIGHT, SKIP_INDEXES_PROP, SPECIALCALL_EVAL, SPECIALCALL_PROP, SPECIALCALL_WITH, TARGETBLOCK_PROP, type, VARIABLE_PROP| Constructor and Description |
|---|
AstNode() |
AstNode(int pos)
Constructs a new AstNode
|
AstNode(int pos,
int len)
Constructs a new AstNode
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChild(AstNode kid)
Adds a child or function to the end of the block.
|
protected void |
assertNotNull(java.lang.Object arg)
Bounces an IllegalArgumentException up if arg is
null. |
static java.lang.RuntimeException |
codeBug() |
int |
compareTo(AstNode other)
Permits AST nodes to be sorted based on start position and length.
|
java.lang.String |
debugPrint()
Returns a debugging representation of the parse tree starting at this
node.
|
int |
depth()
Returns the depth of this node.
|
int |
getAbsolutePosition()
Returns the absolute document position of the node.
|
AstRoot |
getAstRoot()
Returns the root of the tree containing this node.
|
FunctionNode |
getEnclosingFunction()
Returns the innermost enclosing function, or
null if not in a
function. |
Scope |
getEnclosingScope()
Returns the innermost enclosing
Scope node, or null if
we're not nested in a scope. |
int |
getLength()
Returns node length
|
int |
getLineno()
Return the line number recorded for this node.
|
AstNode |
getParent()
Returns the node parent, or
null if it has none |
int |
getPosition()
Returns relative position in parent
|
boolean |
hasSideEffects() |
java.lang.String |
makeIndent(int indent)
Constructs an indentation string.
|
static java.lang.String |
operatorToString(int op)
Returns the string name for this operator.
|
protected <T extends AstNode> |
printList(java.util.List<T> items,
java.lang.StringBuilder sb)
Prints a comma-separated item list into a
StringBuilder. |
void |
setBounds(int position,
int end)
Sets the node start and end positions.
|
void |
setLength(int length)
Sets node length
|
void |
setParent(AstNode parent)
Sets the node parent.
|
void |
setPosition(int position)
Sets relative position in parent
|
void |
setRelative(int parentPosition)
Make this node's position relative to a parent.
|
java.lang.String |
shortName()
Returns a short, descriptive name for the node, such as
"ArrayComprehension".
|
java.lang.String |
toSource()
Prints the source indented to depth 0.
|
abstract java.lang.String |
toSource(int depth)
Emits source code for this node.
|
abstract void |
visit(NodeVisitor visitor)
Visits this node and its children in an arbitrary order.
|
addChildAfter, addChildBefore, addChildrenToBack, addChildrenToFront, addChildToBack, addChildToFront, getChildBefore, getDouble, getExistingIntProp, getFirstChild, getIntProp, getJsDoc, getJsDocNode, getLastChild, getLastSibling, getNext, getProp, getScope, getString, getType, hasChildren, hasConsistentReturnUsage, iterator, labelId, labelId, newNumber, newString, newString, newTarget, putIntProp, putProp, removeChild, removeChildren, removeProp, replaceChild, replaceChildAfter, resetTargets, setDouble, setJsDocNode, setLineno, setScope, setString, setType, toString, toStringTreeprotected int position
protected int length
protected AstNode parent
public AstNode()
public AstNode(int pos)
pos - the start positionpublic AstNode(int pos,
int len)
pos - the start positionlen - the number of characters spanned by the node in the source
textpublic int getPosition()
public void setPosition(int position)
public int getAbsolutePosition()
public int getLength()
public void setLength(int length)
public void setBounds(int position,
int end)
end
- position).public void setRelative(int parentPosition)
parentPosition - the absolute parent position; the current node position is
assumed to be absolute and is decremented by parentPosition.public AstNode getParent()
null if it has nonepublic void setParent(AstNode parent)
parent - the new parent. Can be null.public void addChild(AstNode kid)
kid - the childjava.lang.IllegalArgumentException - if kid is nullpublic AstRoot getAstRoot()
AstRoot at the root of this node's parent chain, or
null if the topmost parent is not an AstRoot.public abstract java.lang.String toSource(int depth)
Note: if the parser was in error-recovery mode, some AST nodes may have
null children that are expected to be non-null when no
errors are present. In this situation, the behavior of the
toSource method is undefined: toSource implementations
may assume that the AST node is error-free, since it is intended to be
invoked only at runtime after a successful parse.
depth - the current recursion depth, typically beginning at 0 when
called on the root node.public java.lang.String toSource()
public java.lang.String makeIndent(int indent)
indent - the number of indentation stepspublic java.lang.String shortName()
public static java.lang.String operatorToString(int op)
op - the token type, e.g. Token.ADD or Token.TYPEOFpublic abstract void visit(NodeVisitor visitor)
It's up to each node subclass to decide the order for processing its
children. The subclass also decides (and should document) which child
nodes are not passed to the NodeVisitor. For instance, nodes
representing keywords like each or in may not be passed
to the visitor object. The visitor can simply query the current node for
these children if desired.
Generally speaking, the order will be deterministic; the order is whatever order is decided by each child node. Normally child nodes will try to visit their children in lexical order, but there may be exceptions to this rule.
visitor - the object to call with this node and its childrenpublic boolean hasSideEffects()
hasSideEffects in class Nodeprotected void assertNotNull(java.lang.Object arg)
null.arg - any method argumentjava.lang.IllegalArgumentException - if the argument is nullprotected <T extends AstNode> void printList(java.util.List<T> items, java.lang.StringBuilder sb)
StringBuilder.items - a list to printsb - a StringBuilder into which to printpublic static java.lang.RuntimeException codeBug()
throws java.lang.RuntimeException
java.lang.RuntimeExceptionKit.codeBug()public FunctionNode getEnclosingFunction()
null if not in a
function. Begins the search with this node's parent.FunctionNode enclosing this node, else nullpublic Scope getEnclosingScope()
Scope node, or null if
we're not nested in a scope. Begins the search with this node's parent.
Note that this is not the same as the defining scope for a Name.Scope enclosing this node, else nullpublic int compareTo(AstNode other)
SortedSet, for
instance.compareTo in interface java.lang.Comparable<AstNode>other - another nodeother's
start position. If tied, -1 if this node's length is less than
other's length. If the lengths are equal, sorts
abitrarily on hashcode unless the nodes are the same per
Object.equals(java.lang.Object).public int depth()
public int getLineno()
public java.lang.String debugPrint()