Package org.codehaus.groovy.syntax

# Class Reduction

java.lang.Object
org.codehaus.groovy.syntax.CSTNode
org.codehaus.groovy.syntax.Reduction

public class Reduction extends CSTNode
A syntax reduction produced by the Parser during syntactic analysis. Represents a node in the concrete syntax tree (CST) that contains a root `Token` and zero or more child `CSTNode` elements.

See Also:
  - `Token`
  - `CSTNode`
  - `Types`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final Reduction`
  `EMPTY`
  Singleton empty reduction (read-only).

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Reduction(Token root)`
  Constructs a Reduction with the specified root token.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `CSTNode`
  `add(CSTNode element)`
  Adds an element to the end of this reduction.
  `Reduction`
  `asReduction()`
  Creates a `Reduction` from this node.
  `CSTNode`
  `get(int index)`
  Returns the element at the specified index, or `null` if the index is out of bounds.
  `Token`
  `getRoot()`
  Returns the root token of this reduction.
  `boolean`
  `isAnExpression()`
  Returns `true` if this node represents a complete expression.
  `boolean`
  `isEmpty()`
  Returns `true` if this reduction is completely empty (no elements, not even a root).
  `void`
  `markAsExpression()`
  Marks this reduction as a complete expression.
  `static Reduction`
  `newContainer()`
  Creates a new Reduction with `Token.NULL` as its root.
  `CSTNode`
  `remove(int index)`
  Removes the element at the specified index.
  `CSTNode`
  `set(int index, CSTNode element)`
  Sets the element at the specified index.
  `int`
  `size()`
  Returns the number of elements in this reduction (including the root token).

  ### Methods inherited from class org.codehaus.groovy.syntax.CSTNode

  `addChildrenOf, canMean, children, get, getDescription, getMeaning, getMeaningAs, getRoot, getRootText, getStartColumn, getStartLine, getType, hasChildren, isA, isAllOf, isOneOf, setMeaning, toString, write, write`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### EMPTY

    public static final Reduction EMPTY
    Singleton empty reduction (read-only).

- ## Constructor Details

  - ### Reduction

    public Reduction(Token root)
    Constructs a Reduction with the specified root token.
    Parameters:
      `root` - the root `Token` of this reduction

- ## Method Details

  - ### newContainer

    public static Reduction newContainer()
    Creates a new Reduction with `Token.NULL` as its root. Useful for creating container nodes with no specific type.
    Returns:
      a new Reduction with a null root token

  - ### isEmpty

    public boolean isEmpty()
    Returns `true` if this reduction is completely empty (no elements, not even a root).
    Overrides:
      `isEmpty` in class `CSTNode`
    Returns:
      `true` if empty

  - ### size

    public int size()
    Returns the number of elements in this reduction (including the root token).
    Specified by:
      `size` in class `CSTNode`
    Returns:
      the number of elements

  - ### get

    public CSTNode get(int index)
    Returns the element at the specified index, or `null` if the index is out of bounds.
    Specified by:
      `get` in class `CSTNode`
    Parameters:
      `index` - the element index (0 for root)
    Returns:
      the element at the index, or `null` if not found

  - ### getRoot

    public Token getRoot()
    Returns the root token of this reduction. The root is always at index 0 and indicates the syntactic type.
    Specified by:
      `getRoot` in class `CSTNode`
    Returns:
      the root token, or `null` if this reduction is empty

  - ### markAsExpression

    public void markAsExpression()
    Marks this reduction as a complete expression. This flag is used by the parser to distinguish complete expressions from partial constructs.
    Overrides:
      `markAsExpression` in class `CSTNode`

  - ### isAnExpression

    public boolean isAnExpression()
    Returns `true` if this node represents a complete expression. A reduction is considered an expression if it has the `Types.COMPLEX_EXPRESSION` meaning or has been explicitly marked as an expression.
    Overrides:
      `isAnExpression` in class `CSTNode`
    Returns:
      `true` if this is an expression

  - ### add

    public CSTNode add(CSTNode element)
    Adds an element to the end of this reduction.
    Overrides:
      `add` in class `CSTNode`
    Parameters:
      `element` - the element to add
    Returns:
      the added element

  - ### set

    public CSTNode set(int index, CSTNode element)
    Sets the element at the specified index. If the index is beyond the current size, intermediate positions are filled with `null`. The root element (index 0) must be a `Token`.
    Overrides:
      `set` in class `CSTNode`
    Parameters:
      `index` - the element index
      `element` - the element to set
    Returns:
      the set element
    Throws:
      `GroovyBugError` - if trying to set a non-Token as root, or if operating on `EMPTY`

  - ### remove

    public CSTNode remove(int index)
    Removes the element at the specified index. Cannot remove the root element (index 0).
    Parameters:
      `index` - the element index (must be > 0)
    Returns:
      the removed element
    Throws:
      `GroovyBugError` - if trying to remove the root element

  - ### asReduction

    public Reduction asReduction()
    Creates a `Reduction` from this node. Returns self if the node is already a `Reduction`.
    Specified by:
      `asReduction` in class `CSTNode`
    Returns:
      this node as a Reduction