Package org.codehaus.groovy.syntax
Class Token
java.lang.Object
org.codehaus.groovy.syntax.CSTNode
org.codehaus.groovy.syntax.Token
A
CSTNode produced by the Lexer during lexical analysis.
Represents a single token in the token stream, including its type, text content,
and position in the source.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConverts this token to aReductionwith this token as the root.asReduction(CSTNode second) Converts this token to aReductionwith this token as the root and the specified node as the second element.asReduction(CSTNode second, CSTNode third) Converts this token to aReductionwith this token as the root and the specified nodes as the second and third elements.asReduction(CSTNode second, CSTNode third, CSTNode fourth) Converts this token to aReductionwith this token as the root and the specified nodes as the second, third, and fourth elements.dup()Creates a shallow copy of this Token, preserving type, text, position, and meaning.get(int index) Returns the element at the specified index.intReturns the current meaning (interpretation) of this token.getRoot()Returns the root token of this node, which is always this token itself.Returns the text content of this token.intReturns the starting column number of this token in the source.intReturns the starting line number of this token in the source.getText()Returns the text content of this token.intgetType()Returns the actual type of this token as determined by the lexer.static TokennewDecimal(String text, int startLine, int startColumn) Factory method to create a decimal number literal token.static TokennewIdentifier(String text, int startLine, int startColumn) Factory method to create an identifier token.static TokennewInteger(String text, int startLine, int startColumn) Factory method to create an integer literal token.static TokennewKeyword(String text, int startLine, int startColumn) Factory method to create a keyword token if the given text represents a known keyword.static TokennewPlaceholder(int meaning) Factory method to create a placeholder token with a specific meaning but unknown text.static TokenFactory method to create a string literal token.static TokennewSymbol(int type, int startLine, int startColumn) Factory method to create a symbol token from a type constant.static TokenFactory method to create a symbol token from text.setMeaning(int meaning) Sets the meaning (interpretation) for this token.voidSets the text content of this token.intsize()Returns the number of elements in this node, which is always 1 (the token itself).Methods inherited from class org.codehaus.groovy.syntax.CSTNode
add, addChildrenOf, canMean, children, get, getDescription, getMeaningAs, getRoot, hasChildren, isA, isAllOf, isAnExpression, isEmpty, isOneOf, markAsExpression, set, toString, write, write
-
Field Details
-
EOF
Sentinel token indicating end-of-file. -
NULL
Sentinel token for null/unknown positions.
-
-
Constructor Details
-
Token
Constructs a Token with the specified type, text, and source position.- Parameters:
type- the token type fromTypestext- the token's text contentstartLine- the source line number (1-based, or -1 if unknown)startColumn- the source column number (1-based, or -1 if unknown)
-
-
Method Details
-
dup
Creates a shallow copy of this Token, preserving type, text, position, and meaning.- Returns:
- a new Token with the same attributes as this one
-
getMeaning
public int getMeaning()Returns the current meaning (interpretation) of this token. May differ fromgetType()if the meaning has been reassigned.- Overrides:
getMeaningin classCSTNode- Returns:
- the token's current meaning type
-
setMeaning
Sets the meaning (interpretation) for this token. Has no effect on the sentinel tokens (EOFandNULL).- Overrides:
setMeaningin classCSTNode- Parameters:
meaning- the new meaning type fromTypes- Returns:
- this token, for convenience chaining
-
getType
public int getType()Returns the actual type of this token as determined by the lexer. -
size
public int size()Returns the number of elements in this node, which is always 1 (the token itself). -
get
Returns the element at the specified index. Only index 0 (the token itself) is valid.- Specified by:
getin classCSTNode- Parameters:
index- the element index- Returns:
- this token if index is 0
- Throws:
GroovyBugError- if index is greater than 0
-
getRoot
Returns the root token of this node, which is always this token itself. -
getRootText
Returns the text content of this token. Equivalent togetText().- Overrides:
getRootTextin classCSTNode- Returns:
- the token's text content
-
getText
Returns the text content of this token.- Returns:
- the token's text content
-
setText
- Parameters:
text- the new text content
-
getStartLine
public int getStartLine()Returns the starting line number of this token in the source.- Overrides:
getStartLinein classCSTNode- Returns:
- the line number (1-based), or -1 if not known
-
getStartColumn
public int getStartColumn()Returns the starting column number of this token in the source.- Overrides:
getStartColumnin classCSTNode- Returns:
- the column number (1-based), or -1 if not known
-
asReduction
Converts this token to aReductionwith this token as the root.- Specified by:
asReductionin classCSTNode- Returns:
- a new Reduction containing this token as its root element
-
asReduction
Converts this token to aReductionwith this token as the root and the specified node as the second element.- Parameters:
second- the second element to add to the reduction- Returns:
- a new Reduction containing this token and the second element
-
asReduction
Converts this token to aReductionwith this token as the root and the specified nodes as the second and third elements.- Parameters:
second- the second element to add to the reductionthird- the third element to add to the reduction- Returns:
- a new Reduction containing this token and the specified elements
-
asReduction
Converts this token to aReductionwith this token as the root and the specified nodes as the second, third, and fourth elements.- Parameters:
second- the second element to add to the reductionthird- the third element to add to the reductionfourth- the fourth element to add to the reduction- Returns:
- a new Reduction containing this token and the specified elements
-
newKeyword
Factory method to create a keyword token if the given text represents a known keyword.- Parameters:
text- the text to check as a keywordstartLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token with the appropriate keyword type, or
nullif the text is not a keyword
-
newString
Factory method to create a string literal token.- Parameters:
text- the string contentstartLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing a string literal
-
newIdentifier
Factory method to create an identifier token.- Parameters:
text- the identifier namestartLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing an identifier
-
newInteger
Factory method to create an integer literal token.- Parameters:
text- the integer literal text (may include prefix and suffix)startLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing an integer literal
-
newDecimal
Factory method to create a decimal number literal token.- Parameters:
text- the decimal literal textstartLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing a decimal number literal
-
newSymbol
Factory method to create a symbol token from a type constant. The symbol text is looked up from the Types registry.- Parameters:
type- the symbol type fromTypesstartLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing a symbol
-
newSymbol
Factory method to create a symbol token from text. The symbol type is looked up from the Types registry.- Parameters:
text- the symbol text (e.g., "+", "-", "{")startLine- the source line number (1-based)startColumn- the source column number (1-based)- Returns:
- a Token representing a symbol
-
newPlaceholder
Factory method to create a placeholder token with a specific meaning but unknown text. Used internally during parsing to hold semantic information.- Parameters:
meaning- the token's meaning type fromTypes- Returns:
- a placeholder Token with empty text and unknown type
-