com.sonar.sslr.api
Class AstNode

java.lang.Object
  extended by 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
protected  AstNodeType type
           
 
Constructor Summary
AstNode(AstNodeType type, String name, Token token)
           
AstNode(Token token)
           
 
Method Summary
 void addChild(AstNode child)
           
 String dumpSourceCode()
          Dump the partial source code covered by this node.
 List<AstNode> findChildren(AstNodeType nodeType)
          Find the all children having the requested type.
 List<AstNode> findDirectChildren(AstNodeType nodeType)
          Find the all children among direct children having the requested type.
 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
 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()
           
 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
 List<Token> getTokens()
          Return all tokens contained in this tree node.
 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 type)
           
 boolean isCopyBookOrGeneratedNode()
           
 boolean isNot(AstNodeType type)
           
 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 previousSibling()
          Get the previous sibling AstNode if exists in the tree.
 void setAstNodeListener(AstListener action)
           
 void setFromIndex(int fromIndex)
           
 void setToIndex(int toIndex)
           
 void startListening(Object output)
           
 void stopListening(Object output)
           
 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,
               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 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.


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 String getTokenValue()
Get the Token's value associated to this AstNode

Returns:
token's 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()

setToIndex

public void setToIndex(int toIndex)

is

public boolean is(AstNodeType type)

setAstNodeListener

public void setAstNodeListener(AstListener action)

startListening

public void startListening(Object output)

stopListening

public void stopListening(Object output)

isNot

public boolean isNot(AstNodeType type)

findFirstDirectChild

public AstNode findFirstDirectChild(AstNodeType... nodeTypes)
Find the first child among all direct children having one of the requested types.

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.

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 List<AstNode> findDirectChildren(AstNodeType nodeType)
Find the all children among direct children having the requested type. As far as possible, this method should be used instead of findChildren(AstNodeType nodeType) which is more CPU intensive.

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

findChildren

public List<AstNode> findChildren(AstNodeType nodeType)
Find the all children having the requested type. Be careful, this method searches among all children whatever is their depth.

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()

dumpSourceCode

public String dumpSourceCode()
Dump the partial source code covered by this node.


getTokens

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


toString

public String toString()
Overrides:
toString in class Object

getLastToken

public Token getLastToken()


Copyright © 2011 SonarSource. All Rights Reserved.