Function parseError

  • If any of the parsers fail, this parser throws an error. This can be handy for showing more specific error messages to users. For example, if you have a parser that parses these two statements:

    import "foo";
    greet "name"

    And the user gives this input:

    import;
    

    You don't want the parser to fail with a generic message, such as 'all parsers have failed'. You want a more specific error, such as 'expected string after import keyword'.

    You could accomplish that with throwErrorUnless:

    const parser = seqC(
    str("import"),
    captureCaptures(
    throwErrorUnless(
    "expected string after `import` keyword",
    spaces,
    capture(quotedString, "moduleName")
    )
    )
    )

    Type Parameters

    Parameters

    • _message: string

      message to fail with

    • Rest ...parsers: T

      parsers to run

    Returns Parser<MergedCaptures<T>>