Add expect() & startOfWord() helpers to new parser
This commit is contained in:
parent
9b81056d1d
commit
63d6fa836a
1 changed files with 15 additions and 7 deletions
|
@ -22,15 +22,11 @@ export function parse(
|
||||||
try {
|
try {
|
||||||
const parser = new Parser(code);
|
const parser = new Parser(code);
|
||||||
const script = parser.parseScript();
|
const script = parser.parseScript();
|
||||||
|
parser.expect("EOF");
|
||||||
// TODO: report error with error position
|
|
||||||
|
|
||||||
if (parser.lastIndex != code.length) {
|
|
||||||
return [false, "Couldn't parse full script"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [true, script];
|
return [true, script];
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
// TODO: report error with error position
|
||||||
return [false, String(ex)];
|
return [false, String(ex)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,8 +73,12 @@ class WipScript {
|
||||||
wordPos: number | undefined = undefined;
|
wordPos: number | undefined = undefined;
|
||||||
// TODO: thing to fail {}a & ""a
|
// TODO: thing to fail {}a & ""a
|
||||||
|
|
||||||
|
startOfWord(): boolean {
|
||||||
|
return this.wipWord.length == 0;
|
||||||
|
}
|
||||||
|
|
||||||
addWordPiece(piece: InterpolatedPiece, pos?: number) {
|
addWordPiece(piece: InterpolatedPiece, pos?: number) {
|
||||||
if (this.wipWord.length == 0) {
|
if (this.startOfWord()) {
|
||||||
this.wordPos = pos;
|
this.wordPos = pos;
|
||||||
}
|
}
|
||||||
this.wipWord.push(piece);
|
this.wipWord.push(piece);
|
||||||
|
@ -129,6 +129,14 @@ class Parser {
|
||||||
return (this.next = ["ERROR", "Token not matched", startPos]);
|
return (this.next = ["ERROR", "Token not matched", startPos]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(type: TokenType) {
|
||||||
|
if (this.next[0] != type) {
|
||||||
|
throw new Error(
|
||||||
|
`Expected ${type}, found ${this.next[0]} (${this.next[1]})`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parseScript(): Script {
|
parseScript(): Script {
|
||||||
const wip = new WipScript();
|
const wip = new WipScript();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue