/
production-parameter.js
58 lines (50 loc) 路 1.75 KB
/
production-parameter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @flow
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 type ParamKind = typeof PARAM_ | typeof PARAM_AWAIT | typeof PARAM_YIELD;
export default class ProductionParameterHandler {
stacks: Array<ParamKind> = [];
enter(flags: ParamKind) {
this.stacks.push(flags);
}
exit() {
this.stacks.pop();
}
currentFlags(): ParamKind {
return this.stacks[this.stacks.length - 1];
}
get hasAwait(): boolean {
return (this.currentFlags() & PARAM_AWAIT) > 0;
}
get hasYield(): boolean {
return (this.currentFlags() & PARAM_YIELD) > 0;
}
}
export function functionFlags(
isAsync: boolean,
isGenerator: boolean,
): ParamKind {
return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
}