perf: Move input to state and precalculate length

This also fixes a bug with async functions
This commit is contained in:
Daniel Tschinder 2019-01-15 12:45:36 -08:00
parent ae154c86ed
commit 2dc1c91955
12 changed files with 160 additions and 126 deletions

View File

@ -16,7 +16,6 @@ export default class BaseParser {
// Initialized by Tokenizer // Initialized by Tokenizer
state: State; state: State;
input: string;
isReservedWord(word: string): boolean { isReservedWord(word: string): boolean {
if (word === "await") { if (word === "await") {

View File

@ -1146,7 +1146,11 @@ export default class ExpressionParser extends LValParser {
const node = this.startNodeAt(startPos, startLoc); const node = this.startNodeAt(startPos, startLoc);
this.addExtra(node, "rawValue", value); this.addExtra(node, "rawValue", value);
this.addExtra(node, "raw", this.input.slice(startPos, this.state.end)); this.addExtra(
node,
"raw",
this.state.input.slice(startPos, this.state.end),
);
node.value = value; node.value = value;
this.next(); this.next();
return this.finishNode(node, type); return this.finishNode(node, type);
@ -1365,7 +1369,7 @@ export default class ExpressionParser extends LValParser {
} }
} }
elem.value = { elem.value = {
raw: this.input raw: this.state.input
.slice(this.state.start, this.state.end) .slice(this.state.start, this.state.end)
.replace(/\r\n?/g, "\n"), .replace(/\r\n?/g, "\n"),
cooked: this.state.value, cooked: this.state.value,
@ -1967,7 +1971,8 @@ export default class ExpressionParser extends LValParser {
if ( if (
(name === "class" || name === "function") && (name === "class" || name === "function") &&
(this.state.lastTokEnd !== this.state.lastTokStart + 1 || (this.state.lastTokEnd !== this.state.lastTokStart + 1 ||
this.input.charCodeAt(this.state.lastTokStart) !== charCodes.dot) this.state.input.charCodeAt(this.state.lastTokStart) !==
charCodes.dot)
) { ) {
this.state.context.pop(); this.state.context.pop();
} }

View File

@ -22,7 +22,6 @@ export default class Parser extends StatementParser {
this.options = options; this.options = options;
this.inModule = this.options.sourceType === "module"; this.inModule = this.options.sourceType === "module";
this.input = input;
this.plugins = pluginsMap(this.options.plugins); this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename; this.filename = options.sourceFilename;
} }

View File

@ -21,7 +21,7 @@ export default class LocationParser extends CommentsParser {
code?: string, code?: string,
} = {}, } = {},
): empty { ): empty {
const loc = getLineInfo(this.input, pos); const loc = getLineInfo(this.state.input, pos);
message += ` (${loc.line}:${loc.column})`; message += ` (${loc.line}:${loc.column})`;
// $FlowIgnore // $FlowIgnore
const err: SyntaxError & { pos: number, loc: Position } = new SyntaxError( const err: SyntaxError & { pos: number, loc: Position } = new SyntaxError(

View File

@ -44,7 +44,7 @@ export default class StatementParser extends ExpressionParser {
const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start); const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
const directive = this.startNodeAt(stmt.start, stmt.loc.start); const directive = this.startNodeAt(stmt.start, stmt.loc.start);
const raw = this.input.slice(expr.start, expr.end); const raw = this.state.input.slice(expr.start, expr.end);
const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes
this.addExtra(directiveLiteral, "raw", raw); this.addExtra(directiveLiteral, "raw", raw);
@ -551,7 +551,9 @@ export default class StatementParser extends ExpressionParser {
parseThrowStatement(node: N.ThrowStatement): N.ThrowStatement { parseThrowStatement(node: N.ThrowStatement): N.ThrowStatement {
this.next(); this.next();
if ( if (
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)) lineBreak.test(
this.state.input.slice(this.state.lastTokEnd, this.state.start),
)
) { ) {
this.raise(this.state.lastTokEnd, "Illegal newline after throw"); this.raise(this.state.lastTokEnd, "Illegal newline after throw");
} }
@ -1521,7 +1523,7 @@ export default class StatementParser extends ExpressionParser {
isAsyncFunction() { isAsyncFunction() {
if (!this.isContextual("async")) return false; if (!this.isContextual("async")) return false;
const { input, pos } = this.state; const { input, pos, length } = this.state;
skipWhiteSpace.lastIndex = pos; skipWhiteSpace.lastIndex = pos;
const skip = skipWhiteSpace.exec(input); const skip = skipWhiteSpace.exec(input);
@ -1533,7 +1535,7 @@ export default class StatementParser extends ExpressionParser {
return ( return (
!lineBreak.test(input.slice(pos, next)) && !lineBreak.test(input.slice(pos, next)) &&
input.slice(next, next + 8) === "function" && input.slice(next, next + 8) === "function" &&
(next + 8 === input.length || !isIdentifierChar(input.charAt(next + 8))) (next + 8 === length || !isIdentifierChar(input.charCodeAt(next + 8)))
); );
} }

View File

@ -87,7 +87,7 @@ export default class UtilParser extends Tokenizer {
hasPrecedingLineBreak(): boolean { hasPrecedingLineBreak(): boolean {
return lineBreak.test( return lineBreak.test(
this.input.slice(this.state.lastTokEnd, this.state.start), this.state.input.slice(this.state.lastTokEnd, this.state.start),
); );
} }

View File

@ -1124,7 +1124,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.types = []; node.types = [];
this.expect(tt.bracketL); this.expect(tt.bracketL);
// We allow trailing commas // We allow trailing commas
while (this.state.pos < this.input.length && !this.match(tt.bracketR)) { while (this.state.pos < this.state.length && !this.match(tt.bracketR)) {
node.types.push(this.flowParseType()); node.types.push(this.flowParseType());
if (this.match(tt.bracketR)) break; if (this.match(tt.bracketR)) break;
this.expect(tt.comma); this.expect(tt.comma);
@ -1934,7 +1934,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// ensure that inside flow types, we bypass the jsx parser plugin // ensure that inside flow types, we bypass the jsx parser plugin
readToken(code: number): void { readToken(code: number): void {
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if ( if (
this.state.inType && this.state.inType &&
(code === charCodes.greaterThan || code === charCodes.lessThan) (code === charCodes.greaterThan || code === charCodes.lessThan)
@ -2686,7 +2686,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
readToken_mult_modulo(code: number): void { readToken_mult_modulo(code: number): void {
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if ( if (
code === charCodes.asterisk && code === charCodes.asterisk &&
next === charCodes.slash && next === charCodes.slash &&
@ -2728,7 +2728,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
if (this.hasPlugin("flow") && this.state.hasFlowComment) { if (this.hasPlugin("flow") && this.state.hasFlowComment) {
const end = this.input.indexOf("*-/", (this.state.pos += 2)); const end = this.state.input.indexOf("*-/", (this.state.pos += 2));
if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment"); if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
this.state.pos = end + 3; this.state.pos = end + 3;
return; return;
@ -2742,20 +2742,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let shiftToFirstNonWhiteSpace = 2; let shiftToFirstNonWhiteSpace = 2;
while ( while (
[charCodes.space, charCodes.tab].includes( [charCodes.space, charCodes.tab].includes(
this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace), this.state.input.charCodeAt(pos + shiftToFirstNonWhiteSpace),
) )
) { ) {
shiftToFirstNonWhiteSpace++; shiftToFirstNonWhiteSpace++;
} }
const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos); const ch2 = this.state.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1); const ch3 = this.state.input.charCodeAt(
shiftToFirstNonWhiteSpace + pos + 1,
);
if (ch2 === charCodes.colon && ch3 === charCodes.colon) { if (ch2 === charCodes.colon && ch3 === charCodes.colon) {
return shiftToFirstNonWhiteSpace + 2; // check for /*:: return shiftToFirstNonWhiteSpace + 2; // check for /*::
} }
if ( if (
this.input.slice( this.state.input.slice(
shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos,
shiftToFirstNonWhiteSpace + pos + 12, shiftToFirstNonWhiteSpace + pos + 12,
) === "flow-include" ) === "flow-include"
@ -2769,7 +2771,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
hasFlowCommentCompletion(): void { hasFlowCommentCompletion(): void {
const end = this.input.indexOf("*/", this.state.pos); const end = this.state.input.indexOf("*/", this.state.pos);
if (end === -1) { if (end === -1) {
this.raise(this.state.pos, "Unterminated comment"); this.raise(this.state.pos, "Unterminated comment");
} }

View File

@ -79,11 +79,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let out = ""; let out = "";
let chunkStart = this.state.pos; let chunkStart = this.state.pos;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.raise(this.state.start, "Unterminated JSX contents"); this.raise(this.state.start, "Unterminated JSX contents");
} }
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
switch (ch) { switch (ch) {
case charCodes.lessThan: case charCodes.lessThan:
@ -95,18 +95,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
return this.getTokenFromCode(ch); return this.getTokenFromCode(ch);
} }
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
return this.finishToken(tt.jsxText, out); return this.finishToken(tt.jsxText, out);
case charCodes.ampersand: case charCodes.ampersand:
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
out += this.jsxReadEntity(); out += this.jsxReadEntity();
chunkStart = this.state.pos; chunkStart = this.state.pos;
break; break;
default: default:
if (isNewLine(ch)) { if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
out += this.jsxReadNewLine(true); out += this.jsxReadNewLine(true);
chunkStart = this.state.pos; chunkStart = this.state.pos;
} else { } else {
@ -117,12 +117,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
jsxReadNewLine(normalizeCRLF: boolean): string { jsxReadNewLine(normalizeCRLF: boolean): string {
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
let out; let out;
++this.state.pos; ++this.state.pos;
if ( if (
ch === charCodes.carriageReturn && ch === charCodes.carriageReturn &&
this.input.charCodeAt(this.state.pos) === charCodes.lineFeed this.state.input.charCodeAt(this.state.pos) === charCodes.lineFeed
) { ) {
++this.state.pos; ++this.state.pos;
out = normalizeCRLF ? "\n" : "\r\n"; out = normalizeCRLF ? "\n" : "\r\n";
@ -139,25 +139,25 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let out = ""; let out = "";
let chunkStart = ++this.state.pos; let chunkStart = ++this.state.pos;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.raise(this.state.start, "Unterminated string constant"); this.raise(this.state.start, "Unterminated string constant");
} }
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
if (ch === quote) break; if (ch === quote) break;
if (ch === charCodes.ampersand) { if (ch === charCodes.ampersand) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
out += this.jsxReadEntity(); out += this.jsxReadEntity();
chunkStart = this.state.pos; chunkStart = this.state.pos;
} else if (isNewLine(ch)) { } else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
out += this.jsxReadNewLine(false); out += this.jsxReadNewLine(false);
chunkStart = this.state.pos; chunkStart = this.state.pos;
} else { } else {
++this.state.pos; ++this.state.pos;
} }
} }
out += this.input.slice(chunkStart, this.state.pos++); out += this.state.input.slice(chunkStart, this.state.pos++);
return this.finishToken(tt.string, out); return this.finishToken(tt.string, out);
} }
@ -165,11 +165,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let str = ""; let str = "";
let count = 0; let count = 0;
let entity; let entity;
let ch = this.input[this.state.pos]; let ch = this.state.input[this.state.pos];
const startPos = ++this.state.pos; const startPos = ++this.state.pos;
while (this.state.pos < this.input.length && count++ < 10) { while (this.state.pos < this.state.length && count++ < 10) {
ch = this.input[this.state.pos++]; ch = this.state.input[this.state.pos++];
if (ch === ";") { if (ch === ";") {
if (str[0] === "#") { if (str[0] === "#") {
if (str[1] === "x") { if (str[1] === "x") {
@ -208,11 +208,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let ch; let ch;
const start = this.state.pos; const start = this.state.pos;
do { do {
ch = this.input.charCodeAt(++this.state.pos); ch = this.state.input.charCodeAt(++this.state.pos);
} while (isIdentifierChar(ch) || ch === charCodes.dash); } while (isIdentifierChar(ch) || ch === charCodes.dash);
return this.finishToken( return this.finishToken(
tt.jsxName, tt.jsxName,
this.input.slice(start, this.state.pos), this.state.input.slice(start, this.state.pos),
); );
} }

View File

@ -107,7 +107,9 @@ tt._function.updateContext = tt._class.updateContext = function(prevType) {
prevType !== tt._else && prevType !== tt._else &&
!( !(
prevType === tt._return && prevType === tt._return &&
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)) lineBreak.test(
this.state.input.slice(this.state.lastTokEnd, this.state.start),
)
) && ) &&
!( !(
(prevType === tt.colon || prevType === tt.braceL) && (prevType === tt.colon || prevType === tt.braceL) &&

View File

@ -185,7 +185,7 @@ export default class Tokenizer extends LocationParser {
this.state.pos = this.state.start; this.state.pos = this.state.start;
while (this.state.pos < this.state.lineStart) { while (this.state.pos < this.state.lineStart) {
this.state.lineStart = this.state.lineStart =
this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1; this.state.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
--this.state.curLine; --this.state.curLine;
} }
this.nextToken(); this.nextToken();
@ -206,7 +206,7 @@ export default class Tokenizer extends LocationParser {
this.state.octalPosition = null; this.state.octalPosition = null;
this.state.start = this.state.pos; this.state.start = this.state.pos;
this.state.startLoc = this.state.curPosition(); this.state.startLoc = this.state.curPosition();
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.finishToken(tt.eof); this.finishToken(tt.eof);
return; return;
} }
@ -214,7 +214,7 @@ export default class Tokenizer extends LocationParser {
if (curContext.override) { if (curContext.override) {
curContext.override(this); curContext.override(this);
} else { } else {
this.readToken(this.input.codePointAt(this.state.pos)); this.readToken(this.state.input.codePointAt(this.state.pos));
} }
} }
@ -254,14 +254,14 @@ export default class Tokenizer extends LocationParser {
skipBlockComment(): void { skipBlockComment(): void {
const startLoc = this.state.curPosition(); const startLoc = this.state.curPosition();
const start = this.state.pos; const start = this.state.pos;
const end = this.input.indexOf("*/", (this.state.pos += 2)); const end = this.state.input.indexOf("*/", (this.state.pos += 2));
if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment"); if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
this.state.pos = end + 2; this.state.pos = end + 2;
lineBreakG.lastIndex = start; lineBreakG.lastIndex = start;
let match; let match;
while ( while (
(match = lineBreakG.exec(this.input)) && (match = lineBreakG.exec(this.state.input)) &&
match.index < this.state.pos match.index < this.state.pos
) { ) {
++this.state.curLine; ++this.state.curLine;
@ -270,7 +270,7 @@ export default class Tokenizer extends LocationParser {
this.pushComment( this.pushComment(
true, true,
this.input.slice(start + 2, end), this.state.input.slice(start + 2, end),
start, start,
this.state.pos, this.state.pos,
startLoc, startLoc,
@ -281,22 +281,22 @@ export default class Tokenizer extends LocationParser {
skipLineComment(startSkip: number): void { skipLineComment(startSkip: number): void {
const start = this.state.pos; const start = this.state.pos;
const startLoc = this.state.curPosition(); const startLoc = this.state.curPosition();
let ch = this.input.charCodeAt((this.state.pos += startSkip)); let ch = this.state.input.charCodeAt((this.state.pos += startSkip));
if (this.state.pos < this.input.length) { if (this.state.pos < this.state.length) {
while ( while (
ch !== charCodes.lineFeed && ch !== charCodes.lineFeed &&
ch !== charCodes.carriageReturn && ch !== charCodes.carriageReturn &&
ch !== charCodes.lineSeparator && ch !== charCodes.lineSeparator &&
ch !== charCodes.paragraphSeparator && ch !== charCodes.paragraphSeparator &&
++this.state.pos < this.input.length ++this.state.pos < this.state.length
) { ) {
ch = this.input.charCodeAt(this.state.pos); ch = this.state.input.charCodeAt(this.state.pos);
} }
} }
this.pushComment( this.pushComment(
false, false,
this.input.slice(start + startSkip, this.state.pos), this.state.input.slice(start + startSkip, this.state.pos),
start, start,
this.state.pos, this.state.pos,
startLoc, startLoc,
@ -308,8 +308,8 @@ export default class Tokenizer extends LocationParser {
// whitespace and comments, and. // whitespace and comments, and.
skipSpace(): void { skipSpace(): void {
loop: while (this.state.pos < this.input.length) { loop: while (this.state.pos < this.state.length) {
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
switch (ch) { switch (ch) {
case charCodes.space: case charCodes.space:
case charCodes.nonBreakingSpace: case charCodes.nonBreakingSpace:
@ -318,7 +318,8 @@ export default class Tokenizer extends LocationParser {
break; break;
case charCodes.carriageReturn: case charCodes.carriageReturn:
if ( if (
this.input.charCodeAt(this.state.pos + 1) === charCodes.lineFeed this.state.input.charCodeAt(this.state.pos + 1) ===
charCodes.lineFeed
) { ) {
++this.state.pos; ++this.state.pos;
} }
@ -332,7 +333,7 @@ export default class Tokenizer extends LocationParser {
break; break;
case charCodes.slash: case charCodes.slash:
switch (this.input.charCodeAt(this.state.pos + 1)) { switch (this.state.input.charCodeAt(this.state.pos + 1)) {
case charCodes.asterisk: case charCodes.asterisk:
this.skipBlockComment(); this.skipBlockComment();
break; break;
@ -387,7 +388,7 @@ export default class Tokenizer extends LocationParser {
} }
const nextPos = this.state.pos + 1; const nextPos = this.state.pos + 1;
const next = this.input.charCodeAt(nextPos); const next = this.state.input.charCodeAt(nextPos);
if (next >= charCodes.digit0 && next <= charCodes.digit9) { if (next >= charCodes.digit0 && next <= charCodes.digit9) {
this.raise(this.state.pos, "Unexpected digit after hash token"); this.raise(this.state.pos, "Unexpected digit after hash token");
} }
@ -410,13 +411,13 @@ export default class Tokenizer extends LocationParser {
} }
readToken_dot(): void { readToken_dot(): void {
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next >= charCodes.digit0 && next <= charCodes.digit9) { if (next >= charCodes.digit0 && next <= charCodes.digit9) {
this.readNumber(true); this.readNumber(true);
return; return;
} }
const next2 = this.input.charCodeAt(this.state.pos + 2); const next2 = this.state.input.charCodeAt(this.state.pos + 2);
if (next === charCodes.dot && next2 === charCodes.dot) { if (next === charCodes.dot && next2 === charCodes.dot) {
this.state.pos += 3; this.state.pos += 3;
this.finishToken(tt.ellipsis); this.finishToken(tt.ellipsis);
@ -434,7 +435,7 @@ export default class Tokenizer extends LocationParser {
return; return;
} }
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next === charCodes.equalsTo) { if (next === charCodes.equalsTo) {
this.finishOp(tt.assign, 2); this.finishOp(tt.assign, 2);
} else { } else {
@ -443,12 +444,12 @@ export default class Tokenizer extends LocationParser {
} }
readToken_interpreter(): boolean { readToken_interpreter(): boolean {
if (this.state.pos !== 0 || this.state.input.length < 2) return false; if (this.state.pos !== 0 || this.state.length < 2) return false;
const start = this.state.pos; const start = this.state.pos;
this.state.pos += 1; this.state.pos += 1;
let ch = this.input.charCodeAt(this.state.pos); let ch = this.state.input.charCodeAt(this.state.pos);
if (ch !== charCodes.exclamationMark) return false; if (ch !== charCodes.exclamationMark) return false;
while ( while (
@ -456,12 +457,12 @@ export default class Tokenizer extends LocationParser {
ch !== charCodes.carriageReturn && ch !== charCodes.carriageReturn &&
ch !== charCodes.lineSeparator && ch !== charCodes.lineSeparator &&
ch !== charCodes.paragraphSeparator && ch !== charCodes.paragraphSeparator &&
++this.state.pos < this.input.length ++this.state.pos < this.state.length
) { ) {
ch = this.input.charCodeAt(this.state.pos); ch = this.state.input.charCodeAt(this.state.pos);
} }
const value = this.input.slice(start + 2, this.state.pos); const value = this.state.input.slice(start + 2, this.state.pos);
this.finishToken(tt.interpreterDirective, value); this.finishToken(tt.interpreterDirective, value);
@ -472,13 +473,13 @@ export default class Tokenizer extends LocationParser {
// '%*' // '%*'
let type = code === charCodes.asterisk ? tt.star : tt.modulo; let type = code === charCodes.asterisk ? tt.star : tt.modulo;
let width = 1; let width = 1;
let next = this.input.charCodeAt(this.state.pos + 1); let next = this.state.input.charCodeAt(this.state.pos + 1);
const exprAllowed = this.state.exprAllowed; const exprAllowed = this.state.exprAllowed;
// Exponentiation operator ** // Exponentiation operator **
if (code === charCodes.asterisk && next === charCodes.asterisk) { if (code === charCodes.asterisk && next === charCodes.asterisk) {
width++; width++;
next = this.input.charCodeAt(this.state.pos + 2); next = this.state.input.charCodeAt(this.state.pos + 2);
type = tt.exponent; type = tt.exponent;
} }
@ -492,10 +493,12 @@ export default class Tokenizer extends LocationParser {
readToken_pipe_amp(code: number): void { readToken_pipe_amp(code: number): void {
// '|&' // '|&'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next === code) { if (next === code) {
if (this.input.charCodeAt(this.state.pos + 2) === charCodes.equalsTo) { if (
this.state.input.charCodeAt(this.state.pos + 2) === charCodes.equalsTo
) {
this.finishOp(tt.assign, 3); this.finishOp(tt.assign, 3);
} else { } else {
this.finishOp( this.finishOp(
@ -531,7 +534,7 @@ export default class Tokenizer extends LocationParser {
readToken_caret(): void { readToken_caret(): void {
// '^' // '^'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next === charCodes.equalsTo) { if (next === charCodes.equalsTo) {
this.finishOp(tt.assign, 2); this.finishOp(tt.assign, 2);
} else { } else {
@ -541,14 +544,17 @@ export default class Tokenizer extends LocationParser {
readToken_plus_min(code: number): void { readToken_plus_min(code: number): void {
// '+-' // '+-'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next === code) { if (next === code) {
if ( if (
next === charCodes.dash && next === charCodes.dash &&
!this.inModule && !this.inModule &&
this.input.charCodeAt(this.state.pos + 2) === charCodes.greaterThan && this.state.input.charCodeAt(this.state.pos + 2) ===
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos)) charCodes.greaterThan &&
lineBreak.test(
this.state.input.slice(this.state.lastTokEnd, this.state.pos),
)
) { ) {
// A `-->` line comment // A `-->` line comment
this.skipLineComment(3); this.skipLineComment(3);
@ -569,16 +575,20 @@ export default class Tokenizer extends LocationParser {
readToken_lt_gt(code: number): void { readToken_lt_gt(code: number): void {
// '<>' // '<>'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
let size = 1; let size = 1;
if (next === code) { if (next === code) {
size = size =
code === charCodes.greaterThan && code === charCodes.greaterThan &&
this.input.charCodeAt(this.state.pos + 2) === charCodes.greaterThan this.state.input.charCodeAt(this.state.pos + 2) ===
charCodes.greaterThan
? 3 ? 3
: 2; : 2;
if (this.input.charCodeAt(this.state.pos + size) === charCodes.equalsTo) { if (
this.state.input.charCodeAt(this.state.pos + size) ===
charCodes.equalsTo
) {
this.finishOp(tt.assign, size + 1); this.finishOp(tt.assign, size + 1);
return; return;
} }
@ -590,8 +600,8 @@ export default class Tokenizer extends LocationParser {
next === charCodes.exclamationMark && next === charCodes.exclamationMark &&
code === charCodes.lessThan && code === charCodes.lessThan &&
!this.inModule && !this.inModule &&
this.input.charCodeAt(this.state.pos + 2) === charCodes.dash && this.state.input.charCodeAt(this.state.pos + 2) === charCodes.dash &&
this.input.charCodeAt(this.state.pos + 3) === charCodes.dash this.state.input.charCodeAt(this.state.pos + 3) === charCodes.dash
) { ) {
// `<!--`, an XML-style comment that should be interpreted as a line comment // `<!--`, an XML-style comment that should be interpreted as a line comment
this.skipLineComment(4); this.skipLineComment(4);
@ -610,11 +620,11 @@ export default class Tokenizer extends LocationParser {
readToken_eq_excl(code: number): void { readToken_eq_excl(code: number): void {
// '=!' // '=!'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (next === charCodes.equalsTo) { if (next === charCodes.equalsTo) {
this.finishOp( this.finishOp(
tt.equality, tt.equality,
this.input.charCodeAt(this.state.pos + 2) === charCodes.equalsTo this.state.input.charCodeAt(this.state.pos + 2) === charCodes.equalsTo
? 3 ? 3
: 2, : 2,
); );
@ -631,8 +641,8 @@ export default class Tokenizer extends LocationParser {
readToken_question(): void { readToken_question(): void {
// '?' // '?'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
const next2 = this.input.charCodeAt(this.state.pos + 2); const next2 = this.state.input.charCodeAt(this.state.pos + 2);
if (next === charCodes.questionMark && !this.state.inType) { if (next === charCodes.questionMark && !this.state.inType) {
if (next2 === charCodes.equalsTo) { if (next2 === charCodes.equalsTo) {
// '??=' // '??='
@ -692,7 +702,8 @@ export default class Tokenizer extends LocationParser {
case charCodes.leftCurlyBrace: case charCodes.leftCurlyBrace:
if ( if (
this.hasPlugin("flow") && this.hasPlugin("flow") &&
this.input.charCodeAt(this.state.pos + 1) === charCodes.verticalBar this.state.input.charCodeAt(this.state.pos + 1) ===
charCodes.verticalBar
) { ) {
this.finishOp(tt.braceBarL, 2); this.finishOp(tt.braceBarL, 2);
} else { } else {
@ -709,7 +720,7 @@ export default class Tokenizer extends LocationParser {
case charCodes.colon: case charCodes.colon:
if ( if (
this.hasPlugin("functionBind") && this.hasPlugin("functionBind") &&
this.input.charCodeAt(this.state.pos + 1) === charCodes.colon this.state.input.charCodeAt(this.state.pos + 1) === charCodes.colon
) { ) {
this.finishOp(tt.doubleColon, 2); this.finishOp(tt.doubleColon, 2);
} else { } else {
@ -728,7 +739,7 @@ export default class Tokenizer extends LocationParser {
return; return;
case charCodes.digit0: { case charCodes.digit0: {
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
// '0x', '0X' - hex number // '0x', '0X' - hex number
if (next === charCodes.lowercaseX || next === charCodes.uppercaseX) { if (next === charCodes.lowercaseX || next === charCodes.uppercaseX) {
this.readRadixNumber(16); this.readRadixNumber(16);
@ -824,7 +835,7 @@ export default class Tokenizer extends LocationParser {
} }
finishOp(type: TokenType, size: number): void { finishOp(type: TokenType, size: number): void {
const str = this.input.slice(this.state.pos, this.state.pos + size); const str = this.state.input.slice(this.state.pos, this.state.pos + size);
this.state.pos += size; this.state.pos += size;
this.finishToken(type, str); this.finishToken(type, str);
} }
@ -833,10 +844,10 @@ export default class Tokenizer extends LocationParser {
const start = this.state.pos; const start = this.state.pos;
let escaped, inClass; let escaped, inClass;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.raise(start, "Unterminated regular expression"); this.raise(start, "Unterminated regular expression");
} }
const ch = this.input.charAt(this.state.pos); const ch = this.state.input.charAt(this.state.pos);
if (lineBreak.test(ch)) { if (lineBreak.test(ch)) {
this.raise(start, "Unterminated regular expression"); this.raise(start, "Unterminated regular expression");
} }
@ -854,14 +865,14 @@ export default class Tokenizer extends LocationParser {
} }
++this.state.pos; ++this.state.pos;
} }
const content = this.input.slice(start, this.state.pos); const content = this.state.input.slice(start, this.state.pos);
++this.state.pos; ++this.state.pos;
let mods = ""; let mods = "";
while (this.state.pos < this.input.length) { while (this.state.pos < this.state.length) {
const char = this.input[this.state.pos]; const char = this.state.input[this.state.pos];
const charCode = this.input.codePointAt(this.state.pos); const charCode = this.state.input.codePointAt(this.state.pos);
if (VALID_REGEX_FLAGS.indexOf(char) > -1) { if (VALID_REGEX_FLAGS.indexOf(char) > -1) {
if (mods.indexOf(char) > -1) { if (mods.indexOf(char) > -1) {
@ -908,12 +919,12 @@ export default class Tokenizer extends LocationParser {
let total = 0; let total = 0;
for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) { for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
const code = this.input.charCodeAt(this.state.pos); const code = this.state.input.charCodeAt(this.state.pos);
let val; let val;
if (this.hasPlugin("numericSeparator")) { if (this.hasPlugin("numericSeparator")) {
const prev = this.input.charCodeAt(this.state.pos - 1); const prev = this.state.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.state.input.charCodeAt(this.state.pos + 1);
if (code === charCodes.underscore) { if (code === charCodes.underscore) {
if (allowedSiblings.indexOf(next) === -1) { if (allowedSiblings.indexOf(next) === -1) {
this.raise(this.state.pos, "Invalid or unexpected token"); this.raise(this.state.pos, "Invalid or unexpected token");
@ -967,18 +978,22 @@ export default class Tokenizer extends LocationParser {
} }
if (this.hasPlugin("bigInt")) { if (this.hasPlugin("bigInt")) {
if (this.input.charCodeAt(this.state.pos) === charCodes.lowercaseN) { if (
this.state.input.charCodeAt(this.state.pos) === charCodes.lowercaseN
) {
++this.state.pos; ++this.state.pos;
isBigInt = true; isBigInt = true;
} }
} }
if (isIdentifierStart(this.input.codePointAt(this.state.pos))) { if (isIdentifierStart(this.state.input.codePointAt(this.state.pos))) {
this.raise(this.state.pos, "Identifier directly after number"); this.raise(this.state.pos, "Identifier directly after number");
} }
if (isBigInt) { if (isBigInt) {
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); const str = this.state.input
.slice(start, this.state.pos)
.replace(/[_n]/g, "");
this.finishToken(tt.bigint, str); this.finishToken(tt.bigint, str);
return; return;
} }
@ -998,7 +1013,7 @@ export default class Tokenizer extends LocationParser {
} }
let octal = let octal =
this.state.pos - start >= 2 && this.state.pos - start >= 2 &&
this.input.charCodeAt(start) === charCodes.digit0; this.state.input.charCodeAt(start) === charCodes.digit0;
if (octal) { if (octal) {
if (this.state.strict) { if (this.state.strict) {
this.raise( this.raise(
@ -1006,30 +1021,30 @@ export default class Tokenizer extends LocationParser {
"Legacy octal literals are not allowed in strict mode", "Legacy octal literals are not allowed in strict mode",
); );
} }
if (/[89]/.test(this.input.slice(start, this.state.pos))) { if (/[89]/.test(this.state.input.slice(start, this.state.pos))) {
octal = false; octal = false;
} }
} }
let next = this.input.charCodeAt(this.state.pos); let next = this.state.input.charCodeAt(this.state.pos);
if (next === charCodes.dot && !octal) { if (next === charCodes.dot && !octal) {
++this.state.pos; ++this.state.pos;
this.readInt(10); this.readInt(10);
isFloat = true; isFloat = true;
next = this.input.charCodeAt(this.state.pos); next = this.state.input.charCodeAt(this.state.pos);
} }
if ( if (
(next === charCodes.uppercaseE || next === charCodes.lowercaseE) && (next === charCodes.uppercaseE || next === charCodes.lowercaseE) &&
!octal !octal
) { ) {
next = this.input.charCodeAt(++this.state.pos); next = this.state.input.charCodeAt(++this.state.pos);
if (next === charCodes.plusSign || next === charCodes.dash) { if (next === charCodes.plusSign || next === charCodes.dash) {
++this.state.pos; ++this.state.pos;
} }
if (this.readInt(10) === null) this.raise(start, "Invalid number"); if (this.readInt(10) === null) this.raise(start, "Invalid number");
isFloat = true; isFloat = true;
next = this.input.charCodeAt(this.state.pos); next = this.state.input.charCodeAt(this.state.pos);
} }
if (this.hasPlugin("bigInt")) { if (this.hasPlugin("bigInt")) {
@ -1041,12 +1056,14 @@ export default class Tokenizer extends LocationParser {
} }
} }
if (isIdentifierStart(this.input.codePointAt(this.state.pos))) { if (isIdentifierStart(this.state.input.codePointAt(this.state.pos))) {
this.raise(this.state.pos, "Identifier directly after number"); this.raise(this.state.pos, "Identifier directly after number");
} }
// remove "_" for numeric literal separator, and "n" for BigInts // remove "_" for numeric literal separator, and "n" for BigInts
const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); const str = this.state.input
.slice(start, this.state.pos)
.replace(/[_n]/g, "");
if (isBigInt) { if (isBigInt) {
this.finishToken(tt.bigint, str); this.finishToken(tt.bigint, str);
@ -1060,13 +1077,13 @@ export default class Tokenizer extends LocationParser {
// Read a string value, interpreting backslash-escapes. // Read a string value, interpreting backslash-escapes.
readCodePoint(throwOnInvalid: boolean): number | null { readCodePoint(throwOnInvalid: boolean): number | null {
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
let code; let code;
if (ch === charCodes.leftCurlyBrace) { if (ch === charCodes.leftCurlyBrace) {
const codePos = ++this.state.pos; const codePos = ++this.state.pos;
code = this.readHexChar( code = this.readHexChar(
this.input.indexOf("}", this.state.pos) - this.state.pos, this.state.input.indexOf("}", this.state.pos) - this.state.pos,
throwOnInvalid, throwOnInvalid,
); );
++this.state.pos; ++this.state.pos;
@ -1091,13 +1108,13 @@ export default class Tokenizer extends LocationParser {
let out = "", let out = "",
chunkStart = ++this.state.pos; chunkStart = ++this.state.pos;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.raise(this.state.start, "Unterminated string constant"); this.raise(this.state.start, "Unterminated string constant");
} }
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
if (ch === quote) break; if (ch === quote) break;
if (ch === charCodes.backslash) { if (ch === charCodes.backslash) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
// $FlowFixMe // $FlowFixMe
out += this.readEscapedChar(false); out += this.readEscapedChar(false);
chunkStart = this.state.pos; chunkStart = this.state.pos;
@ -1113,7 +1130,7 @@ export default class Tokenizer extends LocationParser {
++this.state.pos; ++this.state.pos;
} }
} }
out += this.input.slice(chunkStart, this.state.pos++); out += this.state.input.slice(chunkStart, this.state.pos++);
this.finishToken(tt.string, out); this.finishToken(tt.string, out);
} }
@ -1124,14 +1141,14 @@ export default class Tokenizer extends LocationParser {
chunkStart = this.state.pos, chunkStart = this.state.pos,
containsInvalid = false; containsInvalid = false;
for (;;) { for (;;) {
if (this.state.pos >= this.input.length) { if (this.state.pos >= this.state.length) {
this.raise(this.state.start, "Unterminated template"); this.raise(this.state.start, "Unterminated template");
} }
const ch = this.input.charCodeAt(this.state.pos); const ch = this.state.input.charCodeAt(this.state.pos);
if ( if (
ch === charCodes.graveAccent || ch === charCodes.graveAccent ||
(ch === charCodes.dollarSign && (ch === charCodes.dollarSign &&
this.input.charCodeAt(this.state.pos + 1) === this.state.input.charCodeAt(this.state.pos + 1) ===
charCodes.leftCurlyBrace) charCodes.leftCurlyBrace)
) { ) {
if (this.state.pos === this.state.start && this.match(tt.template)) { if (this.state.pos === this.state.start && this.match(tt.template)) {
@ -1145,12 +1162,12 @@ export default class Tokenizer extends LocationParser {
return; return;
} }
} }
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
this.finishToken(tt.template, containsInvalid ? null : out); this.finishToken(tt.template, containsInvalid ? null : out);
return; return;
} }
if (ch === charCodes.backslash) { if (ch === charCodes.backslash) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
const escaped = this.readEscapedChar(true); const escaped = this.readEscapedChar(true);
if (escaped === null) { if (escaped === null) {
containsInvalid = true; containsInvalid = true;
@ -1159,11 +1176,13 @@ export default class Tokenizer extends LocationParser {
} }
chunkStart = this.state.pos; chunkStart = this.state.pos;
} else if (isNewLine(ch)) { } else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.state.pos); out += this.state.input.slice(chunkStart, this.state.pos);
++this.state.pos; ++this.state.pos;
switch (ch) { switch (ch) {
case charCodes.carriageReturn: case charCodes.carriageReturn:
if (this.input.charCodeAt(this.state.pos) === charCodes.lineFeed) { if (
this.state.input.charCodeAt(this.state.pos) === charCodes.lineFeed
) {
++this.state.pos; ++this.state.pos;
} }
case charCodes.lineFeed: case charCodes.lineFeed:
@ -1186,7 +1205,7 @@ export default class Tokenizer extends LocationParser {
readEscapedChar(inTemplate: boolean): string | null { readEscapedChar(inTemplate: boolean): string | null {
const throwOnInvalid = !inTemplate; const throwOnInvalid = !inTemplate;
const ch = this.input.charCodeAt(++this.state.pos); const ch = this.state.input.charCodeAt(++this.state.pos);
++this.state.pos; ++this.state.pos;
switch (ch) { switch (ch) {
case charCodes.lowercaseN: case charCodes.lowercaseN:
@ -1210,7 +1229,9 @@ export default class Tokenizer extends LocationParser {
case charCodes.lowercaseF: case charCodes.lowercaseF:
return "\f"; return "\f";
case charCodes.carriageReturn: case charCodes.carriageReturn:
if (this.input.charCodeAt(this.state.pos) === charCodes.lineFeed) { if (
this.state.input.charCodeAt(this.state.pos) === charCodes.lineFeed
) {
++this.state.pos; ++this.state.pos;
} }
case charCodes.lineFeed: case charCodes.lineFeed:
@ -1221,7 +1242,7 @@ export default class Tokenizer extends LocationParser {
if (ch >= charCodes.digit0 && ch <= charCodes.digit7) { if (ch >= charCodes.digit0 && ch <= charCodes.digit7) {
const codePos = this.state.pos - 1; const codePos = this.state.pos - 1;
// $FlowFixMe // $FlowFixMe
let octalStr = this.input let octalStr = this.state.input
.substr(this.state.pos - 1, 3) .substr(this.state.pos - 1, 3)
.match(/^[0-7]+/)[0]; .match(/^[0-7]+/)[0];
let octal = parseInt(octalStr, 8); let octal = parseInt(octalStr, 8);
@ -1277,8 +1298,8 @@ export default class Tokenizer extends LocationParser {
const start = this.state.pos; const start = this.state.pos;
let chunkStart = this.state.pos; let chunkStart = this.state.pos;
while (this.state.pos < this.input.length) { while (this.state.pos < this.state.length) {
const ch = this.input.codePointAt(this.state.pos); const ch = this.state.input.codePointAt(this.state.pos);
if (isIdentifierChar(ch)) { if (isIdentifierChar(ch)) {
this.state.pos += ch <= 0xffff ? 1 : 2; this.state.pos += ch <= 0xffff ? 1 : 2;
} else if (this.state.isIterator && ch === charCodes.atSign) { } else if (this.state.isIterator && ch === charCodes.atSign) {
@ -1286,12 +1307,14 @@ export default class Tokenizer extends LocationParser {
} else if (ch === charCodes.backslash) { } else if (ch === charCodes.backslash) {
this.state.containsEsc = true; this.state.containsEsc = true;
word += this.input.slice(chunkStart, this.state.pos); word += this.state.input.slice(chunkStart, this.state.pos);
const escStart = this.state.pos; const escStart = this.state.pos;
const identifierCheck = const identifierCheck =
this.state.pos === start ? isIdentifierStart : isIdentifierChar; this.state.pos === start ? isIdentifierStart : isIdentifierChar;
if (this.input.charCodeAt(++this.state.pos) !== charCodes.lowercaseU) { if (
this.state.input.charCodeAt(++this.state.pos) !== charCodes.lowercaseU
) {
this.raise( this.raise(
this.state.pos, this.state.pos,
"Expecting Unicode escape sequence \\uXXXX", "Expecting Unicode escape sequence \\uXXXX",
@ -1315,7 +1338,7 @@ export default class Tokenizer extends LocationParser {
break; break;
} }
} }
return word + this.input.slice(chunkStart, this.state.pos); return word + this.state.input.slice(chunkStart, this.state.pos);
} }
isIterator(word: string): boolean { isIterator(word: string): boolean {
@ -1369,7 +1392,7 @@ export default class Tokenizer extends LocationParser {
(prevType === tt.name && this.state.exprAllowed) (prevType === tt.name && this.state.exprAllowed)
) { ) {
return lineBreak.test( return lineBreak.test(
this.input.slice(this.state.lastTokEnd, this.state.start), this.state.input.slice(this.state.lastTokEnd, this.state.start),
); );
} }

View File

@ -24,6 +24,7 @@ type TopicContextState = {
export default class State { export default class State {
strict: boolean; strict: boolean;
input: string; input: string;
length: number;
curLine: number; curLine: number;
@ -37,6 +38,7 @@ export default class State {
options.strictMode === false ? false : options.sourceType === "module"; options.strictMode === false ? false : options.sourceType === "module";
this.input = input; this.input = input;
this.length = input.length;
this.curLine = options.startLine; this.curLine = options.startLine;
this.startLoc = this.endLoc = this.curPosition(); this.startLoc = this.endLoc = this.curPosition();

View File

@ -1,4 +1,4 @@
{ {
"sourceType": "module", "sourceType": "module",
"throws": "Unexpected token, expected \"function\" (1:21)" "throws": "Unexpected token, expected \"=>\" (1:31)"
} }