Class ParseTreeNavigator
Provides fluent, index- and name-based navigation over a ParseNode tree produced by ParserEngine.
Each navigation method returns a new ParseTreeNavigator wrapping the target node so that calls can be chained:
var token = nav[0].Child("additionExp")[1][0].Token;
Methods that cannot guarantee a result have a Try variant that returns
null instead of throwing.
public sealed class ParseTreeNavigator
- Inheritance
-
ParseTreeNavigator
- Inherited Members
- Extension Methods
Constructors
ParseTreeNavigator(ParseNode)
Initialises a navigator wrapping node.
public ParseTreeNavigator(ParseNode node)
Parameters
nodeParseNode
Properties
IsError
true when the current node is an ErrorNode.
public bool IsError { get; }
Property Value
IsLexer
true when the current node is a LexerNode.
public bool IsLexer { get; }
Property Value
IsParser
true when the current node is a ParserNode.
public bool IsParser { get; }
Property Value
this[int]
Shorthand for Child(int): navigates to the child at
index.
public ParseTreeNavigator this[int index] { get; }
Parameters
indexint
Property Value
Exceptions
- InvalidOperationException
Thrown when the current node is not a ParserNode or the index is out of range.
Node
The node currently wrapped by this navigator.
public ParseNode Node { get; }
Property Value
RawChildren
The direct children of the current node when it is a ParserNode;
null otherwise.
public IReadOnlyList<ParseNode>? RawChildren { get; }
Property Value
RuleName
Name of the grammar rule that produced the current node.
public string RuleName { get; }
Property Value
Token
The token of the current node when it is a LexerNode;
null otherwise.
public Token? Token { get; }
Property Value
Methods
Child(int)
Navigates to the child at position index among the direct
children of the current node.
public ParseTreeNavigator Child(int index)
Parameters
indexintZero-based child index.
Returns
- ParseTreeNavigator
A new navigator wrapping the selected child.
Exceptions
- InvalidOperationException
Thrown when the current node is not a ParserNode or the index is out of range.
Child(string)
Navigates to the first child whose rule name equals ruleName.
Direct children are searched first; QuantifierNode wrappers are
then searched recursively so that optional or repeated grammar elements
(rule?, rule*, rule+) and grouped sequences
((a b)?) are reachable by name.
public ParseTreeNavigator Child(string ruleName)
Parameters
ruleNamestringRule name to search for.
Returns
- ParseTreeNavigator
A new navigator wrapping the matching child.
Exceptions
- InvalidOperationException
Thrown when no matching child is found or the current node is not a ParserNode.
Children()
Returns navigators for all direct children of the current node. Returns an empty sequence when the current node is not a ParserNode.
public IEnumerable<ParseTreeNavigator> Children()
Returns
Children(string)
Returns navigators for all children whose rule name equals
ruleName.
Direct children are yielded first; QuantifierNode wrapper contents
are then enumerated (recursively for grouped sequences) so that optional or
repeated grammar elements are reachable by name.
Returns an empty sequence when the current node is not a ParserNode
or no matching child exists.
public IEnumerable<ParseTreeNavigator> Children(string ruleName)
Parameters
ruleNamestring
Returns
Descendant(string)
Navigates to the first descendant whose rule name equals
ruleName (depth-first).
public ParseTreeNavigator Descendant(string ruleName)
Parameters
ruleNamestring
Returns
Exceptions
- InvalidOperationException
Thrown when no matching descendant is found.
Descendants()
Returns navigators for every descendant of the current node in depth-first pre-order (the current node itself is excluded).
public IEnumerable<ParseTreeNavigator> Descendants()
Returns
Descendants(string)
Returns navigators for every descendant whose rule name equals
ruleName, in depth-first pre-order.
public IEnumerable<ParseTreeNavigator> Descendants(string ruleName)
Parameters
ruleNamestring
Returns
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
TryChild(int)
Navigates to the child at position index, or returns
null when the current node is not a ParserNode or the
index is out of range.
public ParseTreeNavigator? TryChild(int index)
Parameters
indexint
Returns
TryChild(string)
Navigates to the first child whose rule name equals ruleName,
or returns null when none is found or the current node is not a
ParserNode.
Direct children have priority; QuantifierNode wrapper contents are
searched only when no direct match exists. Grouped-sequence frames inside a
QuantifierNode are traversed recursively.
public ParseTreeNavigator? TryChild(string ruleName)
Parameters
ruleNamestring
Returns
TryDescendant(string)
Navigates to the first descendant whose rule name equals
ruleName, or returns null when none is found.
public ParseTreeNavigator? TryDescendant(string ruleName)
Parameters
ruleNamestring
Returns
Operators
implicit operator ParseTreeNavigator(ParseNode)
Wraps a ParseNode in a new navigator.
public static implicit operator ParseTreeNavigator(ParseNode node)
Parameters
nodeParseNode
Returns
implicit operator ParseNode(ParseTreeNavigator)
Unwraps the underlying ParseNode.
public static implicit operator ParseNode(ParseTreeNavigator nav)