Factor out backslash parse helper

This commit is contained in:
Tangent Wantwight 2024-06-08 13:11:46 -04:00
parent fa3be1e003
commit a2c8eb66b9

View file

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