Simplify token context (#13450)

* refactor: unify tc.brace and tc.templateQuasi

* refactor: remove unused context check

* perf: reduce arrayPrototype call and hoist variables
This commit is contained in:
Huáng Jùnliàng 2021-06-21 10:51:00 -04:00 committed by GitHub
parent 8c229e7657
commit 5145c98a73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 46 deletions

View File

@ -19,7 +19,6 @@
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
import { types as tt, type TokenType } from "../tokenizer/types"; import { types as tt, type TokenType } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import * as N from "../types"; import * as N from "../types";
import LValParser from "./lval"; import LValParser from "./lval";
import { import {
@ -2325,19 +2324,6 @@ export default class ExpressionParser extends LValParser {
name = this.state.value; name = this.state.value;
} else if (type.keyword) { } else if (type.keyword) {
name = type.keyword; name = type.keyword;
// `class` and `function` keywords push function-type token context into this.context.
// But there is no chance to pop the context if the keyword is consumed
// as an identifier such as a property name.
if (type === tt._class || type === tt._function) {
const curContext = this.curContext();
if (
curContext === ct.functionStatement ||
curContext === ct.functionExpression
) {
this.state.context.pop();
}
}
} else { } else {
throw this.unexpected(); throw this.unexpected();
} }

View File

@ -2847,9 +2847,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// by parsing `jsxTagStart` to stop the JSX plugin from // by parsing `jsxTagStart` to stop the JSX plugin from
// messing with the tokens // messing with the tokens
const { context } = this.state; const { context } = this.state;
if (context[context.length - 1] === tc.j_oTag) { const curContext = context[context.length - 1];
if (curContext === tc.j_oTag) {
context.length -= 2; context.length -= 2;
} else if (context[context.length - 1] === tc.j_expr) { } else if (curContext === tc.j_expr) {
context.length -= 1; context.length -= 1;
} }
} }

View File

@ -55,8 +55,10 @@ tt.jsxTagStart = new TokenType("jsxTagStart", { startsExpr: true });
tt.jsxTagEnd = new TokenType("jsxTagEnd"); tt.jsxTagEnd = new TokenType("jsxTagEnd");
tt.jsxTagStart.updateContext = context => { tt.jsxTagStart.updateContext = context => {
context.push(tc.j_expr); // treat as beginning of JSX expression context.push(
context.push(tc.j_oTag); // start opening tag context tc.j_expr, // treat as beginning of JSX expression
tc.j_oTag, // start opening tag context
);
}; };
function isFragment(object: ?N.JSXElement): boolean { function isFragment(object: ?N.JSXElement): boolean {
@ -616,17 +618,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
updateContext(prevType: TokenType): void { updateContext(prevType: TokenType): void {
super.updateContext(prevType); super.updateContext(prevType);
const { context, type } = this.state; const { context, type } = this.state;
if (type === tt.braceL) { if (type === tt.slash && prevType === tt.jsxTagStart) {
const curContext = context[context.length - 1]; // do not consider JSX expr -> JSX open tag -> ... anymore
if (curContext === tc.j_oTag) { // reconsider as closing tag context
context.push(tc.brace); context.splice(-2, 2, tc.j_cTag);
} else if (curContext === tc.j_expr) {
context.push(tc.templateQuasi);
}
this.state.exprAllowed = true;
} else if (type === tt.slash && prevType === tt.jsxTagStart) {
context.length -= 2; // do not consider JSX expr -> JSX open tag -> ... anymore
context.push(tc.j_cTag); // reconsider as closing tag context
this.state.exprAllowed = false; this.state.exprAllowed = false;
} else if (type === tt.jsxTagEnd) { } else if (type === tt.jsxTagEnd) {
const out = context.pop(); const out = context.pop();

View File

@ -1,8 +1,7 @@
// @flow // @flow
// The token context is used to track whether `}` matches // The token context is used to track whether the apostrophe "`"
// a template quasi `${` or other tokens containing `{`: // starts or ends a string template
// namely tt.braceL `{` and tt.braceHashL `#{`
import { types as tt } from "./types"; import { types as tt } from "./types";
@ -20,7 +19,6 @@ export const types: {
[key: string]: TokContext, [key: string]: TokContext,
} = { } = {
brace: new TokContext("{"), brace: new TokContext("{"),
templateQuasi: new TokContext("${"),
template: new TokContext("`", true), template: new TokContext("`", true),
}; };
@ -35,19 +33,22 @@ export const types: {
// `this.prodParam` still has `[Yield]` production because it is not yet updated // `this.prodParam` still has `[Yield]` production because it is not yet updated
tt.braceR.updateContext = context => { tt.braceR.updateContext = context => {
if (context.length > 1) {
context.pop(); context.pop();
}
}; };
// we don't need to update context for tt.braceBarL because we do not pop context for tt.braceBarR // we don't need to update context for tt.braceBarL because we do not pop context for tt.braceBarR
tt.braceL.updateContext = tt.braceHashL.updateContext = context => { // ideally only dollarBraceL "${" needs a non-template context
// in order to indicate that the last "`" in `${`" starts a new string template
// inside a template element within outer string template.
// but when we popped such context in `}`, we lost track of whether this
// `}` matches a `${` or other tokens matching `}`, so we have to push
// such context in every token that `}` will match.
tt.braceL.updateContext =
tt.braceHashL.updateContext =
tt.dollarBraceL.updateContext =
context => {
context.push(types.brace); context.push(types.brace);
}; };
tt.dollarBraceL.updateContext = context => {
context.push(types.templateQuasi);
};
tt.backQuote.updateContext = context => { tt.backQuote.updateContext = context => {
if (context[context.length - 1] === types.template) { if (context[context.length - 1] === types.template) {

View File

@ -127,10 +127,10 @@ export default class State {
lastTokStart: number = 0; lastTokStart: number = 0;
lastTokEnd: number = 0; lastTokEnd: number = 0;
// The context stack is used to superficially track syntactic // The context stack is used to track whether the apostrophe "`" starts
// context to predict whether a regular expression is allowed in a // or ends a string template
// given position.
context: Array<TokContext> = [ct.brace]; context: Array<TokContext> = [ct.brace];
// Used to track whether a JSX element is allowed to form
exprAllowed: boolean = true; exprAllowed: boolean = true;
// Used to signal to callers of `readWord1` whether the word // Used to signal to callers of `readWord1` whether the word