From b536d304201afea5d77068ea19ca7d2fe22231bd Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 8 Jun 2024 00:03:17 -0400 Subject: [PATCH] sketch out backslash escape handling --- src/parser2.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/parser2.ts b/src/parser2.ts index 93829cd..44796cc 100644 --- a/src/parser2.ts +++ b/src/parser2.ts @@ -189,11 +189,53 @@ class Parser { case "]": return wip.finishScript(); + 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 }); + break; + case "text": + switch (chars) { + case "n": + wip.addWordPiece({ text: "\n" }); + 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})` + ); + } + break; + } + case "quote": - case "backslash": case "comment": - case "ERROR": throw new Error(`Unhandled case: ${type} (${chars})`); + case "ERROR": + throw new Error(chars); default: throw new Error(`Unhandled case: ${type satisfies never} (${chars})`); }