@ibyar/expressions 0.5.4 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -0
  3. package/api/abstract.d.ts +3 -3
  4. package/api/abstract.js +2 -2
  5. package/api/abstract.js.map +1 -1
  6. package/api/class/class.d.ts +18 -12
  7. package/api/class/class.js +122 -81
  8. package/api/class/class.js.map +1 -1
  9. package/api/computing/debugger.d.ts +15 -0
  10. package/api/computing/debugger.js +35 -0
  11. package/api/computing/debugger.js.map +1 -0
  12. package/api/computing/rest.d.ts +4 -4
  13. package/api/computing/rest.js.map +1 -1
  14. package/api/definition/array.d.ts +7 -6
  15. package/api/definition/array.js +20 -14
  16. package/api/definition/array.js.map +1 -1
  17. package/api/definition/function.d.ts +35 -46
  18. package/api/definition/function.js +281 -299
  19. package/api/definition/function.js.map +1 -1
  20. package/api/definition/object.d.ts +9 -6
  21. package/api/definition/object.js +34 -4
  22. package/api/definition/object.js.map +1 -1
  23. package/api/definition/statement.js +5 -1
  24. package/api/definition/statement.js.map +1 -1
  25. package/api/definition/values.d.ts +31 -47
  26. package/api/definition/values.js +59 -145
  27. package/api/definition/values.js.map +1 -1
  28. package/api/deserialize/deserialize.d.ts +2 -1
  29. package/api/deserialize/deserialize.js +4 -1
  30. package/api/deserialize/deserialize.js.map +1 -1
  31. package/api/deserialize/type-store.d.ts +2 -2
  32. package/api/deserialize/type-store.js.map +1 -1
  33. package/api/expression.d.ts +5 -7
  34. package/api/expression.js.map +1 -1
  35. package/api/index.d.ts +2 -0
  36. package/api/index.js +2 -0
  37. package/api/index.js.map +1 -1
  38. package/api/module/common.d.ts +29 -0
  39. package/api/module/common.js +71 -0
  40. package/api/module/common.js.map +1 -0
  41. package/api/module/export.d.ts +15 -9
  42. package/api/module/export.js +57 -15
  43. package/api/module/export.js.map +1 -1
  44. package/api/module/import.d.ts +3 -8
  45. package/api/module/import.js +9 -19
  46. package/api/module/import.js.map +1 -1
  47. package/api/{module/app/program.d.ts → program.d.ts} +4 -4
  48. package/api/{module/app/program.js → program.js} +3 -3
  49. package/api/program.js.map +1 -0
  50. package/api/statement/control/block.d.ts +1 -2
  51. package/api/statement/control/block.js +20 -21
  52. package/api/statement/control/block.js.map +1 -1
  53. package/api/statement/control/switch.js +8 -3
  54. package/api/statement/control/switch.js.map +1 -1
  55. package/api/statement/control/terminate.d.ts +36 -11
  56. package/api/statement/control/terminate.js +79 -18
  57. package/api/statement/control/terminate.js.map +1 -1
  58. package/api/statement/declarations/declares.d.ts +7 -7
  59. package/api/statement/declarations/declares.js +1 -1
  60. package/api/statement/declarations/declares.js.map +1 -1
  61. package/api/statement/iterations/for.js +22 -16
  62. package/api/statement/iterations/for.js.map +1 -1
  63. package/api/statement/iterations/while.js +15 -11
  64. package/api/statement/iterations/while.js.map +1 -1
  65. package/api/utils.d.ts +2 -0
  66. package/api/utils.js +3 -1
  67. package/api/utils.js.map +1 -1
  68. package/package.json +4 -4
  69. package/scope/proxy.d.ts +0 -2
  70. package/scope/proxy.js +4 -24
  71. package/scope/proxy.js.map +1 -1
  72. package/scope/stack.d.ts +2 -2
  73. package/scope/stack.js.map +1 -1
  74. package/v8/enums.d.ts +204 -0
  75. package/v8/enums.js +375 -0
  76. package/v8/enums.js.map +1 -0
  77. package/v8/index.d.ts +2 -1
  78. package/v8/index.js +2 -1
  79. package/v8/index.js.map +1 -1
  80. package/v8/inline.d.ts +177 -0
  81. package/v8/inline.js +1973 -0
  82. package/v8/inline.js.map +1 -0
  83. package/v8/nodes.d.ts +8 -7
  84. package/v8/nodes.js +37 -63
  85. package/v8/nodes.js.map +1 -1
  86. package/v8/parser.d.ts +41 -151
  87. package/v8/parser.js +507 -1579
  88. package/v8/parser.js.map +1 -1
  89. package/v8/stream.d.ts +35 -3
  90. package/v8/stream.js +616 -164
  91. package/v8/stream.js.map +1 -1
  92. package/v8/token.d.ts +15 -9
  93. package/v8/token.js +79 -46
  94. package/v8/token.js.map +1 -1
  95. package/visit/visitor.js.map +1 -1
  96. package/api/module/app/program.js.map +0 -1
  97. package/v8/app.d.ts +0 -0
  98. package/v8/app.js +0 -2
  99. package/v8/app.js.map +0 -1
package/v8/parser.js CHANGED
@@ -1,1717 +1,645 @@
1
+ import { JavaScriptInlineParser } from './inline.js';
1
2
  import { Token } from './token.js';
2
- import { TokenStream } from './stream.js';
3
- import { OfNode, Identifier, ThisNode, GetIdentifier, SetIdentifier, AsyncIdentifier, NullNode, StringLiteral, AwaitIdentifier, ConstructorIdentifier, NameIdentifier, EvalIdentifier, ArgumentsIdentifier, TaggedTemplateExpression, TemplateLiteral } from '../api/definition/values.js';
4
- import { EmptyStatement } from '../api/statement/control/empty.js';
5
- import { BlockStatement } from '../api/statement/control/block.js';
6
- import { ArrowFunctionExpression, ArrowFunctionType, FunctionKind, Param, FunctionExpression, FunctionDeclaration } from '../api/definition/function.js';
7
- import { IfStatement } from '../api/statement/control/if.js';
8
- import { NewExpression } from '../api/computing/new.js';
9
- import { SpreadElement } from '../api/computing/spread.js';
10
- import { RestElement } from '../api/computing/rest.js';
3
+ import { Class, ClassBody, ClassDeclaration, ClassExpression, MetaProperty, MethodDefinition, PropertyDefinition, StaticBlock, Super } from '../api/class/class.js';
4
+ import { Identifier, Literal, NullNode } from '../api/definition/values.js';
11
5
  import { AssignmentExpression } from '../api/operators/assignment.js';
