From c1ce90fd63637326b2bba3ab5a28fa7fdd98fa16 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Wed, 5 Jun 2024 20:26:09 -0400 Subject: [PATCH] WIP brace parsing for WIP parser --- src/parser2.ts | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 92c7233..9481d80 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -63,7 +63,7 @@ const Tokens: [TokenType, RegExp][] = [ ["quote", /(")/y], ["backslash", /(\\)/y], ["comment", /(\#)/y], - ["text", /([^\s\\;\[\]]+)/y], + ["text", /([^\s;\{\}\[\]"\\\#]+)/y], ]; class WipScript { @@ -144,9 +144,18 @@ class Parser { const [type, chars, pos] = this.next; switch (type) { case "text": + case "}": wip.addWordPiece({ bare: chars }, pos); break; + case "{": { + this.advance(); + const text = this.parseBrace(); + wip.addWordPiece({ text }, pos); + this.expect("}"); + break; + } + case "[": { this.advance(); const script = this.parseScript(); @@ -168,8 +177,6 @@ class Parser { case "]": return wip.finishScript(); - case "{": - case "}": case "quote": case "backslash": case "comment": @@ -182,4 +189,32 @@ class Parser { this.advance(); } } + + parseBrace(): string { + let wip = ""; + + while (true) { + const [type, chars, pos] = this.next; + switch (type) { + case "{": { + wip += "{"; + this.advance(); + wip += this.parseBrace(); + this.expect("}"); + wip += "}"; + break; + } + case "}": + return wip; + case "EOF": + throw new Error("Reached end of input while parsing a brace word"); + case "ERROR": + throw new Error(chars); + default: + wip += chars; + } + + this.advance(); + } + } }