perf: replace lookahead by lookaheadCharCode (#10371)

* perf: replace lookahead by lookaheadCharCode

* fix: flow ignore

* refactor: add nextTokenStart method

* refactor: duplicated isNewLine code

* refactor: remove lookahead usage from babylon core
This commit is contained in:
Huáng Jùnliàng
2019-10-08 13:09:05 -04:00
committed by Nicolò Ribaudo
parent bc0966a46f
commit 0856618ed5
5 changed files with 62 additions and 49 deletions

View File

@@ -13,6 +13,7 @@ import {
lineBreakG,
isNewLine,
isWhitespace,
skipWhiteSpace,
} from "../util/whitespace";
import State from "./state";
@@ -168,6 +169,18 @@ export default class Tokenizer extends LocationParser {
return curr;
}
nextTokenStart(): number {
const thisTokEnd = this.state.pos;
skipWhiteSpace.lastIndex = thisTokEnd;
const skip = skipWhiteSpace.exec(this.input);
// $FlowIgnore: The skipWhiteSpace ensures to match any string
return thisTokEnd + skip[0].length;
}
lookaheadCharCode(): number {
return this.input.charCodeAt(this.nextTokenStart());
}
// Toggle strict mode. Re-reads the next number or string to please
// pedantic tests (`"use strict"; 010;` should fail).
@@ -267,13 +280,7 @@ export default class Tokenizer extends LocationParser {
const startLoc = this.state.curPosition();
let ch = this.input.charCodeAt((this.state.pos += startSkip));
if (this.state.pos < this.length) {
while (
ch !== charCodes.lineFeed &&
ch !== charCodes.carriageReturn &&
ch !== charCodes.lineSeparator &&
ch !== charCodes.paragraphSeparator &&
++this.state.pos < this.length
) {
while (!isNewLine(ch) && ++this.state.pos < this.length) {
ch = this.input.charCodeAt(this.state.pos);
}
}
@@ -441,13 +448,7 @@ export default class Tokenizer extends LocationParser {
let ch = this.input.charCodeAt(this.state.pos);
if (ch !== charCodes.exclamationMark) return false;
while (
ch !== charCodes.lineFeed &&
ch !== charCodes.carriageReturn &&
ch !== charCodes.lineSeparator &&
ch !== charCodes.paragraphSeparator &&
++this.state.pos < this.length
) {
while (!isNewLine(ch) && ++this.state.pos < this.length) {
ch = this.input.charCodeAt(this.state.pos);
}