From 618de2ac9957c6c12b7df271566e702db2a101fa Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 5 Jun 2024 19:32:58 -0400 Subject: [PATCH] Add source position to words in new parser --- src/parser2.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 75f9842..163ed25 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -66,15 +66,20 @@ class WipScript { script: Command[] = []; wipCommand: Word[] = []; wipWord: InterpolatedPiece[] = []; + wordPos: number | undefined = undefined; // TODO: thing to fail {}a & ""a - addWordPiece(piece: InterpolatedPiece) { + addWordPiece(piece: InterpolatedPiece, pos?: number) { + if (this.wipWord.length == 0) { + this.wordPos = pos; + } this.wipWord.push(piece); } finishWord() { if (this.wipWord.length > 0) { - this.wipCommand.push(SimplifyWord(this.wipWord)); + this.wipCommand.push(SimplifyWord(this.wipWord, this.wordPos)); this.wipWord = []; + this.wordPos = undefined; } } finishCommand() { @@ -123,7 +128,7 @@ class Parser { const [type, chars, pos] = this.next; switch (type) { case "text": - wip.addWordPiece({ bare: chars, pos }); + wip.addWordPiece({ bare: chars }, pos); break; case "whitespace": @@ -147,6 +152,8 @@ class Parser { case "comment": case "ERROR": throw new Error(`Unhandled case: ${type} (${chars})`); + default: + throw new Error(`Unhandled case: ${type satisfies never} (${chars})`); } this.advance();