WIP brace parsing for WIP parser

This commit is contained in:
Tangent Wantwight 2024-06-05 20:26:09 -04:00
parent ab91d71170
commit c1ce90fd63

View file

@ -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();
}
}
}