Skip to content

This site describes solve-engine as it is on main: 2.43.0, which npm does not have yet. npm installs 2.40.0, so a page may show an answer that version does not give yet.

PrecedenceParser

Defined in: packages/engine/src/parser/PrecedenceParser.ts:59

── Hybrid Precedence Climbing Parser ─────────────────────────────────────────

Two-tier dispatch strategy:

Tier 1 (Fast Path): Inline switch on token.typeId for built-in operators. - Prefix: NUMBER, IDENT, LPAREN, MINUS, PLUS, STRING - Infix: checked via static BP_TABLE (Uint8Array indexed by typeId) - No Map.get(), no parselet function call overhead for ~95% of tokens

Tier 2 (Plugin Path): ParseletRegistry fallback for custom/plugin tokens. - Prefix: Map.get(tokenTypeId) for PrefixParselet - Infix: Map.get(tokenTypeId) for InfixParselet - Full flexibility for custom syntax

The parser emits directly to a BytecodeBuilder, no AST intermediate. Implements the same public API as the legacy Parser class so existing parselets continue to work without modification.

new PrecedenceParser(
parseletRegistry,
maxDepth?,
localeCode?,
pluginFunctionIndex?,
calendar?
): PrecedenceParser;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:172

ParameterTypeDefault value
parseletRegistryParseletRegistryundefined
maxDepthnumber50
localeCodestring"en"
pluginFunctionIndex?ReadonlyMap<string, number>undefined
calendar?CalendarBackendDATE_CALENDAR

PrecedenceParser

infixMinBindingPower: number = 0;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:100

The binding power the current infix parselet is being invoked at, i.e. the minBp of the expression it sits inside. Set immediately before each Tier-2 parselet runs. A parselet that decides whether to swallow a following loose operator (UomLiteralParselet with a trailing in/to) reads this to respect precedence it cannot otherwise see: 120 km / 2 hours in kph must group as (120 km / 2 hours) in kph, not 120 km / (2 hours in kph).


readonly static BP_TABLE: Uint8Array;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:121

Static binding power table, built once at module load, shared across all instances. Index = tokenTypeId, value = binding power (0 = not a built-in infix).

consume(expectedType?): Token;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1026

ParameterType
expectedType?string

Token


getCalendar(): CalendarBackend;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:202

The calendar backend a parselet reads a date with at parse time: the engine’s own, so a fused literal reads back through the backend that built it. See the field’s doc for the sites that need it.

CalendarBackend


getLocaleCode(): string;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:193

Get locale code for NumberParselet to normalize separators

string


load(tokens, hasParens?): void;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:220

Load tokens for parsing. Identical to Parser.load().

ParameterTypeDescription
tokensToken[]-
hasParens?booleanif false, skips the O(n) paren balance scan (~90% of expressions)

void


match(expectedType): boolean;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1052

ParameterType
expectedTypestring

boolean


parseExpression(minBp?, _builder?): void;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:290

Parse a full expression starting at the current token position.

ParameterTypeDefault valueDescription
minBpnumber0minimum binding power (precedence climbing threshold). For left-associative operators, the recursive call uses bp + 1. For right-associative operators (^), it uses bp - 1.
_builder?BytecodeBuilderundefinedaccepted for parselet API compatibility; always uses this.builder.

void


peek(): Token | undefined;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1061

Token | undefined


peekAt(offset): Token | undefined;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1085

Read-only lookahead offset tokens past the current position, without consuming anything, peekAt(0) is equivalent to peek. this.tokens is a plain in-memory array (not a stream), so this is a simple, safe index read; no rewind/checkpoint mechanism is needed since nothing is consumed.

ParameterType
offsetnumber

Token | undefined


previous(): Token | undefined;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1089

Token | undefined


setBuilder(builder): void;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:1102

Set the builder to use for the current parse. Called by ExpressionEngine.

ParameterType
builderBytecodeBuilder

void


setDiagnosticPipeline(pipeline, expression): void;

Defined in: packages/engine/src/parser/PrecedenceParser.ts:210

Set the diagnostic pipeline for parselet matching events. Cleared after each non-cached parse to avoid holding refs.

ParameterType
pipelineDiagnosticPipeline | undefined
expressionstring

void