Skip to content

Commit

Permalink
refactor: track AWAIT and YIELD in separate handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 3, 2020
1 parent 0eb55a6 commit e02471f
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 63 deletions.
57 changes: 36 additions & 21 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ import * as charCodes from "charcodes";
import {
BIND_OUTSIDE,
BIND_VAR,
functionFlags,
SCOPE_ARROW,
SCOPE_CLASS,
SCOPE_DIRECT_SUPER,
SCOPE_FUNCTION,
SCOPE_SUPER,
SCOPE_PROGRAM,
SCOPE_ASYNC,
} from "../util/scopeflags";
import {
PARAM_AWAIT,
PARAM_,
functionFlags,
} from "../util/production-parameter";

export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js
Expand Down Expand Up @@ -97,11 +101,12 @@ export default class ExpressionParser extends LValParser {

// Convenience method to parse an Expression only
getExpression(): N.Expression {
let scopeFlags = SCOPE_PROGRAM;
let paramFlags = PARAM_;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
scopeFlags |= SCOPE_ASYNC;
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(scopeFlags);
this.scope.enter(SCOPE_PROGRAM);
this.param.enter(paramFlags);
this.nextToken();
const expr = this.parseExpression();
if (!this.match(tt.eof)) {
Expand All @@ -120,12 +125,18 @@ export default class ExpressionParser extends LValParser {
// and, *if* the syntactic construct they handle is present, wrap
// the AST node that the inner parser gave them in another node.

// Parse a full expression. The optional arguments are used to
// forbid the `in` operator (in for loops initialization expressions)
// and provide reference for storing '=' operator inside shorthand
// property assignment in contexts where both object expression
// and object pattern might appear (so it's possible to raise
// delayed syntax error at correct position).
// Parse a full expression.
// - `noIn`
// is used to forbid the `in` operator (in for loops initialization expressions)
// When `noIn` is true, the production parameter [In] is not present.
// Whenever [?In] appears in the right-hand sides of a production, we pass
// `noIn` to the subroutine calls.

// - `refShorthandDefaultPos`
// provides reference for storing '=' operator inside shorthand
// property assignment in contexts where both object expression
// and object pattern might appear (so it's possible to raise
// delayed syntax error at correct position).

parseExpression(noIn?: boolean, refShorthandDefaultPos?: Pos): N.Expression {
const startPos = this.state.start;
Expand Down Expand Up @@ -157,7 +168,7 @@ export default class ExpressionParser extends LValParser {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
if (this.isContextual("yield")) {
if (this.scope.inGenerator) {
if (this.param.hasYield) {
let left = this.parseYield(noIn);
if (afterLeftParse) {
left = afterLeftParse.call(this, left, startPos, startLoc);
Expand Down Expand Up @@ -346,7 +357,7 @@ export default class ExpressionParser extends LValParser {
if (
this.match(tt.name) &&
this.state.value === "await" &&
this.scope.inAsync
this.param.hasAwait
) {
throw this.raise(
this.state.start,
Expand Down Expand Up @@ -1158,7 +1169,7 @@ export default class ExpressionParser extends LValParser {
this.next();
meta = this.createIdentifier(meta, "function");

if (this.scope.inGenerator && this.eat(tt.dot)) {
if (this.param.hasYield && this.eat(tt.dot)) {
return this.parseMetaProperty(node, meta, "sent");
}
return this.parseFunction(node);
Expand Down Expand Up @@ -1833,13 +1844,15 @@ export default class ExpressionParser extends LValParser {
node.generator = !!isGenerator;
const allowModifiers = isConstructor; // For TypeScript parameter properties
this.scope.enter(
functionFlags(isAsync, node.generator) |
SCOPE_FUNCTION |
SCOPE_SUPER |
(inClassScope ? SCOPE_CLASS : 0) |
(allowDirectSuper ? SCOPE_DIRECT_SUPER : 0),
);
this.param.enter(functionFlags(isAsync, node.generator));
this.parseFunctionParams((node: any), allowModifiers);
this.parseFunctionBodyAndFinish(node, type, true);
this.param.exit();
this.scope.exit();

this.state.yieldPos = oldYieldPos;
Expand All @@ -1857,7 +1870,8 @@ export default class ExpressionParser extends LValParser {
isAsync: boolean,
trailingCommaPos: ?number,
): N.ArrowFunctionExpression {
this.scope.enter(functionFlags(isAsync, false) | SCOPE_ARROW);
this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
this.param.enter(functionFlags(isAsync, false));
this.initFunction(node, isAsync);

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
Expand All @@ -1870,6 +1884,7 @@ export default class ExpressionParser extends LValParser {
if (params) this.setArrowFunctionParameters(node, params, trailingCommaPos);
this.parseFunctionBody(node, true);

this.param.exit();
this.scope.exit();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.yieldPos = oldYieldPos;
Expand Down Expand Up @@ -2143,7 +2158,7 @@ export default class ExpressionParser extends LValParser {
checkKeywords: boolean,
isBinding: boolean,
): void {
if (this.scope.inGenerator && word === "yield") {
if (this.param.hasYield && word === "yield") {
this.raise(
startLoc,
"Can not use 'yield' as identifier inside a generator",
Expand All @@ -2152,7 +2167,7 @@ export default class ExpressionParser extends LValParser {
}

if (word === "await") {
if (this.scope.inAsync) {
if (this.param.hasAwait) {
this.raise(
startLoc,
"Can not use 'await' as identifier inside an async function",
Expand Down Expand Up @@ -2190,7 +2205,7 @@ export default class ExpressionParser extends LValParser {
: isStrictReservedWord;

if (reservedTest(word, this.inModule)) {
if (!this.scope.inAsync && word === "await") {
if (!this.param.hasAwait && word === "await") {
this.raise(
startLoc,
"Can not use keyword 'await' outside an async function",
Expand All @@ -2202,10 +2217,10 @@ export default class ExpressionParser extends LValParser {
}

isAwaitAllowed(): boolean {
if (this.scope.inFunction) return this.scope.inAsync;
if (this.scope.inFunction) return this.param.hasAwait;
if (this.options.allowAwaitOutsideFunction) return true;
if (this.hasPlugin("topLevelAwait")) {
return this.inModule && this.scope.inAsync;
return this.inModule && this.param.hasAwait;
}
return false;
}
Expand Down
14 changes: 10 additions & 4 deletions packages/babel-parser/src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import type { File, JSXOpeningElement } from "../types";
import type { PluginList } from "../plugin-utils";
import { getOptions } from "../options";
import StatementParser from "./statement";
import { SCOPE_ASYNC, SCOPE_PROGRAM } from "../util/scopeflags";
import { SCOPE_PROGRAM } from "../util/scopeflags";
import ScopeHandler from "../util/scope";
import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM_,
} from "../util/production-parameter";

export type PluginsMap = Map<string, { [string]: any }>;

Expand All @@ -25,6 +29,7 @@ export default class Parser extends StatementParser {
this.options = options;
this.inModule = this.options.sourceType === "module";
this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
this.param = new ProductionParameterHandler();
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
}
Expand All @@ -35,11 +40,12 @@ export default class Parser extends StatementParser {
}

parse(): File {
let scopeFlags = SCOPE_PROGRAM;
let paramFlags = PARAM_;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
scopeFlags |= SCOPE_ASYNC;
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(scopeFlags);
this.scope.enter(SCOPE_PROGRAM);
this.param.enter(paramFlags);
const file = this.startNode();
const program = this.startNode();
this.nextToken();
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-parser/src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
BIND_LEXICAL,
BIND_VAR,
BIND_FUNCTION,
functionFlags,
SCOPE_CLASS,
SCOPE_FUNCTION,
SCOPE_OTHER,
SCOPE_SIMPLE_CATCH,
SCOPE_SUPER,
type BindingTypes,
} from "../util/scopeflags";
import { PARAM_, functionFlags } from "../util/production-parameter";

const loopLabel = { kind: "loop" },
switchLabel = { kind: "switch" };
Expand Down Expand Up @@ -1053,7 +1054,8 @@ export default class StatementParser extends ExpressionParser {
this.state.maybeInArrowParameters = false;
this.state.yieldPos = -1;
this.state.awaitPos = -1;
this.scope.enter(functionFlags(node.async, node.generator));
this.scope.enter(SCOPE_FUNCTION);
this.param.enter(functionFlags(isAsync, node.generator));

if (!isStatement) {
node.id = this.parseFunctionId();
Expand All @@ -1072,6 +1074,7 @@ export default class StatementParser extends ExpressionParser {
);
});

this.param.exit();
this.scope.exit();

if (isStatement && !isHangingStatement) {
Expand Down Expand Up @@ -1573,9 +1576,12 @@ export default class StatementParser extends ExpressionParser {
node: N.ClassPrivateProperty,
): N.ClassPrivateProperty {
this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
// [In] production parameter is tracked in parseMaybeAssign
this.param.enter(PARAM_);

node.value = this.eat(tt.eq) ? this.parseMaybeAssign() : null;
this.semicolon();
this.param.exit();

this.scope.exit();

Expand All @@ -1588,6 +1594,8 @@ export default class StatementParser extends ExpressionParser {
}

this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
// [In] production parameter is tracked in parseMaybeAssign
this.param.enter(PARAM_);

if (this.match(tt.eq)) {
this.expectPlugin("classProperties");
Expand All @@ -1598,6 +1606,7 @@ export default class StatementParser extends ExpressionParser {
}
this.semicolon();

this.param.exit();
this.scope.exit();

return this.finishNode(node, "ClassProperty");
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { types as tc } from "../tokenizer/context";
import * as charCodes from "charcodes";
import { isIteratorStart } from "../util/identifier";
import {
functionFlags,
type BindingTypes,
BIND_NONE,
BIND_LEXICAL,
BIND_VAR,
BIND_FUNCTION,
SCOPE_ARROW,
SCOPE_FUNCTION,
SCOPE_OTHER,
} from "../util/scopeflags";

Expand Down Expand Up @@ -1890,7 +1890,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.extra?.trailingComma,
);
// Enter scope, as checkParams defines bindings
this.scope.enter(functionFlags(false, false) | SCOPE_ARROW);
this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
// Use super's method to force the parameters to be checked
super.checkParams(node, false, true);
this.scope.exit();
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../../util/scopeflags";
import TypeScriptScopeHandler from "./scope";
import * as charCodes from "charcodes";
import { PARAM_ } from "../../util/production-parameter";

type TsModifier =
| "readonly"
Expand Down Expand Up @@ -1264,7 +1265,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.body = inner;
} else {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
node.body = this.tsParseModuleBlock();
this.param.exit();
this.scope.exit();
}
return this.finishNode(node, "TSModuleDeclaration");
Expand All @@ -1283,7 +1286,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
node.body = this.tsParseModuleBlock();
this.param.exit();
this.scope.exit();
} else {
this.semicolon();
Expand Down Expand Up @@ -1438,11 +1443,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
const mod: N.TsModuleDeclaration = node;
mod.global = true;
mod.id = expr;
mod.body = this.tsParseModuleBlock();
this.scope.exit();
this.param.exit();
return this.finishNode(mod, "TSModuleDeclaration");
}
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ tt.name.updateContext = function(prevType) {
if (prevType !== tt.dot) {
if (
(this.state.value === "of" && !this.state.exprAllowed) ||
(this.state.value === "yield" && this.scope.inGenerator)
(this.state.value === "yield" && this.param.hasYield)
) {
allowed = true;
}
Expand Down
52 changes: 52 additions & 0 deletions packages/babel-parser/src/util/production-parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const PARAM_ = 0b000, // Initial Parameter flags
PARAM_YIELD = 0b001, // track [Await] production parameter
PARAM_AWAIT = 0b010; // track [Yield] production parameter

// ProductionParameterHandler is a stack fashioned production parameter tracker
// https://tc39.es/ecma262/#sec-grammar-notation
// It only tracks [Await] and [Yield] parameter. The [In] parameter is tracked
// in `noIn` argument of `parseExpression`.
//
// Whenever [+Await]/[+Yield] appears in the right-hand sides of a production,
// we must enter a new tracking stack. For example when parsing
//
// AsyncFunctionDeclaration [Yield, Await]:
// async [no LineTerminator here] function BindingIdentifier[?Yield, ?Await]
// ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
//
// we must follow such process:
//
// 1. parse async keyword
// 2. parse function keyword
// 3. parse bindingIdentifier <= inherit current parameters: [?Await]
// 4. enter new stack with (PARAM_AWAIT)
// 5. parse formal parameters <= must have [Await] parameter [+Await]
// 6. parse function body
// 7. exit current stack

export default class ProductionParameterHandler {
stacks: Array = [];
enter(flags) {
this.stacks.push(flags);
}

exit() {
this.stacks.pop();
}

currentFlags() {
return this.stacks[this.stacks.length - 1];
}

get hasAwait() {
return (this.currentFlags() & PARAM_AWAIT) > 0;
}

get hasYield() {
return (this.currentFlags() & PARAM_YIELD) > 0;
}
}

export function functionFlags(isAsync: boolean, isGenerator: boolean) {
return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
}

0 comments on commit e02471f

Please sign in to comment.