captureCaptures lifts your captures up a level. Suppose you have a parser like this:
captureCaptures
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?"} Copy
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")) Copy
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) Copy
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:
firstPart
secondPart
capture
const greeting = seqC( capture(firstPart, "firstPart"), capture(secondPart, "secondPart")) Copy
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?" }} Copy
{ 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)) Copy
const greeting = seqC( captureCaptures(firstPart), captureCaptures(secondPart))
@param parser - parser to capture captures from @returns - the parser's result set as the captures object
Generated using TypeDoc
captureCaptures
lifts your captures up a level. Suppose you have a parser like this:Now, suppose you decide to refactor this parser into two parsers:
And put them together:
Unfortunately, this will no longer return an object in that shape, because it's not actually capturing anything. Captures in
firstPart
andsecondPart
won't get propagated up. Suppose you try to usecapture
like this:Now you'll get an object that looks like this instead:
What you want is for the captures in the child parsers to get merged up to the parent parser. For that, use
captureCaptures
:@param parser - parser to capture captures from @returns - the parser's result set as the captures object