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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
//! The operator ladder of §13.4 – §13.16, from `Expression` down to `UnaryExpression`.
//!
//! Every function here is on the path a bracket recurses through, so every one of them costs
//! nesting depth — which is why `parse_assignment` also handles the conditional operator, why
//! `parse_unary` also handles update expressions, and why the operator tables are next door in
//! [`super::operator`] rather than being a cascade of one function per grammar layer. See
//! [`super::MAX_NESTING_DEPTH`] and DR-0006.
//!
//! The layers below this one are [`super::member`] (§13.3) and [`super::primary`] (§13.2).
use super::operator::{
OperatorKind, assignment_operator, binary_operator, combine, is_simple_assignment_target,
unary_operator, update_operator,
};
use super::{ParseError, ParseErrorKind, Parser};
use crate::ast::{BinaryOperator, Expr, ExprKind, UpdateOperator};
use crate::lexer::{Goal, ReservedWord, TokenKind};
use crate::span::Span;
/// Whether `in` is a relational operator here — the `[In]` grammar parameter of §13.
///
/// It exists for one construct. `for (a in b; ; )` and `for (a in b)` begin identically, and the
/// second is a `for`-`in` loop, so the head of a `for` has to be read with `in` unavailable or
/// the two could never be told apart. §13.10 gates both `in` alternatives of
/// `RelationalExpression` on `[+In]`, and every other production either propagates the parameter
/// or resets it.
///
/// Resetting is most of the rule and is the half that is easy to get wrong: `for (a[b in c];;)`,
/// `for (f(a in b);;)` and `for ((a in b);;)` all parse, because a bracket starts a fresh
/// `Expression[+In]`. So does the *middle* arm of a conditional — `for (a ? b in c : d;;)` is
/// legal — while the last arm propagates. Passing this as a parameter rather than keeping it on
/// the parser is what makes every one of those a decision the compiler insists on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum AllowIn {
/// `[+In]` — the ordinary case, everywhere but the head of a `for`.
Yes,
/// `[~In]`, which only the head of a `for` asks for.
No,
}
impl Parser<'_> {
/// `Expression` (§13.16) — one or more `AssignmentExpression`s separated by commas.
pub(super) fn parse_expression(&mut self, allow_in: AllowIn) -> Result<Expr, ParseError> {
let first = self.parse_assignment(allow_in)?;
if self.current.kind != TokenKind::Comma {
return Ok(first);
}
let mut parts = vec![first];
while self.current.kind == TokenKind::Comma {
self.advance(Goal::RegExp)?;
parts.push(self.parse_assignment(allow_in)?);
}
let span = match (parts.first(), parts.last()) {
(Some(first), Some(last)) => first.span.to(last.span),
// `parts` was built from one element and only ever grows, so this cannot happen;
// an empty span is the answer that costs nothing if it somehow does.
_ => Span::empty_at(self.current.span.start),
};
Ok(Expr::new(
ExprKind::Sequence(parts.into_boxed_slice()),
span,
))
}
/// `AssignmentExpression` (§13.15) and `ConditionalExpression` (§13.14), in one frame.
///
/// Merged deliberately. They are separate productions, but a `?` and an `=` cannot both
/// follow the same operand — a conditional is not a `LeftHandSideExpression`, so it can only
/// be assigned to in the sense that saying so is an error — and one function fewer on the
/// recursion path is depth that [`MAX_NESTING_DEPTH`] does not have to give up.
///
/// Falling through to the assignment check rather than returning early after a conditional
/// is what makes `(a ? b : c) = d` say "this expression cannot be assigned to" instead of
/// the far less helpful "expected end of input".
/// `RelationalExpression : PrivateIdentifier in ShiftExpression` (§13.10), with the cursor on
/// the name.
///
/// Entered from the top of [`Parser::parse_binary`], which is where every operand position
/// passes through — the left operand here is not an expression, so there is nothing for
/// `parse_unary` to have read and the alternative has to be taken instead of it rather than
/// inside it. The `in` is required, a private name alone being not anything, and `[~In]`
/// refuses it as it refuses any other `in`.
pub(super) fn parse_private_in(&mut self, allow_in: AllowIn) -> Result<Expr, ParseError> {
let token = self.advance(Goal::Div)?;
let name = self.private_name(token)?;
if allow_in == AllowIn::No || self.current.kind != TokenKind::Keyword(ReservedWord::In) {
return Err(self.unexpected("`in`"));
}
self.advance(Goal::RegExp)?;
self.enter()?;
// A `ShiftExpression`, so `#a in b + c` is `#a in (b + c)` and `#a in b == c` is
// `(#a in b) == c` — the ordinary precedence of `in`, reached from the other side.
let object = self.parse_binary(RELATIONAL_PRECEDENCE, allow_in, None);
self.leave();
let object = object?;
let span = token.span.to(object.span);
Ok(Expr::new(
ExprKind::PrivateIn {
name,
object: Box::new(object),
},
span,
))
}
pub(super) fn parse_assignment(&mut self, allow_in: AllowIn) -> Result<Expr, ParseError> {
// §15.3: an `ArrowFunction` is an `AssignmentExpression` and nothing tighter, so this
// is the only level that may produce one — see [`super::arrow`]. What comes back may
// also be a parenthesized expression the attempt had to read to find out, in which case
// it becomes the head of the ordinary operand path rather than being read twice.
// §15.5: a `YieldExpression` is an `AssignmentExpression` and nothing tighter, so this
// level produces it as it does an arrow — and for the same reason. `1 + yield` and
// `yield ? a : b` have no derivation because both operators want a narrower operand.
if self.yield_allowed && self.current.kind == TokenKind::Keyword(ReservedWord::Yield) {
return self.parse_yield(allow_in);
}
let head = match self.parse_arrow_or_group(allow_in)? {
super::arrow::ArrowOrGroup::Arrow(arrow) => return Ok(arrow),
super::arrow::ArrowOrGroup::Operand(expr) => Some(expr),
super::arrow::ArrowOrGroup::Neither => None,
};
let left = self.parse_binary(0, allow_in, head)?;
self.parse_conditional_and_assignment(left, allow_in)
}
/// `? :` and then `=`, with the `ShortCircuitExpression` already read.
///
/// Apart from [`Parser::parse_assignment`] for the reason `parse_binary_tail` is apart from
/// `parse_binary`: §13.10's private-name form arrives here with its left operand already
/// built, and the two tails after it are the same two.
fn parse_conditional_and_assignment(
&mut self,
mut left: Expr,
allow_in: AllowIn,
) -> Result<Expr, ParseError> {
if self.current.kind == TokenKind::Question {
left = self.parse_conditional_tail(left, allow_in)?;
}
let Some(operator) = assignment_operator(self.current.kind) else {
// Nothing will refine this now, so a `{a = 1}` inside it is the Syntax Error
// §13.2.5.1 says it is — unless a literal is still open around it, in which case the
// literal may yet become a pattern and nothing is settled. See
// [`Parser::unrefined_covers`].
self.report_unrefined_cover_grammar()?;
return Ok(left);
};
// §13.15.1's carve-out: an `ObjectLiteral` or `ArrayLiteral` on the left of `=` is not a
// target at all but a pattern that was covered by one, and gets refined instead of
// checked. Only `=` — the compound operators take a `LeftHandSideExpression`, so
// `[a] += b` has no derivation.
let target = if operator == crate::ast::AssignmentOperator::Assign
&& Self::covers_a_pattern(&left)
{
crate::ast::AssignmentTarget::Pattern(self.refine_to_pattern(left)?)
} else {
// §13.15.1. The check is here rather than in the AST because it is a *syntax* error:
// `1 = 2` must be refused before anything runs, not when it runs.
if !is_simple_assignment_target(&left) {
return Err(ParseError {
kind: ParseErrorKind::InvalidAssignmentTarget,
span: left.span,
});
}
// §8.6.4: in strict code the `AssignmentTargetType` of `eval` and `arguments` is
// *invalid*, so assigning to either is refused where reading it is not.
if let ExprKind::Identifier(name) = &left.kind {
self.check_strict_name(name, left.span, true)?;
}
self.report_unrefined_cover_grammar()?;
crate::ast::AssignmentTarget::Simple(left)
};
let target = Box::new(target);
self.advance(Goal::RegExp)?;
self.enter()?;
// `LeftHandSideExpression = AssignmentExpression` — the recursion is on the right, so
// `a = b = c` is `a = (b = c)`.
//
// The right operand survives a refinement of anything around it: refining `[a = <rhs>]`
// makes `<rhs>` the element's initializer, still an expression. So a rule recorded in
// here is not that refinement's to discard.
let value = self.protecting(|parser| parser.parse_assignment(allow_in));
self.leave();
let value = value?;
let span = target.span().to(value.span);
Ok(Expr::new(
ExprKind::Assignment {
operator,
target,
value: Box::new(value),
},
span,
))
}
/// `? AssignmentExpression : AssignmentExpression` (§13.14), with `test` already parsed.
///
/// Both arms are `AssignmentExpression`s, which §13.14's own Note explains: it lets an
/// assignment be governed by either arm, and keeps a comma expression out of the middle.
/// So `a ? b = 1 : c = 2` is two assignments, and `a ? b, c : d` is an error.
fn parse_conditional_tail(
&mut self,
test: Expr,
allow_in: AllowIn,
) -> Result<Expr, ParseError> {
self.advance(Goal::RegExp)?;
self.enter()?;
let arms = self.parse_conditional_arms(allow_in);
self.leave();
let (consequent, alternate) = arms?;
let span = test.span.to(alternate.span);
Ok(Expr::new(
ExprKind::Conditional {
test: Box::new(test),
consequent: Box::new(consequent),
alternate: Box::new(alternate),
},
span,
))
}
/// The two arms of a conditional, split out so their locals do not sit in a frame that the
/// bracket recursion also passes through.
fn parse_conditional_arms(&mut self, allow_in: AllowIn) -> Result<(Expr, Expr), ParseError> {
// `? AssignmentExpression[+In] : AssignmentExpression[?In]` — the middle arm resets
// the parameter and the last one propagates it, so `for (a ? b in c : d;;)` parses
// and an `in` in the last arm still would not.
let consequent = self.parse_assignment(AllowIn::Yes)?;
self.eat(TokenKind::Colon, Goal::RegExp, "`:`")?;
let alternate = self.parse_assignment(allow_in)?;
Ok((consequent, alternate))
}
/// The operator layers of §13.6 – §13.13, by precedence climbing.
///
/// `minimum` is the weakest binding power this call will accept; an operator weaker than it
/// belongs to the caller. Left-associative operators recurse one level tighter so the next
/// one of equal precedence is left for the loop, and `**` recurses at its own level so the
/// next one is taken by the recursion instead — which is the whole of associativity.
fn parse_binary(
&mut self,
minimum: u8,
allow_in: AllowIn,
head: Option<Expr>,
) -> Result<Expr, ParseError> {
// A head that is already read is the parenthesized group the assignment level had to open
// to see whether a `=>` followed it. Passed down rather than kept on the parser, so the
// compiler is what makes sure it reaches exactly one place — and measurably free, the
// restructuring below being what the depth went on.
// §13.10: `RelationalExpression : PrivateIdentifier in ShiftExpression`. A private name
// is not an operand — no production makes one — so `parse_unary` has nothing to read and
// this alternative has to be taken before it rather than through it.
//
// `minimum < RELATIONAL_PRECEDENCE` is the whole of where it may stand, and it is the
// grammar rather than a list: this alternative *is* a `RelationalExpression`, so it fits
// exactly where one fits. An operator whose right operand is a `RelationalExpression` or
// looser admits it — `x && #a in v`, `x == #a in v`, `x | #a in v`, `x, #a in v` — and
// one whose right operand is a `ShiftExpression` or tighter does not, which is why
// `x < #a in v` and `x + #a in v` have no derivation. Every entry from the assignment
// level arrives with `minimum` at zero, so nothing there needs a case of its own.
if head.is_none()
&& minimum < RELATIONAL_PRECEDENCE
&& matches!(self.current.kind, TokenKind::PrivateIdentifier { .. })
{
let relational = self.parse_private_in(allow_in)?;
return self.parse_binary_tail(relational, minimum, allow_in);
}
let left = self.parse_unary(head)?;
self.parse_binary_tail(left, minimum, allow_in)
}
/// The operator ladder itself, with its left operand already read.
///
/// Apart from [`Parser::parse_binary`] because §13.10's `PrivateIdentifier in
/// ShiftExpression` produces a `RelationalExpression` without going through the operand path
/// at all — a private name is not an operand, so there is nothing there for `parse_unary` to
/// have read, and everything looser than a relational still applies to what comes back.
fn parse_binary_tail(
&mut self,
mut left: Expr,
minimum: u8,
allow_in: AllowIn,
) -> Result<Expr, ParseError> {
while let Some(operator) = binary_operator(self.current.kind) {
if operator.precedence < minimum {
break;
}
// §13.10 gates the `in` alternatives of `RelationalExpression` on `[+In]`. Under
// `[~In]` the word is not an operator at all, so the expression simply ends and
// whatever wanted `in` next is welcome to it — in a `for` head, the `for`-`in`
// production. Nothing below here takes the parameter because nothing below
// `RelationalExpression` has one: a `ShiftExpression` cannot contain a bare `in`.
if allow_in == AllowIn::No && operator.kind == OperatorKind::Binary(BinaryOperator::In)
{
break;
}
// §13.6: the left operand of `**` is an `UpdateExpression`, and neither a prefix
// unary nor an `AwaitExpression` is one — §15.8 makes the latter a
// `UnaryExpression` alongside the former, so `await a ** b` needs the same
// parentheses `-a ** b` does. Checked before the operator is consumed so the error
// can point at the operand that is wrong rather than at the operator that noticed.
if operator.kind == OperatorKind::Binary(BinaryOperator::Exponent)
&& matches!(left.kind, ExprKind::Unary { .. } | ExprKind::Await(_))
&& !left.parenthesized
{
return Err(ParseError {
kind: ParseErrorKind::ExponentiationOnUnary,
span: left.span,
});
}
// An operator is followed by an operand, so the goal is `RegExp`: in `a / /b/`, the
// first slash divides and the second opens a literal.
self.advance(Goal::RegExp)?;
let tighter = if operator.right_associative {
operator.precedence
} else {
operator.precedence + 1
};
self.enter()?;
let right = self.parse_binary(tighter, allow_in, None);
self.leave();
let right = right?;
left = combine(left, operator, right)?;
}
Ok(left)
}
/// `UnaryExpression` (§13.5) and `UpdateExpression` (§13.4), in one frame.
///
/// Merged for the same reason the conditional lives inside `parse_assignment`: both are on
/// the path a bracket recurses through, and a frame there costs nesting depth (DR-0006).
pub(super) fn parse_unary(&mut self, head: Option<Expr>) -> Result<Expr, ParseError> {
// §15.8: an `AwaitExpression` is a `UnaryExpression`, so it belongs at this level and
// not at the assignment one a `YieldExpression` is produced at. That is the whole of
// why `await a ? b : c` parses and `yield a ? b : c` does not.
if head.is_none()
&& self.await_allowed
&& self.current.kind == TokenKind::Keyword(ReservedWord::Await)
{
return self.parse_await();
}
// A head that is already read is a parenthesized group, which is a `PrimaryExpression`.
// No prefix operator can apply to it — one would have been read before the `(` — but a
// postfix `++` still can, so it joins the path below rather than skipping it.
if head.is_none()
&& let Some(operator) = unary_operator(self.current.kind)
{
let token = self.advance(Goal::RegExp)?;
self.enter()?;
// `- UnaryExpression`, so the operators stack: `- - a` is two of them.
let argument = self.parse_unary(None);
self.leave();
let argument = argument?;
// §13.5.1: strict code may not `delete` a bare name. `delete a.b` removes a property
// and `delete a` asks to remove a binding, which strict code has no way to express —
// and parentheses do not help, `(a)` being the same identifier.
if operator == crate::ast::UnaryOperator::Delete
&& self.strict
&& matches!(argument.kind, ExprKind::Identifier(_))
{
return Err(ParseError {
kind: ParseErrorKind::StrictDeleteOfName,
span: argument.span,
});
}
// §13.5.1: a private name is not a property key, so there is no property for
// `delete` to remove — and unlike the rule above this one holds in sloppy code too.
if operator == crate::ast::UnaryOperator::Delete && deletes_a_private_member(&argument)
{
return Err(ParseError {
kind: ParseErrorKind::DeleteOfPrivateMember,
span: argument.span,
});
}
let span = token.span.to(argument.span);
return Ok(Expr::new(
ExprKind::Unary {
operator,
argument: Box::new(argument),
},
span,
));
}
if head.is_none()
&& let Some(operator) = update_operator(self.current.kind)
{
let token = self.advance(Goal::RegExp)?;
self.enter()?;
// `++ UnaryExpression` — the operand is a unary expression, not a
// LeftHandSideExpression, so `++ typeof a` parses and is then rejected below for
// having nothing to increment.
let argument = self.parse_unary(None);
self.leave();
let argument = argument?;
return self.update(operator, true, token.span, argument);
}
// `UpdateExpression : LeftHandSideExpression [no LineTerminator here] ++`
let operand = self.parse_member(true, head)?;
let Some(operator) = update_operator(self.current.kind) else {
return Ok(operand);
};
// The first restricted production this parser meets, and the reason every token has
// carried a `newline_before` flag since the lexer's very first slice. `a\n++b` is not
// `a++ b`: the postfix form simply does not match across a line break, which leaves `a`
// and `++b` as two things — and once statements exist, as two statements.
if self.current.newline_before {
return Ok(operand);
}
let token = self.advance(Goal::Div)?;
self.update(operator, false, token.span, operand)
}
/// Build an update node, enforcing §13.4.1.
///
/// `span` is the operator's; the node covers both it and the operand, whichever came first.
fn update(
&self,
operator: UpdateOperator,
prefix: bool,
operator_span: Span,
argument: Expr,
) -> Result<Expr, ParseError> {
// §13.4.1: a Syntax Error if the AssignmentTargetType is invalid. `1++` is refused for
// the same reason `1 = 2` is, and by the same test.
if !is_simple_assignment_target(&argument) {
return Err(ParseError {
kind: ParseErrorKind::InvalidAssignmentTarget,
span: argument.span,
});
}
// §8.6.4 gives `eval` and `arguments` an AssignmentTargetType of *invalid* in strict code,
// and §13.4's four productions each say "if AssignmentTargetType is not simple" — the same
// sentence §13.15.1 says of an assignment. So `"use strict"; arguments--` is refused
// exactly as `"use strict"; arguments = 1` already was.
//
// This takes `&self` for that check alone. Written as an associated function it could not
// ask whether the code is strict, which is why the rule was applied to one of the two
// productions that share it and not the other.
if let ExprKind::Identifier(name) = &argument.kind {
self.check_strict_name(name, argument.span, true)?;
}
let span = operator_span.to(argument.span);
Ok(Expr::new(
ExprKind::Update {
operator,
prefix,
argument: Box::new(argument),
},
span,
))
}
}
/// The precedence a `ShiftExpression` is parsed at, which is what `#a in b` takes on the
/// right — one tighter than the `in` in [`super::operator::binary_operator`]'s table, so the
/// operand stops exactly where an ordinary `in`'s would.
const RELATIONAL_PRECEDENCE: u8 = 9;
/// Whether `delete`'s operand reaches a private member (§13.5.1).
///
/// Only through the chain's own links: `delete (a.#b)` is the same expression parenthesized and is
/// refused too, while `delete a[b].c` is not one of these however `a` got its properties.
fn deletes_a_private_member(argument: &Expr) -> bool {
match &argument.kind {
ExprKind::Member { private, .. } => *private,
ExprKind::OptionalChain(chain) => deletes_a_private_member(chain),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::AssignmentOperator;
use crate::ast::AssignmentTarget;
use crate::parser::test_support::*;
use crate::parser::{parse_expression, parse_script};
#[test]
fn every_prefix_operator_the_grammar_has_today() {
// §13.5. `await` is absent: it is the `[+Await]` alternative and needs a parameter that
// arrives with async functions.
assert_eq!(shape("-a"), "(- a)");
assert_eq!(shape("+a"), "(+ a)");
assert_eq!(shape("!a"), "(! a)");
assert_eq!(shape("~a"), "(~ a)");
assert_eq!(shape("typeof a"), "(typeof a)");
assert_eq!(shape("void a"), "(void a)");
assert_eq!(shape("delete a"), "(delete a)");
// `- UnaryExpression`, so they stack — and `--` would be one token, which is why the
// spaced form is the one that means two negations.
assert_eq!(shape("- - a"), "(- (- a))");
assert_eq!(shape("!!a"), "(! (! a))");
assert_eq!(shape("typeof typeof a"), "(typeof (typeof a))");
// A prefix operator binds tighter than any binary one.
assert_eq!(shape("-a + b"), "(+ (- a) b)");
assert_eq!(shape("-a * b"), "(* (- a) b)");
assert_eq!(shape("typeof a === b"), "(=== (typeof a) b)");
// The span runs from the operator to the end of its operand.
assert_eq!(parse("- a").span, Span::new(0, 3));
}
#[test]
fn the_precedence_ladder_is_the_grammars_nesting_read_as_numbers() {
// Each pair is two adjacent layers of §13.6 – §13.13, checked in both orders so that a
// table entry cannot be right by accident of which side it was written on.
// (`??` against `||` is absent on purpose: §13.13 forbids that pair outright, and it
// has its own test.)
for (source, shaped) in [
("a || b && c", "(|| a (&& b c))"),
("a && b || c", "(|| (&& a b) c)"),
("a && b | c", "(&& a (| b c))"),
("a | b && c", "(&& (| a b) c)"),
("a | b ^ c", "(| a (^ b c))"),
("a ^ b | c", "(| (^ a b) c)"),
("a ^ b & c", "(^ a (& b c))"),
("a & b ^ c", "(^ (& a b) c)"),
("a & b == c", "(& a (== b c))"),
("a == b & c", "(& (== a b) c)"),
("a == b < c", "(== a (< b c))"),
("a < b == c", "(== (< a b) c)"),
("a < b << c", "(< a (<< b c))"),
("a << b < c", "(< (<< a b) c)"),
("a << b + c", "(<< a (+ b c))"),
("a + b << c", "(<< (+ a b) c)"),
("a + b * c", "(+ a (* b c))"),
("a * b + c", "(+ (* a b) c)"),
("a * b ** c", "(* a (** b c))"),
("a ** b * c", "(* (** a b) c)"),
] {
assert_eq!(shape(source), shaped, "parsing {source:?}");
}
// The relational layer holds the two word-shaped operators as well as the symbols.
assert_eq!(shape("a instanceof b == c"), "(== (instanceof a b) c)");
assert_eq!(shape("a in b == c"), "(== (in a b) c)");
assert_eq!(shape("a + b instanceof c"), "(instanceof (+ a b) c)");
// Every remaining operator, so no table entry goes unexercised.
for (source, shaped) in [
("a - b", "(- a b)"),
("a / b", "(/ a b)"),
("a % b", "(% a b)"),
("a >> b", "(>> a b)"),
("a >>> b", "(>>> a b)"),
("a > b", "(> a b)"),
("a <= b", "(<= a b)"),
("a >= b", "(>= a b)"),
("a != b", "(!= a b)"),
("a === b", "(=== a b)"),
("a !== b", "(!== a b)"),
] {
assert_eq!(shape(source), shaped, "parsing {source:?}");
}
// Parentheses override all of it, which is the only reason precedence is bearable.
assert_eq!(shape("(a + b) * c"), "(* (+ a b) c)");
}
#[test]
fn everything_groups_to_the_left_except_exponentiation() {
// `AdditiveExpression : AdditiveExpression + MultiplicativeExpression` — the recursion is
// on the left, so equal precedence groups left.
assert_eq!(shape("a - b - c"), "(- (- a b) c)");
assert_eq!(shape("a / b / c"), "(/ (/ a b) c)");
assert_eq!(shape("a < b < c"), "(< (< a b) c)");
assert_eq!(shape("a && b && c"), "(&& (&& a b) c)");
assert_eq!(shape("a ?? b ?? c"), "(?? (?? a b) c)");
// `ExponentiationExpression : UpdateExpression ** ExponentiationExpression` — the
// recursion is on the *right*, so `2 ** 3 ** 2` is 512 and not 64.
assert_eq!(shape("a ** b ** c"), "(** a (** b c))");
assert_eq!(shape("a ** b ** c ** d"), "(** a (** b (** c d)))");
// A left-associative chain is a loop rather than a recursion, so its length is bounded by
// memory rather than by MAX_NESTING_DEPTH.
let long = vec!["1"; 5000].join(" + ");
assert!(parse_expression(&long).is_ok());
}
#[test]
fn a_prefix_unary_may_not_be_the_left_operand_of_exponentiation() {
// §13.6: `ExponentiationExpression : UpdateExpression ** ExponentiationExpression`. A
// prefix unary is not an `UpdateExpression`, so `-a ** b` has no derivation at all — the
// rule exists because a reader cannot tell whether it would mean `(-a) ** b` or
// `-(a ** b)`, and those differ.
for source in [
"-a ** b",
"+a ** b",
"!a ** b",
"~a ** b",
"typeof a ** b",
"void a ** b",
"delete a ** b",
] {
assert_eq!(
error(source).kind,
ParseErrorKind::ExponentiationOnUnary,
"on {source:?}"
);
}
// The caret goes under the operand that is wrong, not the operator that noticed.
assert_eq!(error("-a ** b").span, Span::new(0, 2));
// Both ways of saying what you meant are fine.
assert_eq!(shape("(-a) ** b"), "(** (- a) b)");
assert_eq!(shape("-(a ** b)"), "(- (** a b))");
// The restriction is on the *left* operand only: the right is an
// `ExponentiationExpression`, which a `UnaryExpression` is.
assert_eq!(shape("a ** -b"), "(** a (- b))");
assert_eq!(shape("a ** typeof b"), "(** a (typeof b))");
// …and only `**` is restricted. Every other operator takes a bare unary on the left.
assert_eq!(shape("-a * b"), "(* (- a) b)");
assert_eq!(shape("-a + b"), "(+ (- a) b)");
}
#[test]
fn coalescing_may_not_be_mixed_with_the_boolean_operators_without_parentheses() {
// §13.13: `CoalesceExpressionHead` admits a `CoalesceExpression` or a
// `BitwiseORExpression` and nothing else, and `ShortCircuitExpression` keeps the two
// families apart in the other direction. Both orders are errors, and for the same reason
// as `**`: nobody would agree on what the unbracketed form meant.
for source in ["a || b ?? c", "a ?? b || c", "a && b ?? c", "a ?? b && c"] {
assert_eq!(
error(source).kind,
ParseErrorKind::MixedCoalesceAndLogical,
"on {source:?}"
);
}
// The caret goes under the operand from the wrong family.
assert_eq!(error("a || b ?? c").span, Span::new(0, 6));
assert_eq!(error("a ?? b || c").span, Span::new(5, 11));
// Parentheses settle it, in either direction.
assert_eq!(shape("(a || b) ?? c"), "(?? (|| a b) c)");
assert_eq!(shape("a ?? (b || c)"), "(?? a (|| b c))");
assert_eq!(shape("(a ?? b) || c"), "(|| (?? a b) c)");
assert_eq!(shape("a || (b ?? c)"), "(|| a (?? b c))");
// `&&` and `||` mix with each other freely — the rule is not symmetric, and a check that
// rejected `a || b && c` would be rejecting ordinary JavaScript.
assert_eq!(shape("a || b && c"), "(|| a (&& b c))");
assert_eq!(shape("a && b || c"), "(|| (&& a b) c)");
// …and `??` chains with itself, since `CoalesceExpressionHead` may be a
// `CoalesceExpression`.
assert_eq!(shape("a ?? b ?? c"), "(?? (?? a b) c)");
// A `??` whose operand is an ordinary binary expression is fine: the boundary is the
// boolean operators, not precedence in general.
assert_eq!(shape("a ?? b + c"), "(?? a (+ b c))");
assert_eq!(shape("a | b ?? c"), "(?? (| a b) c)");
}
#[test]
fn a_conditional_takes_an_assignment_in_each_arm_and_a_comma_in_neither() {
// §13.14: `ShortCircuitExpression ? AssignmentExpression : AssignmentExpression`. The
// Note explains the choice — it lets an assignment be governed by either arm and keeps a
// comma expression out of the middle, which C and Java do not.
assert_eq!(shape("a ? b : c"), "(? a b c)");
assert_eq!(shape("a ? b = 1 : c = 2"), "(? a (= b 1) (= c 2))");
// The test is a ShortCircuitExpression, so everything below it binds tighter.
assert_eq!(shape("a || b ? c : d"), "(? (|| a b) c d)");
assert_eq!(shape("a + b ? c : d"), "(? (+ a b) c d)");
// Nesting: the alternate is an AssignmentExpression, so a chain groups to the right —
// which is what makes `a ? b : c ? d : e` mean what everyone assumes it means.
assert_eq!(shape("a ? b : c ? d : e"), "(? a b (? c d e))");
assert_eq!(shape("a ? b ? c : d : e"), "(? a (? b c d) e)");
// A comma is not an AssignmentExpression, so it cannot appear in an arm unbracketed.
assert_eq!(
error("a ? b, c : d").kind,
ParseErrorKind::Unexpected {
expected: "`:`",
found: TokenKind::Comma,
}
);
assert_eq!(shape("a ? (b, c) : d"), "(? a (, b c) d)");
assert_eq!(
error("a ? b").kind,
ParseErrorKind::Unexpected {
expected: "`:`",
found: TokenKind::Eof,
}
);
assert_eq!(parse("a ? b : c").span, Span::new(0, 9));
// The bracketing flag is part of what a node claims about itself, and nothing else
// in this slice reads it — so it is asserted here rather than left to a later one
// that will (arrow functions cover `(a, b)`, and `delete (x, y)` turns on it).
assert!(!parse("a ? b : c").parenthesized);
assert!(parse("(a ? b : c)").parenthesized);
}
#[test]
fn a_call_is_never_an_assignment_target_however_the_assignment_is_written() {
// §8.6.4 gives a host two conformant answers for a `CallExpression`, and DR-0009 records
// which one this is. `web-compat` would make `f() = 1` parse in sloppy code and throw at
// run time, as browsers do; ViperJS is not a web host and returns `invalid` in both modes.
//
// The shapes are the ones test262 has a test for, each of them flagged `onlyStrict`
// there because only the strict half is host-independent.
for source in [
"f() = 1;",
"f() += 1;",
"f() -= 1;",
"f() &&= 1;",
"f()++;",
"++f();",
"for (f() in x);",
"for (f() of x);",
"(f()) = 1;",
"f`t` = 1;",
] {
assert!(
parse_script(source).is_err(),
"{source:?} assigns to a call and has no derivation here"
);
let strict = format!("\"use strict\"; {source}");
assert!(parse_script(&strict).is_err(), "{strict:?} likewise");
}
// A *property of* a call is an ordinary `MemberExpression` and always was simple.
assert!(parse_script("f().a = 1;").is_ok());
assert!(parse_script("f()[0] = 1;").is_ok());
// …and a call is refused as a destructuring target too, where even a web host refuses
// it: the host option is about the direct form and reaches no further.
assert!(parse_script("[f()] = x;").is_err());
assert!(parse_script("({a: f()} = x);").is_err());
}
#[test]
fn assignment_groups_to_the_right_and_only_targets_what_can_be_assigned_to() {
// §13.15: `LeftHandSideExpression = AssignmentExpression` — the recursion is on the
// right, so a chain groups that way and `a = b = c` gives both `a` and `b` the value.
assert_eq!(shape("a = b"), "(= a b)");
assert_eq!(shape("a = b = c"), "(= a (= b c))");
// Every operator §13.15 lists, including the three with their own productions.
for (source, shaped) in [
("a += b", "(+= a b)"),
("a -= b", "(-= a b)"),
("a *= b", "(*= a b)"),
("a /= b", "(/= a b)"),
("a %= b", "(%= a b)"),
("a **= b", "(**= a b)"),
("a <<= b", "(<<= a b)"),
("a >>= b", "(>>= a b)"),
("a >>>= b", "(>>>= a b)"),
("a &= b", "(&= a b)"),
("a ^= b", "(^= a b)"),
("a |= b", "(|= a b)"),
("a &&= b", "(&&= a b)"),
("a ||= b", "(||= a b)"),
("a ??= b", "(??= a b)"),
] {
assert_eq!(shape(source), shaped, "parsing {source:?}");
}
// The value is a whole AssignmentExpression, so everything binds tighter than `=`.
assert_eq!(shape("a = b + c"), "(= a (+ b c))");
assert_eq!(shape("a += b ? c : d"), "(+= a (? b c d))");
assert_eq!(shape("a = b || c"), "(= a (|| b c))");
// §13.15.1: a Syntax Error if the AssignmentTargetType is not simple. Refused here
// rather than at run time, which is the difference between a program that never starts
// and one that fails halfway through.
for source in [
"1 = 2",
"'a' = 2",
"this = 1",
"null = 1",
"true = 1",
"a + b = c",
"-a = b",
"(a, b) = 1",
"(a ? b : c) = d",
"a ?? b = c",
"/x/ = 1",
"1 += 2",
"1 &&= 2",
] {
assert_eq!(
error(source).kind,
ParseErrorKind::InvalidAssignmentTarget,
"on {source:?}"
);
}
// The caret goes under the thing that cannot be assigned to.
assert_eq!(error("a + b = c").span, Span::new(0, 5));
// Parentheses do not change the answer, and need no rule of their own: `(a)` *is* the
// identifier, because bracketing is a flag rather than a node.
assert_eq!(shape("(a) = 1"), "(= a 1)");
assert_eq!(shape("((a)) = 1"), "(= a 1)");
assert!(matches!(
parse("(a) = 1").kind,
ExprKind::Assignment { ref target, .. }
if matches!(&**target, AssignmentTarget::Simple(expr) if expr.parenthesized)
));
assert_eq!(parse("a = b").span, Span::new(0, 5));
assert!(!parse("a = b").parenthesized);
assert!(parse("(a = b)").parenthesized);
// `&&=`, `||=` and `??=` do not always assign — `a ||= b` leaves `a` alone when it is
// truthy — which is why the operator says so rather than the compiler guessing.
assert!(AssignmentOperator::LogicalOr.short_circuits());
assert!(AssignmentOperator::LogicalAnd.short_circuits());
assert!(AssignmentOperator::NullishCoalescing.short_circuits());
assert!(!AssignmentOperator::Assign.short_circuits());
assert!(!AssignmentOperator::Add.short_circuits());
}
#[test]
fn the_comma_operator_is_the_loosest_thing_there_is_and_is_held_flat() {
// §13.16: `Expression : Expression , AssignmentExpression`.
assert_eq!(shape("a, b"), "(, a b)");
// Flat rather than nested pairs: the grammar's recursion is on the left, so pairs would
// nest once per comma for no gain — evaluation is left to right either way.
assert_eq!(shape("a, b, c"), "(, a b c)");
assert_eq!(shape("a, b, c, d"), "(, a b c d)");
// …but a bracketed sequence is its own node, so flattening cannot cross parentheses.
assert_eq!(shape("(a, b), c"), "(, (, a b) c)");
assert_eq!(shape("a, (b, c)"), "(, a (, b c))");
// Loosest of all: everything else binds tighter.
assert_eq!(shape("a = 1, b = 2"), "(, (= a 1) (= b 2))");
assert_eq!(shape("a ? b : c, d"), "(, (? a b c) d)");
assert_eq!(shape("a + b, c * d"), "(, (+ a b) (* c d))");
assert_eq!(parse("a, b").span, Span::new(0, 4));
assert!(!parse("a, b").parenthesized);
assert!(parse("(a, b)").parenthesized);
// A long list is a loop rather than a recursion, so its length is bounded by memory.
let long = vec!["a"; 5000].join(", ");
assert!(parse_expression(&long).is_ok());
// A trailing comma is not part of the operator — that is argument-list syntax.
assert_eq!(
error("a, b,").kind,
ParseErrorKind::Unexpected {
expected: "an expression",
found: TokenKind::Eof,
}
);
}
}