Class TokenParser<D, E>

A parser with recursive-descent like parsing capabilities.

It can be used to define any LL(k) parser with arbitrary k lookahead, but it falls short of recognising grammars with

  • Recursion
  • Ambiguity If those capabilities are required, consider using a SymbolParser.

It uses a simple approach to parsing where each parser applies a ParseStateTransformer to the input ParseState and returns the result of the ParseStateTransformer transformation.

All parsers of this type resolve their parsing in O(n) linear time to the input. They will all return a single parse result (no ambiguity is supported) or an error.

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

  • TokenParser

Constructors

Properties

Methods

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, ParseState<D, E>> = ...

A memoisation table to map ParseState inputs to ParseState outputs produced by the parser, for those inputs.

transformer: ParseStateTransformer<D, E>

The state transformer function that performs the parser logic.

Methods

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

    Returns a promise that resolves to an array that contains a single ParseState result for compatibility with the SymbolParser interface.

    Returns

    A promise which resolves to an array containing a single ParseState with the parse result 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 this parser for the specified input target, with optional index and data semantic value.

    Returns an array that contains a single ParseState result for compatibility with the SymbolParser interface.

    Returns

    An array containing a single ParseState with the parse result 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>[]