Package: org.codehaus.groovy.syntax

## [Java] Class Token

- org.codehaus.groovy.syntax.Token

-     public class Token
      extends CSTNode

  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.

  See Also:
    Reduction
    Types

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static Token` | `EOF` | Sentinel token indicating end-of-file.
    `static Token` | `NULL` | Sentinel token for null/unknown positions.

  - ### Constructor Summary

    Constructors
    Constructor and description
    `Token(int type, String text, int startLine, int startColumn)` Constructs a Token with the specified type, text, and source position.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public Reduction` | `asReduction()` Converts this token to a Reduction with this token as the root.
    `` | `public Reduction` | `asReduction(CSTNode second)` Converts this token to a Reduction with this token as the root and the specified node as the second element.
    `` | `public Reduction` | `asReduction(CSTNode second, CSTNode third)` Converts this token to a Reduction with this token as the root and the specified nodes as the second and third elements.
    `` | `public Reduction` | `asReduction(CSTNode second, CSTNode third, CSTNode fourth)` Converts this token to a Reduction with this token as the root and the specified nodes as the second, third, and fourth elements.
    `` | `public Token` | `dup()` Creates a shallow copy of this Token, preserving type, text, position, and meaning.
    `` | `public CSTNode` | `get(int index)` Returns the element at the specified index.
    `` | `public int` | `getMeaning()` Returns the current meaning (interpretation) of this token.
    `` | `public Token` | `getRoot()` Returns the root token of this node, which is always this token itself.
    `` | `public String` | `getRootText()` Returns the text content of this token.
    `` | `public int` | `getStartColumn()` Returns the starting column number of this token in the source.
    `` | `public int` | `getStartLine()` Returns the starting line number of this token in the source.
    `` | `public String` | `getText()` Returns the text content of this token.
    `` | `public int` | `getType()` Returns the actual type of this token as determined by the lexer.
    `` | `public static Token` | `newDecimal(String text, int startLine, int startColumn)` Factory method to create a decimal number literal token.
    `` | `public static Token` | `newIdentifier(String text, int startLine, int startColumn)` Factory method to create an identifier token.
    `` | `public static Token` | `newInteger(String text, int startLine, int startColumn)` Factory method to create an integer literal token.
    `` | `public static Token` | `newKeyword(String text, int startLine, int startColumn)` Factory method to create a keyword token if the given text represents a known keyword.
    `` | `public static Token` | `newPlaceholder(int meaning)` Factory method to create a placeholder token with a specific meaning but unknown text.
    `` | `public static Token` | `newString(String text, int startLine, int startColumn)` Factory method to create a string literal token.
    `` | `public static Token` | `newSymbol(int type, int startLine, int startColumn)` Factory method to create a symbol token from a type constant.
    `` | `public static Token` | `newSymbol(String text, int startLine, int startColumn)` Factory method to create a symbol token from text.
    `` | `public CSTNode` | `setMeaning(int meaning)` Sets the meaning (interpretation) for this token.
    `` | `public void` | `setText(String text)` Sets the text content of this token.
    `` | `public int` | `size()` Returns the number of elements in this node, which is always 1 (the token itself).

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class CSTNode` | `add, addChildrenOf, asReduction, canMean, children, get, get, getDescription, getMeaning, getMeaningAs, getRoot, getRoot, getRootText, getStartColumn, getStartLine, getType, hasChildren, isA, isAllOf, isAnExpression, isEmpty, isOneOf, markAsExpression, set, setMeaning, size, toString, write, write`

- - ### Field Detail

    - #### public static final Token EOF

      Sentinel token indicating end-of-file.

    - #### public static final Token NULL

      Sentinel token for null/unknown positions.

  - ### Constructor Detail

    - #### public Token(int type, String text, int startLine, int startColumn)

      Constructs a Token with the specified type, text, and source position.

      Parameters:
        `type` - the token type from Types
        `text` - the token's text content
        `startLine` - the source line number (1-based, or -1 if unknown)
        `startColumn` - the source column number (1-based, or -1 if unknown)

  - ### Method Detail

    - #### public Reduction asReduction()

      Converts this token to a Reduction with this token as the root.

      Returns:
        a new Reduction containing this token as its root element

    - #### public Reduction asReduction(CSTNode second)

      Converts this token to a Reduction with 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

    - #### public Reduction asReduction(CSTNode second, CSTNode third)

      Converts this token to a Reduction with this token as the root and the specified nodes as the second and third elements.

      Parameters:
        `second` - the second element to add to the reduction
        `third` - the third element to add to the reduction
      Returns:
        a new Reduction containing this token and the specified elements

    - #### public Reduction asReduction(CSTNode second, CSTNode third, CSTNode fourth)

      Converts this token to a Reduction with 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 reduction
        `third` - the third element to add to the reduction
        `fourth` - the fourth element to add to the reduction
      Returns:
        a new Reduction containing this token and the specified elements

    - #### public Token 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

    - #### public CSTNode get(int index)

      Returns the element at the specified index. Only index 0 (the token itself) is valid.

      throws:
        GroovyBugError if index is greater than 0
      Parameters:
        `index` - the element index
      Returns:
        this token if index is 0

    - #### public int getMeaning()

      Returns the current meaning (interpretation) of this token. May differ from getType() if the meaning has been reassigned.

      Returns:
        the token's current meaning type

    - #### public Token getRoot()

      Returns the root token of this node, which is always this token itself.

      Returns:
        this token

    - #### public String getRootText()

      Returns the text content of this token. Equivalent to getText().

      Returns:
        the token's text content

    - #### public int getStartColumn()

      Returns the starting column number of this token in the source.

      Returns:
        the column number (1-based), or -1 if not known

    - #### public int getStartLine()

      Returns the starting line number of this token in the source.

      Returns:
        the line number (1-based), or -1 if not known

    - #### public String getText()

      Returns the text content of this token.

      Returns:
        the token's text content

    - #### public int getType()

      Returns the actual type of this token as determined by the lexer.

      Returns:
        the token type from Types

    - #### public static Token newDecimal(String text, int startLine, int startColumn)

      Factory method to create a decimal number literal token.

      Parameters:
        `text` - the decimal literal text
        `startLine` - the source line number (1-based)
        `startColumn` - the source column number (1-based)
      Returns:
        a Token representing a decimal number literal

    - #### public static Token newIdentifier(String text, int startLine, int startColumn)

      Factory method to create an identifier token.

      Parameters:
        `text` - the identifier name
        `startLine` - the source line number (1-based)
        `startColumn` - the source column number (1-based)
      Returns:
        a Token representing an identifier

    - #### public static Token newInteger(String text, int startLine, int startColumn)

      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

    - #### public static Token newKeyword(String text, int startLine, int startColumn)

      Factory method to create a keyword token if the given text represents a known keyword.

      Parameters:
        `text` - the text to check as a keyword
        `startLine` - the source line number (1-based)
        `startColumn` - the source column number (1-based)
      Returns:
        a Token with the appropriate keyword type, or `null` if the text is not a keyword

    - #### public static Token newPlaceholder(int meaning)

      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 from Types
      Returns:
        a placeholder Token with empty text and unknown type

    - #### public static Token newString(String text, int startLine, int startColumn)

      Factory method to create a string literal token.

      Parameters:
        `text` - the string content
        `startLine` - the source line number (1-based)
        `startColumn` - the source column number (1-based)
      Returns:
        a Token representing a string literal

    - #### public static Token newSymbol(int type, int startLine, int startColumn)

      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 from Types
        `startLine` - the source line number (1-based)
        `startColumn` - the source column number (1-based)
      Returns:
        a Token representing a symbol

    - #### public static Token newSymbol(String text, int startLine, int startColumn)

      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

    - #### public CSTNode setMeaning(int meaning)

      Sets the meaning (interpretation) for this token. Has no effect on the sentinel tokens (EOF and NULL).

      Parameters:
        `meaning` - the new meaning type from Types
      Returns:
        this token, for convenience chaining

    - #### public void setText(String text)

      Sets the text content of this token. Has no effect on the sentinel tokens (EOF and NULL).

      Parameters:
        `text` - the new text content

    - #### public int size()

      Returns the number of elements in this node, which is always 1 (the token itself).

      Returns:
        1