From a2c8eb66b991c68aa41b031c554c9857c41ace17 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 8 Jun 2024 13:11:46 -0400 Subject: [PATCH] Factor out backslash parse helper --- src/parser2.ts | 80 +++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index fb7131d..bb0a2ca 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -217,43 +217,7 @@ class Parser { case "backslash": { this.advance(); - const [type, chars, pos] = this.next; - switch (type) { - case "newline": - wip.finishWord(); - break; - - case "whitespace": - case "semicolon": - case "{": - case "}": - case "[": - case "]": - case "quote": - case "backslash": - case "comment": - wip.addWordPiece({ text: chars }, pos); - break; - case "text": - switch (chars) { - case "n": - wip.addWordPiece({ text: "\n" }, pos); - break; - default: - throw new Error(`Unknown backslash escape: ${chars}`); - } - break; - case "EOF": - throw new Error( - "Reached end of input while parsing a backslash escape" - ); - case "ERROR": - throw new Error(chars); - default: - throw new Error( - `Unhandled case: ${type satisfies never} (${chars})` - ); - } + this.parseBackslashEscape(wip, "bare"); break; } @@ -269,6 +233,48 @@ class Parser { } } + parseBackslashEscape(wip: WipScript, wordType: "bare" | "quote") { + const [type, chars, pos] = this.next; + switch (type) { + case "newline": + if (wordType == "bare") { + wip.finishWord(); + } else { + // ignore newline + } + break; + + case "whitespace": + case "semicolon": + case "{": + case "}": + case "[": + case "]": + case "quote": + case "backslash": + case "comment": + wip.addWordPiece({ text: chars }, pos); + break; + case "text": + switch (chars) { + case "n": + wip.addWordPiece({ text: "\n" }, pos); + break; + default: + throw new Error(`Unknown backslash escape: ${chars}`); + } + break; + case "EOF": + throw new Error( + "Reached end of input while parsing a backslash escape" + ); + case "ERROR": + throw new Error(chars); + default: + throw new Error(`Unhandled case: ${type satisfies never} (${chars})`); + } + } + parseBrace(): string { let wip = "";