Capture position information for brace-quoted words

This commit is contained in:
Tangent Wantwight 2023-11-18 18:55:18 -05:00
parent d57f409a13
commit da8429f6df
3 changed files with 23 additions and 12 deletions

View file

@ -47,6 +47,7 @@ exports[`Parsing Notcl Misc Big mess of markup 1`] = `
"pos": 45,
},
{
"pos": 51,
"text": "
This is a paragraph of text, with one [b bold] word. Yes, this means there has to be some magic in text processing... <b>this</b> won't work.
",
@ -80,11 +81,13 @@ exports[`Parsing Notcl Misc Big mess of markup 1`] = `
"pos": 384,
},
{
"pos": 390,
"text": "
First block
",
},
{
"pos": 418,
"text": "
Second block
@ -94,6 +97,7 @@ exports[`Parsing Notcl Misc Big mess of markup 1`] = `
",
},
{
"pos": 534,
"text": "
Since we want escapes to work, these blocks [i will] be subject to substitutions.
",
@ -105,6 +109,7 @@ exports[`Parsing Notcl Misc Big mess of markup 1`] = `
"pos": 651,
},
{
"pos": 656,
"text": "
line endings escaped one slash

View file

@ -303,13 +303,13 @@ b`)
describe("brace words", () => {
it("can parse empty braces", () =>
expect(parse("{}")).toEqual([true, [[{ text: "" }]]]));
expect(parse("{}")).toEqual([true, [[{ text: "", pos: 0 }]]]));
it("can parse braces with text", () =>
expect(parse("{a b c}")).toEqual([true, [[{ text: "a b c" }]]]));
expect(parse("{a b c}")).toEqual([true, [[{ text: "a b c", pos: 0 }]]]));
it("can parse nested braces", () =>
expect(parse("{{}}")).toEqual([true, [[{ text: "{}" }]]]));
expect(parse("{{}}")).toEqual([true, [[{ text: "{}", pos: 0 }]]]));
it("can parse nested braces with text", () =>
expect(parse("{a{b}c}")).toEqual([true, [[{ text: "a{b}c" }]]]));
expect(parse("{a{b}c}")).toEqual([true, [[{ text: "a{b}c", pos: 0 }]]]));
it("must be closed", () =>
expect(parse("{a b")).toMatchObject([false, {}]));
@ -318,32 +318,38 @@ b`)
expect(parse("{}a")).toMatchObject([false, {}]));
it("doesn't count suppressed braces for nesting", () =>
expect(parse(String.raw`{a\{b}`)).toEqual([true, [[{ text: "a\\{b" }]]]));
expect(parse(String.raw`{a\{b}`)).toEqual([
true,
[[{ text: "a\\{b", pos: 0 }]],
]));
it("doesn't count suppressed braces for unnesting", () =>
expect(parse(String.raw`{a\}b}`)).toEqual([true, [[{ text: "a\\}b" }]]]));
expect(parse(String.raw`{a\}b}`)).toEqual([
true,
[[{ text: "a\\}b", pos: 0 }]],
]));
it("nests braces after suppressed backslashes", () =>
expect(parse(String.raw`{a\\{b}}`)).toEqual([
true,
[[{ text: "a\\\\{b}" }]],
[[{ text: "a\\\\{b}", pos: 0 }]],
]));
it("permits newlines in braces", () =>
expect(parse("{\n}")).toEqual([true, [[{ text: "\n" }]]]));
expect(parse("{\n}")).toEqual([true, [[{ text: "\n", pos: 0 }]]]));
it("folds newlines in braces", () =>
expect(
parse(String.raw`{\
}`)
).toEqual([true, [[{ text: " " }]]]));
).toEqual([true, [[{ text: " ", pos: 0 }]]]));
it("doesn't fold newlines in braces with escaped backslashes", () =>
expect(
parse(String.raw`{\\
}`)
).toEqual([true, [[{ text: "\\\\\n" }]]]));
).toEqual([true, [[{ text: "\\\\\n", pos: 0 }]]]));
it("folds newlines in braces with escaped backslashes", () =>
expect(
parse(String.raw`{\\\
}`)
).toEqual([true, [[{ text: "\\\\ " }]]]));
).toEqual([true, [[{ text: "\\\\ ", pos: 0 }]]]));
test.each(['{"}"', '"{"}'])(
"does not permit overlapping braces and quotes {%s}",

View file

@ -78,7 +78,7 @@ const Brace: Pattern<string> = Sequence(
function wordTmpl(bareWordCharRegex: RegExp): Pattern<WordType> {
return Choose<WordType>(
Brace.map((text) => ({ text } as TextWord)),
Brace.map((text, pos) => ({ text, pos } as TextWord)),
QuotedWord,
bareWordTmpl(bareWordCharRegex)
);