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[] = [];
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();