Add source position to words in new parser

This commit is contained in:
Tangent Wantwight 2024-06-05 19:32:58 -04:00
parent 2c55e38822
commit 618de2ac99

View file

@ -66,15 +66,20 @@ class WipScript {
script: Command[] = []; script: Command[] = [];
wipCommand: Word[] = []; wipCommand: Word[] = [];
wipWord: InterpolatedPiece[] = []; wipWord: InterpolatedPiece[] = [];
wordPos: number | undefined = undefined;
// TODO: thing to fail {}a & ""a // 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); this.wipWord.push(piece);
} }
finishWord() { finishWord() {
if (this.wipWord.length > 0) { if (this.wipWord.length > 0) {
this.wipCommand.push(SimplifyWord(this.wipWord)); this.wipCommand.push(SimplifyWord(this.wipWord, this.wordPos));
this.wipWord = []; this.wipWord = [];
this.wordPos = undefined;
} }
} }
finishCommand() { finishCommand() {
@ -123,7 +128,7 @@ class Parser {
const [type, chars, pos] = this.next; const [type, chars, pos] = this.next;
switch (type) { switch (type) {
case "text": case "text":
wip.addWordPiece({ bare: chars, pos }); wip.addWordPiece({ bare: chars }, pos);
break; break;
case "whitespace": case "whitespace":
@ -147,6 +152,8 @@ class Parser {
case "comment": case "comment":
case "ERROR": case "ERROR":
throw new Error(`Unhandled case: ${type} (${chars})`); throw new Error(`Unhandled case: ${type} (${chars})`);
default:
throw new Error(`Unhandled case: ${type satisfies never} (${chars})`);
} }
this.advance(); this.advance();