12
- import { GroupingExpression } from '../api/operators/grouping.js';
13
- import { MemberExpression } from '../api/definition/member.js';
14
- import { BindExpression } from '../api/definition/bind.js';
15
- import { ObjectExpression, Property, ObjectPattern } from '../api/definition/object.js';
16
- import { ArrayExpression, ArrayPattern } from '../api/definition/array.js';
17
- import { CallExpression } from '../api/computing/call.js';
18
- import { DoWhileNode, WhileNode } from '../api/statement/iterations/while.js';
19
- import { CatchClauseNode, ThrowStatement, TryCatchNode } from '../api/computing/throw.js';
20
- import { SwitchCase, DefaultExpression, SwitchStatement } from '../api/statement/control/switch.js';
21
- import { BreakStatement, ContinueStatement } from '../api/statement/control/terminate.js';
22
- import { ReturnStatement } from '../api/computing/return.js';
23
- import { YieldExpression } from '../api/computing/yield.js';
24
- import { VariableDeclarator, VariableDeclarationNode } from '../api/statement/declarations/declares.js';
25
- import { ForNode, ForOfNode, ForInNode, ForAwaitOfNode } from '../api/statement/iterations/for.js';
26
- import { ConditionalExpression } from '../api/operators/ternary.js';
27
- import { PipelineExpression } from '../api/operators/pipeline.js';
28
- import { LogicalExpression } from '../api/operators/logical.js';
29
- import { SequenceExpression } from '../api/operators/comma.js';
30
- import { ChainExpression } from '../api/operators/chaining.js';
31
- import { ExpressionStatement } from '../api/definition/statement.js';
32
- import { buildPostfixExpression, buildUnaryExpression, expressionFromLiteral, shortcutNumericLiteralBinaryExpression } from './nodes.js';
33
- import { BinaryExpression } from '../api/operators/binary.js';
34
- export var ParsingArrowHeadFlag;
35
- (function (ParsingArrowHeadFlag) {
36
- ParsingArrowHeadFlag[ParsingArrowHeadFlag["CertainlyNotArrowHead"] = 0] = "CertainlyNotArrowHead";
37
- ParsingArrowHeadFlag[ParsingArrowHeadFlag["MaybeArrowHead"] = 1] = "MaybeArrowHead";
38
- ParsingArrowHeadFlag[ParsingArrowHeadFlag["AsyncArrowFunction"] = 2] = "AsyncArrowFunction";
39
- })(ParsingArrowHeadFlag || (ParsingArrowHeadFlag = {}));
40
- export var PropertyKind;
41
- (function (PropertyKind) {
42
- PropertyKind[PropertyKind["Value"] = 0] = "Value";
43
- PropertyKind[PropertyKind["Shorthand"] = 1] = "Shorthand";
44
- PropertyKind[PropertyKind["ShorthandOrClassField"] = 2] = "ShorthandOrClassField";
45
- PropertyKind[PropertyKind["Assign"] = 3] = "Assign";
46
- PropertyKind[PropertyKind["Method"] = 4] = "Method";
47
- PropertyKind[PropertyKind["ClassField"] = 5] = "ClassField";
48
- PropertyKind[PropertyKind["AccessorGetter"] = 6] = "AccessorGetter";
49
- PropertyKind[PropertyKind["AccessorSetter"] = 7] = "AccessorSetter";
50
- PropertyKind[PropertyKind["Spread"] = 8] = "Spread";
51
- PropertyKind[PropertyKind["NotSet"] = 9] = "NotSet";
52
- })(PropertyKind || (PropertyKind = {}));
53
- export function parsePropertyKindFromToken(token, info) {
54
- switch (token) {
55
- case Token.COLON:
56
- info.kind = PropertyKind.Value;
57
- return true;
58
- case Token.COMMA:
59
- info.kind = PropertyKind.Shorthand;
60
- return true;
61
- case Token.R_CURLY:
62
- info.kind = PropertyKind.ShorthandOrClassField;
63
- return true;
64
- case Token.ASSIGN:
65
- info.kind = PropertyKind.Assign;
66
- return true;
67
- case Token.L_PARENTHESES:
68
- info.kind = PropertyKind.Method;
69
- return true;
70
- case Token.MUL:
71
- case Token.SEMICOLON:
72
- info.kind = PropertyKind.ClassField;
73
- return true;
74
- default:
75
- break;
76
- }
77
- return false;
78
- }
79
- export var PreParserIdentifierType;
80
- (function (PreParserIdentifierType) {
81
- PreParserIdentifierType["NullIdentifier"] = "NullIdentifier";
82
- PreParserIdentifierType["UnknownIdentifier"] = "UnknownIdentifier";
83
- PreParserIdentifierType["EvalIdentifier"] = "EvalIdentifier";
84
- PreParserIdentifierType["ArgumentsIdentifier"] = "ArgumentsIdentifier";
85
- PreParserIdentifierType["ConstructorIdentifier"] = "ConstructorIdentifier";
86
- PreParserIdentifierType["AwaitIdentifier"] = "AwaitIdentifier";
87
- PreParserIdentifierType["AsyncIdentifier"] = "AsyncIdentifier";
88
- PreParserIdentifierType["NameIdentifier"] = "NameIdentifier";
89
- PreParserIdentifierType["PrivateNameIdentifier"] = "PrivateNameIdentifier";
90
- })(PreParserIdentifierType || (PreParserIdentifierType = {}));
91
- export class AbstractParser {
92
- constructor(scanner) {
93
- this.scanner = scanner;
94
- }
95
- position() {
96
- return this.scanner.getPos();
97
- }
98
- current() {
99
- return this.scanner.currentToken();
100
- }
101
- next() {
102
- return this.scanner.next();
103
- }
104
- peek() {
105
- return this.scanner.peek();
106
- }
107
- peekAhead() {
108
- return this.scanner.peekAhead();
109
- }
110
- peekAheadPosition() {
111
- return this.scanner.peekAheadPosition();
112
- }
113
- peekPosition() {
114
- return this.scanner.peekPosition();
115
- }
116
- consume(token) {
117
- if (this.scanner.next().isNotType(token)) {
118
- throw new Error(this.errorMessage(`Parsing ${JSON.stringify(token)}`));
119
- }
120
- }
121
- check(token) {
122
- const next = this.scanner.peek();
123
- if (next.isType(token)) {
124
- this.scanner.next();
125
- return true;
126
- }
127
- return false;
128
- }
129
- checkValue(value) {
130
- const next = this.scanner.peek();
131
- if (next.value == value) {
132
- this.scanner.next();
133
- return true;
134
- }
135
- return false;
136
- }
137
- expect(token) {
138
- const current = this.scanner.next();
139
- if (current.isNotType(token)) {
140
- throw new Error(this.errorMessage(`Unexpected Token: ${JSON.stringify(token)}, current is ${JSON.stringify(current)}`));
141
- }
142
- }
143
- checkInOrOf() {
144
- if (this.check(Token.IN)) {
145
- return 'IN';
146
- }
147
- else if (this.checkValue(OfNode)) {
148
- return 'OF';
149
- }
150
- return false;
151
- }
152
- peekInOrOf() {
153
- var next = this.peek();
154
- if (next.isType(Token.IN)) {
155
- return 'IN';
156
- }
157
- else if (next.value === OfNode) {
158
- return 'OF';
159
- }
160
- return false;
161
- }
162
- isEvalOrArguments(name) {
163
- if (name.toString() === 'eval') {
164
- return true;
165
- }
166
- else if (name.toString() === 'arguments') {
167
- return true;
168
- }
169
- return false;
170
- }
171
- isNextLetKeyword() {
172
- if (this.peek().isNotType(Token.LET)) {
173
- return false;
174
- }
175
- const nextNextToken = this.peekAhead().token;
176
- switch (nextNextToken) {
177
- case Token.L_CURLY:
178
- case Token.L_BRACKETS:
179
- case Token.IDENTIFIER:
180
- case Token.STATIC:
181
- case Token.LET:
182
- case Token.YIELD:
183
- case Token.AWAIT:
184
- case Token.GET:
185
- case Token.SET:
186
- case Token.ASYNC:
187
- return true;
188
- default:
189
- return false;
190
- }
191
- }
192
- isIdentifier(expression) {
193
- return expression instanceof Identifier;
6
+ import { VariableDeclarationNode, VariableDeclarator } from '../api/statement/declarations/declares.js';
7
+ import { TokenStream } from './stream.js';
8
+ import { Program } from '../api/program.js';
9
+ import { ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, ExportSpecifier } from '../api/module/export.js';
10
+ import { ImportDeclaration, ImportDefaultSpecifier, ImportExpression, ImportNamespaceSpecifier, ImportSpecifier } from '../api/module/import.js';
11
+ import { ImportAttribute } from '../api/module/common.js';
12
+ import { ClassLiteralProperty, ClassLiteralPropertyKind, classPropertyKindFor, createClassInfo, FunctionKind, FunctionSyntaxKind, isAccessor, isStrict, LanguageMode, ParseFunctionFlag, PropertyKind, PropertyKindInfo, PropertyPosition, VariableDeclarationContext } from './enums.js';
13
+ export class JavaScriptParser extends JavaScriptInlineParser {
14
+ static parse(source, { mode } = {}) {
15
+ mode ?? (mode = LanguageMode.Strict);
16
+ const stream = (typeof source === 'string') || Array.isArray(source)
17
+ ? TokenStream.getTokenStream(source, mode) : source;
18
+ const parser = new JavaScriptParser(stream);
19
+ return parser.scan();
194
20
  }
195
- isParenthesized(expression) {
196
- return expression instanceof GroupingExpression || expression instanceof SequenceExpression;
21
+ static parseScript(source, options) {
22
+ return JavaScriptInlineParser.parse(source, options);
197
23
  }
198
- isAssignableIdentifier(expression) {
199
- if (!(expression instanceof Identifier)) {
200
- return false;
201
- }
202
- if (this.isEvalOrArguments(expression)) {
203
- return false;
24
+ scan() {
25
+ const isModule = isStrict(this.languageMode);
26
+ if (isModule) {
27
+ this.setFunctionKind(FunctionKind.Module);
204
28
  }
205
- return true;
206
- }
207
- isPattern(expression) {
208
- return expression instanceof ObjectPattern || expression instanceof ArrayPattern;
209
- }
210
- isProperty(expression) {
211
- return expression instanceof MemberExpression;
212
- }
213
- isCallNew(expression) {
214
- return expression instanceof NewExpression;
215
- }
216
- isCall(expression) {
217
- return expression instanceof CallExpression;
29
+ const body = isModule ? this.parseModuleItemList() : this.parseStatementList(Token.EOS);
30
+ return new Program(isModule ? 'module' : 'script', body);
218
31
  }
219
- isEmptyStatement(expression) {
220
- return expression instanceof EmptyStatement;
221
- }
222
- isThisProperty(expression) {
223
- if (this.isProperty(expression)) {
224
- if (expression.getObject() === ThisNode || expression.getObject().toString() === 'this') {
225
- return true;
226
- }
32
+ parseNewTargetExpression() {
33
+ this.consume(Token.PERIOD);
34
+ const target = this.parsePropertyOrPrivatePropertyName();
35
+ if (target.toString() !== 'target') {
36
+ throw new Error(this.errorMessage(`Expression (new.${target.toString()}) not supported.`));
227
37
  }
228
- return false;
229
- }
230
- isValidReferenceExpression(expression) {
231
- return this.isAssignableIdentifier(expression) || this.isProperty(expression);
38
+ return MetaProperty.NewTarget;
232
39
  }
233
- expectSemicolon() {
234
- const tok = this.peek();
235
- if (tok.isType(Token.SEMICOLON)) {
236
- this.next();
237
- return;
238
- }
239
- if (this.scanner.hasLineTerminatorBeforeNext() || Token.isAutoSemicolon(tok.token)) {
240
- return;
241
- }
242
- if (this.scanner.currentToken().isType(Token.AWAIT)) {
243
- throw new Error(this.errorMessage(`Await Not In Async Context/Function`));
40
+ parseClassExpression() {
41
+ this.consume(Token.CLASS);
42
+ let name;
43
+ let isStrictReserved = false;
44
+ if (this.peekAnyIdentifier()) {
45
+ name = this.parseAndClassifyIdentifier(this.next());
46
+ isStrictReserved = Token.isStrictReservedWord(this.current().token);
244
47
  }
48
+ const classLiteral = this.parseClassLiteral(name, isStrictReserved);
49
+ return new ClassExpression(classLiteral.getBody(), classLiteral.getDecorators(), classLiteral.getId(), classLiteral.getSuperClass());
245
50
  }
246
- peekAnyIdentifier() {
247
- return Token.isAnyIdentifier(this.peek().token);
248
- }
249
- errorMessage(message) {
250
- return this.scanner.createError(message);
251
- }
252
- }
253
- export class JavaScriptParser extends AbstractParser {
254
- static parse(source) {
255
- const stream = (typeof source === 'string' || Array.isArray(source))
256
- ? TokenStream.getTokenStream(source)
257
- : source;
258
- const parser = new JavaScriptParser(stream);
259
- return parser.scan();
260
- }
261
- scan() {
262
- const list = this.parseStatementList(Token.EOS);
263
- if (list.length === 1) {
264
- return list[0];
51
+ parseClassDeclaration(names, defaultExport) {
52
+ const nextToken = this.peek().token;
53
+ const isStrictReserved = Token.isStrictReservedWord(nextToken);
54
+ let name;
55
+ let variableName;
56
+ if (defaultExport && (nextToken == Token.EXTENDS || nextToken == Token.LBRACE)) {
57
+ name = new Literal('default');
58
+ variableName = '.default';
265
59
  }
266
- return new ExpressionStatement(list);
267
- }
268
- parseStatement() {
269
- switch (this.peek().token) {
270
- case Token.L_CURLY:
271
- return this.parseBlock();
272
- case Token.SEMICOLON:
273
- this.consume(Token.SEMICOLON);
274
- return EmptyStatement.INSTANCE;
275
- case Token.IF:
276
- return this.parseIfStatement();
277
- case Token.DO:
278
- return this.parseDoWhileStatement();
279
- case Token.WHILE:
280
- return this.parseWhileStatement();
281
- case Token.FOR:
282
- return this.parseForStatement();
283
- case Token.CONTINUE:
284
- return this.parseContinueStatement();
285
- case Token.BREAK:
286
- return this.parseBreakStatement();
287
- case Token.RETURN:
288
- return this.parseReturnStatement();
289
- case Token.THROW:
290
- return this.parseThrowStatement();
291
- case Token.TRY:
292
- return this.parseTryStatement();
293
- case Token.SWITCH:
294
- return this.parseSwitchStatement();
295
- case Token.VAR:
296
- case Token.LET:
297
- case Token.CONST:
298
- return this.parseVariableDeclarations();
299
- case Token.ASYNC:
300
- if (this.peekAhead().isType(Token.FUNCTION)) {
301
- this.consume(Token.ASYNC);
302
- this.consume(Token.FUNCTION);
303
- if (this.peek().isType(Token.MUL)) {
304
- return this.parseFunctionExpression(FunctionKind.ASYNC_GENERATOR);
305
- }
306
- return this.parseFunctionExpression(FunctionKind.ASYNC);
307
- }
308
- default:
309
- return this.parseExpressionOrLabelledStatement();
60
+ else {
61
+ const identifier = this.parseIdentifier();
62
+ variableName = identifier.getName();
63
+ name = identifier;
310
64
  }
65
+ const classLiteral = this.parseClassLiteral(name, isStrictReserved);
66
+ return new ClassDeclaration(classLiteral.getBody(), classLiteral.getDecorators(), classLiteral.getId(), classLiteral.getSuperClass());
311
67
  }
312
- parseTryStatement() {
313
- this.consume(Token.TRY);
314
- const tryBlock = this.parseBlock();
315
- if (tryBlock instanceof BlockStatement) {
316
- tryBlock.isStatement = true;
317
- }
318
- let peek = this.peek();
319
- if (peek.isNotType(Token.CATCH) && peek.isNotType(Token.FINALLY)) {
320
- throw new Error(this.errorMessage(`Uncaught SyntaxError: Missing catch or finally after try`));
321
- }
322
- let catchBlock;
323
- if (this.check(Token.CATCH)) {
324
- let catchVar;
325
- const hasBinding = this.check(Token.L_PARENTHESES);
326
- if (hasBinding) {
327
- catchVar = this.parseIdentifier();
328
- this.expect(Token.R_PARENTHESES);
68
+ parseClassLiteral(name, nameIsStrictReserved) {
69
+ const isAnonymous = !!!name;
70
+ if (!isAnonymous) {
71
+ if (nameIsStrictReserved) {
72
+ throw new Error(this.errorMessage(`Unexpected Strict Reserved class name`));
329
73
  }
330
- const block = this.parseBlock();
331
- if (block instanceof BlockStatement) {
332
- block.isStatement = true;
74
+ if (this.isEvalOrArguments(name)) {
75
+ throw new Error(this.errorMessage(`Strict Eval Arguments not allowed for class name`));
333
76
  }
334
- catchBlock = new CatchClauseNode(block, catchVar);
335
77
  }
336
- let finallyBlock;
337
- if (this.check(Token.FINALLY)) {
338
- finallyBlock = this.parseBlock();
339
- if (finallyBlock instanceof BlockStatement) {
340
- finallyBlock.isStatement = true;
341
- }
78
+ const classInfo = createClassInfo();
79
+ classInfo.isAnonymous = isAnonymous;
80
+ if (this.check(Token.EXTENDS)) {
81
+ classInfo.extends = this.parseLeftHandSideExpression();
342
82
  }
343
- return new TryCatchNode(tryBlock, catchBlock, finallyBlock);
344
- }
345
- parseBlock() {
346
- this.expect(Token.L_CURLY);
347
- const statements = [];
348
- const block = new BlockStatement(statements, false);
349
- while (this.peek().isNotType(Token.R_CURLY)) {
350
- const stat = this.parseStatementListItem();
351
- if (!stat) {
352
- return block;
83
+ this.expect(Token.LBRACE);
84
+ const hasExtends = !!classInfo.extends;
85
+ while (this.peek().isNotType(Token.RBRACE)) {
86
+ if (this.check(Token.SEMICOLON))
87
+ continue;
88
+ if (this.peek().isType(Token.STATIC) && this.peekAhead().isType(Token.LBRACE)) {
89
+ const staticBlock = this.parseClassStaticBlock(classInfo);
90
+ classInfo.staticElements.push(staticBlock);
91
+ classInfo.hasStaticBlocks = true;
92
+ continue;
353
93
  }
354
- else if (stat instanceof EmptyStatement) {
94
+ let isConstructor = !classInfo.hasSeenConstructor;
95
+ const propInfo = new PropertyKindInfo();
96
+ propInfo.position = PropertyPosition.ClassLiteral;
97
+ const property = this.parseClassPropertyDefinition(classInfo, propInfo, hasExtends);
98
+ const propertyKind = classPropertyKindFor(propInfo.kind);
99
+ if (!classInfo.hasStaticComputedNames && propInfo.isStatic && propInfo.isComputedName) {
100
+ classInfo.hasStaticComputedNames = true;
101
+ }
102
+ isConstructor && (isConstructor = classInfo.hasSeenConstructor);
103
+ const isField = propertyKind == ClassLiteralPropertyKind.FIELD;
104
+ if (propInfo.isPrivate) {
105
+ if (isConstructor) {
106
+ throw new Error(this.errorMessage('private constructor is not allowed'));
107
+ }
108
+ classInfo.requiresBrand || (classInfo.requiresBrand = !isField && !propInfo.isStatic);
109
+ const isMethod = propertyKind == ClassLiteralPropertyKind.METHOD;
110
+ classInfo.hasPrivateMethods || (classInfo.hasPrivateMethods = isMethod);
111
+ classInfo.hasStaticPrivateMethods || (classInfo.hasStaticPrivateMethods = isMethod && propInfo.isStatic);
112
+ this.declarePrivateClassMember(propInfo.name, property, propertyKind, propInfo.isStatic, classInfo);
355
113
  continue;
356
114
  }
357
- statements.push(stat);
358
- }
359
- this.expect(Token.R_CURLY);
360
- return block;
361
- }
362
- parseStatementListItem() {
363
- switch (this.peek().token) {
364
- case Token.FUNCTION:
365
- this.consume(Token.FUNCTION);
366
- if (this.peek().isType(Token.MUL)) {
367
- this.consume(Token.MUL);
368
- return this.parseFunctionExpression(FunctionKind.GENERATOR);
115
+ if (isField) {
116
+ if (propInfo.isComputedName) {
117
+ classInfo.computedFieldCount++;
369
118
  }
370
- return this.parseFunctionExpression(FunctionKind.NORMAL);
371
- case Token.CLASS:
372
- this.consume(Token.CLASS);
373
- return this.parseClassDeclaration(undefined, false);
374
- case Token.VAR:
375
- case Token.LET:
376
- case Token.CONST:
377
- return this.parseVariableDeclarations();
378
- case Token.ASYNC:
379
- if (this.peekAhead().isType(Token.FUNCTION)) {
380
- this.consume(Token.ASYNC);
381
- this.consume(Token.FUNCTION);
382
- if (this.peek().isType(Token.MUL)) {
383
- this.consume(Token.MUL);
384
- return this.parseFunctionExpression(FunctionKind.ASYNC_GENERATOR);
385
- }
386
- return this.parseFunctionExpression(FunctionKind.ASYNC);
387
- }
388
- break;
389
- default:
390
- break;
119
+ this.declarePublicClassField(property, propInfo.isStatic, propInfo.isComputedName, classInfo);
120
+ continue;
121
+ }
122
+ this.declarePublicClassMethod(name, property, isConstructor, classInfo);
391
123
  }
392
- return this.parseStatement();
124
+ this.expect(Token.RBRACE);
125
+ const classBody = this.rewriteClassLiteral(classInfo, name);
126
+ return new Class(classBody, [], name, classInfo.extends);
393
127
  }
394
- parseFunctionExpression(type) {
395
- let funcName;
396
- const peek = this.peek();
397
- if (peek.isNotType(Token.L_PARENTHESES)) {
398
- if (peek.isType(Token.L_BRACKETS)) {
399
- this.consume(Token.L_BRACKETS);
400
- funcName = this.parseMemberExpression();
401
- this.expect(Token.R_BRACKETS);
402
- }
403
- else {
404
- funcName = this.parseIdentifier();
128
+ declarePublicClassMethod(name, property, isConstructor, classInfo) {
129
+ if (isConstructor) {
130
+ if (classInfo.constructor) {
131
+ throw new SyntaxError(this.errorMessage('A class may only have one constructor.'));
405
132
  }
133
+ classInfo.constructor = property;
134
+ Reflect.set(classInfo.constructor, 'id', name);
135
+ return;
406
136
  }
407
- return this.parseFunctionLiteral(type, funcName);
137
+ classInfo.publicMembers.push(property);
408
138
  }
409
- parseIfStatement() {
410
- this.consume(Token.IF);
411
- this.consume(Token.L_PARENTHESES);
412
- const condition = this.parseExpression();
413
- this.consume(Token.R_PARENTHESES);
414
- const thenStatement = this.parseStatement();
415
- if (thenStatement instanceof BlockStatement) {
416
- thenStatement.isStatement = true;
139
+ declarePublicClassField(property, isStatic, isComputedName, classInfo) {
140
+ if (isStatic) {
141
+ classInfo.staticElements.push(property);
417
142
  }
418
- let elseStatement;
419
- if (this.peek().isType(Token.ELSE)) {
420
- this.consume(Token.ELSE);
421
- elseStatement = this.parseStatement();
143
+ else {
144
+ classInfo.instanceFields.push(property);
422
145
  }
423
- return new IfStatement(condition, thenStatement, elseStatement);
424
146
  }
425
- parseDoWhileStatement() {
426
- this.consume(Token.DO);
427
- const body = this.parseStatement();
428
- if (body instanceof BlockStatement) {
429
- body.isStatement = true;
430
- }
431
- this.expect(Token.WHILE);
432
- this.expect(Token.L_PARENTHESES);
433
- const condition = this.parseExpression();
434
- this.expect(Token.R_PARENTHESES);
435
- this.check(Token.SEMICOLON);
436
- return new DoWhileNode(condition, body);
147
+ declarePrivateClassMember(propertyName, property, kind, isStatic, classInfo) {
148
+ classInfo.privateMembers.push(property);
437
149
  }
438
- parseWhileStatement() {
439
- this.consume(Token.WHILE);
440
- this.expect(Token.L_PARENTHESES);
441
- const condition = this.parseExpression();
442
- this.expect(Token.R_PARENTHESES);
443
- const body = this.parseStatement();
444
- if (body instanceof BlockStatement) {
445
- body.isStatement = true;
150
+ parseClassPropertyDefinition(classInfo, propInfo, hasExtends) {
151
+ if (!classInfo) {
152
+ throw new Error(this.errorMessage('class info is undefined'));
446
153
  }
447
- return new WhileNode(condition, body);
448
- }
449
- parseThrowStatement() {
450
- this.consume(Token.THROW);
451
- if (this.scanner.hasLineTerminatorBeforeNext()) {
452
- throw new Error(this.scanner.createError(`New line After Throw`));
154
+ if (propInfo.position !== PropertyPosition.ClassLiteral) {
155
+ throw new Error(this.errorMessage('expected property position ClassLiteral'));
453
156
  }
454
- const exception = this.parseExpression();
455
- this.expectSemicolon();
456
- return new ThrowStatement(exception);
457
- }
458
- parseSwitchStatement() {
459
- this.consume(Token.SWITCH);
460
- this.expect(Token.L_PARENTHESES);
461
- const tag = this.parseExpression();
462
- this.expect(Token.R_PARENTHESES);
463
- const cases = [];
464
- const switchStatement = new SwitchStatement(tag, cases);
465
- let defaultSeen = false;
466
- this.expect(Token.L_CURLY);
467
- while (this.peek().isNotType(Token.R_CURLY)) {
468
- const statements = [];
469
- let label;
470
- if (this.check(Token.CASE)) {
471
- label = this.parseExpression();
157
+ const nameToken = this.peek();
158
+ let nameExpression;
159
+ if (nameToken.isType(Token.STATIC)) {
160
+ this.consume(Token.STATIC);
161
+ if (this.peek().isType(Token.LPAREN)) {
162
+ propInfo.kind = PropertyKind.Method;
163
+ nameExpression = this.parseIdentifier();
164
+ propInfo.name = nameExpression.getName();
472
165
  }
473
- else {
474
- this.expect(Token.DEFAULT);
475
- if (defaultSeen) {
476
- throw new Error(this.errorMessage(`Multiple Defaults In Switch`));
477
- }
478
- defaultSeen = true;
166
+ else if (this.peek().isType(Token.ASSIGN)
167
+ || this.peek().isType(Token.SEMICOLON)
168
+ || this.peek().isType(Token.RBRACK)) {
169
+ nameExpression = this.parseIdentifier();
170
+ propInfo.name = nameExpression.getName();
479
171
  }
480
- this.expect(Token.COLON);
481
- while (this.peek().isNotType(Token.CASE)
482
- && this.peek().isNotType(Token.DEFAULT)
483
- && this.peek().isNotType(Token.R_CURLY)) {
484
- const statement = this.parseStatementListItem();
485
- if (!statement || this.isEmptyStatement(statement)) {
486
- continue;
487
- }
488
- statements.push(statement);
172
+ else {
173
+ propInfo.isStatic = true;
174
+ nameExpression = this.parseProperty(propInfo);
489
175
  }
490
- const block = new BlockStatement(statements, true);
491
- const clause = defaultSeen ? new DefaultExpression(block) : new SwitchCase(label, block);
492
- cases.push(clause);
493
- }
494
- this.expect(Token.R_CURLY);
495
- return switchStatement;
496
- }
497
- parseForStatement() {
498
- this.consume(Token.FOR);
499
- const isAwait = this.check(Token.AWAIT);
500
- this.expect(Token.L_PARENTHESES);
501
- const peek = this.peek();
502
- const startsWithLet = peek.isType(Token.LET) || peek.isType(Token.VAR);
503
- let initializer;
504
- if (peek.isType(Token.CONST) || (startsWithLet && this.isNextLetKeyword())) {
505
- initializer = this.parseVariableDeclarations();
506
- }
507
- else if (peek.isType(Token.SEMICOLON)) {
508
- initializer = EmptyStatement.INSTANCE;
509
176
  }
510
177
  else {
511
- initializer = this.parseExpressionCoverGrammar();
512
- }
513
- if (initializer instanceof BinaryExpression) {
514
- const objectNode = initializer.getRight();
515
- initializer = initializer.getLeft();
516
- this.expect(Token.R_PARENTHESES);
517
- const statement = this.parseStatement();
518
- return new ForInNode(initializer, objectNode, statement);
178
+ nameExpression = this.parseProperty(propInfo);
519
179
  }
520
- const forMode = this.checkInOrOf();
521
- if (forMode) {
522
- const object = forMode === 'IN' ? this.parseAssignmentExpression() : this.parseExpression();
523
- this.expect(Token.R_PARENTHESES);
524
- const statement = this.parseStatement();
525
- if (statement instanceof BlockStatement) {
526
- statement.isStatement = true;
527
- }
528
- if (isAwait && forMode === 'OF') {
529
- return new ForAwaitOfNode(initializer, object, statement);
530
- }
531
- else if (forMode === 'OF') {
532
- return new ForOfNode(initializer, object, statement);
533
- }
534
- else if (forMode === 'IN') {
535
- return new ForInNode(initializer, object, statement);
536
- }
537
- else {
538
- throw new Error(this.errorMessage(`parsing for loop: ${this.position()}`));
180
+ switch (propInfo.kind) {
181
+ case PropertyKind.Assign:
182
+ case PropertyKind.ClassField:
183
+ case PropertyKind.ShorthandOrClassField:
184
+ case PropertyKind.NotSet: {
185
+ propInfo.kind = PropertyKind.ClassField;
186
+ const initializer = this.parseMemberInitializer(classInfo, propInfo.isStatic);
187
+ this.expectSemicolon();
188
+ const result = this.newClassLiteralProperty(nameExpression, initializer, ClassLiteralPropertyKind.FIELD, propInfo.isStatic, propInfo.isComputedName, propInfo.isPrivate);
189
+ this.setFunctionNameFromPropertyName(result, propInfo.name);
190
+ return result;
539
191
  }
540
- }
541
- this.expect(Token.SEMICOLON);
542
- let condition;
543
- if (!this.check(Token.SEMICOLON)) {
544
- condition = this.parseExpression();
545
- this.expect(Token.SEMICOLON);
546
- }
547
- let finalExpression;
548
- if (!this.check(Token.R_PARENTHESES)) {
549
- finalExpression = this.parseExpression();
550
- this.expect(Token.R_PARENTHESES);
551
- }
552
- const body = this.parseStatement();
553
- if (body instanceof BlockStatement) {
554
- body.isStatement = true;
555
- }
556
- return new ForNode(body, initializer, condition, finalExpression);
557
- }
558
- parseVariableDeclarations() {
559
- let mode;
560
- const token = this.peek().token;
561
- switch (token) {
562
- case Token.CONST:
563
- this.consume(token);
564
- mode = 'const';
565
- break;
566
- case Token.VAR:
567
- this.consume(token);
568
- mode = 'var';
569
- break;
570
- case Token.LET:
571
- default:
572
- this.consume(token);
573
- mode = 'let';
574
- break;
575
- }
576
- const variables = [];
577
- do {
578
- let name;
579
- let value;
580
- if (Token.isAnyIdentifier(this.peek().token)) {
581
- name = this.parseAndClassifyIdentifier(this.next());
582
- if (this.isEvalOrArguments(name)) {
583
- throw new Error(this.errorMessage(`Strict Eval Arguments`));
192
+ case PropertyKind.Method: {
193
+ if (!propInfo.isComputedName) {
194
+ this.checkClassMethodName(propInfo, classInfo);
584
195
  }
585
- }
586
- else {
587
- name = this.parseBindingPattern(true);
588
- }
589
- if (this.check(Token.ASSIGN)) {
590
- value = this.parseAssignmentExpression();
591
- }
592
- else if (!this.peekInOrOf()) {
593
- if (mode === 'const' && (name === undefined || value === undefined)) {
594
- throw new Error(this.errorMessage(`Declaration Missing Initializer : ${this.position()}`));
196
+ let kind = this.methodKindFor(propInfo.isStatic, propInfo.funcFlag);
197
+ if (!propInfo.isStatic && propInfo.name.toString() === 'constructor') {
198
+ classInfo.hasSeenConstructor = true;
199
+ kind = hasExtends ? FunctionKind.DerivedConstructor : FunctionKind.BaseConstructor;
595
200
  }
201
+ const value = this.parseFunctionLiteral(kind, FunctionSyntaxKind.AccessorOrMethod, nameExpression);
202
+ const result = this.newClassLiteralProperty(nameExpression, value, ClassLiteralProperty.Kind.METHOD, propInfo.isStatic, propInfo.isComputedName, propInfo.isPrivate);
203
+ this.setFunctionNameFromPropertyName(result, propInfo.name);
204
+ return result;
596
205
  }
597
- variables.push(new VariableDeclarator(name, value));
598
- } while (this.check(Token.COMMA));
599
- return new VariableDeclarationNode(variables, mode);
600
- }
601
- parseBindingPattern(isPattern) {
602
- const token = this.peek().token;
603
- if (Token.isAnyIdentifier(token)) {
604
- const name = this.parseAndClassifyIdentifier(this.next());
605
- if (this.isEvalOrArguments(name)) {
606
- throw new Error(this.errorMessage(`Strict Eval Arguments`));
206
+ case PropertyKind.AccessorGetter:
207
+ case PropertyKind.AccessorSetter: {
208
+ if (propInfo.funcFlag !== ParseFunctionFlag.IsNormal) {
209
+ throw new Error(this.errorMessage('accessor is not normal function'));
210
+ }
211
+ const isGet = propInfo.kind == PropertyKind.AccessorGetter;
212
+ if (!propInfo.isComputedName) {
213
+ this.checkClassMethodName(propInfo, classInfo);
214
+ nameExpression = new Literal(propInfo.name);
215
+ }
216
+ let kind;
217
+ if (propInfo.isStatic) {
218
+ kind = isGet ? FunctionKind.StaticGetterFunction
219
+ : FunctionKind.StaticSetterFunction;
220
+ }
221
+ else {
222
+ kind = isGet ? FunctionKind.GetterFunction
223
+ : FunctionKind.SetterFunction;
224
+ }
225
+ const value = this.parseFunctionLiteral(kind, FunctionSyntaxKind.AccessorOrMethod, nameExpression);
226
+ const propertyKind = isGet ? ClassLiteralProperty.Kind.GETTER : ClassLiteralProperty.Kind.SETTER;
227
+ const result = this.newClassLiteralProperty(nameExpression, value, propertyKind, propInfo.isStatic, propInfo.isComputedName, propInfo.isPrivate);
228
+ const prefix = isGet ? 'get ' : 'set ';
229
+ this.setFunctionNameFromPropertyName(result, propInfo.name, prefix);
230
+ return result;
607
231
  }
608
- return name;
609
- }
610
- if (token == Token.L_BRACKETS) {
611
- return this.parseArrayLiteral(isPattern);
612
- }
613
- else if (token == Token.L_CURLY) {
614
- return this.parseObjectLiteral(isPattern);
615
- }
616
- else {
617
- throw new Error(this.errorMessage(`Unexpected Token: ${this.next().getValue()}`));
232
+ case PropertyKind.Value:
233
+ case PropertyKind.Shorthand:
234
+ case PropertyKind.Spread:
235
+ return NullNode;
618
236
  }
237
+ throw new Error(this.errorMessage('UNREACHABLE'));
619
238
  }
620
- parseAndClassifyIdentifier(next) {
621
- if (next.isType(Token.IDENTIFIER)) {
622
- return next.getValue();
239
+ checkClassMethodName(propInfo, classInfo) {
240
+ if (!(propInfo.kind == PropertyKind.Method || isAccessor(propInfo.kind))) {
241
+ throw new Error(this.errorMessage('not kind of method or setter or getter'));
623
242
  }
624
- else if (next.isType(Token.SET)) {
625
- const value = this.parseFunctionDeclaration();
626
- return new Property(next.getValue(), value, 'set');
243
+ if (propInfo.isPrivate && propInfo.name.toString() === 'constructor') {
244
+ throw new Error(this.errorMessage('constructor is private'));
627
245
  }
628
- else if (next.isType(Token.GET)) {
629
- const value = this.parseFunctionDeclaration();
630
- return new Property(next.getValue(), value, 'get');
246
+ else if (propInfo.isStatic && propInfo.name.toString() === 'prototype') {
247
+ throw new Error(this.errorMessage('static prototype'));
631
248
  }
632
- else if (next.isType(Token.AWAIT)) {
633
- throw new Error(this.errorMessage(`un supported expression (await)`));
249
+ else if (propInfo.name.toString() === 'constructor') {
250
+ if (propInfo.funcFlag !== ParseFunctionFlag.IsNormal || isAccessor(propInfo.kind)) {
251
+ if (propInfo.funcFlag === ParseFunctionFlag.IsGenerator) {
252
+ throw new Error(this.errorMessage('constructor is generator'));
253
+ }
254
+ else if (propInfo.funcFlag === ParseFunctionFlag.IsAsync) {
255
+ throw new Error(this.errorMessage('constructor is async'));
256
+ }
257
+ if (classInfo.hasSeenConstructor) {
258
+ throw new Error(this.errorMessage('duplicate constructor'));
259
+ }
260
+ }
261
+ classInfo.hasSeenConstructor = true;
634
262
  }
635
- return next.getValue();
636
263
  }
637
- parseContinueStatement() {
638
- this.consume(Token.CONTINUE);
639
- this.expectSemicolon();
640
- return ContinueStatement.CONTINUE_INSTANCE;
264
+ parseClassStaticBlock(classInfo) {
265
+ this.consume(Token.STATIC);
266
+ this.setStatue(true, FunctionKind.ClassStaticInitializerFunction);
267
+ const block = this.parseBlock();
268
+ this.restoreStatue();
269
+ classInfo.hasStaticElements = true;
270
+ return new StaticBlock(block.getBody());
641
271
  }
642
- parseBreakStatement() {
643
- this.consume(Token.BREAK);
644
- this.expectSemicolon();
645
- return BreakStatement.BREAK_INSTANCE;
272
+ declareClass(variableName, value, names) {
273
+ names?.push(variableName);
274
+ const proxy = this.declareVariable(variableName, 'let');
275
+ return new AssignmentExpression('=', proxy, value);
646
276
  }
647
- parseReturnStatement() {
648
- this.consume(Token.RETURN);
649
- const tokenExp = this.peek();
650
- let returnValue;
651
- if (this.scanner.hasLineTerminatorBeforeNext() || Token.isAutoSemicolon(tokenExp.token)) {
652
- }
653
- else {
654
- returnValue = this.parseExpression();
655
- }
656
- this.expectSemicolon();
657
- return new ReturnStatement(returnValue);
277
+ declareVariable(name, mode) {
278
+ return new VariableDeclarationNode([new VariableDeclarator(new Identifier(name))], mode);
658
279
  }
659
- parseExpressionOrLabelledStatement() {
660
- switch (this.peek().token) {
661
- case Token.FUNCTION:
662
- case Token.L_CURLY:
663
- throw new Error(this.errorMessage(`Unreachable state`));
664
- case Token.CLASS:
665
- throw new Error(this.errorMessage(`Unexpected Token ${this.next().getValue().toString()}`));
666
- case Token.LET: {
667
- const nextNext = this.peekAhead();
668
- if (nextNext.isNotType(Token.L_BRACKETS) &&
669
- ((nextNext.isNotType(Token.L_CURLY) && nextNext.isNotType(Token.IDENTIFIER)))) {
670
- break;
280
+ newClassLiteralProperty(nameExpression, initializer, kind, isStatic, isComputedName, isPrivate) {
281
+ switch (kind) {
282
+ case ClassLiteralPropertyKind.METHOD:
283
+ if (nameExpression.toString() === 'constructor') {
284
+ return new MethodDefinition('constructor', nameExpression, initializer, [], isComputedName, isStatic);
671
285
  }
672
- throw new Error(this.errorMessage(`Unexpected Lexical Declaration ${this.position()}`));
673
- }
286
+ return new MethodDefinition('method', nameExpression, initializer, [], isComputedName, isStatic);
287
+ case ClassLiteralPropertyKind.GETTER:
288
+ return new MethodDefinition('get', nameExpression, initializer, [], isComputedName, isStatic);
289
+ case ClassLiteralPropertyKind.SETTER:
290
+ return new MethodDefinition('set', nameExpression, initializer, [], isComputedName, isStatic);
291
+ case ClassLiteralPropertyKind.FIELD:
292
+ return new PropertyDefinition(nameExpression, [], isComputedName, isStatic, initializer);
674
293
  default:
675
294
  break;
676
295
  }
677
- const startsWithIdentifier = Token.isAnyIdentifier(this.peek().token);
678
- const expression = this.parseExpressionCoverGrammar();
679
- if (this.peek().isType(Token.COLON) && startsWithIdentifier && this.isIdentifier(expression)) {
680
- this.consume(Token.COLON);
681
- if (this.peek().isType(Token.FUNCTION)) {
682
- return this.parseFunctionDeclaration();
683
- }
684
- return this.parseStatement();
685
- }
686
- this.expectSemicolon();
687
- return expression;
688
- }
689
- parseExpression() {
690
- return this.parseExpressionCoverGrammar();
691
- }
692
- parseFunctionDeclaration() {
693
- this.consume(Token.FUNCTION);
694
- if (this.check(Token.MUL)) {
695
- throw new Error(this.errorMessage(`Error Generator In Single Statement Context`));
696
- }
697
- return this.parseHoistableDeclaration(FunctionKind.NORMAL);
698
- }
699
- parseFunctionDeclarationAndGenerator() {
700
- this.consume(Token.FUNCTION);
701
- if (this.check(Token.MUL)) {
702
- return this.parseHoistableDeclaration(FunctionKind.GENERATOR);
703
- }
704
- return this.parseHoistableDeclaration(FunctionKind.NORMAL);
296
+ throw new Error(this.errorMessage('UNREACHABLE'));
705
297
  }
706
- parseHoistableDeclaration(flag) {
707
- if (FunctionKind.ASYNC === flag && this.check(Token.MUL)) {
708
- flag = FunctionKind.ASYNC_GENERATOR;
298
+ parseMemberInitializer(classInfo, isStatic) {
299
+ const kind = isStatic
300
+ ? FunctionKind.ClassStaticInitializerFunction
301
+ : FunctionKind.ClassMembersInitializerFunction;
302
+ let initializer = undefined;
303
+ if (this.check(Token.ASSIGN)) {
304
+ this.setStatue(true, kind);
305
+ initializer = this.parseAssignmentExpression();
306
+ this.restoreStatue();
709
307
  }
710
- let name;
711
- if (this.peek().isNotType(Token.L_PARENTHESES)) {
712
- name = this.parseIdentifier();
308
+ if (isStatic) {
309
+ classInfo.hasStaticElements = true;
713
310
  }
714
- return this.parseFunctionLiteral(flag, name);
715
- }
716
- parseIdentifier() {
717
- const next = this.next();
718
- if (!Token.isValidIdentifier(next.token)) {
719
- throw new Error(this.errorMessage(`Unexpected Token: ${next.getValue()}`));
720
- }
721
- if (next.isType(Token.IDENTIFIER)) {
722
- return next.getValue();
311
+ else {
312
+ classInfo.hasInstanceMembers = true;
723
313
  }
724
- return this.getIdentifier();
314
+ return initializer;
725
315
  }
726
- getIdentifier() {
727
- const current = this.current();
728
- const string = current.getValue().toString();
729
- switch (current.token) {
730
- case Token.AWAIT:
731
- return AwaitIdentifier;
732
- case Token.ASYNC:
733
- return AsyncIdentifier;
734
- case Token.PRIVATE_NAME:
735
- return new Identifier(`#${string}`);
736
- default:
737
- break;
738
- }
739
- if (string == 'constructor') {
740
- return ConstructorIdentifier;
741
- }
742
- if (string == 'name') {
743
- return NameIdentifier;
744
- }
745
- if (string == 'eval') {
746
- return EvalIdentifier;
747
- }
748
- else if (string == 'arguments') {
749
- return ArgumentsIdentifier;
750
- }
751
- throw new Error(this.errorMessage(`can't identify token ${string}`));
316
+ setFunctionNameFromPropertyName(property, name, prefix) {
752
317
  }
753
- parseFunctionLiteral(flag, name) {
754
- const functionInfo = {};
755
- this.expect(Token.L_PARENTHESES);
756
- const formals = this.parseFormalParameterList(functionInfo);
757
- this.expect(Token.R_PARENTHESES);
758
- const body = this.parseFunctionBody();
759
- if (name) {
760
- return new FunctionDeclaration(formals, body, flag, name, functionInfo.rest);
318
+ parseSuperExpression() {
319
+ this.consume(Token.SUPER);
320
+ if (Token.isProperty(this.peek().token)) {
321
+ if (this.peek().isType(Token.PERIOD) && this.peekAhead().isType(Token.PRIVATE_NAME)) {
322
+ this.consume(Token.PERIOD);
323
+ this.consume(Token.PRIVATE_NAME);
324
+ throw new Error(this.errorMessage('Unexpected Private Field'));
325
+ }
326
+ if (this.peek().isType(Token.QUESTION_PERIOD)) {
327
+ this.consume(Token.QUESTION_PERIOD);
328
+ throw new Error(this.errorMessage('Optional Chaining No Super'));
329
+ }
330
+ return Super.INSTANCE;
761
331
  }
762
- return new FunctionExpression(formals, body, flag, name, functionInfo.rest);
332
+ throw new Error(this.errorMessage('Unexpected Super'));
763
333
  }
764
- parseArrowFunctionBody() {
765
- const isExpression = this.peek().isNotType(Token.L_CURLY);
766
- if (isExpression) {
767
- const expression = this.parseAssignmentExpression();
768
- return expression;
769
- }
770
- else {
771
- return this.parseFunctionBody();
334
+ rewriteClassLiteral(classInfo, name) {
335
+ const body = [];
336
+ if (classInfo.hasSeenConstructor) {
337
+ body.push(classInfo.constructor);
772
338
  }
339
+ body.push(...classInfo.staticElements);
340
+ body.push(...classInfo.instanceFields);
341
+ body.push(...classInfo.publicMembers);
342
+ body.push(...classInfo.privateMembers);
343
+ return new ClassBody(body);
773
344
  }
774
- parseFunctionBody() {
775
- this.expect(Token.L_CURLY);
776
- const list = this.parseStatementList(Token.R_CURLY);
777
- this.expect(Token.R_CURLY);
778
- return list;
779
- }
780
- parseStatementList(endToken) {
781
- const list = [];
782
- while (this.peek().isNotType(endToken)) {
783
- const stat = this.parseStatementListItem();
345
+ parseModuleItemList() {
346
+ const body = [];
347
+ while (this.peek().isNotType(Token.EOS)) {
348
+ const stat = this.parseModuleItem();
784
349
  if (!stat) {
785
350
  break;
786
351
  }
787
352
  if (this.isEmptyStatement(stat)) {
788
353
  continue;
789
354
  }
790
- list.push(stat);
791
- }
792
- return list;
793
- }
794
- parseFormalParameterList(functionInfo) {
795
- const parameters = [];
796
- if (this.peek().isNotType(Token.R_PARENTHESES)) {
797
- while (true) {
798
- const param = this.parseFormalParameter(functionInfo);
799
- parameters.push(param);
800
- if (functionInfo.rest) {
801
- if (this.peek().isType(Token.COMMA)) {
802
- throw new Error(this.errorMessage(`Param After Rest`));
803
- }
804
- break;
805
- }
806
- if (!this.check(Token.COMMA))
807
- break;
808
- if (this.peek().isType(Token.R_PARENTHESES)) {
809
- break;
810
- }
811
- }
355
+ body.push(stat);
812
356
  }
813
- return parameters;
357
+ return body;
814
358
  }
815
- parseFormalParameter(functionInfo) {
816
- functionInfo.rest = this.check(Token.ELLIPSIS);
817
- let pattern = this.parseBindingPattern(true);
818
- let initializer;
819
- if (this.check(Token.ASSIGN)) {
820
- if (functionInfo.rest) {
821
- throw new Error(this.errorMessage(`Rest Default Initializer`));
822
- }
823
- const value = this.parseAssignmentExpression();
824
- initializer = new Param(pattern, value);
825
- }
826
- else {
827
- initializer = new Param(pattern);
359
+ parseModuleItem() {
360
+ const next = this.peek();
361
+ if (next.isType(Token.EXPORT)) {
362
+ return this.parseExportDeclaration();
828
363
  }
829
- return initializer;
830
- }
831
- parseExpressionCoverGrammar(info) {
832
- let variableIndex = 0;
833
- const list = [];
834
- let expression;
835
- while (true) {
836
- if (this.peek().isType(Token.ELLIPSIS)) {
837
- if (info) {
838
- info.rest = true;
839
- }
840
- return this.parseArrowParametersWithRest(list, variableIndex);
841
- }
842
- expression = this.parseAssignmentExpressionCoverGrammar();
843
- list.push(expression);
844
- if (!this.check(Token.COMMA))
845
- break;
846
- if (this.peek().isType(Token.R_PARENTHESES) && this.peekAhead().isType(Token.ARROW)) {
847
- break;
364
+ if (next.isType(Token.IMPORT)) {
365
+ const peekAhead = this.peekAhead();
366
+ if (peekAhead.isNotType(Token.LPAREN) && peekAhead.isNotType(Token.PERIOD)) {
367
+ return this.parseImportDeclaration();
848
368
  }
849
369
  }
850
- if (list.length == 1)
851
- return expression;
852
- return this.expressionListToExpression(list);
370
+ return this.parseStatementListItem();
853
371
  }
854
- parseArrowParametersWithRest(list, variableIndex) {
855
- this.consume(Token.ELLIPSIS);
856
- const pattern = this.parseBindingPattern(true);
857
- if (this.peek().isType(Token.ASSIGN)) {
858
- throw new Error(this.errorMessage(`Error A rest parameter cannot have an initializer`));
859
- }
860
- if (this.peek().isType(Token.COMMA)) {
861
- throw new Error(this.errorMessage(`Error A rest parameter or binding pattern may not have a trailing comma`));
862
- }
863
- if (this.peek().isNotType(Token.R_PARENTHESES) || this.peekAhead().isNotType(Token.ARROW)) {
864
- throw new Error(this.errorMessage(`Error Unexpected Token At ${this.position()}`));
865
- }
866
- list.push(pattern);
867
- return this.expressionListToExpression(list);
868
- }
869
- expressionListToExpression(list) {
870
- if (list.length === 1) {
871
- return list[0];
872
- }
873
- return new SequenceExpression(list);
874
- }
875
- parseMemberExpression() {
876
- const result = this.parsePrimaryExpression();
877
- return this.parseMemberExpressionContinuation(result);
878
- }
879
- toParamNode(expression) {
880
- if (expression instanceof AssignmentExpression) {
881
- return new Param(expression.getLeft(), expression.getRight());
882
- }
883
- if (expression instanceof GroupingExpression) {
884
- return new Param(expression.getNode());
885
- }
886
- return new Param(expression);
887
- }
888
- parsePrimaryExpression() {
889
- let token = this.peek();
890
- if (Token.isAnyIdentifier(token.token)) {
891
- this.consume(token.token);
892
- let kind = ArrowFunctionType.NORMAL;
893
- if (token.isType(Token.ASYNC) && !this.scanner.hasLineTerminatorBeforeNext()) {
894
- if (this.peek().isType(Token.FUNCTION)) {
895
- return this.parseFunctionDeclarationAndGenerator();
896
- }
897
- ;
898
- if (Token.isAnyIdentifier(this.peek().token) && this.peekAhead().isType(Token.ARROW)) {
899
- token = this.next();
900
- kind = ArrowFunctionType.ASYNC;
901
- }
902
- }
903
- if (this.peek().isType(Token.ARROW)) {
904
- const name = this.parseAndClassifyIdentifier(token);
905
- const params = [];
906
- if (name instanceof SequenceExpression) {
907
- params.push(...name.getExpressions().map(this.toParamNode));
372
+ parseExportDeclaration() {
373
+ this.expect(Token.EXPORT);
374
+ let result;
375
+ const names = [];
376
+ switch (this.peek().token) {
377
+ case Token.DEFAULT:
378
+ return this.parseExportDefault();
379
+ case Token.MUL:
380
+ return this.parseExportStar();
381
+ case Token.LBRACE: {
382
+ const exportData = this.parseExportClause();
383
+ let moduleSpecifier;
384
+ let importAssertions;
385
+ if (this.checkContextualKeyword('from')) {
386
+ moduleSpecifier = this.parseModuleSpecifier();
387
+ importAssertions = this.parseImportAssertClause();
388
+ this.expectSemicolon();
908
389
  }
909
390
  else {
910
- params.push(this.toParamNode(name));
391
+ this.expectSemicolon();
911
392
  }
912
- return this.parseArrowFunctionLiteral(params, kind);
393
+ return new ExportNamedDeclaration(exportData, undefined, moduleSpecifier, importAssertions);
913
394
  }
914
- return this.parseAndClassifyIdentifier(token);
915
- }
916
- if (Token.isLiteral(token.token)) {
917
- return expressionFromLiteral(this.next());
918
- }
919
- switch (token.token) {
920
- case Token.NEW:
921
- return this.parseMemberWithPresentNewPrefixesExpression();
922
- case Token.THIS:
923
- this.consume(Token.THIS);
924
- return ThisNode;
925
- case Token.DIV:
926
- case Token.DIV_ASSIGN:
927
- return this.parseRegExpLiteral();
928
- case Token.FUNCTION:
929
- this.consume(Token.FUNCTION);
930
- if (this.peek().isType(Token.MUL)) {
931
- this.consume(Token.MUL);
932
- return this.parseFunctionExpression(FunctionKind.GENERATOR);
933
- }
934
- return this.parseFunctionExpression(FunctionKind.NORMAL);
935
- case Token.SUPER: {
936
- return this.parseSuperExpression();
937
- }
938
- case Token.IMPORT:
939
- return this.parseImportExpressions();
940
- case Token.L_BRACKETS:
941
- return this.parseArrayLiteral(false);
942
- case Token.L_CURLY:
943
- return this.parseObjectLiteral(false);
944
- case Token.L_PARENTHESES: {
945
- this.consume(Token.L_PARENTHESES);
946
- if (this.check(Token.R_PARENTHESES)) {
947
- if (!this.peek().isType(Token.ARROW)) {
948
- throw new Error(this.errorMessage(`Unexpected Token: ${Token.R_PARENTHESES.getName()}`));
949
- }
950
- return this.parseArrowFunctionLiteral([], ArrowFunctionType.NORMAL);
951
- }
952
- const peekToken = this.peek();
953
- let expression;
954
- const info = {};
955
- if (peekToken.isType(Token.FUNCTION)) {
956
- this.consume(Token.FUNCTION);
957
- expression = this.parseFunctionLiteral(FunctionKind.NORMAL);
958
- }
959
- else if (peekToken.isType(Token.ASYNC) && this.peekAhead().isType(Token.FUNCTION)) {
960
- this.consume(Token.ASYNC);
961
- this.consume(Token.FUNCTION);
962
- expression = this.parseFunctionLiteral(FunctionKind.ASYNC);
963
- }
964
- else {
965
- expression = this.parseExpressionCoverGrammar(info);
966
- }
967
- this.expect(Token.R_PARENTHESES);
968
- if (this.peek().isType(Token.ARROW)) {
969
- if (expression instanceof SequenceExpression) {
970
- expression = this.parseArrowFunctionLiteral(expression.getExpressions(), ArrowFunctionType.NORMAL, info.rest);
971
- }
972
- else {
973
- expression = this.parseArrowFunctionLiteral([expression], ArrowFunctionType.NORMAL, info.rest);
974
- }
975
- }
976
- return expression;
395
+ case Token.FUNCTION: {
396
+ const declaration = this.parseFunctionDeclaration();
397
+ const identifier = declaration.getId();
398
+ result = new ExportNamedDeclaration([new ExportSpecifier(identifier, identifier)], declaration);
399
+ break;
977
400
  }
978
401
  case Token.CLASS: {
979
402
  this.consume(Token.CLASS);
980
- let name;
981
- let isStrictReserved = false;
982
- if (this.peekAnyIdentifier()) {
983
- name = this.parseAndClassifyIdentifier(this.next());
984
- isStrictReserved = Token.isStrictReservedWord(this.current().token);
985
- }
986
- return this.parseClassLiteral(name, isStrictReserved);
403
+ const declaration = this.parseClassDeclaration(names, false);
404
+ const identifier = declaration.getId();
405
+ result = new ExportNamedDeclaration([new ExportSpecifier(identifier, identifier)], declaration);
406
+ break;
987
407
  }
988
- case Token.TEMPLATE_LITERALS:
989
- return this.parseTemplateLiteral();
990
- default:
408
+ case Token.VAR:
409
+ case Token.LET:
410
+ case Token.CONST:
411
+ const declaration = this.parseVariableStatement(VariableDeclarationContext.StatementListItem, names);
412
+ const specifiers = declaration.getDeclarations()
413
+ .map(node => new ExportSpecifier(node.getId(), node.getId()));
414
+ result = new ExportNamedDeclaration(specifiers, declaration);
991
415
  break;
416
+ case Token.ASYNC:
417
+ this.consume(Token.ASYNC);
418
+ if (this.peek().isType(Token.FUNCTION) && !this.scanner.hasLineTerminatorBeforeNext()) {
419
+ const declaration = this.parseAsyncFunctionDeclaration(names, false);
420
+ const identifier = declaration.getId();
421
+ result = new ExportNamedDeclaration([new ExportSpecifier(identifier, identifier)], declaration);
422
+ break;
423
+ }
424
+ default:
425
+ throw new SyntaxError(this.errorMessage('Unexpected Token'));
992
426
  }
993
- throw new Error(this.errorMessage(`Unexpected Token: ${JSON.stringify(this.next())}`));
994
- }
995
- parseTemplateLiteral(tag) {
996
- const template = this.next().getValue();
997
- const exprs = template.expressions.map(expr => JavaScriptParser.parse(expr));
998
- if (tag) {
999
- return new TaggedTemplateExpression(tag, template.strings, exprs);
1000
- }
1001
- else {
1002
- return new TemplateLiteral(template.strings, exprs);
1003
- }
1004
- }
1005
- parseMemberWithPresentNewPrefixesExpression() {
1006
- this.consume(Token.NEW);
1007
- let classRef;
1008
- if (this.peek().isType(Token.IMPORT) && this.peekAhead().isType(Token.L_PARENTHESES)) {
1009
- throw new Error(this.errorMessage(`parsing new import (`));
1010
- }
1011
- else if (this.peek().isType(Token.SUPER)) {
1012
- throw new Error(this.errorMessage(`parsing new super() is never allowed`));
1013
- }
1014
- else if (this.peek().isType(Token.PERIOD)) {
1015
- classRef = this.parseNewTargetExpression();
1016
- return this.parseMemberExpressionContinuation(classRef);
1017
- }
1018
- else {
1019
- classRef = this.parseMemberExpression();
1020
- }
1021
- if (this.peek().isType(Token.L_PARENTHESES)) {
1022
- const args = this.parseArguments();
1023
- classRef = new NewExpression(classRef, args);
1024
- return this.parseMemberExpressionContinuation(classRef);
1025
- }
1026
- if (this.peek().isType(Token.QUESTION_PERIOD)) {
1027
- throw new Error(this.errorMessage(`parsing new xxx?.yyy at position`));
1028
- }
1029
- return new NewExpression(classRef);
427
+ return result;
1030
428
  }
1031
- parseArguments(maybeArrow) {
1032
- this.consume(Token.L_PARENTHESES);
1033
- const args = [];
1034
- while (this.peek().isNotType(Token.R_PARENTHESES)) {
1035
- const isSpread = this.check(Token.ELLIPSIS);
1036
- let argument = this.parseAssignmentExpressionCoverGrammar();
1037
- if (ParsingArrowHeadFlag.MaybeArrowHead === maybeArrow) {
1038
- if (isSpread) {
1039
- if (argument instanceof AssignmentExpression) {
1040
- throw new Error(this.errorMessage(` Rest parameter may not have a default initializer'`));
1041
- }
1042
- if (this.peek().isType(Token.COMMA)) {
1043
- throw new Error(this.errorMessage(`parsing '...spread,arg =>'`));
1044
- }
429
+ parseImportDeclaration() {
430
+ this.expect(Token.IMPORT);
431
+ const tok = this.peek();
432
+ if (tok.isType(Token.STRING)) {
433
+ const moduleSpecifier = this.parseModuleSpecifier();
434
+ const importAssertions = this.parseImportAssertClause();
435
+ this.expectSemicolon();
436
+ return new ImportDeclaration(moduleSpecifier, undefined, importAssertions);
437
+ }
438
+ let importDefaultBinding;
439
+ if (tok.isNotType(Token.MUL) && tok.isNotType(Token.LBRACE)) {
440
+ importDefaultBinding = this.parseNonRestrictedIdentifier();
441
+ }
442
+ let moduleNamespaceBinding;
443
+ let namedImports;
444
+ if (importDefaultBinding == undefined || this.check(Token.COMMA)) {
445
+ switch (this.peek().token) {
446
+ case Token.MUL: {
447
+ this.consume(Token.MUL);
448
+ this.expectContextualKeyword('as');
449
+ moduleNamespaceBinding = this.parseNonRestrictedIdentifier();
450
+ break;
1045
451
  }
452
+ case Token.LBRACE:
453
+ namedImports = this.parseNamedImports();
454
+ break;
455
+ default:
456
+ throw new SyntaxError(this.errorMessage('Unexpected Token'));
1046
457
  }
1047
- if (isSpread) {
1048
- argument = new SpreadElement(argument);
1049
- }
1050
- args.push(argument);
1051
- if (!this.check(Token.COMMA))
1052
- break;
1053
458
  }
1054
- if (!this.check(Token.R_PARENTHESES)) {
1055
- throw new Error(this.errorMessage(`parsing arguments call, expecting ')'`));
459
+ this.expectContextualKeyword('from');
460
+ const moduleSpecifier = this.parseModuleSpecifier();
461
+ const importAssertions = this.parseImportAssertClause();
462
+ this.expectSemicolon();
463
+ const specifiers = [];
464
+ if (moduleNamespaceBinding) {
465
+ specifiers.push(new ImportNamespaceSpecifier(moduleNamespaceBinding));
1056
466
  }
1057
- return args;
1058
- }
1059
- parseAssignmentExpressionCoverGrammar() {
1060
- if (this.peek().isType(Token.YIELD)) {
1061
- return this.parseYieldExpression();
467
+ if (importDefaultBinding) {
468
+ specifiers.push(new ImportDefaultSpecifier(importDefaultBinding));
1062
469
  }
1063
- let expression = this.parseConditionalExpression();
1064
- const op = this.peek().token;
1065
- if (!Token.isArrowOrAssignmentOp(op))
1066
- return expression;
1067
- if (op === Token.ARROW) {
1068
- if (!this.isIdentifier(expression) && !this.isParenthesized(expression)) {
1069
- throw new Error(this.errorMessage(`Malformed Arrow Fun Param List`));
1070
- }
1071
- if (expression instanceof SequenceExpression) {
1072
- const params = expression.getExpressions().map(expr => new Param(expr));
1073
- return this.parseArrowFunctionLiteral(params, ArrowFunctionType.NORMAL);
1074
- }
1075
- if (expression instanceof GroupingExpression) {
1076
- return this.parseArrowFunctionLiteral([new Param(expression.getNode())], ArrowFunctionType.NORMAL);
1077
- }
1078
- return this.parseArrowFunctionLiteral([new Param(expression)], ArrowFunctionType.NORMAL);
470
+ if (namedImports?.length) {
471
+ specifiers.push(...namedImports);
1079
472
  }
1080
- if (this.isAssignableIdentifier(expression)) {
1081
- if (this.isParenthesized(expression)) {
1082
- throw new Error(this.errorMessage(`Invalid Destructuring Target`));
1083
- }
473
+ return new ImportDeclaration(moduleSpecifier, specifiers.length ? specifiers : undefined, importAssertions);
474
+ }
475
+ parseImportExpressions() {
476
+ this.consume(Token.IMPORT);
477
+ if (this.check(Token.PERIOD)) {
478
+ this.expectContextualKeyword('meta');
479
+ return MetaProperty.ImportMeta;
1084
480
  }
1085
- else if (this.isProperty(expression)) {
481
+ if (this.peek().isNotType(Token.LPAREN)) {
482
+ throw new SyntaxError(this.errorMessage('Unexpected Token'));
1086
483
  }
1087
- else if (this.isPattern(expression) && Token.isAssignment(op)) {
1088
- if (this.isParenthesized(expression)) {
1089
- }
484
+ this.consume(Token.LPAREN);
485
+ if (this.peek().isType(Token.RPAREN)) {
486
+ throw new SyntaxError(this.errorMessage('Import Missing Specifier'));
1090
487
  }
1091
- else {
1092
- if (!this.isValidReferenceExpression(expression)) {
1093
- throw new Error(this.errorMessage(`Invalid Reference Expression`));
488
+ this.setAcceptIN(true);
489
+ const specifier = this.parseAssignmentExpressionCoverGrammar();
490
+ if (this.check(Token.COMMA)) {
491
+ if (this.check(Token.RPAREN)) {
492
+ return new ImportExpression(specifier);
1094
493
  }
1095
- if (Token.isLogicalAssignmentOp(op)) {
1096
- throw new Error(this.errorMessage(`Invalid Lhs In Assignment`));
494
+ else {
495
+ const importAssertions = this.parseAssignmentExpressionCoverGrammar();
496
+ this.check(Token.COMMA);
497
+ this.expect(Token.RPAREN);
498
+ return new ImportExpression(specifier, importAssertions);
1097
499
  }
1098
500
  }
1099
- this.consume(op);
1100
- const right = this.parseAssignmentExpression();
1101
- if (!Token.isAssignment(op)) {
1102
- throw new Error(this.errorMessage(`Invalid Destructuring Target`));
1103
- }
1104
- return new AssignmentExpression(op.getName(), expression, right);
1105
- }
1106
- parseAssignmentExpression() {
1107
- return this.parseAssignmentExpressionCoverGrammar();
501
+ this.expect(Token.RPAREN);
502
+ this.restoreAcceptIN();
503
+ return new ImportExpression(specifier);
1108
504
  }
1109
- parseArrowFunctionLiteral(parameters, flag, rest) {
1110
- this.consume(Token.ARROW);
1111
- const body = this.parseArrowFunctionBody();
1112
- return new ArrowFunctionExpression(parameters, body, flag, rest);
505
+ parseVariableStatement(varContext, names) {
506
+ const declarations = this.parseVariableDeclarations(varContext);
507
+ this.expectSemicolon();
508
+ names.push(...declarations.getDeclarations().map(d => d.getId().toString()));
509
+ return declarations;
1113
510
  }
1114
- parseRegExpLiteral() {
1115
- if (!this.scanner.scanRegExpPattern()) {
1116
- throw new Error('Unterminated RegExp');
511
+ parseImportAssertClause() {
512
+ if (this.scanner.hasLineTerminatorBeforeNext() || !this.checkContextualKeyword('assert')) {
513
+ return undefined;
1117
514
  }
1118
- return this.scanner.currentToken().getValue();
1119
- }
1120
- parseArrayLiteral(isPattern) {
1121
- this.consume(Token.L_BRACKETS);
1122
- const values = [];
1123
- let firstSpreadIndex = -1;
1124
- while (!this.check(Token.R_BRACKETS)) {
1125
- let elem;
1126
- if (this.peek().isType(Token.COMMA)) {
1127
- this.consume(Token.COMMA);
1128
- continue;
515
+ this.expect(Token.LBRACE);
516
+ const counts = {};
517
+ const importAssertions = [];
518
+ while (this.peek().isNotType(Token.RBRACE)) {
519
+ let attributeKey = this.checkAndGetValue(Token.STRING);
520
+ if (!attributeKey) {
521
+ attributeKey = this.parsePropertyOrPrivatePropertyName();
1129
522
  }
1130
- else if (this.check(Token.ELLIPSIS)) {
1131
- const argument = this.parsePossibleDestructuringSubPattern();
1132
- elem = isPattern
1133
- ? new RestElement(argument)
1134
- : new SpreadElement(argument);
1135
- if (firstSpreadIndex < 0) {
1136
- firstSpreadIndex = values.length;
1137
- }
1138
- if (this.peek().isType(Token.COMMA)) {
1139
- throw new Error(this.errorMessage(`Element After Rest @${this.position()}`));
1140
- }
523
+ this.expect(Token.COLON);
524
+ const attributeValue = this.expectAndGetValue(Token.STRING);
525
+ importAssertions.push(new ImportAttribute(attributeKey, attributeValue));
526
+ counts[attributeKey.toString()] = (counts[attributeKey.toString()] ?? 0) + 1;
527
+ if (counts[attributeKey.toString()] > 1) {
528
+ throw new SyntaxError(this.errorMessage('Import Assertion Duplicate Key'));
1141
529
  }
1142
- else {
1143
- elem = this.parsePossibleDestructuringSubPattern();
530
+ if (this.peek().isType(Token.RBRACE))
531
+ break;
532
+ if (!this.check(Token.COMMA)) {
533
+ throw new SyntaxError('Unexpected Token');
1144
534
  }
1145
- values.push(elem);
1146
- }
1147
- if (isPattern) {
1148
- return new ArrayPattern(values);
1149
535
  }
1150
- return new ArrayExpression(values);
536
+ this.expect(Token.RBRACE);
537
+ return importAssertions;
1151
538
  }
1152
- parsePossibleDestructuringSubPattern() {
1153
- return this.parseAssignmentExpressionCoverGrammar();
1154
- }
1155
- parseObjectLiteral(isPattern) {
1156
- this.consume(Token.L_CURLY);
1157
- const properties = [];
1158
- while (!this.check(Token.R_CURLY)) {
1159
- const property = this.parseObjectPropertyDefinition(isPattern);
1160
- properties.push(property);
1161
- if (this.peek().isNotType(Token.R_CURLY)) {
1162
- this.expect(Token.COMMA);
1163
- }
1164
- }
1165
- if (isPattern) {
1166
- return new ObjectPattern(properties);
1167
- }
1168
- return new ObjectExpression(properties);
539
+ parseModuleSpecifier() {
540
+ return this.expectAndGetValue(Token.STRING);
1169
541
  }
1170
- parseObjectPropertyDefinition(isPattern) {
1171
- const propInfo = { kind: PropertyKind.NotSet };
1172
- const nameExpression = this.parseProperty(propInfo);
1173
- switch (propInfo.kind) {
1174
- case PropertyKind.Spread:
1175
- let value = nameExpression;
1176
- if (isPattern) {
1177
- value = new RestElement(value.getArgument());
1178
- }
1179
- return new Property(value.getArgument(), value, 'init');
1180
- case PropertyKind.Value: {
1181
- this.consume(Token.COLON);
1182
- const value = this.parsePossibleDestructuringSubPattern();
1183
- return new Property(nameExpression, value, 'init');
1184
- }
1185
- case PropertyKind.Assign:
1186
- case PropertyKind.ShorthandOrClassField:
1187
- case PropertyKind.Shorthand: {
1188
- const lhs = new Identifier(propInfo.name);
1189
- if (!this.isAssignableIdentifier(lhs)) {
1190
- throw new Error(this.errorMessage('Strict Eval Arguments'));
1191
- }
1192
- let value;
1193
- if (this.peek().isType(Token.ASSIGN)) {
1194
- this.consume(Token.ASSIGN);
1195
- const rhs = this.parseAssignmentExpression();
1196
- value = new AssignmentExpression(Token.ASSIGN.getName(), lhs, rhs);
1197
- }
1198
- else {
1199
- value = lhs;
1200
- }
1201
- return new Property(nameExpression, value, 'init');
542
+ parseExportClause() {
543
+ this.expect(Token.LBRACE);
544
+ const exportData = [];
545
+ let nameTok = this.peek();
546
+ while (nameTok.isNotType(Token.RBRACE)) {
547
+ const localName = this.parseExportSpecifierName();
548
+ let exportName;
549
+ if (this.checkContextualKeyword('as')) {
550
+ exportName = this.parseExportSpecifierName();
1202
551
  }
1203
- case PropertyKind.Method: {
1204
- const value = this.parseFunctionLiteral(propInfo.funcFlag);
1205
- return new Property(nameExpression, value, 'init');
552
+ else {
553
+ exportName = localName;
1206
554
  }
1207
- case PropertyKind.AccessorGetter:
1208
- case PropertyKind.AccessorSetter: {
1209
- const isGet = propInfo.kind == PropertyKind.AccessorGetter;
1210
- const value = this.parseFunctionLiteral(propInfo.funcFlag);
1211
- return new Property(nameExpression, value, isGet ? 'get' : 'set');
555
+ exportData.push(new ExportSpecifier(exportName, localName));
556
+ if (this.peek().isType(Token.RBRACE))
557
+ break;
558
+ if (!this.check(Token.COMMA)) {
559
+ throw new SyntaxError('Unexpected Token');
1212
560
  }
1213
- case PropertyKind.ClassField:
1214
- case PropertyKind.NotSet:
1215
- return NullNode;
1216
561
  }
562
+ this.expect(Token.RBRACE);
563
+ return exportData;
1217
564
  }
1218
- parseProperty(propInfo) {
1219
- let nextToken = this.peek();
1220
- if (this.check(Token.ASYNC)) {
1221
- nextToken = this.peek();
1222
- if (nextToken.isNotType(Token.MUL)
1223
- && parsePropertyKindFromToken(nextToken.token, propInfo)
1224
- || this.scanner.hasLineTerminatorBeforeNext()) {
1225
- return AsyncIdentifier;
1226
- }
1227
- propInfo.kind = PropertyKind.Method;
1228
- propInfo.funcFlag = FunctionKind.ASYNC;
565
+ parseExportSpecifierName() {
566
+ const next = this.next();
567
+ if (next.isType(Token.IDENTIFIER) || Token.isPropertyName(next.token)) {
568
+ return next.getValue();
1229
569
  }
1230
- if (this.check(Token.MUL)) {
1231
- propInfo.kind = PropertyKind.Method;
1232
- propInfo.funcFlag = FunctionKind.ASYNC_GENERATOR;
570
+ if (next.isType(Token.STRING)) {
571
+ const exportName = next.getValue();
572
+ return exportName;
1233
573
  }
1234
- nextToken = this.peek();
1235
- if (propInfo.kind == PropertyKind.NotSet && nextToken.isType(Token.GET) || nextToken.isType(Token.SET)) {
1236
- const token = this.next();
1237
- if (parsePropertyKindFromToken(this.peek().token, propInfo)) {
1238
- return nextToken.isType(Token.GET) ? GetIdentifier : SetIdentifier;
574
+ throw new SyntaxError(this.errorMessage('Unexpected Token'));
575
+ }
576
+ parseNamedImports() {
577
+ this.expect(Token.LBRACE);
578
+ const result = [];
579
+ while (this.peek().isNotType(Token.RBRACE)) {
580
+ const importName = this.parseExportSpecifierName();
581
+ let localName = importName;
582
+ if (this.checkContextualKeyword('as')) {
583
+ localName = this.parsePropertyOrPrivatePropertyName();
1239
584
  }
1240
- if (token.isType(Token.GET)) {
1241
- propInfo.kind = PropertyKind.AccessorGetter;
585
+ if (!Token.isValidIdentifier(this.current().token, LanguageMode.Strict, false, isStrict(this.languageMode))) {
586
+ throw new SyntaxError(this.errorMessage('Unexpected Reserved Keyword'));
1242
587
  }
1243
- else if (token.isType(Token.SET)) {
1244
- propInfo.kind = PropertyKind.AccessorSetter;
588
+ else if (this.isEvalOrArguments(localName)) {
589
+ throw new SyntaxError(this.errorMessage('Strict Eval Arguments'));
1245
590
  }
1246
- }
1247
- let propertyName;
1248
- switch (nextToken.token) {
1249
- case Token.PRIVATE_NAME:
1250
- this.consume(Token.PRIVATE_NAME);
1251
- if (propInfo.kind == PropertyKind.NotSet) {
1252
- parsePropertyKindFromToken(this.peek().token, propInfo);
1253
- }
1254
- propertyName = this.getIdentifier();
1255
- break;
1256
- case Token.STRING:
1257
- case Token.NUMBER:
1258
- case Token.BIGINT:
1259
- this.consume(nextToken.token);
1260
- propertyName = nextToken.getValue();
1261
- propInfo.name = propertyName.getValue();
1262
- break;
1263
- case Token.L_BRACKETS:
1264
- this.consume(Token.L_BRACKETS);
1265
- propertyName = this.parseAssignmentExpression();
1266
- this.expect(Token.R_BRACKETS);
1267
- if (propInfo.kind === PropertyKind.NotSet) {
1268
- parsePropertyKindFromToken(this.peek().token, propInfo);
1269
- }
1270
- propInfo.name = propertyName.toString();
1271
- return propertyName;
1272
- case Token.ELLIPSIS:
1273
- if (propInfo.kind == PropertyKind.NotSet) {
1274
- this.consume(Token.ELLIPSIS);
1275
- propertyName = this.parsePossibleDestructuringSubPattern();
1276
- propInfo.kind = PropertyKind.Spread;
1277
- if (!this.isValidReferenceExpression(propertyName)) {
1278
- throw new Error(this.errorMessage('Invalid Rest Binding/Assignment Pattern'));
1279
- }
1280
- if (this.peek().isNotType(Token.R_CURLY)) {
1281
- throw new Error(this.errorMessage('Element After Rest'));
1282
- }
1283
- propInfo.name = propertyName.toString();
1284
- return propertyName;
1285
- }
1286
- default:
1287
- propertyName = new StringLiteral(this.parsePropertyName().toString());
1288
- propInfo.name = propertyName.toString();
591
+ result.push(new ImportSpecifier(localName, importName));
592
+ if (this.peek().isType(Token.RBRACE))
1289
593
  break;
594
+ this.expect(Token.COMMA);
1290
595
  }
1291
- if (propInfo.kind === PropertyKind.NotSet) {
1292
- parsePropertyKindFromToken(this.peek().token, propInfo);
1293
- }
1294
- return propertyName;
1295
- }
1296
- parseMemberExpressionContinuation(expression) {
1297
- if (!Token.isMember(this.peek().token))
1298
- return expression;
1299
- return this.doParseMemberExpressionContinuation(expression);
1300
- }
1301
- doParseMemberExpressionContinuation(expression) {
1302
- if (!Token.isMember(this.peek().token)) {
1303
- throw new Error(this.errorMessage(`Parsing member expression`));
1304
- }
1305
- do {
1306
- switch (this.peek().token) {
1307
- case Token.L_BRACKETS: {
1308
- this.consume(Token.L_BRACKETS);
1309
- const index = this.parseExpressionCoverGrammar();
1310
- expression = new MemberExpression(expression, index, true);
1311
- this.expect(Token.R_BRACKETS);
1312
- break;
1313
- }
1314
- case Token.PERIOD: {
1315
- this.consume(Token.PERIOD);
1316
- const key = this.parsePropertyName();
1317
- expression = new MemberExpression(expression, key, false);
1318
- break;
1319
- }
1320
- case Token.TEMPLATE_LITERALS: {
1321
- expression = this.parseTemplateLiteral(expression);
1322
- break;
1323
- }
1324
- default:
1325
- break;
1326
- }
1327
- } while (Token.isMember(this.peek().token));
1328
- return expression;
1329
- }
1330
- parsePropertyName() {
1331
- const next = this.next();
1332
- if (next.getValue() instanceof Identifier) {
1333
- return next.getValue();
1334
- }
1335
- if (Token.isPropertyName(next.token)) {
1336
- return new Identifier(next.token.getName());
1337
- }
1338
- throw new Error(this.errorMessage(`Parsing property expression: Unexpected Token`));
1339
- }
1340
- parsePipelineExpression(expression) {
1341
- let token;
1342
- while (Token.isPipelineOperator(token = this.peek().token)) {
1343
- this.consume(token);
1344
- expression = this.parsePipelineBody(expression);
1345
- }
1346
- return expression;
596
+ this.expect(Token.RBRACE);
597
+ return result;
1347
598
  }
1348
- parsePipelineBody(lhs) {
1349
- let body;
1350
- let token = this.peek();
1351
- switch (token.token) {
599
+ parseExportDefault() {
600
+ this.expect(Token.DEFAULT);
601
+ const localNames = [];
602
+ let result;
603
+ switch (this.peek().token) {
1352
604
  case Token.FUNCTION:
1353
- this.consume(Token.FUNCTION);
1354
- if (this.peek().isType(Token.MUL)) {
1355
- this.consume(Token.MUL);
1356
- body = this.parseFunctionExpression(FunctionKind.GENERATOR);
1357
- break;
1358
- }
1359
- body = this.parseFunctionExpression(FunctionKind.NORMAL);
605
+ result = this.parseHoistableDeclaration(localNames, true);
606
+ break;
607
+ case Token.CLASS:
608
+ this.consume(Token.CLASS);
609
+ result = this.parseClassDeclaration(localNames, true);
1360
610
  break;
1361
611
  case Token.ASYNC:
1362
- if (this.peekAhead().isType(Token.FUNCTION)) {
612
+ if (this.peekAhead().isType(Token.FUNCTION) && !this.scanner.hasLineTerminatorBeforeNext()) {
1363
613
  this.consume(Token.ASYNC);
1364
- this.consume(Token.FUNCTION);
1365
- if (this.peek().isType(Token.MUL)) {
1366
- this.consume(Token.MUL);
1367
- body = this.parseFunctionExpression(FunctionKind.ASYNC_GENERATOR);
1368
- break;
1369
- }
1370
- body = this.parseFunctionExpression(FunctionKind.ASYNC);
614
+ result = this.parseHoistableDeclaration(localNames, true);
1371
615
  break;
1372
616
  }
617
+ default: {
618
+ this.setAcceptIN(true);
619
+ result = this.parseAssignmentExpression();
620
+ this.expectSemicolon();
621
+ this.restoreAcceptIN();
1373
622
  break;
1374
- default:
1375
- break;
1376
- }
1377
- if (body) {
1378
- return new PipelineExpression(lhs, body);
1379
- }
1380
- if (token.isType(Token.L_PARENTHESES)) {
1381
- body = this.parsePrimaryExpression();
1382
- return new PipelineExpression(lhs, body);
1383
- }
1384
- const func = this.parseMemberExpression();
1385
- let args = [];
1386
- switch (this.peek().token) {
1387
- case Token.COLON:
1388
- do {
1389
- this.consume(Token.COLON);
1390
- const isSpread = this.check(Token.ELLIPSIS);
1391
- if (this.peek().isType(Token.CONDITIONAL)) {
1392
- this.consume(Token.CONDITIONAL);
1393
- if (isSpread) {
1394
- args.push('...?');
1395
- }
1396
- else {
1397
- args.push('?');
1398
- }
1399
- }
1400
- else {
1401
- const arg = this.parseAssignmentExpressionCoverGrammar();
1402
- if (isSpread) {
1403
- args.push(new SpreadElement(arg));
1404
- }
1405
- else {
1406
- args.push(arg);
1407
- }
1408
- }
1409
- } while (this.peek().isType(Token.COLON));
1410
- break;
1411
- case Token.L_PARENTHESES:
1412
- this.consume(Token.L_PARENTHESES);
1413
- let indexed = false;
1414
- while (this.peek().isNotType(Token.R_PARENTHESES)) {
1415
- const isSpread = this.check(Token.ELLIPSIS);
1416
- if (this.peek().isType(Token.CONDITIONAL)) {
1417
- this.consume(Token.CONDITIONAL);
1418
- indexed = true;
1419
- if (isSpread) {
1420
- args.push('...?');
1421
- }
1422
- else {
1423
- args.push('?');
1424
- }
1425
- }
1426
- else {
1427
- const arg = this.parseAssignmentExpressionCoverGrammar();
1428
- if (isSpread) {
1429
- args.push(new SpreadElement(arg));
1430
- }
1431
- else {
1432
- args.push(arg);
1433
- }
1434
- }
1435
- this.check(Token.COMMA);
1436
- }
1437
- this.expect(Token.R_PARENTHESES);
1438
- if (!indexed) {
1439
- body = new CallExpression(func, args);
1440
- return new PipelineExpression(lhs, body);
1441
- }
1442
- break;
1443
- default:
1444
- break;
1445
- }
1446
- return new PipelineExpression(lhs, func, args);
1447
- }
1448
- parseConditionalExpression() {
1449
- let expression = this.parseLogicalExpression();
1450
- expression = this.parsePipelineExpression(expression);
1451
- return this.peek().isType(Token.CONDITIONAL) ? this.parseConditionalContinuation(expression) : expression;
1452
- }
1453
- parseLogicalExpression() {
1454
- let expression = this.parseBinaryExpression(6);
1455
- const peek = this.peek();
1456
- if (peek.isType(Token.AND) || peek.isType(Token.OR)) {
1457
- const precedence = peek.token.getPrecedence();
1458
- expression = this.parseBinaryContinuation(expression, 4, precedence);
1459
- }
1460
- else if (peek.isType(Token.NULLISH)) {
1461
- expression = this.parseNullishExpression(expression);
1462
- }
1463
- return expression;
1464
- }
1465
- parseBinaryContinuation(x, prec, prec1) {
1466
- do {
1467
- while (this.peek().token.getPrecedence() === prec1) {
1468
- let y;
1469
- let op = this.next();
1470
- const is_right_associative = op.isType(Token.EXP);
1471
- const next_prec = is_right_associative ? prec1 : prec1 + 1;
1472
- y = this.parseBinaryExpression(next_prec);
1473
- if (Token.isCompare(op.token)) {
1474
- let cmp = op.token;
1475
- switch (op.token) {
1476
- case Token.NE:
1477
- cmp = Token.EQ;
1478
- break;
1479
- case Token.NE_STRICT:
1480
- cmp = Token.EQ_STRICT;
1481
- break;
1482
- default: break;
1483
- }
1484
- x = shortcutNumericLiteralBinaryExpression(x, y, cmp);
1485
- if (op.isNotType(cmp)) {
1486
- x = buildUnaryExpression(x, Token.NOT);
1487
- }
1488
- }
1489
- else {
1490
- x = shortcutNumericLiteralBinaryExpression(x, y, op.token);
1491
- }
1492
- }
1493
- --prec1;
1494
- } while (prec1 >= prec);
1495
- return x;
1496
- }
1497
- parseBinaryExpression(precedence) {
1498
- const x = this.parseUnaryExpression();
1499
- const precedence1 = this.peek().token.getPrecedence();
1500
- if (precedence1 >= precedence) {
1501
- return this.parseBinaryContinuation(x, precedence, precedence1);
1502
- }
1503
- return x;
1504
- }
1505
- parseUnaryExpression() {
1506
- const op = this.peek();
1507
- if (Token.isUnaryOrCount(op.token)) {
1508
- return this.parseUnaryOrPrefixExpression();
1509
- }
1510
- if (op.isType(Token.AWAIT)) {
1511
- return this.parseAwaitExpression();
1512
- }
1513
- return this.parsePostfixExpression();
1514
- }
1515
- parseUnaryOrPrefixExpression() {
1516
- const op = this.next();
1517
- const expression = this.parseUnaryExpression();
1518
- if (Token.isUnary(op.token)) {
1519
- if (op.isType(Token.DELETE)) {
1520
- if (this.isIdentifier(expression)) {
1521
- throw new Error(this.errorMessage(`"delete identifier" is a syntax error in strict mode`));
1522
- }
1523
- if (expression instanceof MemberExpression && expression.getProperty().toString().startsWith('#')) {
1524
- throw new Error(this.errorMessage(`"Delete Private Field" is a syntax error`));
1525
- }
1526
- }
1527
- if (this.peek().isType(Token.EXP)) {
1528
- throw new Error(this.errorMessage(`Unexpected Token Unary Exponentiation`));
1529
- }
1530
- }
1531
- if (Token.isCount(op.token) || Token.isUnary(op.token)) {
1532
- return buildUnaryExpression(expression, op.token);
1533
- }
1534
- throw new Error(this.errorMessage(`while rewrite unary operation`));
1535
- }
1536
- parsePostfixExpression() {
1537
- const expression = this.parseLeftHandSideExpression();
1538
- if (!Token.isCount(this.peek().token) || this.scanner.hasLineTerminatorBeforeNext()) {
1539
- return expression;
1540
- }
1541
- return this.parsePostfixContinuation(expression);
1542
- }
1543
- parsePostfixContinuation(expression) {
1544
- if (!this.isValidReferenceExpression(expression)) {
1545
- throw new Error(this.errorMessage(`Invalid LHS In Postfix Op.`));
1546
- }
1547
- const op = this.next();
1548
- return buildPostfixExpression(expression, op.token);
1549
- }
1550
- parseLeftHandSideExpression() {
1551
- const result = this.parseMemberExpression();
1552
- if (!Token.isPropertyOrCall(this.peek().token))
1553
- return result;
1554
- return this.parseLeftHandSideContinuation(result);
1555
- }
1556
- parseLeftHandSideContinuation(result) {
1557
- if (this.peek().isType(Token.L_PARENTHESES)
1558
- && this.isIdentifier(result)
1559
- && this.scanner.currentToken().isType(Token.ASYNC)
1560
- && !this.scanner.hasLineTerminatorBeforeNext()) {
1561
- const args = this.parseArguments(ParsingArrowHeadFlag.AsyncArrowFunction);
1562
- if (this.peek().isType(Token.ARROW)) {
1563
- if (!args.length)
1564
- return new EmptyStatement;
1565
- return this.expressionListToExpression(args);
1566
- }
1567
- result = new CallExpression(result, args);
1568
- if (!Token.isPropertyOrCall(this.peek().token))
1569
- return result;
1570
- }
1571
- let optionalChaining = false;
1572
- let isOptional = false;
1573
- do {
1574
- switch (this.peek().token) {
1575
- case Token.QUESTION_PERIOD: {
1576
- if (isOptional) {
1577
- throw new Error(this.errorMessage(`Failure Expression`));
1578
- }
1579
- this.consume(Token.QUESTION_PERIOD);
1580
- isOptional = true;
1581
- optionalChaining = true;
1582
- if (Token.isPropertyOrCall(this.peek().token))
1583
- continue;
1584
- const key = this.parsePropertyName();
1585
- result = new MemberExpression(result, key, false, isOptional);
1586
- break;
1587
- }
1588
- case Token.L_BRACKETS: {
1589
- this.consume(Token.L_BRACKETS);
1590
- const index = this.parseExpressionCoverGrammar();
1591
- result = new MemberExpression(result, index, true, isOptional);
1592
- this.expect(Token.R_BRACKETS);
1593
- break;
1594
- }
1595
- case Token.PERIOD: {
1596
- if (isOptional) {
1597
- throw new Error(this.errorMessage(`Unexpected Token:${this.position()}`));
1598
- }
1599
- this.consume(Token.PERIOD);
1600
- const key = this.parsePropertyName();
1601
- result = new MemberExpression(result, key, false, isOptional);
1602
- break;
1603
- }
1604
- case Token.L_PARENTHESES: {
1605
- const args = this.parseArguments();
1606
- if (result.toString() === 'eval') {
1607
- throw new Error(this.errorMessage(`'eval(...)' is not supported.`));
1608
- }
1609
- result = new CallExpression(result, args, isOptional);
1610
- break;
1611
- }
1612
- case Token.BIND: {
1613
- if (isOptional) {
1614
- throw new Error(this.errorMessage(`Unexpected Token:${this.position()}`));
1615
- }
1616
- this.consume(Token.BIND);
1617
- const key = this.parsePropertyName();
1618
- result = new BindExpression(result, key, false, isOptional);
1619
- break;
1620
- }
1621
- case Token.QUESTION_BIND: {
1622
- if (isOptional) {
1623
- throw new Error(this.errorMessage(`Failure Expression`));
1624
- }
1625
- this.consume(Token.QUESTION_BIND);
1626
- isOptional = true;
1627
- optionalChaining = true;
1628
- const key = this.parsePropertyName();
1629
- result = new BindExpression(result, key, true, isOptional);
1630
- break;
1631
- }
1632
- default:
1633
- if (optionalChaining) {
1634
- throw new Error(this.errorMessage(`Optional Chaining No Template support`));
1635
- }
1636
- result = this.parseTemplateLiteral(result);
1637
- break;
1638
- }
1639
- if (isOptional) {
1640
- isOptional = false;
1641
623
  }
1642
- } while (Token.isPropertyOrCall(this.peek().token));
1643
- if (optionalChaining) {
1644
- result = new ChainExpression(result);
1645
624
  }
1646
- return result;
1647
- }
1648
- parseAwaitExpression() {
1649
- this.consume(Token.AWAIT);
1650
- const value = this.parseUnaryExpression();
1651
- if (this.peek().isType(Token.EXP)) {
1652
- throw new Error(this.scanner.createError(`Unexpected Token Unary Exponentiation`));
1653
- }
1654
- return buildUnaryExpression(value, Token.AWAIT);
625
+ return new ExportDefaultDeclaration(result);
1655
626
  }
1656
- parseNullishExpression(expression) {
1657
- const list = [];
1658
- list.push(expression);
1659
- while (this.peek().isType(Token.NULLISH)) {
1660
- this.consume(Token.NULLISH);
1661
- expression = this.parseBinaryExpression(6);
1662
- list.push(expression);
1663
- }
1664
- expression = list.pop();
1665
- expression = list.reverse()
1666
- .reduce((previous, current) => new LogicalExpression(Token.NULLISH.getName(), current, previous), expression);
1667
- return expression;
1668
- }
1669
- parseConditionalContinuation(expression) {
1670
- this.consume(Token.CONDITIONAL);
1671
- const left = this.parseAssignmentExpression();
1672
- this.expect(Token.COLON);
1673
- const right = this.parseAssignmentExpression();
1674
- return new ConditionalExpression(expression, left, right);
1675
- }
1676
- parseYieldExpression() {
1677
- this.consume(Token.YIELD);
1678
- let delegating = false;
1679
- let expression;
1680
- if (this.check(Token.MUL)) {
1681
- delegating = true;
1682
- }
1683
- switch (this.peek().token) {
1684
- case Token.EOS:
1685
- case Token.SEMICOLON:
1686
- case Token.R_CURLY:
1687
- case Token.R_BRACKETS:
1688
- case Token.R_PARENTHESES:
1689
- case Token.COLON:
1690
- case Token.COMMA:
1691
- case Token.IN:
1692
- if (!delegating)
1693
- break;
1694
- throw new Error(this.errorMessage(`Delegating yields require an RHS`));
1695
- default:
1696
- expression = this.parseAssignmentExpressionCoverGrammar();
1697
- break;
627
+ parseExportStar() {
628
+ this.consume(Token.MUL);
629
+ if (!this.peekContextualKeyword('as')) {
630
+ this.expectContextualKeyword('from');
631
+ const moduleSpecifier = this.parseModuleSpecifier();
632
+ const importAssertions = this.parseImportAssertClause();
633
+ this.expectSemicolon();
634
+ return new ExportAllDeclaration(moduleSpecifier, undefined, importAssertions);
1698
635
  }
1699
- return new YieldExpression(delegating, expression);
1700
- }
1701
- parseNewTargetExpression() {
1702
- throw new Error(this.errorMessage('Expression (new.target) not supported.'));
1703
- }
1704
- parseClassDeclaration(names, defaultExport) {
1705
- throw new Error(this.errorMessage(`Expression (class) not supported.`));
1706
- }
1707
- parseClassLiteral(name, isStrictReserved) {
1708
- throw new Error(this.errorMessage(`Expression (class) not supported.`));
1709
- }
1710
- parseSuperExpression() {
1711
- throw new Error(this.errorMessage('Expression (supper) not supported.'));
1712
- }
1713
- parseImportExpressions() {
1714
- throw new Error(this.errorMessage('Expression (import) not supported.'));
636
+ this.expectContextualKeyword('as');
637
+ const exportName = this.parseExportSpecifierName();
638
+ this.expectContextualKeyword('from');
639
+ const moduleSpecifier = this.parseModuleSpecifier();
640
+ const importAssertions = this.parseImportAssertClause();
641
+ this.expectSemicolon();
642
+ return new ExportAllDeclaration(moduleSpecifier, exportName, importAssertions);
1715
643
  }
1716
644
  }
1717
645
  //# sourceMappingURL=parser.js.map