From da8429f6dfafa0342903eb9c733cee5e1300b1fd Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 18 Nov 2023 18:55:18 -0500 Subject: [PATCH] Capture position information for brace-quoted words --- src/__snapshots__/parser.test.ts.snap | 5 +++++ src/parser.test.ts | 28 ++++++++++++++++----------- src/parser.ts | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/__snapshots__/parser.test.ts.snap b/src/__snapshots__/parser.test.ts.snap index dc0145e..ad0fc65 100644 --- a/src/__snapshots__/parser.test.ts.snap +++ b/src/__snapshots__/parser.test.ts.snap @@ -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... this 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 diff --git a/src/parser.test.ts b/src/parser.test.ts index 0119664..d213652 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -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}", diff --git a/src/parser.ts b/src/parser.ts index 2c01a68..5b0a299 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -78,7 +78,7 @@ const Brace: Pattern = Sequence( function wordTmpl(bareWordCharRegex: RegExp): Pattern { return Choose( - Brace.map((text) => ({ text } as TextWord)), + Brace.map((text, pos) => ({ text, pos } as TextWord)), QuotedWord, bareWordTmpl(bareWordCharRegex) );