parser: don't mutate or clone state arrays when doing a lookahead - fixes #2211
This commit is contained in:
parent
860322f7b8
commit
ac9ee75dac
@ -56,7 +56,9 @@ export default class Tokenizer {
|
|||||||
// Move to the next token
|
// Move to the next token
|
||||||
|
|
||||||
next() {
|
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.lastTokEnd = this.state.end;
|
||||||
this.state.lastTokStart = this.state.start;
|
this.state.lastTokStart = this.state.start;
|
||||||
@ -102,9 +104,13 @@ export default class Tokenizer {
|
|||||||
|
|
||||||
lookahead() {
|
lookahead() {
|
||||||
var old = this.state;
|
var old = this.state;
|
||||||
this.state = old.clone();
|
this.state = old.clone(true);
|
||||||
|
|
||||||
|
this.isLookahead = true;
|
||||||
this.next();
|
this.next();
|
||||||
var curr = this.state.clone();
|
this.isLookahead = false;
|
||||||
|
|
||||||
|
var curr = this.state.clone(true);
|
||||||
this.state = old;
|
this.state = old;
|
||||||
return curr;
|
return curr;
|
||||||
}
|
}
|
||||||
@ -172,8 +178,10 @@ export default class Tokenizer {
|
|||||||
range: [start, end]
|
range: [start, end]
|
||||||
};
|
};
|
||||||
|
|
||||||
this.state.tokens.push(comment);
|
if (!this.isLookahead) {
|
||||||
this.state.comments.push(comment);
|
this.state.tokens.push(comment);
|
||||||
|
this.state.comments.push(comment);
|
||||||
|
}
|
||||||
this.addComment(comment);
|
this.addComment(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,11 +70,15 @@ export default class State {
|
|||||||
return new Position(this.curLine, this.pos - this.lineStart);
|
return new Position(this.curLine, this.pos - this.lineStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
clone() {
|
clone(skipArrays?) {
|
||||||
var state = new State;
|
var state = new State;
|
||||||
for (var key in this) {
|
for (var key in this) {
|
||||||
var val = this[key];
|
var val = this[key];
|
||||||
if (Array.isArray(val)) val = val.slice();
|
|
||||||
|
if (!skipArrays && Array.isArray(val)) {
|
||||||
|
val = val.slice();
|
||||||
|
}
|
||||||
|
|
||||||
state[key] = val;
|
state[key] = val;
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user