com.sonar.sslr.api
Class AstNode

java.lang.Object
  extended by com.sonar.sslr.api.AstNode

public class AstNode
extends java.lang.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
protected  AstNodeType type
           
 
Constructor Summary
AstNode(AstNodeType type, java.lang.String name, Token token)
           
AstNode(Token token)
           
 
Method Summary
 void addChild(AstNode child)
           
 java.util.List<AstNode> findChildren(AstNodeType... nodeTypes)
          Find the all children having the requested type(s).
 java.util.List<AstNode> findDirectChildren(AstNodeType... nodeTypes)
          Find the all children among direct children having the requested type(s).
 AstNode findFirstChild(AstNodeType... nodeTypes)
          Find the first child among all children and grand-children having one of the requested types.
 AstNode findFirstDirectChild(AstNodeType... nodeTypes)
          Find the first child among all direct children having one of the requested types.
 AstNode findFirstParent(AstNodeType nodeType)
          Find the first parent with the desired node type
 AstNode getChild(int index)
          Get the desired child
 java.util.List<AstNode> getChildren()
          Get the list of children.
 AstNode getFirstChild()
          Get the first child of this node
 int getFromIndex()
           
 AstNode getLastChild()
          Get the last child of this node
 Token getLastToken()
           
 java.lang.String getName()
           
 int getNumberOfChildren()
           
 AstNode getParent()
          Get the parent of this node in the tree.
 int getToIndex()
           
 Token getToken()
          Get the Token associated to this AstNode
 int getTokenLine()
          Get the Token's line associated to this AstNode
 java.lang.String getTokenOriginalValue()
          Get the Token's original value associated to this AstNode
 java.util.List<Token> getTokens()
          Return all tokens contained in this tree node.
 java.lang.String getTokenValue()
          Get the Token's value associated to this AstNode
 AstNodeType getType()
           
 boolean hasChildren()
           
 boolean hasChildren(AstNodeType... nodeTypes)
           
 boolean hasDirectChildren(AstNodeType... nodeTypes)
           
 boolean hasParents(AstNodeType nodeType)
           
 boolean hasToBeSkippedFromAst()
           
 boolean hasToken()
           
 boolean is(AstNodeType... types)
           
 boolean isCopyBookOrGeneratedNode()
           
 boolean isNot(AstNodeType... types)
           
 AstNode nextAstNode()
          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 nextSibling()
          Get the next sibling AstNode if exists in the tree.
 AstNode previousAstNode()
          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 previousSibling()
          Get the previous sibling AstNode if exists in the tree.
 void setFromIndex(int fromIndex)
           
 void setToIndex(int toIndex)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

type

protected final AstNodeType type
Constructor Detail

AstNode

public AstNode(Token token)

AstNode

public AstNode(AstNodeType type,
               java.lang.String name,
               Token token)
Method Detail

getParent

public AstNode getParent()
Get the parent of this node in the tree.

Parameters:
parent -

addChild

public void addChild(AstNode child)

hasChildren

public boolean hasChildren()
Returns:
true if this AstNode has some children.

getChildren

public java.util.List<AstNode> getChildren()
Get the list of children.

Returns:
list of children

getNumberOfChildren

public int getNumberOfChildren()

getChild

public AstNode getChild(int index)
Get the desired child

Parameters:
index - the index of the child (start at 0)
Returns:
the AstNode child

nextAstNode

public AstNode nextAstNode()
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.


previousAstNode

public AstNode previousAstNode()
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.


nextSibling

public AstNode nextSibling()
Get the next sibling AstNode if exists in the tree.

Returns:
next sibling AstNode

previousSibling

public AstNode previousSibling()
Get the previous sibling AstNode if exists in the tree.

Returns:
previous sibling AstNode

getTokenValue

public java.lang.String getTokenValue()
Get the Token's value associated to this AstNode

Returns:
token's value

getTokenOriginalValue

public java.lang.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 java.lang.String getName()

getFromIndex

public int getFromIndex()

setFromIndex

public void setFromIndex(int fromIndex)

getToIndex

public int getToIndex()

hasToBeSkippedFromAst

public boolean hasToBeSkippedFromAst()

setToIndex

public void setToIndex(int toIndex)

is

public boolean is(AstNodeType... types)

isNot

public boolean isNot(AstNodeType... types)

findFirstDirectChild

public AstNode findFirstDirectChild(AstNodeType... nodeTypes)
Find the first child among all direct children having one of the requested types.
 In the following case, findFirstDirectChild('B') would return 'B2' :

   A1
    |__ C1
    |    |__ B1
    |__ B2
    |__ B3
 

Parameters:
list - of desired node types
Returns:
the first child or null

findFirstChild

public AstNode findFirstChild(AstNodeType... nodeTypes)
Find the first child among all children and grand-children having one of the requested types.
 In the following case, findFirstChild('B') would return 'B1' :

   A1
    |__ C1
    |    |__ B1
    |__ B2
    |__ B3
 

Parameters:
AstNodeType - list of desired node types
Returns:
the first child or null

getFirstChild

public AstNode getFirstChild()
Get the first child of this node

Returns:
the first child or null if there is no child

findDirectChildren

public java.util.List<AstNode> findDirectChildren(AstNodeType... nodeTypes)
Find the all children among direct children having the requested type(s).
 In the following case, findDirectChildren('B') would return 'B2' and 'B3' :

   A1
    |__ C1
    |    |__ B1
    |__ B2
    |__ B3
 

Parameters:
AstNodeType - list of desired the node types
Returns:
the list of matching children

findChildren

public java.util.List<AstNode> findChildren(AstNodeType... nodeTypes)
Find the all children having the requested type(s). Be careful, this method searches among all children whatever is their depth, so favor findDirectChildren(AstNodeType... nodeType) when possible.
 In the following case, findChildren('B', 'C') would return 'C1', 'B1', 'B2' and 'B3' :

   A1
    |__ C1
    |    |__ B1
    |__ B2
    |__ D1
    |__ B3
 

Parameters:
AstNodeType - the node type
Returns:
the list of matching children

getLastChild

public AstNode getLastChild()
Get the last child of this node

Returns:
the last child or null if there is no child

hasDirectChildren

public boolean hasDirectChildren(AstNodeType... nodeTypes)
Returns:
true if this node has some direct children with the requested node types

hasChildren

public boolean hasChildren(AstNodeType... nodeTypes)
Returns:
true if this node has some children and/or grand-children with the requested node types

hasParents

public boolean hasParents(AstNodeType nodeType)
Returns:
true if this node has a parent or a grand-parent with the requested node type.

findFirstParent

public AstNode findFirstParent(AstNodeType nodeType)
Find the first parent with the desired node type

Parameters:
AstNodeType - the desired Ast node type
Returns:
the parent/grand-parent or null

isCopyBookOrGeneratedNode

public boolean isCopyBookOrGeneratedNode()

getType

public AstNodeType getType()

getTokens

public java.util.List<Token> getTokens()
Return all tokens contained in this tree node. Those tokens can be directly or indirectly attached to this node.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getLastToken

public Token getLastToken()


Copyright © 2012 SonarSource. All Rights Reserved.