Support comments in new parser

This commit is contained in:
Tangent Wantwight 2024-06-08 12:32:58 -04:00
parent b536d30420
commit fa3be1e003
2 changed files with 34 additions and 5 deletions

View file

@ -77,6 +77,10 @@ class WipScript {
return this.wipWord.length == 0;
}
startOfCommand(): boolean {
return this.wipWord.length == 0 && this.wipCommand.length == 0;
}
addWordPiece(piece: InterpolatedPiece, pos?: number) {
if (this.endOfWordError) {
throw new Error(this.endOfWordError);
@ -185,6 +189,28 @@ class Parser {
wip.finishCommand();
break;
case "comment":
if (wip.startOfCommand()) {
skipComment: while (this.advance()) {
const [type, chars, pos] = this.next;
switch (type) {
case "newline":
case "EOF":
break skipComment;
case "backslash":
this.advance();
continue;
case "ERROR":
throw new Error(chars);
default:
continue;
}
}
} else {
wip.addWordPiece({ bare: chars }, pos);
}
break;
case "EOF":
case "]":
return wip.finishScript();
@ -206,12 +232,12 @@ class Parser {
case "quote":
case "backslash":
case "comment":
wip.addWordPiece({ text: chars });
wip.addWordPiece({ text: chars }, pos);
break;
case "text":
switch (chars) {
case "n":
wip.addWordPiece({ text: "\n" });
wip.addWordPiece({ text: "\n" }, pos);
break;
default:
throw new Error(`Unknown backslash escape: ${chars}`);
@ -232,7 +258,6 @@ class Parser {
}
case "quote":
case "comment":
throw new Error(`Unhandled case: ${type} (${chars})`);
case "ERROR":
throw new Error(chars);

View file

@ -1,4 +1,4 @@
import { escapeHtml } from './helpers';
import { escapeHtml } from "./helpers";
export type SourcePos = number;
@ -117,7 +117,11 @@ export function SimplifyWord(
if (consolidated.length == 0) {
return { text: "", pos: sourcePosition };
} else if (consolidated.length == 1 && IsTextPiece(consolidated[0])) {
if (pieces.every((piece) => "bare" in piece)) {
return { bare: AsText(consolidated[0]), pos: sourcePosition };
} else {
return { ...consolidated[0], pos: sourcePosition };
}
} else {
return { pieces: consolidated };
}