Function or

  • or takes an array of parsers and runs them sequentially. It returns the results of the first parser that succeeds. You can use capture in an or:

    const parser = or(capture(digit, "num"), capture(word, "name"));
    

    or supports backtracking by returning a nextParser:

    const parser = or(str("hello"), str("hello!"));

    // this will match the first parser
    const result = parser("hello");

    // but or returns the untried parsers as a new parser
    result.nextParser("hello!"); // works

    // result.nextParser is the same as or(str("hello!"))

    Type Parameters

    Parameters

    • Rest ...parsers: T

      parsers to try

    Returns PickParserType<T>

    • a parser that tries each parser in order. Returns the result of the first parser that succeeds.

Generated using TypeDoc