Function captureCaptures

  • captureCaptures lifts your captures up a level. Suppose you have a parser like this:

    const greeting = seqC(
    str("hello"),
    spaces,
    capture(word, "name"),
    str("!"),
    spaces,
    capture(manynTillStr("?"), "secondPart")
    )

    This parses a greeting like "hello Adit! How was your day?" into:

    ```ts
    {
    name: "Adit",
    secondPart: "How was your day?"
    }

    Now, suppose you decide to refactor this parser into two parsers:

    const firstPart = seqC(
    str("hello"),
    spaces,
    capture(word, "name"),
    str("!")
    )

    const secondPart = seqC(
    spaces,
    capture(manyTillStr("?"), "secondPart")
    )

    And put them together:

    const greeting = seqC(
    firstPart,
    secondPart
    )

    Unfortunately, this will no longer return an object in that shape, because it's not actually capturing anything. Captures in firstPart and secondPart won't get propagated up. Suppose you try to use capture like this:

    const greeting = seqC(
    capture(firstPart, "firstPart"),
    capture(secondPart, "secondPart")
    )

    Now you'll get an object that looks like this instead:

    {
    firstPart: {
    name: "Adit"
    },
    secondPart: {
    secondPart: "How was your day?"
    }
    }

    What you want is for the captures in the child parsers to get merged up to the parent parser. For that, use captureCaptures:

    const greeting = seqC(
    captureCaptures(firstPart),
    captureCaptures(secondPart)
    )

    @param parser - parser to capture captures from @returns - the parser's result set as the captures object

    Type Parameters

    Parameters

    Returns CaptureParser<T, T>

Generated using TypeDoc