Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80555 views
1
import {LooseParser} from "./state"
2
import {isDummy} from "./parseutil"
3
import {tokTypes as tt} from ".."
4
5
const lp = LooseParser.prototype
6
7
lp.checkLVal = function(expr, binding) {
8
if (!expr) return expr
9
switch (expr.type) {
10
case "Identifier":
11
return expr
12
13
case "MemberExpression":
14
return binding ? this.dummyIdent() : expr
15
16
case "ParenthesizedExpression":
17
expr.expression = this.checkLVal(expr.expression, binding)
18
return expr
19
20
// FIXME recursively check contents
21
case "ObjectPattern":
22
case "ArrayPattern":
23
case "RestElement":
24
case "AssignmentPattern":
25
if (this.options.ecmaVersion >= 6) return expr
26
27
default:
28
return this.dummyIdent()
29
}
30
}
31
32
lp.parseExpression = function(noIn) {
33
let start = this.storeCurrentPos()
34
let expr = this.parseMaybeAssign(noIn)
35
if (this.tok.type === tt.comma) {
36
let node = this.startNodeAt(start)
37
node.expressions = [expr]
38
while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn))
39
return this.finishNode(node, "SequenceExpression")
40
}
41
return expr
42
}
43
44
lp.parseParenExpression = function() {
45
this.pushCx()
46
this.expect(tt.parenL)
47
let val = this.parseExpression()
48
this.popCx()
49
this.expect(tt.parenR)
50
return val
51
}
52
53
lp.parseMaybeAssign = function(noIn) {
54
let start = this.storeCurrentPos()
55
let left = this.parseMaybeConditional(noIn)
56
if (this.tok.type.isAssign) {
57
let node = this.startNodeAt(start)
58
node.operator = this.tok.value
59
node.left = this.tok.type === tt.eq ? this.toAssignable(left) : this.checkLVal(left)
60
this.next()
61
node.right = this.parseMaybeAssign(noIn)
62
return this.finishNode(node, "AssignmentExpression")
63
}
64
return left
65
}
66
67
lp.parseMaybeConditional = function(noIn) {
68
let start = this.storeCurrentPos()
69
let expr = this.parseExprOps(noIn)
70
if (this.eat(tt.question)) {
71
let node = this.startNodeAt(start)
72
node.test = expr
73
node.consequent = this.parseMaybeAssign()
74
node.alternate = this.expect(tt.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent()
75
return this.finishNode(node, "ConditionalExpression")
76
}
77
return expr
78
}
79
80
lp.parseExprOps = function(noIn) {
81
let start = this.storeCurrentPos()
82
let indent = this.curIndent, line = this.curLineStart
83
return this.parseExprOp(this.parseMaybeUnary(noIn), start, -1, noIn, indent, line)
84
}
85
86
lp.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
87
if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) return left
88
let prec = this.tok.type.binop
89
if (prec != null && (!noIn || this.tok.type !== tt._in)) {
90
if (prec > minPrec) {
91
let node = this.startNodeAt(start)
92
node.left = left
93
node.operator = this.tok.value
94
this.next()
95
if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) {
96
node.right = this.dummyIdent()
97
} else {
98
let rightStart = this.storeCurrentPos()
99
node.right = this.parseExprOp(this.parseMaybeUnary(noIn), rightStart, prec, noIn, indent, line)
100
}
101
this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression")
102
return this.parseExprOp(node, start, minPrec, noIn, indent, line)
103
}
104
}
105
return left
106
}
107
108
lp.parseMaybeUnary = function(noIn) {
109
if (this.tok.type.prefix) {
110
let node = this.startNode(), update = this.tok.type === tt.incDec
111
node.operator = this.tok.value
112
node.prefix = true
113
this.next()
114
node.argument = this.parseMaybeUnary(noIn)
115
if (update) node.argument = this.checkLVal(node.argument)
116
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
117
} else if (this.tok.type === tt.ellipsis) {
118
let node = this.startNode()
119
this.next()
120
node.argument = this.parseMaybeUnary(noIn)
121
return this.finishNode(node, "SpreadElement")
122
}
123
let start = this.storeCurrentPos()
124
let expr = this.parseExprSubscripts()
125
while (this.tok.type.postfix && !this.canInsertSemicolon()) {
126
let node = this.startNodeAt(start)
127
node.operator = this.tok.value
128
node.prefix = false
129
node.argument = this.checkLVal(expr)
130
this.next()
131
expr = this.finishNode(node, "UpdateExpression")
132
}
133
return expr
134
}
135
136
lp.parseExprSubscripts = function() {
137
let start = this.storeCurrentPos()
138
return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart)
139
}
140
141
lp.parseSubscripts = function(base, start, noCalls, startIndent, line) {
142
for (;;) {
143
if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine()) {
144
if (this.tok.type == tt.dot && this.curIndent == startIndent)
145
--startIndent
146
else
147
return base
148
}
149
150
if (this.eat(tt.dot)) {
151
let node = this.startNodeAt(start)
152
node.object = base
153
if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine())
154
node.property = this.dummyIdent()
155
else
156
node.property = this.parsePropertyAccessor() || this.dummyIdent()
157
node.computed = false
158
base = this.finishNode(node, "MemberExpression")
159
} else if (this.tok.type == tt.bracketL) {
160
this.pushCx()
161
this.next()
162
let node = this.startNodeAt(start)
163
node.object = base
164
node.property = this.parseExpression()
165
node.computed = true
166
this.popCx()
167
this.expect(tt.bracketR)
168
base = this.finishNode(node, "MemberExpression")
169
} else if (!noCalls && this.tok.type == tt.parenL) {
170
let node = this.startNodeAt(start)
171
node.callee = base
172
node.arguments = this.parseExprList(tt.parenR)
173
base = this.finishNode(node, "CallExpression")
174
} else if (this.tok.type == tt.backQuote) {
175
let node = this.startNodeAt(start)
176
node.tag = base
177
node.quasi = this.parseTemplate()
178
base = this.finishNode(node, "TaggedTemplateExpression")
179
} else {
180
return base
181
}
182
}
183
}
184
185
lp.parseExprAtom = function() {
186
let node
187
switch (this.tok.type) {
188
case tt._this:
189
case tt._super:
190
let type = this.tok.type === tt._this ? "ThisExpression" : "Super"
191
node = this.startNode()
192
this.next()
193
return this.finishNode(node, type)
194
195
case tt.name:
196
let start = this.storeCurrentPos()
197
let id = this.parseIdent()
198
return this.eat(tt.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id]) : id
199
200
case tt.regexp:
201
node = this.startNode()
202
let val = this.tok.value
203
node.regex = {pattern: val.pattern, flags: val.flags}
204
node.value = val.value
205
node.raw = this.input.slice(this.tok.start, this.tok.end)
206
this.next()
207
return this.finishNode(node, "Literal")
208
209
case tt.num: case tt.string:
210
node = this.startNode()
211
node.value = this.tok.value
212
node.raw = this.input.slice(this.tok.start, this.tok.end)
213
this.next()
214
return this.finishNode(node, "Literal")
215
216
case tt._null: case tt._true: case tt._false:
217
node = this.startNode()
218
node.value = this.tok.type === tt._null ? null : this.tok.type === tt._true
219
node.raw = this.tok.type.keyword
220
this.next()
221
return this.finishNode(node, "Literal")
222
223
case tt.parenL:
224
let parenStart = this.storeCurrentPos()
225
this.next()
226
let inner = this.parseExpression()
227
this.expect(tt.parenR)
228
if (this.eat(tt.arrow)) {
229
return this.parseArrowExpression(this.startNodeAt(parenStart), inner.expressions || (isDummy(inner) ? [] : [inner]))
230
}
231
if (this.options.preserveParens) {
232
let par = this.startNodeAt(parenStart)
233
par.expression = inner
234
inner = this.finishNode(par, "ParenthesizedExpression")
235
}
236
return inner
237
238
case tt.bracketL:
239
node = this.startNode()
240
node.elements = this.parseExprList(tt.bracketR, true)
241
return this.finishNode(node, "ArrayExpression")
242
243
case tt.braceL:
244
return this.parseObj()
245
246
case tt._class:
247
return this.parseClass()
248
249
case tt._function:
250
node = this.startNode()
251
this.next()
252
return this.parseFunction(node, false)
253
254
case tt._new:
255
return this.parseNew()
256
257
case tt._yield:
258
node = this.startNode()
259
this.next()
260
if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type != tt.star && !this.tok.type.startsExpr)) {
261
node.delegate = false
262
node.argument = null
263
} else {
264
node.delegate = this.eat(tt.star)
265
node.argument = this.parseMaybeAssign()
266
}
267
return this.finishNode(node, "YieldExpression")
268
269
case tt.backQuote:
270
return this.parseTemplate()
271
272
default:
273
return this.dummyIdent()
274
}
275
}
276
277
lp.parseNew = function() {
278
let node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart
279
let meta = this.parseIdent(true)
280
if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) {
281
node.meta = meta
282
node.property = this.parseIdent(true)
283
return this.finishNode(node, "MetaProperty")
284
}
285
let start = this.storeCurrentPos()
286
node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line)
287
if (this.tok.type == tt.parenL) {
288
node.arguments = this.parseExprList(tt.parenR)
289
} else {
290
node.arguments = []
291
}
292
return this.finishNode(node, "NewExpression")
293
}
294
295
lp.parseTemplateElement = function() {
296
let elem = this.startNode()
297
elem.value = {
298
raw: this.input.slice(this.tok.start, this.tok.end),
299
cooked: this.tok.value
300
}
301
this.next()
302
elem.tail = this.tok.type === tt.backQuote
303
return this.finishNode(elem, "TemplateElement")
304
}
305
306
lp.parseTemplate = function() {
307
let node = this.startNode()
308
this.next()
309
node.expressions = []
310
let curElt = this.parseTemplateElement()
311
node.quasis = [curElt]
312
while (!curElt.tail) {
313
this.next()
314
node.expressions.push(this.parseExpression())
315
if (this.expect(tt.braceR)) {
316
curElt = this.parseTemplateElement()
317
} else {
318
curElt = this.startNode()
319
curElt.value = {cooked: '', raw: ''}
320
curElt.tail = true
321
}
322
node.quasis.push(curElt)
323
}
324
this.expect(tt.backQuote)
325
return this.finishNode(node, "TemplateLiteral")
326
}
327
328
lp.parseObj = function() {
329
let node = this.startNode()
330
node.properties = []
331
this.pushCx()
332
let indent = this.curIndent + 1, line = this.curLineStart
333
this.eat(tt.braceL)
334
if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
335
while (!this.closes(tt.braceR, indent, line)) {
336
let prop = this.startNode(), isGenerator, start
337
if (this.options.ecmaVersion >= 6) {
338
start = this.storeCurrentPos()
339
prop.method = false
340
prop.shorthand = false
341
isGenerator = this.eat(tt.star)
342
}
343
this.parsePropertyName(prop)
344
if (isDummy(prop.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue }
345
if (this.eat(tt.colon)) {
346
prop.kind = "init"
347
prop.value = this.parseMaybeAssign()
348
} else if (this.options.ecmaVersion >= 6 && (this.tok.type === tt.parenL || this.tok.type === tt.braceL)) {
349
prop.kind = "init"
350
prop.method = true
351
prop.value = this.parseMethod(isGenerator)
352
} else if (this.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
353
!prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
354
(this.tok.type != tt.comma && this.tok.type != tt.braceR)) {
355
prop.kind = prop.key.name
356
this.parsePropertyName(prop)
357
prop.value = this.parseMethod(false)
358
} else {
359
prop.kind = "init"
360
if (this.options.ecmaVersion >= 6) {
361
if (this.eat(tt.eq)) {
362
let assign = this.startNodeAt(start)
363
assign.operator = "="
364
assign.left = prop.key
365
assign.right = this.parseMaybeAssign()
366
prop.value = this.finishNode(assign, "AssignmentExpression")
367
} else {
368
prop.value = prop.key
369
}
370
} else {
371
prop.value = this.dummyIdent()
372
}
373
prop.shorthand = true
374
}
375
node.properties.push(this.finishNode(prop, "Property"))
376
this.eat(tt.comma)
377
}
378
this.popCx()
379
if (!this.eat(tt.braceR)) {
380
// If there is no closing brace, make the node span to the start
381
// of the next token (this is useful for Tern)
382
this.last.end = this.tok.start
383
if (this.options.locations) this.last.loc.end = this.tok.loc.start
384
}
385
return this.finishNode(node, "ObjectExpression")
386
}
387
388
lp.parsePropertyName = function(prop) {
389
if (this.options.ecmaVersion >= 6) {
390
if (this.eat(tt.bracketL)) {
391
prop.computed = true
392
prop.key = this.parseExpression()
393
this.expect(tt.bracketR)
394
return
395
} else {
396
prop.computed = false
397
}
398
}
399
let key = (this.tok.type === tt.num || this.tok.type === tt.string) ? this.parseExprAtom() : this.parseIdent()
400
prop.key = key || this.dummyIdent()
401
}
402
403
lp.parsePropertyAccessor = function() {
404
if (this.tok.type === tt.name || this.tok.type.keyword) return this.parseIdent()
405
}
406
407
lp.parseIdent = function() {
408
let name = this.tok.type === tt.name ? this.tok.value : this.tok.type.keyword
409
if (!name) return this.dummyIdent()
410
let node = this.startNode()
411
this.next()
412
node.name = name
413
return this.finishNode(node, "Identifier")
414
}
415
416
lp.initFunction = function(node) {
417
node.id = null
418
node.params = []
419
if (this.options.ecmaVersion >= 6) {
420
node.generator = false
421
node.expression = false
422
}
423
}
424
425
// Convert existing expression atom to assignable pattern
426
// if possible.
427
428
lp.toAssignable = function(node, binding) {
429
if (this.options.ecmaVersion >= 6 && node) {
430
switch (node.type) {
431
case "ObjectExpression":
432
node.type = "ObjectPattern"
433
let props = node.properties
434
for (let i = 0; i < props.length; i++)
435
this.toAssignable(props[i].value, binding)
436
break
437
438
case "ArrayExpression":
439
node.type = "ArrayPattern"
440
this.toAssignableList(node.elements, binding)
441
break
442
443
case "SpreadElement":
444
node.type = "RestElement"
445
node.argument = this.toAssignable(node.argument, binding)
446
break
447
448
case "AssignmentExpression":
449
node.type = "AssignmentPattern"
450
break
451
}
452
}
453
return this.checkLVal(node, binding)
454
}
455
456
lp.toAssignableList = function(exprList, binding) {
457
for (let i = 0; i < exprList.length; i++)
458
exprList[i] = this.toAssignable(exprList[i], binding)
459
return exprList
460
}
461
462
lp.parseFunctionParams = function(params) {
463
params = this.parseExprList(tt.parenR)
464
return this.toAssignableList(params, true)
465
}
466
467
lp.parseMethod = function(isGenerator) {
468
let node = this.startNode()
469
this.initFunction(node)
470
node.params = this.parseFunctionParams()
471
node.generator = isGenerator || false
472
node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tt.braceL
473
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
474
return this.finishNode(node, "FunctionExpression")
475
}
476
477
lp.parseArrowExpression = function(node, params) {
478
this.initFunction(node)
479
node.params = this.toAssignableList(params, true)
480
node.expression = this.tok.type !== tt.braceL
481
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
482
return this.finishNode(node, "ArrowFunctionExpression")
483
}
484
485
lp.parseExprList = function(close, allowEmpty) {
486
this.pushCx()
487
let indent = this.curIndent, line = this.curLineStart, elts = []
488
this.next(); // Opening bracket
489
while (!this.closes(close, indent + 1, line)) {
490
if (this.eat(tt.comma)) {
491
elts.push(allowEmpty ? null : this.dummyIdent())
492
continue
493
}
494
let elt = this.parseMaybeAssign()
495
if (isDummy(elt)) {
496
if (this.closes(close, indent, line)) break
497
this.next()
498
} else {
499
elts.push(elt)
500
}
501
this.eat(tt.comma)
502
}
503
this.popCx()
504
if (!this.eat(close)) {
505
// If there is no closing brace, make the node span to the start
506
// of the next token (this is useful for Tern)
507
this.last.end = this.tok.start
508
if (this.options.locations) this.last.loc.end = this.tok.loc.start
509
}
510
return elts
511
}
512
513