Package org.codehaus.groovy.syntax
Class CSTNode
java.lang.Object
org.codehaus.groovy.syntax.CSTNode
Abstract base class for nodes in the concrete syntax tree (CST) produced by parsing.
Every CST node has a
Token as its root element, which indicates the node's type.
This class provides methods for querying node meaning, type, structure, and converting
between different node representations.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an element to this node.voidaddChildrenOf(CSTNode of) Adds all children of the specified node to this node.abstract ReductionConverts this node to aReduction.booleancanMean(int type) Returnstrueif this node can be coerced to the specified type.intchildren()Returns the number of child elements (excluding the root).abstract CSTNodeget(int index) Returns the element at the specified index, ornullif not found.get(int index, boolean safe) Returns the element at the specified index, orToken.NULLif the element is not found andsafeistrue.Returns a human-readable description of this node's semantic meaning.intReturns the current semantic meaning (interpretation) of this node.intgetMeaningAs(int[] types) Returns the first matching meaning of the specified types.abstract TokengetRoot()Returns the root token of this node.getRoot(boolean safe) Returns the root token of this node, returningToken.NULLif the actual root isnullandsafeistrue.Returns the text content of the root token.intReturns the starting column number of this node in the source.intReturns the starting line number of this node in the source.intgetType()Returns the actual syntactic type of this node as determined by the parser.booleanReturnstrueif this node has any children (non-root elements).booleanisA(int type) Returnstrueif this node's meaning matches the specified type.booleanisAllOf(int[] types) Returnstrueif this node's meaning matches all of the specified types.booleanReturnstrueif this node represents a complete expression.booleanisEmpty()Returnstrueif this node is completely empty (no root element).booleanisOneOf(int[] types) Returnstrueif this node's meaning matches any of the specified types.voidMarks this node as a complete expression.Sets the element at the specified index.setMeaning(int meaning) Sets the semantic meaning (interpretation) of this node.abstract intsize()Returns the number of elements in this node (including the root).toString()Returns a formatted string representation of this node and its children.voidwrite(PrintWriter writer) Writes a formatted representation of this node to the specified writer.protected voidwrite(PrintWriter writer, String indent) Writes a formatted representation of this node to the specified writer, with indentation for readability.
-
Constructor Details
-
CSTNode
public CSTNode()
-
-
Method Details
-
getMeaning
public int getMeaning()Returns the current semantic meaning (interpretation) of this node. For nodes without a root, returnsTypes.UNKNOWN.- Returns:
- the node's meaning type from
Types
-
setMeaning
Sets the semantic meaning (interpretation) of this node. The meaning may differ from the node's actual type and is often assigned during semantic analysis after parsing.- Parameters:
meaning- the new meaning type fromTypes- Returns:
- this node for convenience chaining
-
getType
public int getType()Returns the actual syntactic type of this node as determined by the parser. For nodes without a root, returnsTypes.UNKNOWN.- Returns:
- the node's type from
Types
-
canMean
public boolean canMean(int type) Returnstrueif this node can be coerced to the specified type. This is determined by the type hierarchy defined inTypes.- Parameters:
type- the type to check against- Returns:
trueif coercion is possible
-
isA
public boolean isA(int type) Returnstrueif this node's meaning matches the specified type.- Parameters:
type- the type to check- Returns:
trueif the node's meaning is of the specified type
-
isOneOf
public boolean isOneOf(int[] types) Returnstrueif this node's meaning matches any of the specified types.- Parameters:
types- an array of types to check- Returns:
trueif the node's meaning matches at least one of the types
-
isAllOf
public boolean isAllOf(int[] types) Returnstrueif this node's meaning matches all of the specified types.- Parameters:
types- an array of types to check- Returns:
trueif the node's meaning matches all of the types
-
getMeaningAs
public int getMeaningAs(int[] types) Returns the first matching meaning of the specified types. Useful for determining which of several possible meanings this node has.- Parameters:
types- an array of types to check- Returns:
- the first matching type, or
Types.UNKNOWNif no match is found
-
isEmpty
public boolean isEmpty()Returnstrueif this node is completely empty (no root element).- Returns:
trueif empty,falseotherwise
-
size
public abstract int size()Returns the number of elements in this node (including the root).- Returns:
- the number of elements
-
hasChildren
public boolean hasChildren()Returnstrueif this node has any children (non-root elements).- Returns:
trueif the node has children
-
children
public int children()Returns the number of child elements (excluding the root).- Returns:
- the number of children
-
get
Returns the element at the specified index, ornullif not found.- Parameters:
index- the element index (0 for root)- Returns:
- the element at the index, or
null
-
get
Returns the element at the specified index, orToken.NULLif the element is not found andsafeistrue.- Parameters:
index- the element indexsafe- iftrue, returnsToken.NULLinstead ofnullfor missing elements- Returns:
- the element at the index,
Token.NULLif safe and missing, ornull
-
getRoot
Returns the root token of this node. By convention, all nodes have aTokenas their root element (at index 0), which indicates the syntactic type of the node.- Returns:
- the root token, or
nullif the node is empty
-
getRoot
Returns the root token of this node, returningToken.NULLif the actual root isnullandsafeistrue.- Parameters:
safe- iftrue, returnsToken.NULLinstead ofnull- Returns:
- the root token
-
getRootText
Returns the text content of the root token. UsesgetRoot(true)to ensure a non-null root.- Returns:
- the text of the root token
-
getDescription
Returns a human-readable description of this node's semantic meaning.- Returns:
- a description string from
Types
-
getStartLine
public int getStartLine()Returns the starting line number of this node in the source.- Returns:
- the line number (1-based), or -1 if not known
-
getStartColumn
public int getStartColumn()Returns the starting column number of this node in the source.- Returns:
- the column number (1-based), or -1 if not known
-
markAsExpression
public void markAsExpression()Marks this node as a complete expression. Not all node types support this operation.- Throws:
GroovyBugError- if this node type does not support marking
-
isAnExpression
public boolean isAnExpression()Returnstrueif this node represents a complete expression.- Returns:
trueif this is an expression
-
add
Adds an element to this node. Not all node types support this operation.- Parameters:
element- the element to add- Returns:
- the added element
- Throws:
GroovyBugError- if this node type does not support adding
-
addChildrenOf
Adds all children of the specified node to this node. Skips the root element and copies only the children.- Parameters:
of- the source node whose children are to be copied
-
set
Sets the element at the specified index. Not all node types support this operation.- Parameters:
index- the element indexelement- the element to set- Returns:
- the set element
- Throws:
GroovyBugError- if this node type does not support setting
-
asReduction
Converts this node to aReduction. If this node is already a Reduction, returns itself.- Returns:
- this node as a Reduction
-
toString
Returns a formatted string representation of this node and its children. -
write
Writes a formatted representation of this node to the specified writer.- Parameters:
writer- thePrintWriterto write to
-
write
Writes a formatted representation of this node to the specified writer, with indentation for readability. The indentation is increased for each level of recursion to show the tree structure.- Parameters:
writer- thePrintWriterto write toindent- the indentation string to prepend to each line
-