Simplify parsing of enchanted words- special case grammar not needed

This commit is contained in:
Tangent Wantwight 2023-08-31 22:58:44 -04:00
parent 53bca8f878
commit 1b8929657a
3 changed files with 21 additions and 16 deletions

View file

@ -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([

View file

@ -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<string> = Sequence(
function wordTmpl(bareWordCharRegex: RegExp): Pattern<WordType> {
return Choose<WordType>(
EnchantedWord,
Brace.map((text) => ({ text } as TextWord)),
QuotedWord,
bareWordTmpl(bareWordCharRegex)

View file

@ -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"
* ```
*/