From 63d6fa836a7b9af60f6f4d60c30cc1896caa9f7f Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 5 Jun 2024 20:02:20 -0400 Subject: [PATCH] Add expect() & startOfWord() helpers to new parser --- src/parser2.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 83ca5fe..513f563 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -22,15 +22,11 @@ export function parse( try { const parser = new Parser(code); const script = parser.parseScript(); - - // TODO: report error with error position - - if (parser.lastIndex != code.length) { - return [false, "Couldn't parse full script"]; - } + parser.expect("EOF"); return [true, script]; } catch (ex) { + // TODO: report error with error position return [false, String(ex)]; } } @@ -77,8 +73,12 @@ class WipScript { wordPos: number | undefined = undefined; // TODO: thing to fail {}a & ""a + startOfWord(): boolean { + return this.wipWord.length == 0; + } + addWordPiece(piece: InterpolatedPiece, pos?: number) { - if (this.wipWord.length == 0) { + if (this.startOfWord()) { this.wordPos = pos; } this.wipWord.push(piece); @@ -129,6 +129,14 @@ class Parser { return (this.next = ["ERROR", "Token not matched", startPos]); } + expect(type: TokenType) { + if (this.next[0] != type) { + throw new Error( + `Expected ${type}, found ${this.next[0]} (${this.next[1]})` + ); + } + } + parseScript(): Script { const wip = new WipScript();