-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
parse.mjs
456 lines (411 loc) · 14.1 KB
/
parse.mjs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import * as acorn from 'acorn';
import { surroundingAgent } from './engine.mjs';
import { ExportEntryRecord, SourceTextModuleRecord } from './modules.mjs';
import { Value } from './value.mjs';
import {
ModuleRequests_ModuleItemList,
ImportEntries_ModuleItemList,
ExportEntries_ModuleItemList,
ImportedLocalNames,
} from './static-semantics/all.mjs';
import { ValueSet } from './helpers.mjs';
const HasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
function deepFreeze(o) {
Object.freeze(o);
Object.getOwnPropertyNames(o).forEach((prop) => {
if (HasOwnProperty(o, prop)
&& o[prop] !== null
&& (typeof o[prop] === 'object' || typeof o[prop] === 'function')
&& !Object.isFrozen(o[prop])) {
deepFreeze(o[prop]);
}
});
return o;
}
// Copied from acorn/src/scopeflags.js.
const SCOPE_FUNCTION = 2;
const SCOPE_ASYNC = 4;
const SCOPE_GENERATOR = 8;
function functionFlags(async, generator) {
// eslint-disable-next-line no-bitwise
return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0);
}
const optionalChainToken = { label: '?.' };
const nullishCoalescingToken = { label: '??', binop: 0 };
const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
const Parser = acorn.Parser.extend((P) => (class Parse262 extends P {
constructor(options = {}, source) {
super({
...options,
ecmaVersion: 2020,
// adds needed ParenthesizedExpression production
preserveParens: true,
locations: true,
}, source);
if (options.strict === true) {
this.strict = true;
}
this.containsTopLevelAwait = false;
}
parse() {
const body = super.parse();
body.containsTopLevelAwait = this.containsTopLevelAwait;
deepFreeze(body);
return body;
}
finishNode(node, type) {
node.strict = this.strict;
const ret = super.finishNode(node, type);
node.sourceText = () => this.input.slice(node.start, node.end);
if (ret.type === 'MethodDefinition' && ret.static) {
ret.start += 7; // don't include `static` in the source text
}
if (ret.type === 'Literal' && typeof ret.value === 'bigint') {
if (/^0[^xbo]/.test(ret.bigint)) {
this.raise(ret.start, 'Invalid or unexpected token');
}
}
return ret;
}
getTokenFromCode(code) {
if (code === 63) {
this.pos += 1;
if (surroundingAgent.feature('OptionalChaining')) {
const next = this.input.charCodeAt(this.pos);
if (next === 46) {
const nextNext = this.input.charCodeAt(this.pos + 1);
if (nextNext < 48 || nextNext > 57) {
this.pos += 1;
return this.finishToken(optionalChainToken);
}
}
}
if (surroundingAgent.feature('NullishCoalescing')) {
const next = this.input.charCodeAt(this.pos);
if (next === 63) {
this.pos += 1;
return this.finishToken(nullishCoalescingToken, nullishCoalescingToken.label);
}
}
return this.finishToken(acorn.tokTypes.question);
}
return super.getTokenFromCode(code);
}
parseStatement(context, topLevel, exports) {
if (this.type === acorn.tokTypes._import && surroundingAgent.feature('import.meta')) { // eslint-disable-line no-underscore-dangle
skipWhiteSpace.lastIndex = this.pos;
const skip = skipWhiteSpace.exec(this.input);
const next = this.pos + skip[0].length;
const nextCh = this.input.charCodeAt(next);
if (nextCh === 40 || nextCh === 46) { // '(' '.'
const node = this.startNode();
return this.parseExpressionStatement(node, this.parseExpression());
}
}
return super.parseStatement(context, topLevel, exports);
}
parseExprImport() {
const node = this.startNode();
const meta = this.parseIdent(true);
switch (this.type) {
case acorn.tokTypes.parenL:
return this.parseDynamicImport(node);
case acorn.tokTypes.dot:
if (!surroundingAgent.feature('import.meta')) {
return this.unexpected();
}
if (!(this.inModule || this.allowImportExportAnywhere)) {
return this.unexpected();
}
this.next();
node.meta = meta;
node.property = this.parseIdent(true);
if (node.property.name !== 'meta' || this.containsEsc) {
return this.unexpected();
}
return this.finishNode(node, 'MetaProperty');
default:
return this.unexpected();
}
}
parseSubscripts(base, startPos, startLoc, noCalls) {
if (noCalls) {
return super.parseSubscripts(base, startPos, startLoc, noCalls);
}
const maybeAsyncArrow = base.type === 'Identifier'
&& base.name === 'async'
&& this.lastTokEnd === base.end
&& !this.canInsertSemicolon()
&& this.input.slice(base.start, base.end) === 'async';
/**
* Optional chains are hard okay?
*
* a.b?.c
* @=>
* OptionalExpression a.b?.c
* MemberExpression a.b
* OptionalChain ?.c
*
* a.b?.c.d.e
* @=>
* OptionalExpressoin a.b?.c.d.e
* MemberExpression a.b
* OptionalChain ?.c.d.e
* OptionalChain ?.c.d
* OptionalChain ?.c
* Identifier .d
* Identifier .e
*
* a.b?.c.d
* @=>
* OptionalExpression a.b?.c.d
* MemberExpression a.b
* OptionalChain ?.c.d
* OptionalChain ?.c
* Identifier .d
*
* a.b?.c.d?.e.f
* @=>
* OptionalExpression a.b?.c.d?.e.f
* OptionalExpression a.b?.c.d
* MemberExpression a.b
* OptionalChain ?.c.d
* OptionalChain ?.c
* Identifier .d
* OptionalChain ?.e.f
* OptionalChain ?.e
* Identifier .f
*/
while (true) {
if (this.eat(optionalChainToken)) {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.chain = this.parseOptionalChain(startPos, startLoc);
base = this.finishNode(node, 'OptionalExpression');
} else {
const element = this.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow);
if (element === base) {
break;
}
base = element;
}
}
return base;
}
parseOptionalChain(startPos, startLoc) {
let base = this.startNodeAt(startPos, startLoc);
if (this.eat(acorn.tokTypes.bracketL)) {
base.property = this.parseExpression();
this.expect(acorn.tokTypes.bracketR);
base.computed = true;
base = this.finishNode(base, 'OptionalChain');
} else if (this.eat(acorn.tokTypes.parenL)) {
base.arguments = this.parseExprList(acorn.tokTypes.parenR, this.options.ecmaVersion >= 8, false, undefined);
} else {
base.property = this.parseIdent(true);
base.computed = false;
}
base.base = null;
base = this.finishNode(base, 'OptionalChain');
while (true) {
const computed = this.eat(acorn.tokTypes.bracketL);
if (computed || this.eat(acorn.tokTypes.dot)) {
const node = this.startNodeAt(startPos, startLoc);
node.base = base;
node.property = computed ? this.parseExpression() : this.parseIdent(true);
if (computed) {
this.expect(acorn.tokTypes.bracketR);
}
node.computed = computed;
base = this.finishNode(node, 'OptionalChain');
} else if (this.eat(acorn.tokTypes.parenL)) {
const node = this.startNodeAt(startPos, startLoc);
node.base = base;
node.arguments = this.parseExprList(acorn.tokTypes.parenR, this.options.ecmaVersion >= 8, false, undefined);
base = this.finishNode(node, 'OptionalChain');
} else if (this.eat(acorn.tokTypes.backQuote)) {
this.raise(this.start, 'Cannot tag an optional chain');
} else {
break;
}
}
return base;
}
buildBinary(startPos, startLoc, left, right, op, logical) {
if (op === '??') {
if (left.type === 'LogicalExpression') {
this.raise(left.start, 'Cannot mix &&, ||, and ??');
}
if (right.type === 'LogicalExpression') {
this.raise(right.start, 'Cannot mix &&, ||, and ??');
}
} else if (logical) {
if (left.operator === '??') {
this.raise(left.start, 'Cannot mix &&, ||, and ??');
}
if (right.operator === '??') {
this.raise(right.start, 'Cannot mix &&, ||, and ??');
}
}
return super.buildBinary(startPos, startLoc, left, right, op, logical);
}
parseAwait(...args) {
const node = super.parseAwait(...args);
if (!this.inFunction) {
this.containsTopLevelAwait = true;
}
return node;
}
// Adapted from several different places in Acorn.
static parseFunctionBody(sourceText, async, generator) {
const parser = new Parser({
sourceType: 'script',
}, sourceText);
// Parser.prototype.parse()
const node = parser.startNode();
parser.nextToken();
// Parser.prototype.parseFunction()
parser.initFunction(node);
parser.enterScope(functionFlags(async, generator));
// Parser.prototype.parseBlock()
const body = [];
while (!parser.eat(acorn.tokTypes.eof)) {
const stmt = parser.parseStatement(null);
body.push(stmt);
}
// Parser.prototype.parseFunctionBody()
parser.adaptDirectivePrologue(body);
deepFreeze(body);
return body;
}
}));
export function ParseAsFunctionBody(sourceText) {
return Parser.parseFunctionBody(sourceText, false, false);
}
export function ParseAsGeneratorBody(sourceText) {
return Parser.parseFunctionBody(sourceText, false, true);
}
export function ParseAsAsyncFunctionBody(sourceText) {
return Parser.parseFunctionBody(sourceText, true, false);
}
export function ParseAsAsyncGeneratorBody(sourceText) {
return Parser.parseFunctionBody(sourceText, true, true);
}
// Adapted from several different places in Acorn.
// `strict` refers to ContainsUseStrict of the corresponding function body.
export function ParseAsFormalParameters(sourceText, strict, enableAwait, enableYield) {
// Adapted from different places in Acorn.
const parser = new Parser({
sourceType: 'script',
}, sourceText);
parser.strict = strict;
// Parser.prototype.parse()
const node = parser.startNode();
parser.nextToken();
// Parser.prototype.parseFunction()
parser.initFunction(node);
parser.enterScope(functionFlags(enableAwait, enableYield));
// Parser.prototype.parseFunctionParams()
const params = parser.parseBindingList(acorn.tokTypes.eof, false, true);
parser.checkYieldAwaitInDefaultParams();
// Parser.prototype.parseFunctionBody()
const simple = parser.isSimpleParamList(params);
if (strict && !simple) {
parser.raiseRecoverable(node.start, 'Illegal \'use strict\' directive in function with non-simple parameter list');
}
parser.checkParams({ params }, !strict && simple);
deepFreeze(params);
return params;
}
export const emptyConstructorNode = Parser.parse('(class { constructor() {} })').body[0].expression.expression.body.body[0];
export const forwardingConstructorNode = Parser.parse('(class extends X { constructor(...args) { super(...args); } })').body[0].expression.expression.body.body[0];
function forwardError(fn) {
try {
return fn();
} catch (e) {
if (e.name === 'SyntaxError') {
return [surroundingAgent.Throw('SyntaxError', 'Raw', e.message).Value];
} else {
throw e;
}
}
}
export function ParseScript(sourceText, realm, hostDefined = {}, strict) {
const body = forwardError(() => Parser.parse(sourceText, {
sourceType: 'script',
strict,
}));
if (Array.isArray(body)) {
return body;
}
return {
Realm: realm,
Environment: undefined,
ECMAScriptCode: body,
HostDefined: hostDefined,
};
}
export function ParseModule(sourceText, realm, hostDefined = {}) {
// Assert: sourceText is an ECMAScript source text (see clause 10).
const body = forwardError(() => Parser.parse(sourceText, {
sourceType: 'module',
allowAwaitOutsideFunction: surroundingAgent.feature('TopLevelAwait'),
}));
if (Array.isArray(body)) {
return body;
}
const requestedModules = ModuleRequests_ModuleItemList(body.body);
const importEntries = ImportEntries_ModuleItemList(body.body);
const importedBoundNames = new ValueSet(ImportedLocalNames(importEntries));
const indirectExportEntries = [];
const localExportEntries = [];
const starExportEntries = [];
const exportEntries = ExportEntries_ModuleItemList(body.body);
for (const ee of exportEntries) {
if (ee.ModuleRequest === Value.null) {
if (!importedBoundNames.has(ee.LocalName)) {
localExportEntries.push(ee);
} else {
const ie = importEntries.find((e) => e.LocalName.stringValue() === ee.LocalName.stringValue());
if (ie.ImportName.stringValue() === '*') {
// Assert: This is a re-export of an imported module namespace object.
localExportEntries.push(ee);
} else {
indirectExportEntries.push(new ExportEntryRecord({
ModuleRequest: ie.ModuleRequest,
ImportName: ie.ImportName,
LocalName: Value.null,
ExportName: ee.ExportName,
}));
}
}
} else if (ee.ImportName.stringValue() === '*') {
starExportEntries.push(ee);
} else {
indirectExportEntries.push(ee);
}
}
return new SourceTextModuleRecord({
Realm: realm,
Environment: Value.undefined,
Namespace: Value.undefined,
ImportMeta: Value.undefined,
Async: body.containsTopLevelAwait ? Value.true : Value.false,
AsyncEvaluating: Value.false,
TopLevelCapability: Value.undefined,
AsyncParentModules: Value.undefined,
PendingAsyncDependencies: Value.undefined,
Status: 'unlinked',
EvaluationError: Value.undefined,
HostDefined: hostDefined,
ECMAScriptCode: body,
Context: Value.undefined,
RequestedModules: requestedModules,
ImportEntries: importEntries,
LocalExportEntries: localExportEntries,
IndirectExportEntries: indirectExportEntries,
StarExportEntries: starExportEntries,
DFSIndex: Value.undefined,
DFSAncestorIndex: Value.undefined,
});
}