From c61496fcc3ea203ea5d8de00df46befe6c147cb5 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 5 Jun 2024 20:44:13 -0400 Subject: [PATCH] Be strict about trailing characters after braces. Don't give mid-word braces magic meaning. --- src/parser2.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 9481d80..be21529 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -71,24 +71,31 @@ class WipScript { wipCommand: Word[] = []; wipWord: InterpolatedPiece[] = []; wordPos: number | undefined = undefined; - // TODO: thing to fail {}a & ""a + endOfWordError: string | undefined = undefined; startOfWord(): boolean { return this.wipWord.length == 0; } addWordPiece(piece: InterpolatedPiece, pos?: number) { + if (this.endOfWordError) { + throw new Error(this.endOfWordError); + } if (this.startOfWord()) { this.wordPos = pos; } this.wipWord.push(piece); } + freezeWord(error: string) { + this.endOfWordError = error; + } finishWord() { if (this.wipWord.length > 0) { this.wipCommand.push(SimplifyWord(this.wipWord, this.wordPos)); - this.wipWord = []; - this.wordPos = undefined; } + this.wipWord = []; + this.wordPos = undefined; + this.endOfWordError = undefined; } finishCommand() { this.finishWord(); @@ -149,10 +156,15 @@ class Parser { break; case "{": { - this.advance(); - const text = this.parseBrace(); - wip.addWordPiece({ text }, pos); - this.expect("}"); + if (wip.startOfWord()) { + this.advance(); + const text = this.parseBrace(); + wip.addWordPiece({ text }, pos); + this.expect("}"); + wip.freezeWord("Extra characters after closing brace"); + } else { + wip.addWordPiece({ bare: chars }, pos); + } break; }