Function set

  • Return a parser that takes a key and a value. The parser consumes no input and always succeeds, and returns null as the result. It also returns a captures object with that key-value pair set. This is useful when you need to inject a key-value pair into captures for a seq.

    For example, here is a Markdown heading parser.

    export const headingParser: Parser<Heading> = seqC(
    capture(count(char("#")), "level"),
    spaces,
    capture(many1Till(or(char("\n"), eof)), "content")
    );

    This parser returns

    {
    level: number,
    content: string
    }

    but the type of heading is actually

    type Heading = {
    type: "heading";
    level: number;
    content: string;
    };

    The type key is missing. You can use set to inject the type key-value pair into captures:

    export const headingParser: Parser<Heading> = seqC(
    set("type", "heading"),
    capture(count(char("#")), "level"),
    spaces,
    capture(many1Till(or(char("\n"), eof)), "content")
    );

    Type Parameters

    • const K extends string
    • const V

    Parameters

    • key: K

      key to set on captures object

    • value: V

      value to set on captures object

    Returns CaptureParser<null, Record<K, V>>

Generated using TypeDoc