Table of Contents

Class ParseTreeCompiler<TContext, TResult>

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

Generic depth-first parse-tree compiler that separates traversal into two ordered phases for each node:

  1. Descent (top-down) Called when the node is first reached. Handlers can enrich the context for the subtree below — for example pushing a new scope, recording parameter names, or resolving a function signature before its body is compiled.
  2. Ascent (bottom-up) Called after all children have been compiled. Handlers receive the ordered list of child results and must return the compiled result for this node — for example folding two sub-expressions into a binary operation.

Handlers can be registered by grammar-rule name (exact match, O(1) lookup) or by an arbitrary predicate over the ParseTreeNavigator (checked in registration order). Rule-name handlers are always checked first; predicate handlers are tried only when no name-based handler matches. Fall-back Default* handlers are consulted last.

The context is treated as immutable per descent step: each descent handler receives the context that was active when the node was entered and must return the context to hand to the node's children. Returning the same instance is always valid; creating a derived instance (e.g. a new scope) isolates sibling subtrees.

public sealed class ParseTreeCompiler<TContext, TResult>

Type Parameters

TContext

Descending context type (e.g. a symbol table, a scope, a type environment).

TResult

Result type returned by the ascent phase (e.g. an AST node, an expression, a value).

Inheritance
ParseTreeCompiler<TContext, TResult>
Inherited Members
Extension Methods

Methods

Compile(ParseNode, TContext)

Compiles the parse tree rooted at root, starting with initialContext as the top-level context.

public TResult? Compile(ParseNode root, TContext initialContext)

Parameters

root ParseNode

Root node of the parse tree.

initialContext TContext

Context to pass to the root node's descent handler.

Returns

TResult

The result produced by the root node's ascent handler.

DefaultAscend(Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?>)

Sets the fallback ascent handler used when no rule-specific handler is registered.

Defaults to returning default(TResult).

public ParseTreeCompiler<TContext, TResult> DefaultAscend(Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?> handler)

Parameters

handler Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult>, TResult>

Returns

ParseTreeCompiler<TContext, TResult>

DefaultDescend(Func<ParseTreeNavigator, TContext, TContext>)

Sets the fallback descent handler used when no rule-specific handler is registered.

Defaults to passing the parent context through to children unchanged.

public ParseTreeCompiler<TContext, TResult> DefaultDescend(Func<ParseTreeNavigator, TContext, TContext> handler)

Parameters

handler Func<ParseTreeNavigator, TContext, TContext>

Returns

ParseTreeCompiler<TContext, TResult>

OnAscend(Func<ParseTreeNavigator, bool>, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?>)

Registers a compilation handler called when ascending from nodes for which predicate returns true.

Predicate-based handlers are checked in registration order, only after no rule-name handler has matched the current node. The first matching predicate wins.

public ParseTreeCompiler<TContext, TResult> OnAscend(Func<ParseTreeNavigator, bool> predicate, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?> handler)

Parameters

predicate Func<ParseTreeNavigator, bool>

Function that receives the current node's navigator and returns true when this handler should be applied.

handler Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult>, TResult>

Function (navigator, contextAtEntry, childResults) → result.

Returns

ParseTreeCompiler<TContext, TResult>

OnAscend(Func<ParseTreeNavigator, bool>, Func<ParseTreeNavigator, TContext, TResult?>)

Convenience overload of OnAscend(Func<ParseTreeNavigator, bool>, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?>) for nodes that do not need to inspect child results.

public ParseTreeCompiler<TContext, TResult> OnAscend(Func<ParseTreeNavigator, bool> predicate, Func<ParseTreeNavigator, TContext, TResult?> handler)

Parameters

predicate Func<ParseTreeNavigator, bool>

Selector function.

handler Func<ParseTreeNavigator, TContext, TResult>

Function (navigator, contextAtEntry) → result.

Returns

ParseTreeCompiler<TContext, TResult>

OnAscend(string, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?>)

Registers a compilation handler called when ascending from nodes whose rule name equals ruleName.

The handler receives the node navigator, the context that was active when the node was entered, and the ordered list of results produced by its children (null entries appear when a child produced no result).

public ParseTreeCompiler<TContext, TResult> OnAscend(string ruleName, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?> handler)

Parameters

ruleName string

Grammar rule name to match.

handler Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult>, TResult>

Function (navigator, contextAtEntry, childResults) → result.

Returns

ParseTreeCompiler<TContext, TResult>

Exceptions

InvalidOperationException

Thrown when an ascent handler is already registered for ruleName.

OnAscend(string, Func<ParseTreeNavigator, TContext, TResult?>)

Convenience overload of OnAscend(string, Func<ParseTreeNavigator, TContext, IReadOnlyList<TResult?>, TResult?>) for nodes that do not need to inspect child results — typically leaf (lexer) nodes or transparent pass-through rules.

public ParseTreeCompiler<TContext, TResult> OnAscend(string ruleName, Func<ParseTreeNavigator, TContext, TResult?> handler)

Parameters

ruleName string

Grammar rule name to match.

handler Func<ParseTreeNavigator, TContext, TResult>

Function (navigator, contextAtEntry) → result.

Returns

ParseTreeCompiler<TContext, TResult>

OnDescend(Func<ParseTreeNavigator, bool>, Func<ParseTreeNavigator, TContext, TContext>)

Registers a context-enrichment handler called when descending into nodes for which predicate returns true.

Predicate-based handlers are checked in registration order, only after no rule-name handler has matched the current node. The first matching predicate wins.

public ParseTreeCompiler<TContext, TResult> OnDescend(Func<ParseTreeNavigator, bool> predicate, Func<ParseTreeNavigator, TContext, TContext> handler)

Parameters

predicate Func<ParseTreeNavigator, bool>

Function that receives the current node's navigator and returns true when this handler should be applied.

handler Func<ParseTreeNavigator, TContext, TContext>

Function (navigator, parentContext) → contextForChildren.

Returns

ParseTreeCompiler<TContext, TResult>

OnDescend(string, Func<ParseTreeNavigator, TContext, TContext>)

Registers a context-enrichment handler called when descending into nodes whose rule name equals ruleName.

The handler receives the node navigator and the context inherited from the parent, and must return the context to pass to this node's children. Returning the parent context unchanged is always valid.

public ParseTreeCompiler<TContext, TResult> OnDescend(string ruleName, Func<ParseTreeNavigator, TContext, TContext> handler)

Parameters

ruleName string

Grammar rule name to match.

handler Func<ParseTreeNavigator, TContext, TContext>

Function (navigator, parentContext) → contextForChildren.

Returns

ParseTreeCompiler<TContext, TResult>

Exceptions

InvalidOperationException

Thrown when a descent handler is already registered for ruleName.

OnError(Func<ParseTreeNavigator, TContext, TResult?>)

Sets the handler invoked when an ErrorNode is encountered anywhere in the tree during traversal.

The default behaviour is to return default(TResult) without throwing.

public ParseTreeCompiler<TContext, TResult> OnError(Func<ParseTreeNavigator, TContext, TResult?> handler)

Parameters

handler Func<ParseTreeNavigator, TContext, TResult>

Returns

ParseTreeCompiler<TContext, TResult>