Class SymbolParser<D, E>

A parser that augments the capabilities of TokenParser to recognise grammars with:

  • Recursion
  • Ambiguity When simpler grammars are to be parsed, that do not involve recursion or ambiguity, it is suggested to use a TokenParser instead.

It can be used to define any Generalised LL (GLL) parser

It uses a ParseStack stack structure along with localised bookeeping to provide the capabilities needed for GLL parsing. Localised Bookeeping: An alternative to the Graph Structured Stack (GSS) needed for GLL parsing. Each parser uses localised memoisation to map parse inputs to outputs produced by the parser and continuations that act on those outputs, assigned to the parser's ContinuationParseStateTransformer funtions over the execution. It also checks the ParseStack before pushing it's ContinuationParseStateTransformer, so that it does not gets executed twice for the same data. This effectively makes the parser act as a localised GSS node and eliminates the need for more centeralised data structures, while achieving the O(n^3) desired complexity the GSS data structure promises in GLL parsing

All parsers of this type resolve their parsing in O(n^3) worse time to the input. They all return one or more valid parses (ambiguous grammars have more than one valid parses) or an error. If recursive grammars are parsed, only the largest parse possible will be returned (greedy approach). Spurious ambiguity is pruned and all results with the same semantics are considered the same result. Essential ambiguity on the other hand is fully supported and if 2 results have different semantics, they will both be returned. This comes at a cost, as now the worst case rises to exponential O(2^k) complexity, where k all different results with different semantics.

Spurious ambiguity: A sentence is spuriously ambiguous if all its parse trees have the exact same semantics Essential ambiguity: A sentence is essentially ambiguous if it has at least 2 parse trees with different semantics

A constant parameter MAX_AMBIGUITY_BREADTH is employed to keep the breadth of possible tree production under control and prevent stack overflows.

Localised Memoisation is employed on the parser level to store intermediate results for specific inputs and prevent recalculations and loops that might occur. There exists an automatic flush policy on each parser where the memoisation tables will be flushed each time the target of parsing changes, where it usually means that a new parsing is requested and, thus, the old memoisation table entries are now irrelevant. This is there to prevent heap allocation issues that might occur.

Type Parameters

Hierarchy

  • SymbolParser

Constructors

Properties

currentTarget: string = ''

The current target for the current execution.

Each execution has a unique target that stays the same during execution. When the target changes, we know that a new execution takes place and can clear the memotable.

memotable: Map<string, {
    continuations: Continuation<D, E>[];
    results: Map<string, ParseState<D, E>>;
}> = ...

A map from ParseState inputs to ParseState outputs produced by the parser, for those inputs and Continuation functions to which those outputs are to be passed.

Semantic values from input states are also used to distinguish different parsing paths to provide support for essential ambiguity

The state transformer function that performs the parser logic.

MAX_AMBIGUITY_BREADTH: number = Infinity

Restricts the maximum number of possible parse tree branches to the value specified.

If it is defined as Infinity, then no restriction is applied.

Methods

  • Runs the parser for the specified target and optional index and data asynchronously

    Returns a promise that resolves to an array that contains all ParseState results produced.

    Returns

    A promise which resolves to an array containing all ParseState results or an error.

    Parameters

    • target: string

      The complete input to be parsed.

    • Optional data: D

      An optional initial semantic value. Defaults to null

    • Optional index: number

      An optional initial index in the target. Defaults to 0.

    Returns Promise<ParseState<D, E>[]>

  • Runs the parser for the specified target, with optional index and data semantic value.

    It returns a Generator which will lazily generate all possible parse results from the parser execution, one by one. If the parser parses an infinitely ambiguous grammar, the generator will always produce new results.

    Returns

    A Generator which produces new ParseState results on each iteration.

    Parameters

    • target: string

      The complete input to be parsed.

    • Optional data: D

      An optional initial semantic value. Defaults to null

    • Optional index: number

      An optional initial index in the target. Defaults to 0.

    Returns Generator<ParseState<D, E>, ParseState<D, E>[], undefined>

  • Runs the parser for the specified input target, with optional index and data semantic value.

    Returns an array that contains all the ParseState results produced.

    Returns

    An array containing all ParseState results or an error.

    Parameters

    • target: string

      The complete input to be parsed.

    • Optional data: D

      An optional initial semantic value. Defaults to null

    • Optional index: number

      An optional initial index in the target. Defaults to 0.

    Returns ParseState<D, E>[]

  • Defines a SymbolParser that will execute parserThunk to get a parser and execute it.

    It employs localised memoisation of the parser returned by parserThunk so that it does not have to be redefined on every execution.

    It's purpose is to defer the execution of a SymbolParser constructor for later, in cases where it contains a reference to itself (i.e. for recursion) It can be used to construct recursive parsers that would otherwise could not be possible to define due to JavaScript's eager evaluation.

    Returns

    A new SymbolParser that will lazily execute parserThunk's returned parser.

    Type Parameters

    Parameters

    Returns SymbolParser<D, E>