Package com.google.javascript.rhino
Class Node
java.lang.Object
com.google.javascript.rhino.Node
- All Implemented Interfaces:
Serializable
,Cloneable
This class implements the root of the intermediate representation.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Iterator to go up the ancestor tree.class
An inner class that provides back-door access to the license property of the JSDocInfo property for this node.static class
A helper class for getting and setting the side-effect flags. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
COLUMN_BITS represents how many of the lower-order bits of sourcePosition are reserved for storing the column number.static final int
COLUMN_MASK stores a value where bits storing the column number are set, and bits storing the line are not set.static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
MAX_COLUMN_NUMBER represents the maximum column number that can be represented.static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addChildAfter
(Node newChild, Node node) Add 'child' after 'node'.void
addChildBefore
(Node newChild, Node node) Add 'child' before 'node'.void
addChildrenAfter
(Node children, Node node) Add all children after 'node'.void
addChildrenToBack
(Node children) void
addChildrenToFront
(Node children) void
addChildToBack
(Node child) void
addChildToFront
(Node child) void
addSuppression
(String warning) Adds a warning to be suppressed.void
appendStringTree
(Appendable appendable) checkTreeEquals
(Node node2) Checks if the subtree under this node is the same as another subtree.children()
Return an iterable object that iterates over this node's children.clonePropsFrom
(Node other) Clone the properties from the provided node without copying the property object.copyInformationFrom
(Node other) Copies source file and name information from the other node given to the current node.copyInformationFromForTree
(Node other) Copies source file and name information from the other node to the entire tree rooted at this node.void
Removes all children from this node and isolates the children from each other.Removes this node from its parent.protected static int
extractCharno
(int lineCharNo) Extracts the character number and character number from a merged line char number (seemergeLineCharNo(int, int)
).protected static int
extractLineno
(int lineCharNo) Extracts the line number and character number from a merged line char number (seemergeLineCharNo(int, int)
).getAncestor
(int level) Gets the ancestor node relative to this.Iterates all of the node's ancestors excluding itself.boolean
getBooleanProp
(int propType) int
getChildAtIndex
(int i) getChildBefore
(Node child) int
Returns the set of ES5 directives for this node.double
Can only be called when getType() == TokenStream.NUMBERint
getExistingIntProp
(int propType) int
getIndexOfChild
(Node child) int
getIntProp
(int propType) Returns the integer value for the property, or 0 if the property is not defined.Get theJSDocInfo
attached to this node.int
int
getNext()
getProp
(int propType) This function takes a set of GETPROP nodes and produces a string that is each property separated by dots.int
Returns the side effects flags for this node.int
int
Returns the source file associated with this input.Can only be called when node has String context.int
getType()
boolean
boolean
boolean
Check for more than one child more efficiently than by iterating over all the children as is done with Node.getChildCount().boolean
Check for one child more efficiently than by iterating over all the children as is done with Node.getChildCount().boolean
isAdd()
AST type check methodsboolean
isAnd()
boolean
boolean
isAssign()
boolean
boolean
isBlock()
boolean
isBreak()
boolean
isCall()
boolean
isCase()
boolean
isCast()
boolean
isCatch()
boolean
isComma()
boolean
boolean
boolean
isDec()
boolean
boolean
boolean
isDo()
boolean
isEmpty()
boolean
isEquivalentTo
(Node node) Returns true if this node is equivalent semantically to anotherboolean
isEquivalentToTyped
(Node node) Returns true if this node is equivalent semantically to another and the types are equivalent.boolean
boolean
isFalse()
boolean
isFor()
boolean
boolean
boolean
boolean
boolean
boolean
isHook()
boolean
isIf()
boolean
isIn()
boolean
isInc()
boolean
boolean
isLabel()
boolean
boolean
Returns true if this node is a function or constructor call that returns a primitive or a local object (an object that has no other references).boolean
isName()
boolean
isNE()
boolean
isNew()
boolean
Returns true if this node is a function or constructor call that has no side effects.boolean
isNot()
boolean
isNull()
boolean
isNumber()
boolean
boolean
boolean
Returns whether this node is an optional argument node.boolean
isOr()
boolean
boolean
Returns whether a node corresponds to a simple or a qualified name, such asx
ora.b.c
orthis.a
.boolean
This should only be called for STRING nodes children of OBJECTLIT.boolean
isRegExp()
boolean
isReturn()
boolean
isScript()
boolean
boolean
isString()
boolean
boolean
isSwitch()
boolean
Returns whether this is a synthetic block that should not be considered a real source block.boolean
isThis()
boolean
isThrow()
boolean
isTrue()
boolean
isTry()
boolean
isTypeOf()
boolean
Returns whether a node corresponds to a simple or a qualified name without a "this" reference, such asa.b.c
, but notthis.a
.boolean
isVar()
boolean
Returns whether this node is a variable length argument node.boolean
isVoid()
boolean
isWhile()
boolean
isWith()
protected static int
mergeLineCharNo
(int lineno, int charno) Merges the line number and character number in one integer.static Node
newNumber
(double number) static Node
newNumber
(double number, int lineno, int charno) static Node
static Node
static Node
static Node
void
putBooleanProp
(int propType, boolean value) void
putIntProp
(int propType, int value) void
void
removeChild
(Node child) Detach a child from its parent and siblings.removeChildAfter
(Node prev) Removes the first child of Node.void
removeProp
(int propType) void
replaceChild
(Node child, Node newChild) Detaches child from Node and replaces it with newChild.void
replaceChildAfter
(Node prevChild, Node newChild) void
setCharno
(int charno) void
setDirectives
(Set<String> val) Sets the ES5 directives on this node.void
setDouble
(double value) Can only be called when getType() == Token.NUMBERvoid
setInputId
(InputId inputId) void
setIsSyntheticBlock
(boolean val) Sets whether this is a synthetic block that should not be considered a real source block.setJSDocInfo
(JSDocInfo info) Sets theJSDocInfo
attached to this node.void
void
setLength
(int length) void
setLineno
(int lineno) void
setOptionalArg
(boolean optionalArg) Sets whether this node is an optional argument node.void
This should only be called for STRING nodes children of OBJECTLIT.void
setSideEffectFlags
(int flags) Marks this function or constructor call's side effect flags.void
void
setSourceEncodedPosition
(int sourcePosition) void
setSourceEncodedPositionForTree
(int sourcePosition) void
Sets the source file to a non-extern file of the given name.void
void
Can only be called for a Token.STRING or Token.NAME.void
setType
(int type) void
setVarArgs
(boolean varArgs) Sets whether this node is a variable length argument node.void
setWasEmptyNode
(boolean val) Sets whether this is a synthetic block that should not be considered a real source block.siblings()
Return an iterable object that iterates over this node's siblings.srcrefTree
(Node other) toString()
toString
(boolean printSource, boolean printAnnotations, boolean printType) useSourceInfoFrom
(Node other) Overwrite all the source information in this node with that ofother
.useSourceInfoFromForTree
(Node other) Overwrite all the source information in this node and its subtree with that ofother
.useSourceInfoIfMissingFrom
(Node other) Overwrite all the source information in this node with that ofother
iff the source info is missing.Overwrite all the source information in this node and its subtree with that ofother
iff the source info is missing.boolean
Returns whether this is a synthetic block that should not be considered a real source block.
-
Field Details
-
JSDOC_INFO_PROP
public static final int JSDOC_INFO_PROP- See Also:
-
VAR_ARGS_NAME
public static final int VAR_ARGS_NAME- See Also:
-
INCRDECR_PROP
public static final int INCRDECR_PROP- See Also:
-
QUOTED_PROP
public static final int QUOTED_PROP- See Also:
-
OPT_ARG_NAME
public static final int OPT_ARG_NAME- See Also:
-
SYNTHETIC_BLOCK_PROP
public static final int SYNTHETIC_BLOCK_PROP- See Also:
-
EMPTY_BLOCK
public static final int EMPTY_BLOCK- See Also:
-
ORIGINALNAME_PROP
public static final int ORIGINALNAME_PROP- See Also:
-
SIDE_EFFECT_FLAGS
public static final int SIDE_EFFECT_FLAGS- See Also:
-
IS_CONSTANT_NAME
public static final int IS_CONSTANT_NAME- See Also:
-
IS_NAMESPACE
public static final int IS_NAMESPACE- See Also:
-
IS_DISPATCHER
public static final int IS_DISPATCHER- See Also:
-
DIRECTIVES
public static final int DIRECTIVES- See Also:
-
DIRECT_EVAL
public static final int DIRECT_EVAL- See Also:
-
FREE_CALL
public static final int FREE_CALL- See Also:
-
STATIC_SOURCE_FILE
public static final int STATIC_SOURCE_FILE- See Also:
-
LENGTH
public static final int LENGTH- See Also:
-
INPUT_ID
public static final int INPUT_ID- See Also:
-
SLASH_V
public static final int SLASH_V- See Also:
-
INFERRED_FUNCTION
public static final int INFERRED_FUNCTION- See Also:
-
LAST_PROP
public static final int LAST_PROP- See Also:
-
DECR_FLAG
public static final int DECR_FLAG- See Also:
-
POST_FLAG
public static final int POST_FLAG- See Also:
-
COLUMN_BITS
public static final int COLUMN_BITSCOLUMN_BITS represents how many of the lower-order bits of sourcePosition are reserved for storing the column number. Bits above these store the line number. This gives us decent position information for everything except files already passed through a minimizer, where lines might be longer than 4096 characters.- See Also:
-
MAX_COLUMN_NUMBER
public static final int MAX_COLUMN_NUMBERMAX_COLUMN_NUMBER represents the maximum column number that can be represented. JSCompiler's modifications to Rhino cause all tokens located beyond the maximum column to MAX_COLUMN_NUMBER.- See Also:
-
COLUMN_MASK
public static final int COLUMN_MASKCOLUMN_MASK stores a value where bits storing the column number are set, and bits storing the line are not set. It's handy for separating column number from line number.- See Also:
-
FLAG_GLOBAL_STATE_UNMODIFIED
public static final int FLAG_GLOBAL_STATE_UNMODIFIED- See Also:
-
FLAG_THIS_UNMODIFIED
public static final int FLAG_THIS_UNMODIFIED- See Also:
-
FLAG_ARGUMENTS_UNMODIFIED
public static final int FLAG_ARGUMENTS_UNMODIFIED- See Also:
-
FLAG_NO_THROWS
public static final int FLAG_NO_THROWS- See Also:
-
FLAG_LOCAL_RESULTS
public static final int FLAG_LOCAL_RESULTS- See Also:
-
SIDE_EFFECTS_FLAGS_MASK
public static final int SIDE_EFFECTS_FLAGS_MASK- See Also:
-
SIDE_EFFECTS_ALL
public static final int SIDE_EFFECTS_ALL- See Also:
-
NO_SIDE_EFFECTS
public static final int NO_SIDE_EFFECTS- See Also:
-
-
Constructor Details
-
Node
public Node(int nodeType) -
Node
-
Node
-
Node
-
Node
-
Node
public Node(int nodeType, int lineno, int charno) -
Node
-
Node
-
Node
-
Node
-
Node
-
Node
-
-
Method Details
-
newNumber
-
newNumber
-
newString
-
newString
-
newString
-
newString
-
getType
public int getType() -
setType
public void setType(int type) -
hasChildren
public boolean hasChildren() -
getFirstChild
-
getLastChild
-
getNext
-
getChildBefore
-
getChildAtIndex
-
getIndexOfChild
-
getLastSibling
-
addChildToFront
-
addChildToBack
-
addChildrenToFront
-
addChildrenToBack
-
addChildBefore
Add 'child' before 'node'. -
addChildAfter
Add 'child' after 'node'. -
addChildrenAfter
Add all children after 'node'. -
removeChild
Detach a child from its parent and siblings. -
replaceChild
Detaches child from Node and replaces it with newChild. -
replaceChildAfter
-
clonePropsFrom
Clone the properties from the provided node without copying the property object. The receiving node may not have any existing properties.- Parameters:
other
- The node to clone properties from.- Returns:
- this node.
-
removeProp
public void removeProp(int propType) -
getProp
-
getBooleanProp
public boolean getBooleanProp(int propType) -
getIntProp
public int getIntProp(int propType) Returns the integer value for the property, or 0 if the property is not defined. -
getExistingIntProp
public int getExistingIntProp(int propType) -
putProp
-
putBooleanProp
public void putBooleanProp(int propType, boolean value) -
putIntProp
public void putIntProp(int propType, int value) -
getDouble
Can only be called when getType() == TokenStream.NUMBER- Throws:
UnsupportedOperationException
-
setDouble
Can only be called when getType() == Token.NUMBER- Parameters:
value
- value to set.- Throws:
UnsupportedOperationException
-
getString
Can only be called when node has String context.- Throws:
UnsupportedOperationException
-
setString
Can only be called for a Token.STRING or Token.NAME.- Parameters:
value
- the value to set.- Throws:
UnsupportedOperationException
-
toString
-
toString
-
toStringTree
-
appendStringTree
- Throws:
IOException
-
setStaticSourceFile
-
setSourceFileForTesting
Sets the source file to a non-extern file of the given name. -
getSourceFileName
-
getStaticSourceFile
Returns the source file associated with this input. May be null -
setInputId
- Parameters:
inputId
-
-
getInputId
- Returns:
- The Id of the CompilerInput associated with this Node.
-
isFromExterns
public boolean isFromExterns() -
getLength
public int getLength() -
setLength
public void setLength(int length) -
getLineno
public int getLineno() -
getCharno
public int getCharno() -
getSourceOffset
public int getSourceOffset() -
getSourcePosition
public int getSourcePosition() -
setLineno
public void setLineno(int lineno) -
setCharno
public void setCharno(int charno) -
setSourceEncodedPosition
public void setSourceEncodedPosition(int sourcePosition) -
setSourceEncodedPositionForTree
public void setSourceEncodedPositionForTree(int sourcePosition) -
mergeLineCharNo
protected static int mergeLineCharNo(int lineno, int charno) Merges the line number and character number in one integer. The Character number takes the first 12 bits and the line number takes the rest. If the character number is greater than212-1
it is adjusted to212-1
. -
extractLineno
protected static int extractLineno(int lineCharNo) Extracts the line number and character number from a merged line char number (seemergeLineCharNo(int, int)
). -
extractCharno
protected static int extractCharno(int lineCharNo) Extracts the character number and character number from a merged line char number (seemergeLineCharNo(int, int)
). -
children
Return an iterable object that iterates over this node's children. The iterator does not support the optional operation
Iterator.remove()
.To iterate over a node's siblings, one can write
Node n = ...; for (Node child : n.children()) { ...
-
siblings
Return an iterable object that iterates over this node's siblings. The iterator does not support the optional operation
Iterator.remove()
.To iterate over a node's siblings, one can write
Node n = ...; for (Node sibling : n.siblings()) { ...
-
getParent
-
getAncestor
Gets the ancestor node relative to this.- Parameters:
level
- 0 = this, 1 = the parent, etc.
-
getAncestors
Iterates all of the node's ancestors excluding itself. -
hasOneChild
public boolean hasOneChild()Check for one child more efficiently than by iterating over all the children as is done with Node.getChildCount().- Returns:
- Whether the node has exactly one child.
-
hasMoreThanOneChild
public boolean hasMoreThanOneChild()Check for more than one child more efficiently than by iterating over all the children as is done with Node.getChildCount().- Returns:
- Whether the node more than one child.
-
getChildCount
public int getChildCount() -
hasChild
-
checkTreeEquals
Checks if the subtree under this node is the same as another subtree. Returns null if it's equal, or a message describing the differences. -
isEquivalentTo
Returns true if this node is equivalent semantically to another -
isEquivalentToTyped
Returns true if this node is equivalent semantically to another and the types are equivalent. -
getQualifiedName
This function takes a set of GETPROP nodes and produces a string that is each property separated by dots. If the node ultimately under the left sub-tree is not a simple name, this is not a valid qualified name.- Returns:
- a null if this is not a qualified name, or a dot-separated string of the name and properties.
-
isQualifiedName
public boolean isQualifiedName()Returns whether a node corresponds to a simple or a qualified name, such asx
ora.b.c
orthis.a
. -
isUnscopedQualifiedName
public boolean isUnscopedQualifiedName()Returns whether a node corresponds to a simple or a qualified name without a "this" reference, such asa.b.c
, but notthis.a
. -
detachFromParent
Removes this node from its parent. Equivalent to: node.getParent().removeChild(); -
removeFirstChild
Removes the first child of Node. Equivalent to: node.removeChild(node.getFirstChild());- Returns:
- The removed Node.
-
removeChildren
- Returns:
- A Node that is the head of the list of children.
-
detachChildren
public void detachChildren()Removes all children from this node and isolates the children from each other. -
removeChildAfter
-
cloneNode
- Returns:
- A detached clone of the Node, specifically excluding its children.
-
cloneTree
- Returns:
- A detached clone of the Node and all its children.
-
copyInformationFrom
Copies source file and name information from the other node given to the current node. Used for maintaining debug information across node append and remove operations.- Returns:
- this
-
copyInformationFromForTree
Copies source file and name information from the other node to the entire tree rooted at this node.- Returns:
- this
-
useSourceInfoFrom
Overwrite all the source information in this node with that ofother
. -
srcref
-
useSourceInfoFromForTree
Overwrite all the source information in this node and its subtree with that ofother
. -
srcrefTree
-
useSourceInfoIfMissingFrom
Overwrite all the source information in this node with that ofother
iff the source info is missing. -
useSourceInfoIfMissingFromForTree
Overwrite all the source information in this node and its subtree with that ofother
iff the source info is missing. -
getJSType
-
setJSType
-
getJsDocBuilderForNode
-
getJSDocInfo
Get theJSDocInfo
attached to this node.- Returns:
- the information or
null
if no JSDoc is attached to this node
-
setJSDocInfo
Sets theJSDocInfo
attached to this node. -
setVarArgs
public void setVarArgs(boolean varArgs) Sets whether this node is a variable length argument node. This method is meaningful only onToken.NAME
nodes used to define aToken.FUNCTION
's argument list. -
isVarArgs
public boolean isVarArgs()Returns whether this node is a variable length argument node. This method's return value is meaningful only onToken.NAME
nodes used to define aToken.FUNCTION
's argument list. -
setOptionalArg
public void setOptionalArg(boolean optionalArg) Sets whether this node is an optional argument node. This method is meaningful only onToken.NAME
nodes used to define aToken.FUNCTION
's argument list. -
isOptionalArg
public boolean isOptionalArg()Returns whether this node is an optional argument node. This method's return value is meaningful only onToken.NAME
nodes used to define aToken.FUNCTION
's argument list. -
setIsSyntheticBlock
public void setIsSyntheticBlock(boolean val) Sets whether this is a synthetic block that should not be considered a real source block. -
isSyntheticBlock
public boolean isSyntheticBlock()Returns whether this is a synthetic block that should not be considered a real source block. -
setDirectives
Sets the ES5 directives on this node. -
getDirectives
Returns the set of ES5 directives for this node. -
addSuppression
Adds a warning to be suppressed. This is indistinguishable from having a@suppress
tag in the code. -
setWasEmptyNode
public void setWasEmptyNode(boolean val) Sets whether this is a synthetic block that should not be considered a real source block. -
wasEmptyNode
public boolean wasEmptyNode()Returns whether this is a synthetic block that should not be considered a real source block. -
setSideEffectFlags
public void setSideEffectFlags(int flags) Marks this function or constructor call's side effect flags. This property is only meaningful forToken.CALL
andToken.NEW
nodes. -
setSideEffectFlags
-
getSideEffectFlags
public int getSideEffectFlags()Returns the side effects flags for this node. -
isOnlyModifiesThisCall
public boolean isOnlyModifiesThisCall()- Returns:
- Whether the only side-effect is "modifies this"
-
isNoSideEffectsCall
public boolean isNoSideEffectsCall()Returns true if this node is a function or constructor call that has no side effects. -
isLocalResultCall
public boolean isLocalResultCall()Returns true if this node is a function or constructor call that returns a primitive or a local object (an object that has no other references). -
isQuotedString
public boolean isQuotedString()This should only be called for STRING nodes children of OBJECTLIT. -
setQuotedString
public void setQuotedString()This should only be called for STRING nodes children of OBJECTLIT. -
isAdd
public boolean isAdd()AST type check methods -
isAnd
public boolean isAnd() -
isArrayLit
public boolean isArrayLit() -
isAssign
public boolean isAssign() -
isAssignAdd
public boolean isAssignAdd() -
isBlock
public boolean isBlock() -
isBreak
public boolean isBreak() -
isCall
public boolean isCall() -
isCase
public boolean isCase() -
isCast
public boolean isCast() -
isCatch
public boolean isCatch() -
isComma
public boolean isComma() -
isContinue
public boolean isContinue() -
isDebugger
public boolean isDebugger() -
isDec
public boolean isDec() -
isDefaultCase
public boolean isDefaultCase() -
isDelProp
public boolean isDelProp() -
isDo
public boolean isDo() -
isEmpty
public boolean isEmpty() -
isExprResult
public boolean isExprResult() -
isFalse
public boolean isFalse() -
isFor
public boolean isFor() -
isFunction
public boolean isFunction() -
isGetterDef
public boolean isGetterDef() -
isGetElem
public boolean isGetElem() -
isGetProp
public boolean isGetProp() -
isHook
public boolean isHook() -
isIf
public boolean isIf() -
isIn
public boolean isIn() -
isInc
public boolean isInc() -
isInstanceOf
public boolean isInstanceOf() -
isLabel
public boolean isLabel() -
isLabelName
public boolean isLabelName() -
isName
public boolean isName() -
isNE
public boolean isNE() -
isNew
public boolean isNew() -
isNot
public boolean isNot() -
isNull
public boolean isNull() -
isNumber
public boolean isNumber() -
isObjectLit
public boolean isObjectLit() -
isOr
public boolean isOr() -
isParamList
public boolean isParamList() -
isRegExp
public boolean isRegExp() -
isReturn
public boolean isReturn() -
isScript
public boolean isScript() -
isSetterDef
public boolean isSetterDef() -
isString
public boolean isString() -
isStringKey
public boolean isStringKey() -
isSwitch
public boolean isSwitch() -
isThis
public boolean isThis() -
isThrow
public boolean isThrow() -
isTrue
public boolean isTrue() -
isTry
public boolean isTry() -
isTypeOf
public boolean isTypeOf() -
isVar
public boolean isVar() -
isVoid
public boolean isVoid() -
isWhile
public boolean isWhile() -
isWith
public boolean isWith()
-