From 1b8929657add2697d891722bb08b2fdbf2a5d40b Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Thu, 31 Aug 2023 22:58:44 -0400 Subject: [PATCH] Simplify parsing of enchanted words- special case grammar not needed --- src/parser.test.ts | 28 +++++++++++++++++++--------- src/parser.ts | 8 +------- src/words.ts | 1 + 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/parser.test.ts b/src/parser.test.ts index de9d893..24e56e2 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -85,14 +85,12 @@ b`) ).toEqual([true, []])); }); - describe("enchanted words", () => { + describe("interpolated words", () => { it("can parse a simple word", () => expect(parse("a")).toEqual([true, [[{ enchanted: "a" }]]])); it("can parse a word with non-special punctuation", () => expect(parse("-switch")).toEqual([true, [[{ enchanted: "-switch" }]]])); - }); - describe("interpolated words", () => { it("accepts empty quotes", () => expect(parse('""')).toEqual([true, [[{ text: "" }]]])); it("accepts quoted words", () => @@ -112,9 +110,9 @@ b`) expect(parse("a\\ b")).toEqual([true, [[{ text: "a b" }]]])); it("treats a non-leading quote as a plain character", () => - expect(parse('a"')).toEqual([true, [[{ text: 'a"' }]]])); + expect(parse('a"')).toEqual([true, [[{ enchanted: 'a"' }]]])); it("treats a non-leading brace as a plain character", () => - expect(parse("a{")).toEqual([true, [[{ text: "a{" }]]])); + expect(parse("a{")).toEqual([true, [[{ enchanted: "a{" }]]])); it("treats an escaped quote as a plain character", () => expect(parse('\\"')).toEqual([true, [[{ text: '"' }]]])); it("treats an escaped brace as a plain character", () => @@ -195,7 +193,13 @@ b`) it("can parse pre-word command interpolations", () => expect(parse("[a]b")).toEqual([ true, - [[{ pieces: [{ script: [[{ enchanted: "a" }]] }, { text: "b" }] }]], + [ + [ + { + pieces: [{ script: [[{ enchanted: "a" }]] }, { enchanted: "b" }], + }, + ], + ], ])); it("can parse mid-word command interpolations", () => expect(parse("a[b]c")).toEqual([ @@ -204,9 +208,9 @@ b`) [ { pieces: [ - { text: "a" }, + { enchanted: "a" }, { script: [[{ enchanted: "b" }]] }, - { text: "c" }, + { enchanted: "c" }, ], }, ], @@ -215,7 +219,13 @@ b`) it("can parse end-word command interpolations", () => expect(parse("a[b]")).toEqual([ true, - [[{ pieces: [{ text: "a" }, { script: [[{ enchanted: "b" }]] }] }]], + [ + [ + { + pieces: [{ enchanted: "a" }, { script: [[{ enchanted: "b" }]] }], + }, + ], + ], ])); it("can parse multiple command interpolations in a word", () => expect(parse("[a][b]")).toEqual([ diff --git a/src/parser.ts b/src/parser.ts index b4ea579..8b2bb2d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -10,7 +10,6 @@ import { Use, } from "./peg"; import { - EnchantedWord as EnchantedWordType, InterpolatedPiece, Script, ScriptPiece, @@ -25,10 +24,6 @@ const Comment = Regex(/#[^\n]*/y) const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace"); -const EnchantedWord = Regex(/[^\]\[\}\{$\\";\s]+(?=[\s;\]]|$)/y) - .map(([enchanted]) => ({ enchanted } as EnchantedWordType)) - .expects("ENCHANTED_WORD"); - const BackslashEscape = Regex(/\\(.)/y) .expects("\\") .map(([, char]) => ({ text: char })); @@ -54,7 +49,7 @@ function bareWordTmpl(charRegex: RegExp) { Bracket, Regex(charRegex) .expects("CHAR") - .map(([text]) => ({ text })) + .map(([text]) => ({ enchanted: text })) ) ) ).map(([, pieces]) => SimplifyWord(pieces)); @@ -96,7 +91,6 @@ const Brace: Pattern = Sequence( function wordTmpl(bareWordCharRegex: RegExp): Pattern { return Choose( - EnchantedWord, Brace.map((text) => ({ text } as TextWord)), QuotedWord, bareWordTmpl(bareWordCharRegex) diff --git a/src/words.ts b/src/words.ts index 31e26e8..01fe836 100644 --- a/src/words.ts +++ b/src/words.ts @@ -9,6 +9,7 @@ import { escapeHtml } from "./helpers"; * ```tcl * puts -stderr text ;# -stderr can be interpreted as a flag and is not part of the message to print * puts "-stderr" text ;# -stderr is not a flag, but is part of the message to print + * puts [return -stderr] text ;# -stderr is not a flag, but is part of the message to print * puts $var text ;# The value of $var is part of the message to print, even if the value happens to be "-stderr" * ``` */