[]
        
(Showing Draft Content)

SyntaxNode

Class SyntaxNode

java.lang.Object
com.grapecity.documents.excel.expressions.SyntaxNode
Direct Known Subclasses:
NonTerminalNode, TerminalNode

public abstract class SyntaxNode extends Object
Represents a node in a formula syntax tree.

SyntaxNode is the abstract base class for all nodes in a parsed formula expression. A node can be terminal or non-terminal, depending on whether it represents a leaf value or a composite expression that contains nested nodes.

Use this type when working with formula syntax trees returned by parsing APIs or when traversing expression structures programmatically. Child nodes are exposed through getChildren(); terminal nodes return no children, while non-terminal nodes expose their immediate descendants.

This class also serves as the common base for converting syntax trees back to formula text and for implementing tree traversal patterns in derived node types.


 SyntaxNode node = SyntaxNode.Parse("LEFT(sheet1!$B$3,1)");
 List<SyntaxNode> children = node.getChildren();
 String formulaText = node.toString();
 
  • Constructor Details

    • SyntaxNode

      public SyntaxNode()
  • Method Details

    • getChildren

      public abstract List<SyntaxNode> getChildren()
      Gets the child nodes of this syntax node.

      Use this method to traverse the formula syntax tree and inspect the immediate operands or arguments represented by the current node.

      
       SyntaxNode node = SyntaxNode.Parse("SUM(A1:A3)+B1");
       List<SyntaxNode> children = node.getChildren();
       SyntaxNode left = children.get(0);
       SyntaxNode right = children.get(1);
       
      Returns:
      A list that contains the immediate child nodes of this node, or an empty list if this node has no children.
    • toString

      public String toString()
      Returns the formula text represented by this syntax node.
      Overrides:
      toString in class Object
      Returns:
      A String containing the formula text for this syntax node.
    • toString

      public String toString(UnParseContext context)
      Returns the formula text for this node using the specified unparse context.

      Use UnParseContext to control how the syntax node is converted back to a formula string, including the base row, base column, and whether references are written in R1C1 style.

      
       ParseContext parseContext = new ParseContext();
       parseContext.setIsR1C1(true);
       SyntaxNode node = SyntaxNode.Parse("R1C:R8C[4]*9", parseContext);
      
       UnParseContext context = new UnParseContext();
       context.setIsR1C1(true);
       context.setBaseRow(1);
       context.setBaseColumn(7);
       String formulaText = node.toString(context);
       
      Parameters:
      context - The UnParseContext that controls how this node is converted to formula text. Must not be null.
      Returns:
      The formula text for this node.
      Throws:
      NullPointerException - if context is null.
    • Parse

      public static SyntaxNode Parse(String text)
      Loads a SyntaxNode from a string that contains a formula.

      This method calls Parse(String,ParseContext) with the default parse context.

      If text is null or empty, this method returns EmptyNode.GetInstance().

      
       SyntaxNode node = SyntaxNode.Parse("SUM(A1:A3)+B1");
       String formulaText = node.toString();
       
      Parameters:
      text - A string that contains the formula to parse. This value can be null or empty.
      Returns:
      A SyntaxNode populated from the specified formula text. Returns EmptyNode.GetInstance() if text is null or empty.
      Throws:
      IllegalArgumentException - if text contains invalid formula syntax.
    • Parse

      public static SyntaxNode Parse(String text, ParseContext context)
      Loads a SyntaxNode from a formula string using the specified ParseContext.

      Use the parse context to control how references in the formula are interpreted, including the 0-based base row, 0-based base column, and whether the formula uses R1C1 reference style.

      If text is null or empty, this method returns the singleton empty node. If the formula text is invalid, this method throws an IllegalArgumentException.

      
       ParseContext context = new ParseContext();
       context.setBaseRow(1);
       context.setBaseColumn(7);
       context.setIsR1C1(true);
      
       SyntaxNode node = SyntaxNode.Parse("R1C:R8C[4]*9", context);
       String formula = node.toString();
       
      Parameters:
      text - The formula text to parse. If null or empty, this method returns an empty node.
      context - The parse context that specifies the base row, base column, and whether the formula uses R1C1 reference style.
      Returns:
      A SyntaxNode populated from the specified formula text, or the empty node if text is null or empty.
      Throws:
      IllegalArgumentException - if text is not a valid formula.
    • equals

      public boolean equals(Object obj)
      Checks whether this syntax node is equal to another object.

      If obj is a SyntaxNode, this method compares it with the current node. If obj is not a SyntaxNode, this method returns false.

      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare with the current syntax node. This can be another SyntaxNode or any other object.
      Returns:
      true if obj is the same instance as the current node or an equivalent SyntaxNode; otherwise, false.
    • hashCode

      public int hashCode()
      Returns the hash code of the current node type.

      This object does not support a content-based hash code because its fields are mutable. The returned value is the hash code of the node's runtime type.

      Overrides:
      hashCode in class Object
      Returns:
      The hash code of the current node type.
    • clone

      public final SyntaxNode clone()
      Clones this syntax node.

      This method creates a copy of the current formula syntax node by traversing the syntax tree and rebuilding the node structure. If the node is mutable, the returned value is a cloned node. Otherwise, this method returns an equivalent node from the object pool.

      Overrides:
      clone in class Object
      Returns:
      A cloned SyntaxNode if this node is mutable; otherwise, an equivalent node from the object pool.