replace regex parsing with linear parsing

This commit is contained in:
Dave Holoway
2020-06-15 11:15:25 +01:00
parent 18d56e0bc0
commit 01ae51d91a
17 changed files with 531 additions and 324 deletions

View File

@@ -15,6 +15,7 @@ class TokenList {
this.inc();
/** @type {ParseProblem[]} */
this.problems = [];
this.marks = [];
}
inc() {
@@ -25,6 +26,19 @@ class TokenList {
}
}
}
mark() {
this.marks.unshift(this.idx);
}
/**
* Returns the array of tokens from the last mark() point, trimming any trailing whitespace tokens
*/
markEnd() {
let i = this.idx;
while (this.tokens[--i].kind === 'wsc') { }
return this.tokens.slice(this.marks.shift(), i + 1);
}
/**
* Token lookahead. The current token is unaffected by this method.
@@ -43,16 +57,25 @@ class TokenList {
return token;
}
/**
* Check if the current token matches the specified value, returns and consumes it
* @param {string} value
*/
getIfValue(value) {
const token = this.current;
if (token && token.value === value) {
this.inc();
return token;
}
return null;
}
/**
* Check if the current token matches the specified value and consumes it
* @param {string} value
*/
isValue(value) {
if (this.current && this.current.value === value) {
this.inc();
return true;
}
return false;
return this.getIfValue(value) !== null;
}
/**