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], ["quote", /(")/y],
["backslash", /(\\)/y], ["backslash", /(\\)/y],
["comment", /(\#)/y], ["comment", /(\#)/y],
["text", /([^\s\\;\[\]]+)/y], ["text", /([^\s;\{\}\[\]"\\\#]+)/y],
]; ];
class WipScript { class WipScript {
@ -144,9 +144,18 @@ class Parser {
const [type, chars, pos] = this.next; const [type, chars, pos] = this.next;
switch (type) { switch (type) {
case "text": case "text":
case "}":
wip.addWordPiece({ bare: chars }, pos); wip.addWordPiece({ bare: chars }, pos);
break; break;
case "{": {
this.advance();
const text = this.parseBrace();
wip.addWordPiece({ text }, pos);
this.expect("}");
break;
}
case "[": { case "[": {
this.advance(); this.advance();
const script = this.parseScript(); const script = this.parseScript();
@ -168,8 +177,6 @@ class Parser {
case "]": case "]":
return wip.finishScript(); return wip.finishScript();
case "{":
case "}":
case "quote": case "quote":
case "backslash": case "backslash":
case "comment": case "comment":
@ -182,4 +189,32 @@ class Parser {
this.advance(); 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();
}
}
} }