Table of Contents

Class ParseTreeNavigator

Namespace
Utils.Parser.Runtime
Assembly
Utils.Parser.dll

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

node ParseNode

Properties

IsError

true when the current node is an ErrorNode.

public bool IsError { get; }

Property Value

bool

IsLexer

true when the current node is a LexerNode.

public bool IsLexer { get; }

Property Value

bool

IsParser

true when the current node is a ParserNode.

public bool IsParser { get; }

Property Value

bool

this[int]

Shorthand for Child(int): navigates to the child at index.

public ParseTreeNavigator this[int index] { get; }

Parameters

index int

Property Value

ParseTreeNavigator

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

ParseNode

RawChildren

The direct children of the current node when it is a ParserNode; null otherwise.

public IReadOnlyList<ParseNode>? RawChildren { get; }

Property Value

IReadOnlyList<ParseNode>

RuleName

Name of the grammar rule that produced the current node.

public string RuleName { get; }

Property Value

string

Token

The token of the current node when it is a LexerNode; null otherwise.

public Token? Token { get; }

Property Value

Token

Methods

Child(int)

Navigates to the child at position index among the direct children of the current node.

public ParseTreeNavigator Child(int index)

Parameters

index int

Zero-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

ruleName string

Rule 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

IEnumerable<ParseTreeNavigator>

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

ruleName string

Returns

IEnumerable<ParseTreeNavigator>

Descendant(string)

Navigates to the first descendant whose rule name equals ruleName (depth-first).

public ParseTreeNavigator Descendant(string ruleName)

Parameters

ruleName string

Returns

ParseTreeNavigator

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

IEnumerable<ParseTreeNavigator>

Descendants(string)

Returns navigators for every descendant whose rule name equals ruleName, in depth-first pre-order.

public IEnumerable<ParseTreeNavigator> Descendants(string ruleName)

Parameters

ruleName string

Returns

IEnumerable<ParseTreeNavigator>

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

index int

Returns

ParseTreeNavigator

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

ruleName string

Returns

ParseTreeNavigator

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

ruleName string

Returns

ParseTreeNavigator

Operators

implicit operator ParseTreeNavigator(ParseNode)

Wraps a ParseNode in a new navigator.

public static implicit operator ParseTreeNavigator(ParseNode node)

Parameters

node ParseNode

Returns

ParseTreeNavigator

implicit operator ParseNode(ParseTreeNavigator)

Unwraps the underlying ParseNode.

public static implicit operator ParseNode(ParseTreeNavigator nav)

Parameters

nav ParseTreeNavigator

Returns

ParseNode