Function optional

  • It defines a TokenParser that will match parser if it matches. If parser does not match, it will not consume any input and stay in place.

    It works like the question mark ( ? ) operator in regular expressions.

    The same behavior is implemented for SymbolParser in the following way:

     const optional_a: SymbolParser<null, null> = alternatives([
    recursive(str('a')),
    empty,
    ]);

    Returns

    A new TokenParser that will optionally match parser.

    Type Parameters

    Parameters

    • parser: TokenParser<D, E>

      The parser to be optionally matched.

    Returns TokenParser<D, E>