parser: don't mutate or clone state arrays when doing a lookahead - fixes #2211

This commit is contained in:
Sebastian McKenzie 2015-08-15 19:07:42 -04:00
parent 860322f7b8
commit ac9ee75dac
2 changed files with 19 additions and 7 deletions

View File

@ -56,7 +56,9 @@ export default class Tokenizer {
// Move to the next token
next() {
this.state.tokens.push(new Token(this.state));
if (!this.isLookahead) {
this.state.tokens.push(new Token(this.state));
}
this.state.lastTokEnd = this.state.end;
this.state.lastTokStart = this.state.start;
@ -102,9 +104,13 @@ export default class Tokenizer {
lookahead() {
var old = this.state;
this.state = old.clone();
this.state = old.clone(true);
this.isLookahead = true;
this.next();
var curr = this.state.clone();
this.isLookahead = false;
var curr = this.state.clone(true);
this.state = old;
return curr;
}
@ -172,8 +178,10 @@ export default class Tokenizer {
range: [start, end]
};
this.state.tokens.push(comment);
this.state.comments.push(comment);
if (!this.isLookahead) {
this.state.tokens.push(comment);
this.state.comments.push(comment);
}
this.addComment(comment);
}

View File

@ -70,11 +70,15 @@ export default class State {
return new Position(this.curLine, this.pos - this.lineStart);
}
clone() {
clone(skipArrays?) {
var state = new State;
for (var key in this) {
var val = this[key];
if (Array.isArray(val)) val = val.slice();
if (!skipArrays && Array.isArray(val)) {
val = val.slice();
}
state[key] = val;
}
return state;