Function chain

  • Generates a new TokenParser or SymbolParser that will chain parsers together one after the other unconditionally in the order specified. It will optionally execute some action on semantic data values returned by parsers and return the semantic value returned by action. At least one parser must be passed. If at least one of the parsers fail, the ParseError will be returned and action will not be executed. If the ParseError needs to be handled, the chain parser can always be nested in a map parser.

    It can be used where there are multiple chains to be done at once, (ex: at grammar rules). It is actually a simple fold operation on the chain parser with an optional action on the end of them.

    It is suggested in cases where chain nesting is used:

     const a: TokenParser<any, any> = str('a');
    const b: TokenParser<any, any> = str('b');
    const ab: TokenParser<any, any> = chain(a, (_) => b);
    const better_ab: TokenParser<any, any> = chain([a, b]);

    The action is made to simulate Bison's "actions" and can be used to define an Abstract Syntax Tree (AST) for the input by changing the semantic data.

    Returns

    A new TokenParser or SymbolParser that will chain parsers and execute them along with an optional action at the end of the chain.

    See

    Type Parameters

    Parameters

    • parsers: [TokenParser<D, E>, ...TokenParser<D, E>[]]

      The parsers to be chained

    • Optional action: ((data: D[]) => D)

      A callback that accepts all semantic values from parsers executions and returns a new semantic value.

        • (data: D[]): D
        • Parameters

          • data: D[]

          Returns D

    Returns TokenParser<D, E>

  • Type Parameters

    Parameters

    • parsers: [SymbolParser<D, E>, ...SymbolParser<D, E>[]]
    • Optional action: ((data: D[]) => D)
        • (data: D[]): D
        • Parameters

          • data: D[]

          Returns D

    Returns SymbolParser<D, E>