The OpenLanguage.WordprocessingML.Ast namespace contains the core nodes that form the Abstract Syntax Tree (AST) for parsed expressions and field instructions.
The abstract base class for all AST nodes. It defines the fundamental structure for tree traversal and reconstruction.
Children<O>(): Enumerates child nodes of a specific type.ReplaceChild(int index, Node replacement): Replaces a child node at a given index.ToString(): Reconstructs the node's original string representation, including whitespace.ValueString(): Gets the node's content without leading/trailing whitespace.
The base class for all nodes that can be evaluated to a value. It inherits from Node and adds:
LeadingWhitespaceandTrailingWhitespace: Lists ofWhitespaceNodes for round-trip fidelity.Precedence: An integer value for handling operator precedence.
StringLiteralNode: Represents literal text.NumericLiteralNode<T>: Represents numbers (integer, float, etc.).CharacterLiteralNode: Base for single characters like operators and delimiters.PlusLiteralNode,MinusLiteralNode,CommaNode, etc.
IdentifierNode: Represents unquoted identifiers, like bookmark names.Quoted<T>: Wraps a node that was enclosed in quotes.ParenthesizedExpressionNode: Represents an expression enclosed in().BracedExpressionNode: Represents an expression enclosed in{}.BracketedExpressionNode: Represents an expression enclosed in[].
UnaryOperatorNode: Base class for operators with one operand (e.g.,-5).UnaryMinusNode,UnaryPlusNode.
BinaryOperatorNode: Base class for operators with two operands (e.g.,A + B).AddNode,SubtractNode,MultiplyNode,DivideNode,EqualNode, etc.
FunctionNode: Base class for a function's name (e.g.,SUM).FunctionCallNode: Base class for a complete function call (e.g.,SUM(A1, B2)).ExpressionListNode: Represents a comma-delimited list of arguments in a function call.
MergeFieldNode: Represents a«MERGEFIELD»placeholder.
CellReferenceNode: Base class for nodes that refer to table cells.A1CellNode: Represents a cell in A1-style notation (e.g.,A1).CellRangeNode: Represents a range of cells (e.g.,A1:B2).TableReferenceNode: Represents a reference to a table by name.
The AST is designed to be fully navigable and mutable.
using OpenLanguage.WordprocessingML.Ast;
using OpenLanguage.WordprocessingML.Expression;
// Parse an expression to get an AST
var ast = ExpressionParser.Parse("(1 + 2) * 3");
// Traverse the AST
if (ast is MultiplyNode multiply)
{
Console.WriteLine($"Operator: {multiply.Operator}"); // *
if (multiply.LeftOperand is ParenthesizedExpressionNode p)
{
// Modify a node
if (p.Inner is AddNode add)
{
add.RightOperand = new NumericLiteralNode<int>("5", 5, "D");
}
}
}
// Reconstruct the modified expression
Console.WriteLine(ast.ToString()); // (1 + 5) * 3This structured AST allows for complex analysis, transformation, and validation of WordprocessingML expressions and field instructions.