Package com.sonar.sslr.api
Class AstNode
- java.lang.Object
-
- com.sonar.sslr.api.AstNode
-
public class AstNode extends Object
the parser is in charge to construct an abstract syntax tree (AST) which is a tree representation of the abstract syntactic structure of source code. Each node of the tree is an AstNode and each node denotes a construct occurring in the source code which starts at a given Token.- See Also:
Token
-
-
Field Summary
Fields Modifier and Type Field Description protected AstNodeType
type
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addChild(AstNode child)
List<AstNode>
findChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetDescendants(AstNodeType...)
instead, but don't forget that those methods behave differently due to bug SSLR-249List<AstNode>
findDirectChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetChildren(AstNodeType...)
insteadAstNode
findFirstChild(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetFirstDescendant(AstNodeType...)
insteadAstNode
findFirstDirectChild(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetFirstChild(AstNodeType...)
insteadAstNode
findFirstParent(AstNodeType nodeType)
Deprecated.in 1.17, usegetFirstAncestor(AstNodeType)
insteadAstNode
getChild(int index)
Deprecated.in 1.19.2, usegetFirstChild(AstNodeType...)
insteadList<AstNode>
getChildren()
Get the list of children.List<AstNode>
getChildren(AstNodeType... nodeTypes)
Returns children of specified types.List<AstNode>
getDescendants(AstNodeType... nodeTypes)
Returns descendants of specified types.AstNode
getFirstAncestor(AstNodeType nodeType)
AstNode
getFirstAncestor(AstNodeType... nodeTypes)
AstNode
getFirstChild()
Returns the first child of this node.AstNode
getFirstChild(AstNodeType... nodeTypes)
Returns first child of one of specified types.AstNode
getFirstDescendant(AstNodeType... nodeTypes)
Returns first descendant of one of specified types.int
getFromIndex()
AstNode
getLastChild()
Returns the last child of this node.AstNode
getLastChild(AstNodeType... nodeTypes)
Returns last child of one of specified types.Token
getLastToken()
String
getName()
AstNode
getNextAstNode()
Get the next sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.AstNode
getNextSibling()
Get the next sibling AstNode if exists in the tree.int
getNumberOfChildren()
AstNode
getParent()
Get the parent of this node in the tree.AstNode
getPreviousAstNode()
Get the previous sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.AstNode
getPreviousSibling()
Get the previous sibling AstNode if exists in the tree.int
getToIndex()
Token
getToken()
Get the Token associated to this AstNodeint
getTokenLine()
Get the Token's line associated to this AstNodeString
getTokenOriginalValue()
Get the Token's original value associated to this AstNodeList<Token>
getTokens()
Return all tokens contained in this tree node.String
getTokenValue()
Get the Token's value associated to this AstNodeAstNodeType
getType()
boolean
hasAncestor(AstNodeType nodeType)
boolean
hasAncestor(AstNodeType... nodeTypes)
boolean
hasChildren()
boolean
hasChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usehasDescendant(AstNodeType...)
instead.boolean
hasDescendant(AstNodeType... nodeTypes)
boolean
hasDirectChildren(AstNodeType... nodeTypes)
boolean
hasParent(AstNodeType... nodeTypes)
boolean
hasParents(AstNodeType nodeType)
Deprecated.in 1.17, usehasAncestor(AstNodeType)
insteadboolean
hasToBeSkippedFromAst()
For internal use only.boolean
hasToken()
boolean
is(AstNodeType... types)
boolean
isCopyBookOrGeneratedNode()
boolean
isNot(AstNodeType... types)
AstNode
nextAstNode()
Deprecated.in 1.17, usegetNextAstNode()
insteadAstNode
nextSibling()
Deprecated.in 1.17, usegetNextSibling()
insteadAstNode
previousAstNode()
Deprecated.in 1.17, usegetPreviousAstNode()
insteadAstNode
previousSibling()
Deprecated.in 1.17, usegetPreviousSibling()
AstSelect
select()
Deprecated.in 1.22void
setFromIndex(int fromIndex)
void
setToIndex(int toIndex)
String
toString()
-
-
-
Field Detail
-
type
protected AstNodeType type
-
-
Constructor Detail
-
AstNode
public AstNode(Token token)
-
AstNode
public AstNode(AstNodeType type, String name, @Nullable Token token)
-
-
Method Detail
-
getParent
public AstNode getParent()
Get the parent of this node in the tree.
-
addChild
public void addChild(AstNode child)
-
hasChildren
public boolean hasChildren()
- Returns:
- true if this AstNode has some children.
-
getNumberOfChildren
public int getNumberOfChildren()
-
getChild
@Deprecated public AstNode getChild(int index)
Deprecated.in 1.19.2, usegetFirstChild(AstNodeType...)
instead
-
nextAstNode
@Deprecated public AstNode nextAstNode()
Deprecated.in 1.17, usegetNextAstNode()
instead
-
getNextAstNode
public AstNode getNextAstNode()
Get the next sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.- Since:
- 1.17
-
previousAstNode
@Deprecated public AstNode previousAstNode()
Deprecated.in 1.17, usegetPreviousAstNode()
instead
-
getPreviousAstNode
public AstNode getPreviousAstNode()
Get the previous sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.- Since:
- 1.17
-
nextSibling
@Deprecated public AstNode nextSibling()
Deprecated.in 1.17, usegetNextSibling()
instead
-
getNextSibling
public AstNode getNextSibling()
Get the next sibling AstNode if exists in the tree.- Returns:
- next sibling, or null if not exists
- Since:
- 1.17
-
previousSibling
@Deprecated public AstNode previousSibling()
Deprecated.in 1.17, usegetPreviousSibling()
-
getPreviousSibling
public AstNode getPreviousSibling()
Get the previous sibling AstNode if exists in the tree.- Returns:
- previous sibling, or null if not exists
- Since:
- 1.17
-
getTokenValue
public String getTokenValue()
Get the Token's value associated to this AstNode- Returns:
- token's value
-
getTokenOriginalValue
public String getTokenOriginalValue()
Get the Token's original value associated to this AstNode- Returns:
- token's original value
-
getToken
public Token getToken()
Get the Token associated to this AstNode
-
getTokenLine
public int getTokenLine()
Get the Token's line associated to this AstNode- Returns:
- token's line
-
hasToken
public boolean hasToken()
-
getName
public String getName()
-
getFromIndex
public int getFromIndex()
-
setFromIndex
public void setFromIndex(int fromIndex)
-
getToIndex
public int getToIndex()
-
hasToBeSkippedFromAst
public boolean hasToBeSkippedFromAst()
For internal use only.
-
setToIndex
public void setToIndex(int toIndex)
-
is
public boolean is(AstNodeType... types)
-
isNot
public boolean isNot(AstNodeType... types)
-
findFirstDirectChild
@Deprecated public AstNode findFirstDirectChild(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetFirstChild(AstNodeType...)
instead
-
getFirstChild
public AstNode getFirstChild(AstNodeType... nodeTypes)
Returns first child of one of specified types.In the following case,
getFirstChild("B")
would return "B2":A1 |__ C1 | |__ B1 |__ B2 |__ B3
- Returns:
- first child of one of specified types, or null if not found
- Since:
- 1.17
-
findFirstChild
@Deprecated public AstNode findFirstChild(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetFirstDescendant(AstNodeType...)
instead
-
getFirstDescendant
public AstNode getFirstDescendant(AstNodeType... nodeTypes)
Returns first descendant of one of specified types.In the following case,
getFirstDescendant("B")
would return "B1":A1 |__ C1 | |__ B1 |__ B2 |__ B3
- Returns:
- first descendant of one of specified types, or null if not found
- Since:
- 1.17
-
getFirstChild
public AstNode getFirstChild()
Returns the first child of this node.- Returns:
- the first child, or null if there is no child
-
findDirectChildren
@Deprecated public List<AstNode> findDirectChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetChildren(AstNodeType...)
instead
-
getChildren
public List<AstNode> getChildren(AstNodeType... nodeTypes)
Returns children of specified types. In the following case,getChildren("B")
would return "B2" and "B3":A1 |__ C1 | |__ B1 |__ B2 |__ B3
- Returns:
- children of specified types, never null
- Since:
- 1.17
-
findChildren
@Deprecated public List<AstNode> findChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usegetDescendants(AstNodeType...)
instead, but don't forget that those methods behave differently due to bug SSLR-249
-
getDescendants
public List<AstNode> getDescendants(AstNodeType... nodeTypes)
Returns descendants of specified types. Be careful, this method searches among all descendants whatever is their depth, so favorgetChildren(AstNodeType...)
when possible.In the following case,
getDescendants("B", "C")
would return "C1", "B1", "B2" and "B3":A1 |__ C1 | |__ B1 |__ B2 |__ D1 |__ B3
- Returns:
- descendants of specified types, never null
- Since:
- 1.17
-
getLastChild
public AstNode getLastChild()
Returns the last child of this node.- Returns:
- the last child, or null if there is no child
-
getLastChild
@Nullable public AstNode getLastChild(AstNodeType... nodeTypes)
Returns last child of one of specified types.In the following case,
getLastChild("B")
would return "B3":A1 |__ C1 | |__ B1 |__ B2 |__ B3 |__ B4
- Returns:
- last child of one of specified types, or null if not found
- Since:
- 1.20
-
hasDirectChildren
public boolean hasDirectChildren(AstNodeType... nodeTypes)
- Returns:
- true if this node has some children with the requested node types
-
hasChildren
@Deprecated public boolean hasChildren(AstNodeType... nodeTypes)
Deprecated.in 1.17, usehasDescendant(AstNodeType...)
instead. Be careful the name of this method is misleading as the check is done on descendant nodes and not only on child nodes.
-
hasDescendant
public boolean hasDescendant(AstNodeType... nodeTypes)
- Returns:
- true if this node has a descendant of one of specified types
- Since:
- 1.17
-
hasParents
@Deprecated public boolean hasParents(AstNodeType nodeType)
Deprecated.in 1.17, usehasAncestor(AstNodeType)
instead
-
hasParent
public boolean hasParent(AstNodeType... nodeTypes)
- Since:
- 1.19.2
-
hasAncestor
public boolean hasAncestor(AstNodeType nodeType)
- Returns:
- true if this node has an ancestor of the specified type
- Since:
- 1.17
-
hasAncestor
public boolean hasAncestor(AstNodeType... nodeTypes)
- Returns:
- true if this node has an ancestor of one of specified types
- Since:
- 1.19.2
-
findFirstParent
@Deprecated public AstNode findFirstParent(AstNodeType nodeType)
Deprecated.in 1.17, usegetFirstAncestor(AstNodeType)
instead
-
getFirstAncestor
public AstNode getFirstAncestor(AstNodeType nodeType)
- Returns:
- first ancestor of the specified type, or null if not found
- Since:
- 1.17
-
getFirstAncestor
public AstNode getFirstAncestor(AstNodeType... nodeTypes)
- Returns:
- first ancestor of one of specified types, or null if not found
- Since:
- 1.19.2
-
isCopyBookOrGeneratedNode
public boolean isCopyBookOrGeneratedNode()
-
getType
public AstNodeType getType()
-
getTokens
public List<Token> getTokens()
Return all tokens contained in this tree node. Those tokens can be directly or indirectly attached to this node.
-
getLastToken
public Token getLastToken()
-
select
@Deprecated public AstSelect select()
Deprecated.in 1.22- Since:
- 1.18
-
-