Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80762 views
1
// A recursive descent parser operates by defining functions for all
2
// syntactic elements, and recursively calling those, each function
3
// advancing the input stream and returning an AST node. Precedence
4
// of constructs (for example, the fact that `!x[1]` means `!(x[1])`
5
// instead of `(!x)[1]` is handled by the fact that the parser
6
// function that parses unary prefix operators is called first, and
7
// in turn calls the function that parses `[]` subscripts — that
8
// way, it'll receive the node for `x[1]` already parsed, and wraps
9
// *that* in the unary operator node.
10
//
11
// Acorn uses an [operator precedence parser][opp] to handle binary
12
// operator precedence, because it is much more compact than using
13
// the technique outlined above, which uses different, nesting
14
// functions to specify precedence, for all of the ten binary
15
// precedence levels that JavaScript defines.
16
//
17
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
18
19
import {types as tt} from "./tokentype"
20
import {Parser} from "./state"
21
import {reservedWords} from "./identifier"
22
import {has} from "./util"
23
24
const pp = Parser.prototype
25
26
// Check if property name clashes with already added.
27
// Object/class getters and setters are not allowed to clash —
28
// either with each other or with an init property — and in
29
// strict mode, init properties are also not allowed to be repeated.
30
31
pp.checkPropClash = function(prop, propHash) {
32
if (this.options.ecmaVersion >= 6) return
33
let key = prop.key, name
34
switch (key.type) {
35
case "Identifier": name = key.name; break
36
case "Literal": name = String(key.value); break
37
default: return
38
}
39
let kind = prop.kind || "init", other
40
if (has(propHash, name)) {
41
other = propHash[name]
42
let isGetSet = kind !== "init"
43
if ((this.strict || isGetSet) && other[kind] || !(isGetSet ^ other.init))
44
this.raise(key.start, "Redefinition of property")
45
} else {
46
other = propHash[name] = {
47
init: false,
48
get: false,
49
set: false
50
}
51
}
52
other[kind] = true
53
}
54
55
// ### Expression parsing
56
57
// These nest, from the most general expression type at the top to
58
// 'atomic', nondivisible expression types at the bottom. Most of
59
// the functions will simply let the function(s) below them parse,
60
// and, *if* the syntactic construct they handle is present, wrap
61
// the AST node that the inner parser gave them in another node.
62
63
// Parse a full expression. The optional arguments are used to
64
// forbid the `in` operator (in for loops initalization expressions)
65
// and provide reference for storing '=' operator inside shorthand
66
// property assignment in contexts where both object expression
67
// and object pattern might appear (so it's possible to raise
68
// delayed syntax error at correct position).
69
70
pp.parseExpression = function(noIn, refShorthandDefaultPos) {
71
let startPos = this.start, startLoc = this.startLoc
72
let expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos)
73
if (this.type === tt.comma) {
74
let node = this.startNodeAt(startPos, startLoc)
75
node.expressions = [expr]
76
while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos))
77
return this.finishNode(node, "SequenceExpression")
78
}
79
return expr
80
}
81
82
// Parse an assignment expression. This includes applications of
83
// operators like `+=`.
84
85
pp.parseMaybeAssign = function(noIn, refShorthandDefaultPos, afterLeftParse) {
86
if (this.type == tt._yield && this.inGenerator) return this.parseYield()
87
88
let failOnShorthandAssign
89
if (!refShorthandDefaultPos) {
90
refShorthandDefaultPos = {start: 0}
91
failOnShorthandAssign = true
92
} else {
93
failOnShorthandAssign = false
94
}
95
let startPos = this.start, startLoc = this.startLoc
96
if (this.type == tt.parenL || this.type == tt.name)
97
this.potentialArrowAt = this.start
98
let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos)
99
if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc)
100
if (this.type.isAssign) {
101
let node = this.startNodeAt(startPos, startLoc)
102
node.operator = this.value
103
node.left = this.type === tt.eq ? this.toAssignable(left) : left
104
refShorthandDefaultPos.start = 0 // reset because shorthand default was used correctly
105
this.checkLVal(left)
106
this.next()
107
node.right = this.parseMaybeAssign(noIn)
108
return this.finishNode(node, "AssignmentExpression")
109
} else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
110
this.unexpected(refShorthandDefaultPos.start)
111
}
112
return left
113
}
114
115
// Parse a ternary conditional (`?:`) operator.
116
117
pp.parseMaybeConditional = function(noIn, refShorthandDefaultPos) {
118
let startPos = this.start, startLoc = this.startLoc
119
let expr = this.parseExprOps(noIn, refShorthandDefaultPos)
120
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
121
if (this.eat(tt.question)) {
122
let node = this.startNodeAt(startPos, startLoc)
123
node.test = expr
124
node.consequent = this.parseMaybeAssign()
125
this.expect(tt.colon)
126
node.alternate = this.parseMaybeAssign(noIn)
127
return this.finishNode(node, "ConditionalExpression")
128
}
129
return expr
130
}
131
132
// Start the precedence parser.
133
134
pp.parseExprOps = function(noIn, refShorthandDefaultPos) {
135
let startPos = this.start, startLoc = this.startLoc
136
let expr = this.parseMaybeUnary(refShorthandDefaultPos)
137
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
138
return this.parseExprOp(expr, startPos, startLoc, -1, noIn)
139
}
140
141
// Parse binary operators with the operator precedence parsing
142
// algorithm. `left` is the left-hand side of the operator.
143
// `minPrec` provides context that allows the function to stop and
144
// defer further parser to one of its callers when it encounters an
145
// operator that has a lower precedence than the set it is parsing.
146
147
pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, noIn) {
148
let prec = this.type.binop
149
if (prec != null && (!noIn || this.type !== tt._in)) {
150
if (prec > minPrec) {
151
let node = this.startNodeAt(leftStartPos, leftStartLoc)
152
node.left = left
153
node.operator = this.value
154
let op = this.type
155
this.next()
156
let startPos = this.start, startLoc = this.startLoc
157
node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec, noIn)
158
this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression")
159
return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn)
160
}
161
}
162
return left
163
}
164
165
// Parse unary operators, both prefix and postfix.
166
167
pp.parseMaybeUnary = function(refShorthandDefaultPos) {
168
if (this.type.prefix) {
169
let node = this.startNode(), update = this.type === tt.incDec
170
node.operator = this.value
171
node.prefix = true
172
this.next()
173
node.argument = this.parseMaybeUnary()
174
if (refShorthandDefaultPos && refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
175
if (update) this.checkLVal(node.argument)
176
else if (this.strict && node.operator === "delete" &&
177
node.argument.type === "Identifier")
178
this.raise(node.start, "Deleting local variable in strict mode")
179
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
180
}
181
let startPos = this.start, startLoc = this.startLoc
182
let expr = this.parseExprSubscripts(refShorthandDefaultPos)
183
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
184
while (this.type.postfix && !this.canInsertSemicolon()) {
185
let node = this.startNodeAt(startPos, startLoc)
186
node.operator = this.value
187
node.prefix = false
188
node.argument = expr
189
this.checkLVal(expr)
190
this.next()
191
expr = this.finishNode(node, "UpdateExpression")
192
}
193
return expr
194
}
195
196
// Parse call, dot, and `[]`-subscript expressions.
197
198
pp.parseExprSubscripts = function(refShorthandDefaultPos) {
199
let startPos = this.start, startLoc = this.startLoc
200
let expr = this.parseExprAtom(refShorthandDefaultPos)
201
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr
202
return this.parseSubscripts(expr, startPos, startLoc)
203
}
204
205
pp.parseSubscripts = function(base, startPos, startLoc, noCalls) {
206
for (;;) {
207
if (this.eat(tt.dot)) {
208
let node = this.startNodeAt(startPos, startLoc)
209
node.object = base
210
node.property = this.parseIdent(true)
211
node.computed = false
212
base = this.finishNode(node, "MemberExpression")
213
} else if (this.eat(tt.bracketL)) {
214
let node = this.startNodeAt(startPos, startLoc)
215
node.object = base
216
node.property = this.parseExpression()
217
node.computed = true
218
this.expect(tt.bracketR)
219
base = this.finishNode(node, "MemberExpression")
220
} else if (!noCalls && this.eat(tt.parenL)) {
221
let node = this.startNodeAt(startPos, startLoc)
222
node.callee = base
223
node.arguments = this.parseExprList(tt.parenR, false)
224
base = this.finishNode(node, "CallExpression")
225
} else if (this.type === tt.backQuote) {
226
let node = this.startNodeAt(startPos, startLoc)
227
node.tag = base
228
node.quasi = this.parseTemplate()
229
base = this.finishNode(node, "TaggedTemplateExpression")
230
} else {
231
return base
232
}
233
}
234
}
235
236
// Parse an atomic expression — either a single token that is an
237
// expression, an expression started by a keyword like `function` or
238
// `new`, or an expression wrapped in punctuation like `()`, `[]`,
239
// or `{}`.
240
241
pp.parseExprAtom = function(refShorthandDefaultPos) {
242
let node, canBeArrow = this.potentialArrowAt == this.start
243
switch (this.type) {
244
case tt._this:
245
case tt._super:
246
let type = this.type === tt._this ? "ThisExpression" : "Super"
247
node = this.startNode()
248
this.next()
249
return this.finishNode(node, type)
250
251
case tt._yield:
252
if (this.inGenerator) this.unexpected()
253
254
case tt.name:
255
let startPos = this.start, startLoc = this.startLoc
256
let id = this.parseIdent(this.type !== tt.name)
257
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow))
258
return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id])
259
return id
260
261
case tt.regexp:
262
let value = this.value
263
node = this.parseLiteral(value.value)
264
node.regex = {pattern: value.pattern, flags: value.flags}
265
return node
266
267
case tt.num: case tt.string:
268
return this.parseLiteral(this.value)
269
270
case tt._null: case tt._true: case tt._false:
271
node = this.startNode()
272
node.value = this.type === tt._null ? null : this.type === tt._true
273
node.raw = this.type.keyword
274
this.next()
275
return this.finishNode(node, "Literal")
276
277
case tt.parenL:
278
return this.parseParenAndDistinguishExpression(canBeArrow)
279
280
case tt.bracketL:
281
node = this.startNode()
282
this.next()
283
// check whether this is array comprehension or regular array
284
if (this.options.ecmaVersion >= 7 && this.type === tt._for) {
285
return this.parseComprehension(node, false)
286
}
287
node.elements = this.parseExprList(tt.bracketR, true, true, refShorthandDefaultPos)
288
return this.finishNode(node, "ArrayExpression")
289
290
case tt.braceL:
291
return this.parseObj(false, refShorthandDefaultPos)
292
293
case tt._function:
294
node = this.startNode()
295
this.next()
296
return this.parseFunction(node, false)
297
298
case tt._class:
299
return this.parseClass(this.startNode(), false)
300
301
case tt._new:
302
return this.parseNew()
303
304
case tt.backQuote:
305
return this.parseTemplate()
306
307
default:
308
this.unexpected()
309
}
310
}
311
312
pp.parseLiteral = function(value) {
313
let node = this.startNode()
314
node.value = value
315
node.raw = this.input.slice(this.start, this.end)
316
this.next()
317
return this.finishNode(node, "Literal")
318
}
319
320
pp.parseParenExpression = function() {
321
this.expect(tt.parenL)
322
let val = this.parseExpression()
323
this.expect(tt.parenR)
324
return val
325
}
326
327
pp.parseParenAndDistinguishExpression = function(canBeArrow) {
328
let startPos = this.start, startLoc = this.startLoc, val
329
if (this.options.ecmaVersion >= 6) {
330
this.next()
331
332
if (this.options.ecmaVersion >= 7 && this.type === tt._for) {
333
return this.parseComprehension(this.startNodeAt(startPos, startLoc), true)
334
}
335
336
let innerStartPos = this.start, innerStartLoc = this.startLoc
337
let exprList = [], first = true
338
let refShorthandDefaultPos = {start: 0}, spreadStart, innerParenStart
339
while (this.type !== tt.parenR) {
340
first ? first = false : this.expect(tt.comma)
341
if (this.type === tt.ellipsis) {
342
spreadStart = this.start
343
exprList.push(this.parseParenItem(this.parseRest()))
344
break
345
} else {
346
if (this.type === tt.parenL && !innerParenStart) {
347
innerParenStart = this.start
348
}
349
exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem))
350
}
351
}
352
let innerEndPos = this.start, innerEndLoc = this.startLoc
353
this.expect(tt.parenR)
354
355
if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) {
356
if (innerParenStart) this.unexpected(innerParenStart)
357
return this.parseParenArrowList(startPos, startLoc, exprList)
358
}
359
360
if (!exprList.length) this.unexpected(this.lastTokStart)
361
if (spreadStart) this.unexpected(spreadStart)
362
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
363
364
if (exprList.length > 1) {
365
val = this.startNodeAt(innerStartPos, innerStartLoc)
366
val.expressions = exprList
367
this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc)
368
} else {
369
val = exprList[0]
370
}
371
} else {
372
val = this.parseParenExpression()
373
}
374
375
if (this.options.preserveParens) {
376
let par = this.startNodeAt(startPos, startLoc)
377
par.expression = val
378
return this.finishNode(par, "ParenthesizedExpression")
379
} else {
380
return val
381
}
382
}
383
384
pp.parseParenItem = function(item) {
385
return item
386
}
387
388
pp.parseParenArrowList = function(startPos, startLoc, exprList) {
389
return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList)
390
}
391
392
// New's precedence is slightly tricky. It must allow its argument
393
// to be a `[]` or dot subscript expression, but not a call — at
394
// least, not without wrapping it in parentheses. Thus, it uses the
395
396
const empty = []
397
398
pp.parseNew = function() {
399
let node = this.startNode()
400
let meta = this.parseIdent(true)
401
if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) {
402
node.meta = meta
403
node.property = this.parseIdent(true)
404
if (node.property.name !== "target")
405
this.raise(node.property.start, "The only valid meta property for new is new.target")
406
return this.finishNode(node, "MetaProperty")
407
}
408
let startPos = this.start, startLoc = this.startLoc
409
node.callee = this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true)
410
if (this.eat(tt.parenL)) node.arguments = this.parseExprList(tt.parenR, false)
411
else node.arguments = empty
412
return this.finishNode(node, "NewExpression")
413
}
414
415
// Parse template expression.
416
417
pp.parseTemplateElement = function() {
418
let elem = this.startNode()
419
elem.value = {
420
raw: this.input.slice(this.start, this.end),
421
cooked: this.value
422
}
423
this.next()
424
elem.tail = this.type === tt.backQuote
425
return this.finishNode(elem, "TemplateElement")
426
}
427
428
pp.parseTemplate = function() {
429
let node = this.startNode()
430
this.next()
431
node.expressions = []
432
let curElt = this.parseTemplateElement()
433
node.quasis = [curElt]
434
while (!curElt.tail) {
435
this.expect(tt.dollarBraceL)
436
node.expressions.push(this.parseExpression())
437
this.expect(tt.braceR)
438
node.quasis.push(curElt = this.parseTemplateElement())
439
}
440
this.next()
441
return this.finishNode(node, "TemplateLiteral")
442
}
443
444
// Parse an object literal or binding pattern.
445
446
pp.parseObj = function(isPattern, refShorthandDefaultPos) {
447
let node = this.startNode(), first = true, propHash = {}
448
node.properties = []
449
this.next()
450
while (!this.eat(tt.braceR)) {
451
if (!first) {
452
this.expect(tt.comma)
453
if (this.afterTrailingComma(tt.braceR)) break
454
} else first = false
455
456
let prop = this.startNode(), isGenerator, startPos, startLoc
457
if (this.options.ecmaVersion >= 6) {
458
prop.method = false
459
prop.shorthand = false
460
if (isPattern || refShorthandDefaultPos) {
461
startPos = this.start
462
startLoc = this.startLoc
463
}
464
if (!isPattern)
465
isGenerator = this.eat(tt.star)
466
}
467
this.parsePropertyName(prop)
468
this.parsePropertyValue(prop, isPattern, isGenerator, startPos, startLoc, refShorthandDefaultPos)
469
this.checkPropClash(prop, propHash)
470
node.properties.push(this.finishNode(prop, "Property"))
471
}
472
return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
473
}
474
475
pp.parsePropertyValue = function(prop, isPattern, isGenerator, startPos, startLoc, refShorthandDefaultPos) {
476
if (this.eat(tt.colon)) {
477
prop.value = isPattern ? this.parseMaybeDefault(this.start, this.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos)
478
prop.kind = "init"
479
} else if (this.options.ecmaVersion >= 6 && this.type === tt.parenL) {
480
if (isPattern) this.unexpected()
481
prop.kind = "init"
482
prop.method = true
483
prop.value = this.parseMethod(isGenerator)
484
} else if (this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
485
(prop.key.name === "get" || prop.key.name === "set") &&
486
(this.type != tt.comma && this.type != tt.braceR)) {
487
if (isGenerator || isPattern) this.unexpected()
488
prop.kind = prop.key.name
489
this.parsePropertyName(prop)
490
prop.value = this.parseMethod(false)
491
} else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
492
prop.kind = "init"
493
if (isPattern) {
494
if (this.isKeyword(prop.key.name) ||
495
(this.strict && (reservedWords.strictBind(prop.key.name) || reservedWords.strict(prop.key.name))) ||
496
(!this.options.allowReserved && this.isReservedWord(prop.key.name)))
497
this.raise(prop.key.start, "Binding " + prop.key.name)
498
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key)
499
} else if (this.type === tt.eq && refShorthandDefaultPos) {
500
if (!refShorthandDefaultPos.start)
501
refShorthandDefaultPos.start = this.start
502
prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key)
503
} else {
504
prop.value = prop.key
505
}
506
prop.shorthand = true
507
} else this.unexpected()
508
}
509
510
pp.parsePropertyName = function(prop) {
511
if (this.options.ecmaVersion >= 6) {
512
if (this.eat(tt.bracketL)) {
513
prop.computed = true
514
prop.key = this.parseMaybeAssign()
515
this.expect(tt.bracketR)
516
return prop.key
517
} else {
518
prop.computed = false
519
}
520
}
521
return prop.key = (this.type === tt.num || this.type === tt.string) ? this.parseExprAtom() : this.parseIdent(true)
522
}
523
524
// Initialize empty function node.
525
526
pp.initFunction = function(node) {
527
node.id = null
528
if (this.options.ecmaVersion >= 6) {
529
node.generator = false
530
node.expression = false
531
}
532
}
533
534
// Parse object or class method.
535
536
pp.parseMethod = function(isGenerator) {
537
let node = this.startNode()
538
this.initFunction(node)
539
this.expect(tt.parenL)
540
node.params = this.parseBindingList(tt.parenR, false, false)
541
let allowExpressionBody
542
if (this.options.ecmaVersion >= 6) {
543
node.generator = isGenerator
544
allowExpressionBody = true
545
} else {
546
allowExpressionBody = false
547
}
548
this.parseFunctionBody(node, allowExpressionBody)
549
return this.finishNode(node, "FunctionExpression")
550
}
551
552
// Parse arrow function expression with given parameters.
553
554
pp.parseArrowExpression = function(node, params) {
555
this.initFunction(node)
556
node.params = this.toAssignableList(params, true)
557
this.parseFunctionBody(node, true)
558
return this.finishNode(node, "ArrowFunctionExpression")
559
}
560
561
// Parse function body and check parameters.
562
563
pp.parseFunctionBody = function(node, allowExpression) {
564
let isExpression = allowExpression && this.type !== tt.braceL
565
566
if (isExpression) {
567
node.body = this.parseMaybeAssign()
568
node.expression = true
569
} else {
570
// Start a new scope with regard to labels and the `inFunction`
571
// flag (restore them to their old value afterwards).
572
let oldInFunc = this.inFunction, oldInGen = this.inGenerator, oldLabels = this.labels
573
this.inFunction = true; this.inGenerator = node.generator; this.labels = []
574
node.body = this.parseBlock(true)
575
node.expression = false
576
this.inFunction = oldInFunc; this.inGenerator = oldInGen; this.labels = oldLabels
577
}
578
579
// If this is a strict mode function, verify that argument names
580
// are not repeated, and it does not try to bind the words `eval`
581
// or `arguments`.
582
if (this.strict || !isExpression && node.body.body.length && this.isUseStrict(node.body.body[0])) {
583
let nameHash = {}, oldStrict = this.strict
584
this.strict = true
585
if (node.id)
586
this.checkLVal(node.id, true)
587
for (let i = 0; i < node.params.length; i++)
588
this.checkLVal(node.params[i], true, nameHash)
589
this.strict = oldStrict
590
}
591
}
592
593
// Parses a comma-separated list of expressions, and returns them as
594
// an array. `close` is the token type that ends the list, and
595
// `allowEmpty` can be turned on to allow subsequent commas with
596
// nothing in between them to be parsed as `null` (which is needed
597
// for array literals).
598
599
pp.parseExprList = function(close, allowTrailingComma, allowEmpty, refShorthandDefaultPos) {
600
let elts = [], first = true
601
while (!this.eat(close)) {
602
if (!first) {
603
this.expect(tt.comma)
604
if (allowTrailingComma && this.afterTrailingComma(close)) break
605
} else first = false
606
607
if (allowEmpty && this.type === tt.comma) {
608
elts.push(null)
609
} else {
610
if (this.type === tt.ellipsis)
611
elts.push(this.parseSpread(refShorthandDefaultPos))
612
else
613
elts.push(this.parseMaybeAssign(false, refShorthandDefaultPos))
614
}
615
}
616
return elts
617
}
618
619
// Parse the next token as an identifier. If `liberal` is true (used
620
// when parsing properties), it will also convert keywords into
621
// identifiers.
622
623
pp.parseIdent = function(liberal) {
624
let node = this.startNode()
625
if (liberal && this.options.allowReserved == "never") liberal = false
626
if (this.type === tt.name) {
627
if (!liberal &&
628
((!this.options.allowReserved && this.isReservedWord(this.value)) ||
629
(this.strict && reservedWords.strict(this.value)) &&
630
(this.options.ecmaVersion >= 6 ||
631
this.input.slice(this.start, this.end).indexOf("\\") == -1)))
632
this.raise(this.start, "The keyword '" + this.value + "' is reserved")
633
node.name = this.value
634
} else if (liberal && this.type.keyword) {
635
node.name = this.type.keyword
636
} else {
637
this.unexpected()
638
}
639
this.next()
640
return this.finishNode(node, "Identifier")
641
}
642
643
// Parses yield expression inside generator.
644
645
pp.parseYield = function() {
646
let node = this.startNode()
647
this.next()
648
if (this.type == tt.semi || this.canInsertSemicolon() || (this.type != tt.star && !this.type.startsExpr)) {
649
node.delegate = false
650
node.argument = null
651
} else {
652
node.delegate = this.eat(tt.star)
653
node.argument = this.parseMaybeAssign()
654
}
655
return this.finishNode(node, "YieldExpression")
656
}
657
658
// Parses array and generator comprehensions.
659
660
pp.parseComprehension = function(node, isGenerator) {
661
node.blocks = []
662
while (this.type === tt._for) {
663
let block = this.startNode()
664
this.next()
665
this.expect(tt.parenL)
666
block.left = this.parseBindingAtom()
667
this.checkLVal(block.left, true)
668
this.expectContextual("of")
669
block.right = this.parseExpression()
670
this.expect(tt.parenR)
671
node.blocks.push(this.finishNode(block, "ComprehensionBlock"))
672
}
673
node.filter = this.eat(tt._if) ? this.parseParenExpression() : null
674
node.body = this.parseExpression()
675
this.expect(isGenerator ? tt.parenR : tt.bracketR)
676
node.generator = isGenerator
677
return this.finishNode(node, "ComprehensionExpression")
678
}
679
680