perf: remove double check for keywords in readWord
Instead of calling isKeyword we simple check directly if the keyword token is available
This commit is contained in:
parent
b66d921053
commit
4e5e319d5b
@ -24,6 +24,7 @@ import LValParser from "./lval";
|
|||||||
import {
|
import {
|
||||||
isStrictReservedWord,
|
isStrictReservedWord,
|
||||||
isStrictBindReservedWord,
|
isStrictBindReservedWord,
|
||||||
|
isKeyword,
|
||||||
} from "../util/identifier";
|
} from "../util/identifier";
|
||||||
import type { Pos, Position } from "../util/location";
|
import type { Pos, Position } from "../util/location";
|
||||||
import * as charCodes from "charcodes";
|
import * as charCodes from "charcodes";
|
||||||
@ -2020,7 +2021,7 @@ export default class ExpressionParser extends LValParser {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isReservedWord(word) || (checkKeywords && this.isKeyword(word))) {
|
if (this.isReservedWord(word) || (checkKeywords && isKeyword(word))) {
|
||||||
this.raise(startLoc, word + " is a reserved word");
|
this.raise(startLoc, word + " is a reserved word");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1190,9 +1190,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
case "any":
|
case "any":
|
||||||
return this.finishNode(node, "AnyTypeAnnotation");
|
return this.finishNode(node, "AnyTypeAnnotation");
|
||||||
|
|
||||||
case "void":
|
|
||||||
return this.finishNode(node, "VoidTypeAnnotation");
|
|
||||||
|
|
||||||
case "bool":
|
case "bool":
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return this.finishNode(node, "BooleanTypeAnnotation");
|
return this.finishNode(node, "BooleanTypeAnnotation");
|
||||||
@ -1369,6 +1366,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
"NumberLiteralTypeAnnotation",
|
"NumberLiteralTypeAnnotation",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
case tt._void:
|
||||||
|
this.next();
|
||||||
|
return this.finishNode(node, "VoidTypeAnnotation");
|
||||||
|
|
||||||
case tt._null:
|
case tt._null:
|
||||||
this.next();
|
this.next();
|
||||||
return this.finishNode(node, "NullLiteralTypeAnnotation");
|
return this.finishNode(node, "NullLiteralTypeAnnotation");
|
||||||
@ -1922,16 +1923,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't consider `void` to be a keyword as then it'll use the void token type
|
|
||||||
// and set startExpr
|
|
||||||
isKeyword(name: string): boolean {
|
|
||||||
if (this.state.inType && name === "void") {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return super.isKeyword(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensure that inside flow types, we bypass the jsx parser plugin
|
// ensure that inside flow types, we bypass the jsx parser plugin
|
||||||
getTokenFromCode(code: number): void {
|
getTokenFromCode(code: number): void {
|
||||||
const next = this.state.input.charCodeAt(this.state.pos + 1);
|
const next = this.state.input.charCodeAt(this.state.pos + 1);
|
||||||
|
|||||||
@ -3,11 +3,7 @@
|
|||||||
import type { Options } from "../options";
|
import type { Options } from "../options";
|
||||||
import type { Position } from "../util/location";
|
import type { Position } from "../util/location";
|
||||||
import * as charCodes from "charcodes";
|
import * as charCodes from "charcodes";
|
||||||
import {
|
import { isIdentifierStart, isIdentifierChar } from "../util/identifier";
|
||||||
isIdentifierStart,
|
|
||||||
isIdentifierChar,
|
|
||||||
isKeyword,
|
|
||||||
} from "../util/identifier";
|
|
||||||
import { types as tt, keywords as keywordTypes, type TokenType } from "./types";
|
import { types as tt, keywords as keywordTypes, type TokenType } from "./types";
|
||||||
import { type TokContext, types as ct } from "./context";
|
import { type TokContext, types as ct } from "./context";
|
||||||
import LocationParser from "../parser/location";
|
import LocationParser from "../parser/location";
|
||||||
@ -157,12 +153,6 @@ export default class Tokenizer extends LocationParser {
|
|||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
isKeyword(word: string): boolean {
|
|
||||||
return isKeyword(word);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
lookahead(): State {
|
lookahead(): State {
|
||||||
const old = this.state;
|
const old = this.state;
|
||||||
this.state = old.clone(true);
|
this.state = old.clone(true);
|
||||||
@ -1336,16 +1326,12 @@ export default class Tokenizer extends LocationParser {
|
|||||||
|
|
||||||
readWord(): void {
|
readWord(): void {
|
||||||
const word = this.readWord1();
|
const word = this.readWord1();
|
||||||
let type = tt.name;
|
const type = keywordTypes[word] || tt.name;
|
||||||
|
|
||||||
if (this.isKeyword(word)) {
|
if (type.keyword && this.state.containsEsc) {
|
||||||
if (this.state.containsEsc) {
|
|
||||||
this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
|
this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
type = keywordTypes[word];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow @@iterator and @@asyncIterator as a identifier only inside type
|
// Allow @@iterator and @@asyncIterator as a identifier only inside type
|
||||||
if (
|
if (
|
||||||
this.state.isIterator &&
|
this.state.isIterator &&
|
||||||
|
|||||||
@ -133,20 +133,20 @@ export const types: { [name: string]: TokenType } = {
|
|||||||
incDec: new TokenType("++/--", { prefix, postfix, startsExpr }),
|
incDec: new TokenType("++/--", { prefix, postfix, startsExpr }),
|
||||||
bang: new TokenType("!", { beforeExpr, prefix, startsExpr }),
|
bang: new TokenType("!", { beforeExpr, prefix, startsExpr }),
|
||||||
tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }),
|
tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }),
|
||||||
pipeline: new BinopTokenType("|>", 0),
|
pipeline: BinopTokenType("|>", 0),
|
||||||
nullishCoalescing: new BinopTokenType("??", 1),
|
nullishCoalescing: BinopTokenType("??", 1),
|
||||||
logicalOR: new BinopTokenType("||", 1),
|
logicalOR: BinopTokenType("||", 1),
|
||||||
logicalAND: new BinopTokenType("&&", 2),
|
logicalAND: BinopTokenType("&&", 2),
|
||||||
bitwiseOR: new BinopTokenType("|", 3),
|
bitwiseOR: BinopTokenType("|", 3),
|
||||||
bitwiseXOR: new BinopTokenType("^", 4),
|
bitwiseXOR: BinopTokenType("^", 4),
|
||||||
bitwiseAND: new BinopTokenType("&", 5),
|
bitwiseAND: BinopTokenType("&", 5),
|
||||||
equality: new BinopTokenType("==/!=", 6),
|
equality: BinopTokenType("==/!=", 6),
|
||||||
relational: new BinopTokenType("</>", 7),
|
relational: BinopTokenType("</>", 7),
|
||||||
bitShift: new BinopTokenType("<</>>", 8),
|
bitShift: BinopTokenType("<</>>", 8),
|
||||||
plusMin: new TokenType("+/-", { beforeExpr, binop: 9, prefix, startsExpr }),
|
plusMin: new TokenType("+/-", { beforeExpr, binop: 9, prefix, startsExpr }),
|
||||||
modulo: new BinopTokenType("%", 10),
|
modulo: BinopTokenType("%", 10),
|
||||||
star: new BinopTokenType("*", 10),
|
star: BinopTokenType("*", 10),
|
||||||
slash: new BinopTokenType("/", 10),
|
slash: BinopTokenType("/", 10),
|
||||||
exponent: new TokenType("**", {
|
exponent: new TokenType("**", {
|
||||||
beforeExpr,
|
beforeExpr,
|
||||||
binop: 11,
|
binop: 11,
|
||||||
@ -154,45 +154,53 @@ export const types: { [name: string]: TokenType } = {
|
|||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
export const keywords = {
|
function makeKeywordProps(
|
||||||
break: new KeywordTokenType("break"),
|
name: string,
|
||||||
case: new KeywordTokenType("case", { beforeExpr }),
|
conf: any,
|
||||||
catch: new KeywordTokenType("catch"),
|
): PropertyDescriptor<TokenType> {
|
||||||
continue: new KeywordTokenType("continue"),
|
return { value: KeywordTokenType(name, conf), enumerable: true };
|
||||||
debugger: new KeywordTokenType("debugger"),
|
}
|
||||||
default: new KeywordTokenType("default", { beforeExpr }),
|
|
||||||
do: new KeywordTokenType("do", { isLoop, beforeExpr }),
|
// $FlowIssue
|
||||||
else: new KeywordTokenType("else", { beforeExpr }),
|
export const keywords = Object.create(null, {
|
||||||
finally: new KeywordTokenType("finally"),
|
break: makeKeywordProps("break"),
|
||||||
for: new KeywordTokenType("for", { isLoop }),
|
case: makeKeywordProps("case", { beforeExpr }),
|
||||||
function: new KeywordTokenType("function", { startsExpr }),
|
catch: makeKeywordProps("catch"),
|
||||||
if: new KeywordTokenType("if"),
|
continue: makeKeywordProps("continue"),
|
||||||
return: new KeywordTokenType("return", { beforeExpr }),
|
debugger: makeKeywordProps("debugger"),
|
||||||
switch: new KeywordTokenType("switch"),
|
default: makeKeywordProps("default", { beforeExpr }),
|
||||||
throw: new KeywordTokenType("throw", { beforeExpr, prefix, startsExpr }),
|
do: makeKeywordProps("do", { isLoop, beforeExpr }),
|
||||||
try: new KeywordTokenType("try"),
|
else: makeKeywordProps("else", { beforeExpr }),
|
||||||
var: new KeywordTokenType("var"),
|
finally: makeKeywordProps("finally"),
|
||||||
let: new KeywordTokenType("let"),
|
for: makeKeywordProps("for", { isLoop }),
|
||||||
const: new KeywordTokenType("const"),
|
function: makeKeywordProps("function", { startsExpr }),
|
||||||
while: new KeywordTokenType("while", { isLoop }),
|
if: makeKeywordProps("if"),
|
||||||
with: new KeywordTokenType("with"),
|
return: makeKeywordProps("return", { beforeExpr }),
|
||||||
new: new KeywordTokenType("new", { beforeExpr, startsExpr }),
|
switch: makeKeywordProps("switch"),
|
||||||
this: new KeywordTokenType("this", { startsExpr }),
|
throw: makeKeywordProps("throw", { beforeExpr, prefix, startsExpr }),
|
||||||
super: new KeywordTokenType("super", { startsExpr }),
|
try: makeKeywordProps("try"),
|
||||||
class: new KeywordTokenType("class", { startsExpr }),
|
var: makeKeywordProps("var"),
|
||||||
extends: new KeywordTokenType("extends", { beforeExpr }),
|
let: makeKeywordProps("let"),
|
||||||
export: new KeywordTokenType("export"),
|
const: makeKeywordProps("const"),
|
||||||
import: new KeywordTokenType("import", { startsExpr }),
|
while: makeKeywordProps("while", { isLoop }),
|
||||||
yield: new KeywordTokenType("yield", { beforeExpr, startsExpr }),
|
with: makeKeywordProps("with"),
|
||||||
null: new KeywordTokenType("null", { startsExpr }),
|
new: makeKeywordProps("new"),
|
||||||
true: new KeywordTokenType("true", { startsExpr }),
|
this: makeKeywordProps("this", { startsExpr }),
|
||||||
false: new KeywordTokenType("false", { startsExpr }),
|
super: makeKeywordProps("super", { startsExpr }),
|
||||||
in: new KeywordTokenType("in", { beforeExpr, binop: 7 }),
|
class: makeKeywordProps("class", { startsExpr }),
|
||||||
instanceof: new KeywordTokenType("instanceof", { beforeExpr, binop: 7 }),
|
extends: makeKeywordProps("extends", { beforeExpr }),
|
||||||
typeof: new KeywordTokenType("typeof", { beforeExpr, prefix, startsExpr }),
|
export: makeKeywordProps("export"),
|
||||||
void: new KeywordTokenType("void", { beforeExpr, prefix, startsExpr }),
|
import: makeKeywordProps("import", { startsExpr }),
|
||||||
delete: new KeywordTokenType("delete", { beforeExpr, prefix, startsExpr }),
|
yield: makeKeywordProps("yield", { beforeExpr, startsExpr }),
|
||||||
};
|
null: makeKeywordProps("null", { startsExpr }),
|
||||||
|
true: makeKeywordProps("true", { startsExpr }),
|
||||||
|
false: makeKeywordProps("false", { startsExpr }),
|
||||||
|
in: makeKeywordProps("in", { beforeExpr, binop: 7 }),
|
||||||
|
instanceof: makeKeywordProps("instanceof", { beforeExpr, binop: 7 }),
|
||||||
|
typeof: makeKeywordProps("typeof", { beforeExpr, prefix, startsExpr }),
|
||||||
|
void: makeKeywordProps("void", { beforeExpr, prefix, startsExpr }),
|
||||||
|
delete: makeKeywordProps("delete", { beforeExpr, prefix, startsExpr }),
|
||||||
|
});
|
||||||
|
|
||||||
// Map keyword names to token types.
|
// Map keyword names to token types.
|
||||||
Object.keys(keywords).forEach(name => {
|
Object.keys(keywords).forEach(name => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user