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 = "";