From fa3be1e003aa7fbd3318518d071fd5e5a299f6b9 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 8 Jun 2024 12:32:58 -0400 Subject: [PATCH] Support comments in new parser --- src/parser2.ts | 31 ++++++++++++++++++++++++++++--- src/words.ts | 8 ++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 44796cc..fb7131d 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -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); diff --git a/src/words.ts b/src/words.ts index 5c8f5b7..7d70c6e 100644 --- a/src/words.ts +++ b/src/words.ts @@ -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])) { - return { ...consolidated[0], pos: sourcePosition }; + if (pieces.every((piece) => "bare" in piece)) { + return { bare: AsText(consolidated[0]), pos: sourcePosition }; + } else { + return { ...consolidated[0], pos: sourcePosition }; + } } else { return { pieces: consolidated }; }