1 use crate::context_stack::{BindingKind, ContextMetadata, ControlInfo, LabelKind};
2 use crate::early_error_checker::EarlyErrorChecker;
3 use crate::error::{BoxedParseError, ParseError, Result};
4 use crate::Token;
5 use ast::{
6     arena,
7     source_atom_set::{CommonSourceAtomSetIndices, SourceAtomSet},
8     source_location_accessor::SourceLocationAccessor,
9     source_slice_list::SourceSliceList,
10     types::*,
11     SourceLocation,
12 };
13 use bumpalo::{vec, Bump};
14 use std::cell::RefCell;
15 use std::rc::Rc;
16 
17 pub struct AstBuilder<'alloc> {
18     pub allocator: &'alloc Bump,
19 
20     context_metadata: ContextMetadata,
21 
22     atoms: Rc<RefCell<SourceAtomSet<'alloc>>>,
23 
24     slices: Rc<RefCell<SourceSliceList<'alloc>>>,
25 }
26 
27 pub trait AstBuilderDelegate<'alloc> {
ast_builder_refmut(&mut self) -> &mut AstBuilder<'alloc>28     fn ast_builder_refmut(&mut self) -> &mut AstBuilder<'alloc>;
29 }
30 
31 impl<'alloc> AstBuilder<'alloc> {
new( allocator: &'alloc Bump, atoms: Rc<RefCell<SourceAtomSet<'alloc>>>, slices: Rc<RefCell<SourceSliceList<'alloc>>>, ) -> Self32     pub fn new(
33         allocator: &'alloc Bump,
34         atoms: Rc<RefCell<SourceAtomSet<'alloc>>>,
35         slices: Rc<RefCell<SourceSliceList<'alloc>>>,
36     ) -> Self {
37         Self {
38             allocator,
39             context_metadata: ContextMetadata::new(),
40             atoms,
41             slices,
42         }
43     }
44 
alloc<T>(&self, value: T) -> arena::Box<'alloc, T>45     pub fn alloc<T>(&self, value: T) -> arena::Box<'alloc, T> {
46         arena::alloc(self.allocator, value)
47     }
48 
alloc_with<F, T>(&self, gen: F) -> arena::Box<'alloc, T> where F: FnOnce() -> T,49     pub fn alloc_with<F, T>(&self, gen: F) -> arena::Box<'alloc, T>
50     where
51         F: FnOnce() -> T,
52     {
53         arena::alloc_with(self.allocator, gen)
54     }
55 
alloc_str(&self, s: &str) -> &'alloc str56     pub fn alloc_str(&self, s: &str) -> &'alloc str {
57         arena::alloc_str(self.allocator, s)
58     }
59 
new_vec<T>(&self) -> arena::Vec<'alloc, T>60     fn new_vec<T>(&self) -> arena::Vec<'alloc, T> {
61         arena::Vec::new_in(self.allocator)
62     }
63 
new_vec_single<T>(&self, value: T) -> arena::Vec<'alloc, T>64     fn new_vec_single<T>(&self, value: T) -> arena::Vec<'alloc, T> {
65         vec![in self.allocator; value]
66     }
67 
collect_vec_from_results<T, C>(&self, results: C) -> Result<'alloc, arena::Vec<'alloc, T>> where C: IntoIterator<Item = Result<'alloc, T>>,68     fn collect_vec_from_results<T, C>(&self, results: C) -> Result<'alloc, arena::Vec<'alloc, T>>
69     where
70         C: IntoIterator<Item = Result<'alloc, T>>,
71     {
72         let mut out = self.new_vec();
73         for result in results {
74             out.push(result?);
75         }
76         Ok(out)
77     }
78 
push<T>(&self, list: &mut arena::Vec<'alloc, T>, value: T)79     fn push<T>(&self, list: &mut arena::Vec<'alloc, T>, value: T) {
80         list.push(value);
81     }
82 
append<T>(&self, list: &mut arena::Vec<'alloc, T>, elements: &mut arena::Vec<'alloc, T>)83     fn append<T>(&self, list: &mut arena::Vec<'alloc, T>, elements: &mut arena::Vec<'alloc, T>) {
84         list.append(elements);
85     }
86 
87     // IdentifierReference : Identifier
identifier_reference( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Identifier>>88     pub fn identifier_reference(
89         &self,
90         token: arena::Box<'alloc, Token>,
91     ) -> Result<'alloc, arena::Box<'alloc, Identifier>> {
92         self.on_identifier_reference(&token)?;
93         Ok(self.alloc_with(|| self.identifier(token)))
94     }
95 
96     // BindingIdentifier : Identifier
binding_identifier( &mut self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>>97     pub fn binding_identifier(
98         &mut self,
99         token: arena::Box<'alloc, Token>,
100     ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>> {
101         self.on_binding_identifier(&token)?;
102         let loc = token.loc;
103         Ok(self.alloc_with(|| BindingIdentifier {
104             name: self.identifier(token),
105             loc,
106         }))
107     }
108 
109     // BindingIdentifier : `yield`
binding_identifier_yield( &mut self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>>110     pub fn binding_identifier_yield(
111         &mut self,
112         token: arena::Box<'alloc, Token>,
113     ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>> {
114         self.on_binding_identifier(&token)?;
115         let loc = token.loc;
116         Ok(self.alloc_with(|| BindingIdentifier {
117             name: Identifier {
118                 value: CommonSourceAtomSetIndices::yield_(),
119                 loc,
120             },
121             loc,
122         }))
123     }
124 
125     // BindingIdentifier : `await`
binding_identifier_await( &mut self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>>126     pub fn binding_identifier_await(
127         &mut self,
128         token: arena::Box<'alloc, Token>,
129     ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>> {
130         self.on_binding_identifier(&token)?;
131         let loc = token.loc;
132         Ok(self.alloc_with(|| BindingIdentifier {
133             name: Identifier {
134                 value: CommonSourceAtomSetIndices::await_(),
135                 loc,
136             },
137             loc,
138         }))
139     }
140 
141     // LabelIdentifier : Identifier
label_identifier( &mut self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Label>>142     pub fn label_identifier(
143         &mut self,
144         token: arena::Box<'alloc, Token>,
145     ) -> Result<'alloc, arena::Box<'alloc, Label>> {
146         self.on_label_identifier(&token)?;
147         let loc = token.loc;
148         Ok(self.alloc_with(|| Label {
149             value: token.value.as_atom(),
150             loc,
151         }))
152     }
153 
154     // PrimaryExpression : `this`
this_expr( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>155     pub fn this_expr(
156         &self,
157         token: arena::Box<'alloc, Token>,
158     ) -> arena::Box<'alloc, Expression<'alloc>> {
159         let loc = token.loc;
160         self.alloc_with(|| Expression::ThisExpression { loc })
161     }
162 
163     // PrimaryExpression : IdentifierReference
identifier_expr( &self, name: arena::Box<'alloc, Identifier>, ) -> arena::Box<'alloc, Expression<'alloc>>164     pub fn identifier_expr(
165         &self,
166         name: arena::Box<'alloc, Identifier>,
167     ) -> arena::Box<'alloc, Expression<'alloc>> {
168         let loc = name.loc;
169         self.alloc_with(|| {
170             Expression::IdentifierExpression(IdentifierExpression {
171                 name: name.unbox(),
172                 loc,
173             })
174         })
175     }
176 
177     // PrimaryExpression : RegularExpressionLiteral
regexp_literal( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>178     pub fn regexp_literal(
179         &self,
180         token: arena::Box<'alloc, Token>,
181     ) -> arena::Box<'alloc, Expression<'alloc>> {
182         let source = self.slices.borrow().get(token.value.as_slice());
183 
184         debug_assert!(source.chars().nth(0).unwrap() == '/');
185 
186         let end = source.rfind('/').unwrap();
187 
188         let pattern = self.slices.borrow_mut().push(&source[1..end]);
189         let flags = &source[end + 1..];
190 
191         let mut global: bool = false;
192         let mut ignore_case: bool = false;
193         let mut multi_line: bool = false;
194         let mut dot_all: bool = false;
195         let mut unicode: bool = false;
196         let mut sticky: bool = false;
197         for c in flags.chars() {
198             if c == 'g' {
199                 global = true;
200             }
201             if c == 'i' {
202                 ignore_case = true;
203             }
204             if c == 'm' {
205                 multi_line = true;
206             }
207             if c == 's' {
208                 dot_all = true;
209             }
210             if c == 'u' {
211                 unicode = true;
212             }
213             if c == 'y' {
214                 sticky = true;
215             }
216         }
217 
218         let loc = token.loc;
219         self.alloc_with(|| Expression::LiteralRegExpExpression {
220             pattern,
221             global,
222             ignore_case,
223             multi_line,
224             dot_all,
225             sticky,
226             unicode,
227             loc,
228         })
229     }
230 
231     // PrimaryExpression : TemplateLiteral
untagged_template_expr( &self, template_literal: arena::Box<'alloc, TemplateExpression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>232     pub fn untagged_template_expr(
233         &self,
234         template_literal: arena::Box<'alloc, TemplateExpression<'alloc>>,
235     ) -> arena::Box<'alloc, Expression<'alloc>> {
236         self.alloc_with(|| Expression::TemplateExpression(template_literal.unbox()))
237     }
238 
239     // PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList
uncover_parenthesized_expression( &self, parenthesized: arena::Box<'alloc, CoverParenthesized<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>240     pub fn uncover_parenthesized_expression(
241         &self,
242         parenthesized: arena::Box<'alloc, CoverParenthesized<'alloc>>,
243     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
244         match parenthesized.unbox() {
245             CoverParenthesized::Expression { expression, .. } => {
246                 // TODO - does this need to rewalk the expression to look for
247                 // invalid ObjectPattern or ArrayPattern syntax?
248                 Ok(expression)
249             }
250             CoverParenthesized::Parameters(_parameters) => Err(ParseError::NotImplemented(
251                 "parenthesized expression with `...` should be a syntax error",
252             )
253             .into()),
254         }
255     }
256 
257     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `)`
cover_parenthesized_expression( &self, open_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, CoverParenthesized<'alloc>>258     pub fn cover_parenthesized_expression(
259         &self,
260         open_token: arena::Box<'alloc, Token>,
261         expression: arena::Box<'alloc, Expression<'alloc>>,
262         close_token: arena::Box<'alloc, Token>,
263     ) -> arena::Box<'alloc, CoverParenthesized<'alloc>> {
264         self.alloc_with(|| CoverParenthesized::Expression {
265             expression,
266             loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
267         })
268     }
269 
270     // CoverParenthesizedExpressionAndArrowParameterList : `(` `)`
empty_parameter_list(&self) -> arena::Vec<'alloc, Parameter<'alloc>>271     pub fn empty_parameter_list(&self) -> arena::Vec<'alloc, Parameter<'alloc>> {
272         self.new_vec()
273     }
274 
275     /// Used when parsing `([a, b=2]=arr) =>` to reinterpret as parameter bindings
276     /// the snippets `a` and `b=2`, which were previously parsed as assignment targets.
assignment_target_maybe_default_to_binding( &self, target: AssignmentTargetMaybeDefault<'alloc>, ) -> Result<'alloc, Parameter<'alloc>>277     fn assignment_target_maybe_default_to_binding(
278         &self,
279         target: AssignmentTargetMaybeDefault<'alloc>,
280     ) -> Result<'alloc, Parameter<'alloc>> {
281         match target {
282             AssignmentTargetMaybeDefault::AssignmentTarget(target) => Ok(Parameter::Binding(
283                 self.assignment_target_to_binding(target)?,
284             )),
285 
286             AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(
287                 AssignmentTargetWithDefault { binding, init, loc },
288             ) => Ok(Parameter::BindingWithDefault(BindingWithDefault {
289                 binding: self.assignment_target_to_binding(binding)?,
290                 init,
291                 loc,
292             })),
293         }
294     }
295 
assignment_target_property_to_binding_property( &self, target: AssignmentTargetProperty<'alloc>, ) -> Result<'alloc, BindingProperty<'alloc>>296     fn assignment_target_property_to_binding_property(
297         &self,
298         target: AssignmentTargetProperty<'alloc>,
299     ) -> Result<'alloc, BindingProperty<'alloc>> {
300         Ok(match target {
301             AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(
302                 AssignmentTargetPropertyIdentifier {
303                     binding: AssignmentTargetIdentifier { name, loc },
304                     init,
305                     loc: loc2,
306                 },
307             ) => BindingProperty::BindingPropertyIdentifier(BindingPropertyIdentifier {
308                 binding: BindingIdentifier { name, loc },
309                 init,
310                 loc: loc2,
311             }),
312 
313             AssignmentTargetProperty::AssignmentTargetPropertyProperty(
314                 AssignmentTargetPropertyProperty { name, binding, loc },
315             ) => BindingProperty::BindingPropertyProperty(BindingPropertyProperty {
316                 name,
317                 binding: self.assignment_target_maybe_default_to_binding(binding)?,
318                 loc,
319             }),
320         })
321     }
322 
323     /// Refine an AssignmentRestProperty into a BindingRestProperty.
assignment_rest_property_to_binding_identifier( &self, target: AssignmentTarget<'alloc>, ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>>324     fn assignment_rest_property_to_binding_identifier(
325         &self,
326         target: AssignmentTarget<'alloc>,
327     ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>> {
328         match target {
329             // ({...x} = dv) => {}
330             AssignmentTarget::SimpleAssignmentTarget(
331                 SimpleAssignmentTarget::AssignmentTargetIdentifier(AssignmentTargetIdentifier {
332                     name,
333                     loc,
334                 }),
335             ) => Ok(self.alloc_with(|| BindingIdentifier { name, loc })),
336 
337             // ({...x.y} = dv) => {}
338             _ => Err(ParseError::ObjectBindingPatternWithInvalidRest.into()),
339         }
340     }
341 
342     /// Refine the left-hand side of `=` to a parameter binding. The spec says:
343     ///
344     /// > When the production *ArrowParameters* :
345     /// > *CoverParenthesizedExpressionAndArrowParameterList* is recognized,
346     /// > the following grammar is used to refine the interpretation of
347     /// > *CoverParenthesizedExpressionAndArrowParameterList*:
348     /// >
349     /// > *ArrowFormalParameters*\[Yield, Await\] :
350     /// > `(` *UniqueFormalParameters*\[?Yield, ?Await\] `)`
351     ///
352     /// Of course, rather than actually reparsing the arrow function parameters,
353     /// we work by refining the AST we already built.
354     ///
355     /// When parsing `(a = 1, [b, c] = obj) => {}`, the assignment targets `a`
356     /// and `[b, c]` are passed to this method.
assignment_target_to_binding( &self, target: AssignmentTarget<'alloc>, ) -> Result<'alloc, Binding<'alloc>>357     fn assignment_target_to_binding(
358         &self,
359         target: AssignmentTarget<'alloc>,
360     ) -> Result<'alloc, Binding<'alloc>> {
361         match target {
362             // (a = dv) => {}
363             AssignmentTarget::SimpleAssignmentTarget(
364                 SimpleAssignmentTarget::AssignmentTargetIdentifier(AssignmentTargetIdentifier {
365                     name,
366                     loc,
367                 }),
368             ) => Ok(Binding::BindingIdentifier(BindingIdentifier { name, loc })),
369 
370             // This case is always an early SyntaxError.
371             // (a.x = dv) => {}
372             // (a[i] = dv) => {}
373             AssignmentTarget::SimpleAssignmentTarget(
374                 SimpleAssignmentTarget::MemberAssignmentTarget(_),
375             ) => Err(ParseError::InvalidParameter.into()),
376 
377             // ([a, b] = dv) => {}
378             AssignmentTarget::AssignmentTargetPattern(
379                 AssignmentTargetPattern::ArrayAssignmentTarget(ArrayAssignmentTarget {
380                     elements,
381                     rest,
382                     loc,
383                 }),
384             ) => {
385                 let elements: arena::Vec<'alloc, Option<AssignmentTargetMaybeDefault<'alloc>>> =
386                     elements;
387                 let elements: arena::Vec<'alloc, Option<Parameter<'alloc>>> = self
388                     .collect_vec_from_results(elements.into_iter().map(|maybe_target| {
389                         maybe_target
390                             .map(|target| self.assignment_target_maybe_default_to_binding(target))
391                             .transpose()
392                     }))?;
393                 let rest: Option<Result<'alloc, arena::Box<'alloc, Binding<'alloc>>>> = rest.map(
394                     |rest_target| -> Result<'alloc, arena::Box<'alloc, Binding<'alloc>>> {
395                         Ok(self.alloc(self.assignment_target_to_binding(rest_target.unbox())?))
396                     },
397                 );
398                 let rest: Option<arena::Box<'alloc, Binding<'alloc>>> = rest.transpose()?;
399                 Ok(Binding::BindingPattern(BindingPattern::ArrayBinding(
400                     ArrayBinding {
401                         elements,
402                         rest,
403                         loc,
404                     },
405                 )))
406             }
407 
408             // ({a, b: c} = dv) => {}
409             AssignmentTarget::AssignmentTargetPattern(
410                 AssignmentTargetPattern::ObjectAssignmentTarget(ObjectAssignmentTarget {
411                     properties,
412                     rest,
413                     loc,
414                 }),
415             ) => {
416                 let properties =
417                     self.collect_vec_from_results(properties.into_iter().map(|target| {
418                         self.assignment_target_property_to_binding_property(target)
419                     }))?;
420 
421                 let rest = if let Some(rest_target) = rest {
422                     Some(self.assignment_rest_property_to_binding_identifier(rest_target.unbox())?)
423                 } else {
424                     None
425                 };
426                 Ok(Binding::BindingPattern(BindingPattern::ObjectBinding(
427                     ObjectBinding {
428                         properties,
429                         rest,
430                         loc,
431                     },
432                 )))
433             }
434         }
435     }
436 
object_property_to_binding_property( &self, op: ObjectProperty<'alloc>, ) -> Result<'alloc, BindingProperty<'alloc>>437     fn object_property_to_binding_property(
438         &self,
439         op: ObjectProperty<'alloc>,
440     ) -> Result<'alloc, BindingProperty<'alloc>> {
441         match op {
442             ObjectProperty::NamedObjectProperty(NamedObjectProperty::DataProperty(
443                 DataProperty {
444                     property_name,
445                     expression,
446                     loc,
447                 },
448             )) => Ok(BindingProperty::BindingPropertyProperty(
449                 BindingPropertyProperty {
450                     name: property_name,
451                     binding: self.expression_to_parameter(expression.unbox())?,
452                     loc,
453                 },
454             )),
455 
456             ObjectProperty::NamedObjectProperty(NamedObjectProperty::MethodDefinition(_)) => {
457                 Err(ParseError::ObjectPatternWithMethod.into())
458             }
459 
460             ObjectProperty::ShorthandProperty(ShorthandProperty {
461                 name: IdentifierExpression { name, loc },
462                 ..
463             }) => {
464                 // TODO - CoverInitializedName can't be represented in an
465                 // ObjectProperty, but we need it here.
466                 Ok(BindingProperty::BindingPropertyIdentifier(
467                     BindingPropertyIdentifier {
468                         binding: BindingIdentifier { name, loc },
469                         init: None,
470                         loc,
471                     },
472                 ))
473             }
474 
475             ObjectProperty::SpreadProperty(_expression) => {
476                 Err(ParseError::ObjectPatternWithNonFinalRest.into())
477             }
478         }
479     }
480 
481     /// Refine an instance of "*PropertyDefinition* : `...`
482     /// *AssignmentExpression*" into a *BindingRestProperty*.
spread_expression_to_rest_binding( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>>483     fn spread_expression_to_rest_binding(
484         &self,
485         expression: arena::Box<'alloc, Expression<'alloc>>,
486     ) -> Result<'alloc, arena::Box<'alloc, BindingIdentifier>> {
487         Ok(match expression.unbox() {
488             Expression::IdentifierExpression(IdentifierExpression { name, loc }) => {
489                 self.alloc_with(|| BindingIdentifier { name, loc })
490             }
491             _ => {
492                 return Err(ParseError::ObjectBindingPatternWithInvalidRest.into());
493             }
494         })
495     }
496 
pop_trailing_spread_property( &self, properties: &mut arena::Vec<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>>, ) -> Option<arena::Box<'alloc, Expression<'alloc>>>497     fn pop_trailing_spread_property(
498         &self,
499         properties: &mut arena::Vec<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>>,
500     ) -> Option<arena::Box<'alloc, Expression<'alloc>>> {
501         // Check whether we want to pop a PropertyDefinition
502         match properties.last().map(|boxed| &**boxed) {
503             Some(ObjectProperty::SpreadProperty(_)) => {}
504             _ => return None,
505         }
506 
507         // We do.
508         match properties.pop().unwrap().unbox() {
509             ObjectProperty::SpreadProperty(expression) => Some(expression),
510             _ => panic!("bug"), // can't happen: we just checked this above
511         }
512     }
513 
514     /// Refine an *ObjectLiteral* into an *ObjectBindingPattern*.
object_expression_to_object_binding( &self, object: ObjectExpression<'alloc>, ) -> Result<'alloc, ObjectBinding<'alloc>>515     fn object_expression_to_object_binding(
516         &self,
517         object: ObjectExpression<'alloc>,
518     ) -> Result<'alloc, ObjectBinding<'alloc>> {
519         let mut properties = object.properties;
520         let loc = object.loc;
521         let rest = self.pop_trailing_spread_property(&mut properties);
522         Ok(ObjectBinding {
523             properties: self.collect_vec_from_results(
524                 properties
525                     .into_iter()
526                     .map(|prop| self.object_property_to_binding_property(prop.unbox())),
527             )?,
528             rest: rest
529                 .map(|expression| self.spread_expression_to_rest_binding(expression))
530                 .transpose()?,
531             loc,
532         })
533     }
534 
array_elements_to_parameters( &self, elements: arena::Vec<'alloc, ArrayExpressionElement<'alloc>>, ) -> Result<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>535     fn array_elements_to_parameters(
536         &self,
537         elements: arena::Vec<'alloc, ArrayExpressionElement<'alloc>>,
538     ) -> Result<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>> {
539         self.collect_vec_from_results(elements.into_iter().map(|element| match element {
540                 ArrayExpressionElement::Expression(expr) =>
541                     Ok(Some(self.expression_to_parameter(expr.unbox())?)),
542                 ArrayExpressionElement::SpreadElement(_expr) =>
543                     // ([...a, b]) => {}
544                     Err(ParseError::ArrayPatternWithNonFinalRest.into()),
545                 ArrayExpressionElement::Elision { .. } => Ok(None),
546             }))
547     }
548 
pop_trailing_spread_element( &self, elements: &mut arena::Vec<'alloc, ArrayExpressionElement<'alloc>>, ) -> Option<arena::Box<'alloc, Expression<'alloc>>>549     fn pop_trailing_spread_element(
550         &self,
551         elements: &mut arena::Vec<'alloc, ArrayExpressionElement<'alloc>>,
552     ) -> Option<arena::Box<'alloc, Expression<'alloc>>> {
553         // Check whether we want to pop an element.
554         match elements.last() {
555             Some(ArrayExpressionElement::SpreadElement(_)) => {}
556             _ => return None,
557         }
558 
559         // We do.
560         match elements.pop() {
561             Some(ArrayExpressionElement::SpreadElement(expression)) => Some(expression),
562             _ => panic!("bug"), // can't happen: we just checked this above
563         }
564     }
565 
expression_to_binding_no_default( &self, expression: Expression<'alloc>, ) -> Result<'alloc, Binding<'alloc>>566     fn expression_to_binding_no_default(
567         &self,
568         expression: Expression<'alloc>,
569     ) -> Result<'alloc, Binding<'alloc>> {
570         match expression {
571             Expression::IdentifierExpression(IdentifierExpression { name, loc }) => {
572                 Ok(Binding::BindingIdentifier(BindingIdentifier { name, loc }))
573             }
574 
575             Expression::ArrayExpression(ArrayExpression { mut elements, loc }) => {
576                 let rest = self.pop_trailing_spread_element(&mut elements);
577                 let elements = self.array_elements_to_parameters(elements)?;
578                 let rest = rest
579                     .map(|expr| match self.expression_to_parameter(expr.unbox())? {
580                         Parameter::Binding(b) => Ok(self.alloc_with(|| b)),
581                         Parameter::BindingWithDefault(_) => {
582                             let err: BoxedParseError =
583                                 ParseError::ArrayBindingPatternWithInvalidRest.into();
584                             Err(err)
585                         }
586                     })
587                     .transpose()?;
588                 Ok(Binding::BindingPattern(BindingPattern::ArrayBinding(
589                     ArrayBinding {
590                         elements,
591                         rest,
592                         loc,
593                     },
594                 )))
595             }
596 
597             Expression::ObjectExpression(object) => Ok(Binding::BindingPattern(
598                 BindingPattern::ObjectBinding(self.object_expression_to_object_binding(object)?),
599             )),
600 
601             _ => Err(ParseError::InvalidParameter.into()),
602         }
603     }
604 
expression_to_parameter( &self, expression: Expression<'alloc>, ) -> Result<'alloc, Parameter<'alloc>>605     fn expression_to_parameter(
606         &self,
607         expression: Expression<'alloc>,
608     ) -> Result<'alloc, Parameter<'alloc>> {
609         match expression {
610             Expression::AssignmentExpression {
611                 binding,
612                 expression,
613                 loc,
614             } => Ok(Parameter::BindingWithDefault(BindingWithDefault {
615                 binding: self.assignment_target_to_binding(binding)?,
616                 init: expression,
617                 loc,
618             })),
619 
620             other => Ok(Parameter::Binding(
621                 self.expression_to_binding_no_default(other)?,
622             )),
623         }
624     }
625 
626     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `,` `)`
627     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `,` `...` BindingIdentifier `)`
628     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `,` `...` BindingPattern `)`
expression_to_parameter_list( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Vec<'alloc, Parameter<'alloc>>>629     pub fn expression_to_parameter_list(
630         &self,
631         expression: arena::Box<'alloc, Expression<'alloc>>,
632     ) -> Result<'alloc, arena::Vec<'alloc, Parameter<'alloc>>> {
633         // When the production
634         // *ArrowParameters* `:` *CoverParenthesizedExpressionAndArrowParameterList*
635         // is recognized the following grammar is used to refine the
636         // interpretation of
637         // *CoverParenthesizedExpressionAndArrowParameterList*:
638         //
639         //     ArrowFormalParameters[Yield, Await]:
640         //         `(` UniqueFormalParameters[?Yield, ?Await] `)`
641         match expression.unbox() {
642             Expression::BinaryExpression {
643                 operator: BinaryOperator::Comma { .. },
644                 left,
645                 right,
646                 ..
647             } => {
648                 let mut parameters = self.expression_to_parameter_list(left)?;
649                 self.push(
650                     &mut parameters,
651                     self.expression_to_parameter(right.unbox())?,
652                 );
653                 Ok(parameters)
654             }
655             other => Ok(self.new_vec_single(self.expression_to_parameter(other)?)),
656         }
657     }
658 
659     /// Used to convert `async(x, y, ...z)` from a *CallExpression* to async
660     /// arrow function parameters.
arguments_to_parameter_list( &self, arguments: Arguments<'alloc>, ) -> Result<'alloc, arena::Box<'alloc, FormalParameters<'alloc>>>661     fn arguments_to_parameter_list(
662         &self,
663         arguments: Arguments<'alloc>,
664     ) -> Result<'alloc, arena::Box<'alloc, FormalParameters<'alloc>>> {
665         let loc = arguments.loc;
666         let mut items = self.new_vec();
667         let mut rest: Option<Binding<'alloc>> = None;
668         for arg in arguments.args {
669             if rest.is_some() {
670                 return Err(ParseError::ArrowParametersWithNonFinalRest.into());
671             }
672             match arg {
673                 Argument::Expression(expr) => {
674                     self.push(&mut items, self.expression_to_parameter(expr.unbox())?);
675                 }
676                 Argument::SpreadElement(spread_expr) => {
677                     rest = Some(self.expression_to_binding_no_default(spread_expr.unbox())?);
678                 }
679             }
680         }
681         Ok(self.alloc_with(|| FormalParameters { items, rest, loc }))
682     }
683 
684     // CoverParenthesizedExpressionAndArrowParameterList : `(` `)`
685     // CoverParenthesizedExpressionAndArrowParameterList : `(` `...` BindingIdentifier `)`
686     // CoverParenthesizedExpressionAndArrowParameterList : `(` `...` BindingPattern `)`
687     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `,` `...` BindingIdentifier `)`
688     // CoverParenthesizedExpressionAndArrowParameterList : `(` Expression `,` `...` BindingPattern `)`
cover_arrow_parameter_list( &self, open_token: arena::Box<'alloc, Token>, parameters: arena::Vec<'alloc, Parameter<'alloc>>, rest: Option<arena::Box<'alloc, Binding<'alloc>>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, CoverParenthesized<'alloc>>689     pub fn cover_arrow_parameter_list(
690         &self,
691         open_token: arena::Box<'alloc, Token>,
692         parameters: arena::Vec<'alloc, Parameter<'alloc>>,
693         rest: Option<arena::Box<'alloc, Binding<'alloc>>>,
694         close_token: arena::Box<'alloc, Token>,
695     ) -> arena::Box<'alloc, CoverParenthesized<'alloc>> {
696         self.alloc_with(|| {
697             CoverParenthesized::Parameters(self.alloc_with(|| FormalParameters {
698                 items: parameters,
699                 rest: rest.map(|boxed| boxed.unbox()),
700                 loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
701             }))
702         })
703     }
704 
705     // Literal : NullLiteral
null_literal( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>706     pub fn null_literal(
707         &self,
708         token: arena::Box<'alloc, Token>,
709     ) -> arena::Box<'alloc, Expression<'alloc>> {
710         let loc = token.loc;
711         self.alloc_with(|| Expression::LiteralNullExpression { loc })
712     }
713 
714     // Literal : BooleanLiteral
boolean_literal( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>715     pub fn boolean_literal(
716         &self,
717         token: arena::Box<'alloc, Token>,
718     ) -> arena::Box<'alloc, Expression<'alloc>> {
719         let loc = token.loc;
720         let s = token.value.as_atom();
721         assert!(
722             s == CommonSourceAtomSetIndices::true_() || s == CommonSourceAtomSetIndices::false_()
723         );
724 
725         self.alloc_with(|| Expression::LiteralBooleanExpression {
726             value: s == CommonSourceAtomSetIndices::true_(),
727             loc,
728         })
729     }
730 
numeric_literal_value(token: arena::Box<'alloc, Token>) -> f64731     fn numeric_literal_value(token: arena::Box<'alloc, Token>) -> f64 {
732         token.unbox().value.as_number()
733     }
734 
735     // Literal : NumericLiteral
numeric_literal( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>736     pub fn numeric_literal(
737         &self,
738         token: arena::Box<'alloc, Token>,
739     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
740         let loc = token.loc;
741         Ok(self.alloc_with(|| {
742             Expression::LiteralNumericExpression(NumericLiteral {
743                 value: Self::numeric_literal_value(token),
744                 loc,
745             })
746         }))
747     }
748 
749     // Literal : NumericLiteral
750     //
751     // where NumericLiteral is either:
752     //   * DecimalBigIntegerLiteral
753     //   * NonDecimalIntegerLiteralBigIntLiteralSuffix
bigint_literal( &self, _token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>754     pub fn bigint_literal(
755         &self,
756         _token: arena::Box<'alloc, Token>,
757     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
758         Err(ParseError::NotImplemented("BigInt").into())
759     }
760 
761     // Literal : StringLiteral
string_literal( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>762     pub fn string_literal(
763         &self,
764         token: arena::Box<'alloc, Token>,
765     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
766         let loc = token.loc;
767         // Hack: Prevent emission for scripts with "use strict"
768         // directive.
769         let value = token.value.as_atom();
770         if value == CommonSourceAtomSetIndices::use_strict() {
771             return Err(ParseError::NotImplemented("use strict directive").into());
772         }
773 
774         Ok(self.alloc_with(|| Expression::LiteralStringExpression { value, loc }))
775     }
776 
777     // ArrayLiteral : `[` Elision? `]`
array_literal_empty( &self, open_token: arena::Box<'alloc, Token>, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>778     pub fn array_literal_empty(
779         &self,
780         open_token: arena::Box<'alloc, Token>,
781         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
782         close_token: arena::Box<'alloc, Token>,
783     ) -> arena::Box<'alloc, Expression<'alloc>> {
784         self.alloc_with(|| {
785             Expression::ArrayExpression(match elision {
786                 None => ArrayExpression {
787                     elements: self.new_vec(),
788                     loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
789                 },
790                 Some(mut array) => {
791                     array.loc.set_range(open_token.loc, close_token.loc);
792                     array.unbox()
793                 }
794             })
795         })
796     }
797 
798     // ArrayLiteral : `[` ElementList `]`
array_literal( &self, open_token: arena::Box<'alloc, Token>, mut array: arena::Box<'alloc, ArrayExpression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>799     pub fn array_literal(
800         &self,
801         open_token: arena::Box<'alloc, Token>,
802         mut array: arena::Box<'alloc, ArrayExpression<'alloc>>,
803         close_token: arena::Box<'alloc, Token>,
804     ) -> arena::Box<'alloc, Expression<'alloc>> {
805         array.loc.set_range(open_token.loc, close_token.loc);
806         self.alloc_with(|| Expression::ArrayExpression(array.unbox()))
807     }
808 
809     // ArrayLiteral : `[` ElementList `,` Elision? `]`
array_literal_with_trailing_elision( &self, open_token: arena::Box<'alloc, Token>, mut array: arena::Box<'alloc, ArrayExpression<'alloc>>, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>810     pub fn array_literal_with_trailing_elision(
811         &self,
812         open_token: arena::Box<'alloc, Token>,
813         mut array: arena::Box<'alloc, ArrayExpression<'alloc>>,
814         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
815         close_token: arena::Box<'alloc, Token>,
816     ) -> arena::Box<'alloc, Expression<'alloc>> {
817         if let Some(mut more) = elision {
818             self.append(&mut array.elements, &mut more.elements);
819         }
820         array.loc.set_range(open_token.loc, close_token.loc);
821         self.alloc_with(|| Expression::ArrayExpression(array.unbox()))
822     }
823 
824     // ElementList : Elision? AssignmentExpression
element_list_first( &self, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, element: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>825     pub fn element_list_first(
826         &self,
827         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
828         element: arena::Box<'alloc, Expression<'alloc>>,
829     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
830         let mut array = elision.unwrap_or_else(|| {
831             self.alloc_with(|| ArrayExpression {
832                 elements: self.new_vec(),
833                 // This will be overwritten once the enclosing array gets
834                 // parsed.
835                 loc: SourceLocation::default(),
836             })
837         });
838         self.push(
839             &mut array.elements,
840             ArrayExpressionElement::Expression(element),
841         );
842         array
843     }
844 
845     // ElementList : Elision? SpreadElement
element_list_first_spread( &self, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, spread_element: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>846     pub fn element_list_first_spread(
847         &self,
848         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
849         spread_element: arena::Box<'alloc, Expression<'alloc>>,
850     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
851         let mut array = elision.unwrap_or_else(|| {
852             self.alloc_with(|| ArrayExpression {
853                 elements: self.new_vec(),
854                 // This will be overwritten once the enclosing array gets
855                 // parsed.
856                 loc: SourceLocation::default(),
857             })
858         });
859         self.push(
860             &mut array.elements,
861             ArrayExpressionElement::SpreadElement(spread_element),
862         );
863         array
864     }
865 
866     // ElementList : ElementList `,` Elision? AssignmentExpression
element_list_append( &self, mut array: arena::Box<'alloc, ArrayExpression<'alloc>>, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, element: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>867     pub fn element_list_append(
868         &self,
869         mut array: arena::Box<'alloc, ArrayExpression<'alloc>>,
870         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
871         element: arena::Box<'alloc, Expression<'alloc>>,
872     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
873         if let Some(mut elision) = elision {
874             self.append(&mut array.elements, &mut elision.elements);
875         }
876         self.push(
877             &mut array.elements,
878             ArrayExpressionElement::Expression(element),
879         );
880         array
881     }
882 
883     // ElementList : ElementList `,` Elision? SpreadElement
element_list_append_spread( &self, mut array: arena::Box<'alloc, ArrayExpression<'alloc>>, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, spread_element: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>884     pub fn element_list_append_spread(
885         &self,
886         mut array: arena::Box<'alloc, ArrayExpression<'alloc>>,
887         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
888         spread_element: arena::Box<'alloc, Expression<'alloc>>,
889     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
890         if let Some(mut elision) = elision {
891             self.append(&mut array.elements, &mut elision.elements);
892         }
893         self.push(
894             &mut array.elements,
895             ArrayExpressionElement::SpreadElement(spread_element),
896         );
897         array
898     }
899 
900     // Elision : `,`
elision_single( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>901     pub fn elision_single(
902         &self,
903         token: arena::Box<'alloc, Token>,
904     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
905         let loc = token.loc;
906         self.alloc_with(|| ArrayExpression {
907             elements: self.new_vec_single(ArrayExpressionElement::Elision { loc }),
908             // This will be overwritten once the enclosing array gets parsed.
909             loc: SourceLocation::default(),
910         })
911     }
912 
913     // Elision : Elision `,`
elision_append( &self, mut array: arena::Box<'alloc, ArrayExpression<'alloc>>, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, ArrayExpression<'alloc>>914     pub fn elision_append(
915         &self,
916         mut array: arena::Box<'alloc, ArrayExpression<'alloc>>,
917         token: arena::Box<'alloc, Token>,
918     ) -> arena::Box<'alloc, ArrayExpression<'alloc>> {
919         let loc = token.loc;
920         self.push(&mut array.elements, ArrayExpressionElement::Elision { loc });
921         array
922     }
923 
924     // SpreadElement : `...` AssignmentExpression
spread_element( &self, expr: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>925     pub fn spread_element(
926         &self,
927         expr: arena::Box<'alloc, Expression<'alloc>>,
928     ) -> arena::Box<'alloc, Expression<'alloc>> {
929         expr
930     }
931 
932     // ObjectLiteral : `{` `}`
object_literal_empty( &self, open_token: arena::Box<'alloc, Token>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>933     pub fn object_literal_empty(
934         &self,
935         open_token: arena::Box<'alloc, Token>,
936         close_token: arena::Box<'alloc, Token>,
937     ) -> arena::Box<'alloc, Expression<'alloc>> {
938         self.alloc_with(|| {
939             Expression::ObjectExpression(ObjectExpression {
940                 properties: self.new_vec(),
941                 loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
942             })
943         })
944     }
945 
946     // ObjectLiteral : `{` PropertyDefinitionList `}`
947     // ObjectLiteral : `{` PropertyDefinitionList `,` `}`
object_literal( &self, open_token: arena::Box<'alloc, Token>, mut object: arena::Box<'alloc, ObjectExpression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>948     pub fn object_literal(
949         &self,
950         open_token: arena::Box<'alloc, Token>,
951         mut object: arena::Box<'alloc, ObjectExpression<'alloc>>,
952         close_token: arena::Box<'alloc, Token>,
953     ) -> arena::Box<'alloc, Expression<'alloc>> {
954         object.loc.set_range(open_token.loc, close_token.loc);
955         self.alloc_with(|| Expression::ObjectExpression(object.unbox()))
956     }
957 
958     // PropertyDefinitionList : PropertyDefinition
property_definition_list_single( &self, property: arena::Box<'alloc, ObjectProperty<'alloc>>, ) -> arena::Box<'alloc, ObjectExpression<'alloc>>959     pub fn property_definition_list_single(
960         &self,
961         property: arena::Box<'alloc, ObjectProperty<'alloc>>,
962     ) -> arena::Box<'alloc, ObjectExpression<'alloc>> {
963         self.alloc_with(|| ObjectExpression {
964             properties: self.new_vec_single(property),
965             // This will be overwritten once the enclosing object gets parsed.
966             loc: SourceLocation::default(),
967         })
968     }
969 
970     // PropertyDefinitionList : PropertyDefinitionList `,` PropertyDefinition
property_definition_list_append( &self, mut object: arena::Box<'alloc, ObjectExpression<'alloc>>, property: arena::Box<'alloc, ObjectProperty<'alloc>>, ) -> arena::Box<'alloc, ObjectExpression<'alloc>>971     pub fn property_definition_list_append(
972         &self,
973         mut object: arena::Box<'alloc, ObjectExpression<'alloc>>,
974         property: arena::Box<'alloc, ObjectProperty<'alloc>>,
975     ) -> arena::Box<'alloc, ObjectExpression<'alloc>> {
976         self.push(&mut object.properties, property);
977         object
978     }
979 
980     // PropertyDefinition : IdentifierReference
shorthand_property( &self, name: arena::Box<'alloc, Identifier>, ) -> arena::Box<'alloc, ObjectProperty<'alloc>>981     pub fn shorthand_property(
982         &self,
983         name: arena::Box<'alloc, Identifier>,
984     ) -> arena::Box<'alloc, ObjectProperty<'alloc>> {
985         let loc = name.loc;
986         self.alloc_with(|| {
987             ObjectProperty::ShorthandProperty(ShorthandProperty {
988                 name: IdentifierExpression {
989                     name: name.unbox(),
990                     loc,
991                 },
992                 loc,
993             })
994         })
995     }
996 
997     // PropertyDefinition : PropertyName `:` AssignmentExpression
property_definition( &self, name: arena::Box<'alloc, PropertyName<'alloc>>, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ObjectProperty<'alloc>>998     pub fn property_definition(
999         &self,
1000         name: arena::Box<'alloc, PropertyName<'alloc>>,
1001         expression: arena::Box<'alloc, Expression<'alloc>>,
1002     ) -> arena::Box<'alloc, ObjectProperty<'alloc>> {
1003         let name_loc = name.get_loc();
1004         let expression_loc = expression.get_loc();
1005         self.alloc_with(|| {
1006             ObjectProperty::NamedObjectProperty(NamedObjectProperty::DataProperty(DataProperty {
1007                 property_name: name.unbox(),
1008                 expression,
1009                 loc: SourceLocation::from_parts(name_loc, expression_loc),
1010             }))
1011         })
1012     }
1013 
1014     // PropertyDefinition : MethodDefinition
property_definition_method( &self, method: arena::Box<'alloc, MethodDefinition<'alloc>>, ) -> arena::Box<'alloc, ObjectProperty<'alloc>>1015     pub fn property_definition_method(
1016         &self,
1017         method: arena::Box<'alloc, MethodDefinition<'alloc>>,
1018     ) -> arena::Box<'alloc, ObjectProperty<'alloc>> {
1019         self.alloc_with(|| {
1020             ObjectProperty::NamedObjectProperty(NamedObjectProperty::MethodDefinition(
1021                 method.unbox(),
1022             ))
1023         })
1024     }
1025 
1026     // PropertyDefinition : `...` AssignmentExpression
property_definition_spread( &self, spread: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ObjectProperty<'alloc>>1027     pub fn property_definition_spread(
1028         &self,
1029         spread: arena::Box<'alloc, Expression<'alloc>>,
1030     ) -> arena::Box<'alloc, ObjectProperty<'alloc>> {
1031         self.alloc_with(|| ObjectProperty::SpreadProperty(spread))
1032     }
1033 
1034     // LiteralPropertyName : IdentifierName
property_name_identifier( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>>1035     pub fn property_name_identifier(
1036         &self,
1037         token: arena::Box<'alloc, Token>,
1038     ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>> {
1039         let value = token.value.as_atom();
1040         if value == CommonSourceAtomSetIndices::__proto__() {
1041             return Err(ParseError::NotImplemented("__proto__ as property name").into());
1042         }
1043 
1044         let loc = token.loc;
1045         Ok(self.alloc_with(|| PropertyName::StaticPropertyName(StaticPropertyName { value, loc })))
1046     }
1047 
1048     // LiteralPropertyName : StringLiteral
property_name_string( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>>1049     pub fn property_name_string(
1050         &self,
1051         token: arena::Box<'alloc, Token>,
1052     ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>> {
1053         let value = token.value.as_atom();
1054         if value == CommonSourceAtomSetIndices::__proto__() {
1055             return Err(ParseError::NotImplemented("__proto__ as property name").into());
1056         }
1057 
1058         let loc = token.loc;
1059         Ok(self.alloc_with(|| PropertyName::StaticPropertyName(StaticPropertyName { value, loc })))
1060     }
1061 
1062     // LiteralPropertyName : NumericLiteral
property_name_numeric( &self, token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>>1063     pub fn property_name_numeric(
1064         &self,
1065         token: arena::Box<'alloc, Token>,
1066     ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>> {
1067         let loc = token.loc;
1068         let value = Self::numeric_literal_value(token);
1069         Ok(self
1070             .alloc_with(|| PropertyName::StaticNumericPropertyName(NumericLiteral { value, loc })))
1071     }
1072 
1073     // LiteralPropertyName : NumericLiteral
1074     //
1075     // where NumericLiteral is either:
1076     //   * DecimalBigIntegerLiteral
1077     //   * NonDecimalIntegerLiteralBigIntLiteralSuffix
property_name_bigint( &self, _token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>>1078     pub fn property_name_bigint(
1079         &self,
1080         _token: arena::Box<'alloc, Token>,
1081     ) -> Result<'alloc, arena::Box<'alloc, PropertyName<'alloc>>> {
1082         Err(ParseError::NotImplemented("BigInt").into())
1083     }
1084 
1085     // ComputedPropertyName : `[` AssignmentExpression `]`
computed_property_name( &self, open_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, PropertyName<'alloc>>1086     pub fn computed_property_name(
1087         &self,
1088         open_token: arena::Box<'alloc, Token>,
1089         expression: arena::Box<'alloc, Expression<'alloc>>,
1090         close_token: arena::Box<'alloc, Token>,
1091     ) -> arena::Box<'alloc, PropertyName<'alloc>> {
1092         self.alloc_with(|| {
1093             PropertyName::ComputedPropertyName(ComputedPropertyName {
1094                 expression,
1095                 loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
1096             })
1097         })
1098     }
1099 
1100     // CoverInitializedName : IdentifierReference Initializer
cover_initialized_name( &self, _name: arena::Box<'alloc, Identifier>, _initializer: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>>1101     pub fn cover_initialized_name(
1102         &self,
1103         _name: arena::Box<'alloc, Identifier>,
1104         _initializer: arena::Box<'alloc, Expression<'alloc>>,
1105     ) -> Result<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>> {
1106         // Awkward. This needs to be stored somehow until we reach an enclosing
1107         // context where it can be reinterpreted as a default value in an
1108         // object destructuring assignment pattern.
1109         Err(ParseError::NotImplemented("default initializers in object patterns").into())
1110     }
1111 
1112     // TemplateLiteral : NoSubstitutionTemplate
template_literal( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, TemplateExpression<'alloc>>1113     pub fn template_literal(
1114         &self,
1115         token: arena::Box<'alloc, Token>,
1116     ) -> arena::Box<'alloc, TemplateExpression<'alloc>> {
1117         let loc = token.loc;
1118         self.alloc_with(|| TemplateExpression {
1119             tag: None,
1120             elements: self.new_vec_single(TemplateExpressionElement::TemplateElement(
1121                 TemplateElement {
1122                     raw_value: token.value.as_atom(),
1123                     loc,
1124                 },
1125             )),
1126             loc,
1127         })
1128     }
1129 
1130     // SubstitutionTemplate : TemplateHead Expression TemplateSpans
substitution_template( &self, _head: arena::Box<'alloc, Token>, _expression: arena::Box<'alloc, Expression<'alloc>>, _spans: arena::Box<'alloc, Void>, ) -> Result<'alloc, arena::Box<'alloc, TemplateExpression<'alloc>>>1131     pub fn substitution_template(
1132         &self,
1133         _head: arena::Box<'alloc, Token>,
1134         _expression: arena::Box<'alloc, Expression<'alloc>>,
1135         _spans: arena::Box<'alloc, Void>,
1136     ) -> Result<'alloc, arena::Box<'alloc, TemplateExpression<'alloc>>> {
1137         Err(ParseError::NotImplemented("template strings").into())
1138     }
1139 
1140     // TemplateSpans : TemplateTail
1141     // TemplateSpans : TemplateMiddleList TemplateTail
template_spans( &self, _middle_list: Option<arena::Box<'alloc, Void>>, _tail: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>1142     pub fn template_spans(
1143         &self,
1144         _middle_list: Option<arena::Box<'alloc, Void>>,
1145         _tail: arena::Box<'alloc, Token>,
1146     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
1147         Err(ParseError::NotImplemented("template strings").into())
1148     }
1149 
1150     // TemplateMiddleList : TemplateMiddle Expression
template_middle_list_single( &self, _middle: arena::Box<'alloc, Token>, _expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>1151     pub fn template_middle_list_single(
1152         &self,
1153         _middle: arena::Box<'alloc, Token>,
1154         _expression: arena::Box<'alloc, Expression<'alloc>>,
1155     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
1156         Err(ParseError::NotImplemented("template strings").into())
1157     }
1158 
1159     // TemplateMiddleList : TemplateMiddleList TemplateMiddle Expression
template_middle_list_append( &self, _middle_list: arena::Box<'alloc, Void>, _middle: arena::Box<'alloc, Token>, _expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>1160     pub fn template_middle_list_append(
1161         &self,
1162         _middle_list: arena::Box<'alloc, Void>,
1163         _middle: arena::Box<'alloc, Token>,
1164         _expression: arena::Box<'alloc, Expression<'alloc>>,
1165     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
1166         Err(ParseError::NotImplemented("template strings").into())
1167     }
1168 
1169     // MemberExpression : MemberExpression `[` Expression `]`
1170     // CallExpression : CallExpression `[` Expression `]`
computed_member_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1171     pub fn computed_member_expr(
1172         &self,
1173         object: arena::Box<'alloc, Expression<'alloc>>,
1174         expression: arena::Box<'alloc, Expression<'alloc>>,
1175         close_token: arena::Box<'alloc, Token>,
1176     ) -> arena::Box<'alloc, Expression<'alloc>> {
1177         let object_loc = object.get_loc();
1178         self.alloc_with(|| {
1179             Expression::MemberExpression(MemberExpression::ComputedMemberExpression(
1180                 ComputedMemberExpression {
1181                     object: ExpressionOrSuper::Expression(object),
1182                     expression,
1183                     loc: SourceLocation::from_parts(object_loc, close_token.loc),
1184                 },
1185             ))
1186         })
1187     }
1188 
1189     // OptionalExpression : MemberExpression OptionalChain
1190     // OptionalExpression : CallExpression OptionalChain
1191     // OptionalExpression : OptionalExpression OptionalChain
optional_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, tail: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1192     pub fn optional_expr(
1193         &self,
1194         object: arena::Box<'alloc, Expression<'alloc>>,
1195         tail: arena::Box<'alloc, Expression<'alloc>>,
1196     ) -> arena::Box<'alloc, Expression<'alloc>> {
1197         let object_loc = object.get_loc();
1198         let expression_loc = tail.get_loc();
1199         self.alloc_with(|| Expression::OptionalExpression {
1200             object: ExpressionOrSuper::Expression(object),
1201             tail,
1202             loc: SourceLocation::from_parts(object_loc, expression_loc),
1203         })
1204     }
1205 
1206     // OptionalChain : `?.` `[` Expression `]`
optional_computed_member_expr_tail( &self, start_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1207     pub fn optional_computed_member_expr_tail(
1208         &self,
1209         start_token: arena::Box<'alloc, Token>,
1210         expression: arena::Box<'alloc, Expression<'alloc>>,
1211         close_token: arena::Box<'alloc, Token>,
1212     ) -> arena::Box<'alloc, Expression<'alloc>> {
1213         self.alloc_with(|| {
1214             Expression::OptionalChain(OptionalChain::ComputedMemberExpressionTail {
1215                 expression,
1216                 loc: SourceLocation::from_parts(start_token.loc, close_token.loc),
1217             })
1218         })
1219     }
1220 
1221     // OptionalChain : `?.` Expression
optional_static_member_expr_tail( &self, start_token: arena::Box<'alloc, Token>, identifier_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1222     pub fn optional_static_member_expr_tail(
1223         &self,
1224         start_token: arena::Box<'alloc, Token>,
1225         identifier_token: arena::Box<'alloc, Token>,
1226     ) -> arena::Box<'alloc, Expression<'alloc>> {
1227         let identifier_token_loc = identifier_token.loc;
1228         self.alloc_with(|| {
1229             Expression::OptionalChain(OptionalChain::StaticMemberExpressionTail {
1230                 property: self.identifier_name(identifier_token),
1231                 loc: SourceLocation::from_parts(start_token.loc, identifier_token_loc),
1232             })
1233         })
1234     }
1235 
1236     // OptionalChain : `?.` PrivateIdentifier
optional_private_field_member_expr_tail( &self, start_token: arena::Box<'alloc, Token>, private_identifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1237     pub fn optional_private_field_member_expr_tail(
1238         &self,
1239         start_token: arena::Box<'alloc, Token>,
1240         private_identifier: arena::Box<'alloc, Token>,
1241     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1242         let private_identifier_loc = private_identifier.loc;
1243         let field = self.private_identifier(private_identifier)?;
1244         Ok(self.alloc_with(|| {
1245             Expression::OptionalChain(OptionalChain::PrivateFieldExpressionTail {
1246                 field,
1247                 loc: SourceLocation::from_parts(start_token.loc, private_identifier_loc),
1248             })
1249         }))
1250     }
1251 
1252     // OptionalChain : `?.` Arguments
optional_call_expr_tail( &self, start_token: arena::Box<'alloc, Token>, arguments: arena::Box<'alloc, Arguments<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1253     pub fn optional_call_expr_tail(
1254         &self,
1255         start_token: arena::Box<'alloc, Token>,
1256         arguments: arena::Box<'alloc, Arguments<'alloc>>,
1257     ) -> arena::Box<'alloc, Expression<'alloc>> {
1258         let arguments_loc = arguments.loc;
1259         self.alloc_with(|| {
1260             Expression::OptionalChain(OptionalChain::CallExpressionTail {
1261                 arguments: arguments.unbox(),
1262                 loc: SourceLocation::from_parts(start_token.loc, arguments_loc),
1263             })
1264         })
1265     }
1266 
1267     // OptionalChain : `?.` TemplateLiteral
error_optional_chain_with_template( &self, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1268     pub fn error_optional_chain_with_template(
1269         &self,
1270     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1271         Err(ParseError::IllegalCharacter('`').into())
1272     }
1273 
1274     // OptionalChain : OptionalChain `[` Expression `]`
optional_computed_member_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1275     pub fn optional_computed_member_expr(
1276         &self,
1277         object: arena::Box<'alloc, Expression<'alloc>>,
1278         expression: arena::Box<'alloc, Expression<'alloc>>,
1279         close_token: arena::Box<'alloc, Token>,
1280     ) -> arena::Box<'alloc, Expression<'alloc>> {
1281         let object_loc = object.get_loc();
1282         self.alloc_with(|| {
1283             Expression::OptionalChain(OptionalChain::ComputedMemberExpression(
1284                 ComputedMemberExpression {
1285                     object: ExpressionOrSuper::Expression(object),
1286                     expression,
1287                     loc: SourceLocation::from_parts(object_loc, close_token.loc),
1288                 },
1289             ))
1290         })
1291     }
1292 
1293     // OptionalChain : OptionalChain `.` Expression
optional_static_member_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, identifier_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1294     pub fn optional_static_member_expr(
1295         &self,
1296         object: arena::Box<'alloc, Expression<'alloc>>,
1297         identifier_token: arena::Box<'alloc, Token>,
1298     ) -> arena::Box<'alloc, Expression<'alloc>> {
1299         let object_loc = object.get_loc();
1300         let identifier_token_loc = identifier_token.loc;
1301         self.alloc_with(|| {
1302             Expression::OptionalChain(OptionalChain::StaticMemberExpression(
1303                 StaticMemberExpression {
1304                     object: ExpressionOrSuper::Expression(object),
1305                     property: self.identifier_name(identifier_token),
1306                     loc: SourceLocation::from_parts(object_loc, identifier_token_loc),
1307                 },
1308             ))
1309         })
1310     }
1311 
1312     // OptionalChain : OptionalChain `.` PrivateIdentifier
optional_private_field_member_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, private_identifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1313     pub fn optional_private_field_member_expr(
1314         &self,
1315         object: arena::Box<'alloc, Expression<'alloc>>,
1316         private_identifier: arena::Box<'alloc, Token>,
1317     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1318         let object_loc = object.get_loc();
1319         let private_identifier_loc = private_identifier.loc;
1320         let field = self.private_identifier(private_identifier)?;
1321         Ok(self.alloc_with(|| {
1322             Expression::OptionalChain(OptionalChain::PrivateFieldExpression(
1323                 PrivateFieldExpression {
1324                     object: ExpressionOrSuper::Expression(object),
1325                     field,
1326                     loc: SourceLocation::from_parts(object_loc, private_identifier_loc),
1327                 },
1328             ))
1329         }))
1330     }
1331 
1332     // OptionalChain : OptionalChain Arguments
optional_call_expr( &self, callee: arena::Box<'alloc, Expression<'alloc>>, arguments: arena::Box<'alloc, Arguments<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1333     pub fn optional_call_expr(
1334         &self,
1335         callee: arena::Box<'alloc, Expression<'alloc>>,
1336         arguments: arena::Box<'alloc, Arguments<'alloc>>,
1337     ) -> arena::Box<'alloc, Expression<'alloc>> {
1338         let callee_loc = callee.get_loc();
1339         let arguments_loc = arguments.loc;
1340         self.alloc_with(|| {
1341             Expression::OptionalChain(OptionalChain::CallExpression(CallExpression {
1342                 callee: ExpressionOrSuper::Expression(callee),
1343                 arguments: arguments.unbox(),
1344                 loc: SourceLocation::from_parts(callee_loc, arguments_loc),
1345             }))
1346         })
1347     }
1348 
identifier(&self, token: arena::Box<'alloc, Token>) -> Identifier1349     fn identifier(&self, token: arena::Box<'alloc, Token>) -> Identifier {
1350         Identifier {
1351             value: token.value.as_atom(),
1352             loc: token.loc,
1353         }
1354     }
1355 
identifier_name(&self, token: arena::Box<'alloc, Token>) -> IdentifierName1356     fn identifier_name(&self, token: arena::Box<'alloc, Token>) -> IdentifierName {
1357         IdentifierName {
1358             value: token.value.as_atom(),
1359             loc: token.loc,
1360         }
1361     }
1362 
private_identifier( &self, _token: arena::Box<'alloc, Token>, ) -> Result<'alloc, PrivateIdentifier>1363     fn private_identifier(
1364         &self,
1365         _token: arena::Box<'alloc, Token>,
1366     ) -> Result<'alloc, PrivateIdentifier> {
1367         Err(ParseError::NotImplemented(
1368             "private fields depends on shell switch, that is not supported",
1369         )
1370         .into())
1371         /*
1372                 PrivateIdentifier {
1373                     value: token.value.as_atom(),
1374                     loc: token.loc,
1375                 }
1376         */
1377     }
1378 
1379     // MemberExpression : MemberExpression `.` IdentifierName
1380     // CallExpression : CallExpression `.` IdentifierName
static_member_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, identifier_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1381     pub fn static_member_expr(
1382         &self,
1383         object: arena::Box<'alloc, Expression<'alloc>>,
1384         identifier_token: arena::Box<'alloc, Token>,
1385     ) -> arena::Box<'alloc, Expression<'alloc>> {
1386         let object_loc = object.get_loc();
1387         let identifier_token_loc = identifier_token.loc;
1388         self.alloc_with(|| {
1389             Expression::MemberExpression(MemberExpression::StaticMemberExpression(
1390                 StaticMemberExpression {
1391                     object: ExpressionOrSuper::Expression(object),
1392                     property: self.identifier_name(identifier_token),
1393                     loc: SourceLocation::from_parts(object_loc, identifier_token_loc),
1394                 },
1395             ))
1396         })
1397     }
1398 
1399     // MemberExpression : MemberExpression TemplateLiteral
1400     // CallExpression : CallExpression TemplateLiteral
tagged_template_expr( &self, tag: arena::Box<'alloc, Expression<'alloc>>, mut template_literal: arena::Box<'alloc, TemplateExpression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1401     pub fn tagged_template_expr(
1402         &self,
1403         tag: arena::Box<'alloc, Expression<'alloc>>,
1404         mut template_literal: arena::Box<'alloc, TemplateExpression<'alloc>>,
1405     ) -> arena::Box<'alloc, Expression<'alloc>> {
1406         template_literal.tag = Some(tag);
1407         self.alloc_with(|| Expression::TemplateExpression(template_literal.unbox()))
1408     }
1409 
1410     // MemberExpression : `new` MemberExpression Arguments
new_expr_with_arguments( &self, new_token: arena::Box<'alloc, Token>, callee: arena::Box<'alloc, Expression<'alloc>>, arguments: arena::Box<'alloc, Arguments<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1411     pub fn new_expr_with_arguments(
1412         &self,
1413         new_token: arena::Box<'alloc, Token>,
1414         callee: arena::Box<'alloc, Expression<'alloc>>,
1415         arguments: arena::Box<'alloc, Arguments<'alloc>>,
1416     ) -> arena::Box<'alloc, Expression<'alloc>> {
1417         let arguments_loc = arguments.loc;
1418         self.alloc_with(|| Expression::NewExpression {
1419             callee,
1420             arguments: arguments.unbox(),
1421             loc: SourceLocation::from_parts(new_token.loc, arguments_loc),
1422         })
1423     }
1424 
1425     // MemberExpression : MemberExpression `.` PrivateIdentifier
private_field_expr( &self, object: arena::Box<'alloc, Expression<'alloc>>, private_identifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1426     pub fn private_field_expr(
1427         &self,
1428         object: arena::Box<'alloc, Expression<'alloc>>,
1429         private_identifier: arena::Box<'alloc, Token>,
1430     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1431         let object_loc = object.get_loc();
1432         let field_loc = private_identifier.loc;
1433         let field = self.private_identifier(private_identifier)?;
1434         Ok(self.alloc_with(|| {
1435             Expression::MemberExpression(MemberExpression::PrivateFieldExpression(
1436                 PrivateFieldExpression {
1437                     object: ExpressionOrSuper::Expression(object),
1438                     field,
1439                     loc: SourceLocation::from_parts(object_loc, field_loc),
1440                 },
1441             ))
1442         }))
1443     }
1444 
1445     // SuperProperty : `super` `[` Expression `]`
super_property_computed( &self, super_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1446     pub fn super_property_computed(
1447         &self,
1448         super_token: arena::Box<'alloc, Token>,
1449         expression: arena::Box<'alloc, Expression<'alloc>>,
1450         close_token: arena::Box<'alloc, Token>,
1451     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1452         self.check_super()?;
1453 
1454         let super_loc = super_token.loc;
1455         Ok(self.alloc_with(|| {
1456             Expression::MemberExpression(MemberExpression::ComputedMemberExpression(
1457                 ComputedMemberExpression {
1458                     object: ExpressionOrSuper::Super { loc: super_loc },
1459                     expression: expression,
1460                     loc: SourceLocation::from_parts(super_loc, close_token.loc),
1461                 },
1462             ))
1463         }))
1464     }
1465 
1466     // SuperProperty : `super` `.` IdentifierName
super_property_static( &self, super_token: arena::Box<'alloc, Token>, identifier_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1467     pub fn super_property_static(
1468         &self,
1469         super_token: arena::Box<'alloc, Token>,
1470         identifier_token: arena::Box<'alloc, Token>,
1471     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1472         self.check_super()?;
1473 
1474         let super_loc = super_token.loc;
1475         let identifier_loc = identifier_token.loc;
1476         Ok(self.alloc_with(|| {
1477             Expression::MemberExpression(MemberExpression::StaticMemberExpression(
1478                 StaticMemberExpression {
1479                     object: ExpressionOrSuper::Super { loc: super_loc },
1480                     property: self.identifier_name(identifier_token),
1481                     loc: SourceLocation::from_parts(super_loc, identifier_loc),
1482                 },
1483             ))
1484         }))
1485     }
1486 
1487     // NewTarget : `new` `.` `target`
new_target_expr( &self, new_token: arena::Box<'alloc, Token>, target_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1488     pub fn new_target_expr(
1489         &self,
1490         new_token: arena::Box<'alloc, Token>,
1491         target_token: arena::Box<'alloc, Token>,
1492     ) -> arena::Box<'alloc, Expression<'alloc>> {
1493         return self.alloc_with(|| Expression::NewTargetExpression {
1494             loc: SourceLocation::from_parts(new_token.loc, target_token.loc),
1495         });
1496     }
1497 
1498     // NewExpression : `new` NewExpression
new_expr_without_arguments( &self, new_token: arena::Box<'alloc, Token>, callee: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1499     pub fn new_expr_without_arguments(
1500         &self,
1501         new_token: arena::Box<'alloc, Token>,
1502         callee: arena::Box<'alloc, Expression<'alloc>>,
1503     ) -> arena::Box<'alloc, Expression<'alloc>> {
1504         let callee_loc = callee.get_loc();
1505         self.alloc_with(|| Expression::NewExpression {
1506             callee,
1507             arguments: Arguments {
1508                 args: self.new_vec(),
1509                 loc: SourceLocation::new(callee_loc.end, callee_loc.end),
1510             },
1511             loc: SourceLocation::from_parts(new_token.loc, callee_loc),
1512         })
1513     }
1514 
1515     // CallExpression : CallExpression Arguments
1516     // CoverCallExpressionAndAsyncArrowHead : MemberExpression Arguments
1517     // CallMemberExpression : MemberExpression Arguments
call_expr( &self, callee: arena::Box<'alloc, Expression<'alloc>>, arguments: arena::Box<'alloc, Arguments<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1518     pub fn call_expr(
1519         &self,
1520         callee: arena::Box<'alloc, Expression<'alloc>>,
1521         arguments: arena::Box<'alloc, Arguments<'alloc>>,
1522     ) -> arena::Box<'alloc, Expression<'alloc>> {
1523         let callee_loc = callee.get_loc();
1524         let arguments_loc = arguments.loc;
1525         self.alloc_with(|| {
1526             Expression::CallExpression(CallExpression {
1527                 callee: ExpressionOrSuper::Expression(callee),
1528                 arguments: arguments.unbox(),
1529                 loc: SourceLocation::from_parts(callee_loc, arguments_loc),
1530             })
1531         })
1532     }
1533 
1534     // SuperCall : `super` Arguments
super_call( &self, super_token: arena::Box<'alloc, Token>, arguments: arena::Box<'alloc, Arguments<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1535     pub fn super_call(
1536         &self,
1537         super_token: arena::Box<'alloc, Token>,
1538         arguments: arena::Box<'alloc, Arguments<'alloc>>,
1539     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1540         self.check_super()?;
1541 
1542         let super_loc = super_token.loc;
1543         let arguments_loc = arguments.loc;
1544         Ok(self.alloc_with(|| {
1545             Expression::CallExpression(CallExpression {
1546                 callee: ExpressionOrSuper::Super { loc: super_loc },
1547                 arguments: arguments.unbox(),
1548                 loc: SourceLocation::from_parts(super_loc, arguments_loc),
1549             })
1550         }))
1551     }
1552 
1553     // ImportCall : `import` `(` AssignmentExpression `)`
import_call( &self, import_token: arena::Box<'alloc, Token>, argument: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Expression<'alloc>>1554     pub fn import_call(
1555         &self,
1556         import_token: arena::Box<'alloc, Token>,
1557         argument: arena::Box<'alloc, Expression<'alloc>>,
1558         close_token: arena::Box<'alloc, Token>,
1559     ) -> arena::Box<'alloc, Expression<'alloc>> {
1560         self.alloc_with(|| Expression::ImportCallExpression {
1561             argument,
1562             loc: SourceLocation::from_parts(import_token.loc, close_token.loc),
1563         })
1564     }
1565 
1566     // Arguments : `(` `)`
arguments_empty( &self, open_token: arena::Box<'alloc, Token>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Arguments<'alloc>>1567     pub fn arguments_empty(
1568         &self,
1569         open_token: arena::Box<'alloc, Token>,
1570         close_token: arena::Box<'alloc, Token>,
1571     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1572         self.alloc_with(|| Arguments {
1573             args: self.new_vec(),
1574             loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
1575         })
1576     }
1577 
arguments_single( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Arguments<'alloc>>1578     pub fn arguments_single(
1579         &self,
1580         expression: arena::Box<'alloc, Expression<'alloc>>,
1581     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1582         self.alloc_with(|| Arguments {
1583             args: self.new_vec_single(Argument::Expression(expression)),
1584             // This will be overwritten once the enclosing arguments gets
1585             // parsed.
1586             loc: SourceLocation::default(),
1587         })
1588     }
1589 
arguments_spread_single( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Arguments<'alloc>>1590     pub fn arguments_spread_single(
1591         &self,
1592         expression: arena::Box<'alloc, Expression<'alloc>>,
1593     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1594         self.alloc_with(|| Arguments {
1595             args: self.new_vec_single(Argument::SpreadElement(expression)),
1596             // This will be overwritten once the enclosing arguments gets
1597             // parsed.
1598             loc: SourceLocation::default(),
1599         })
1600     }
1601 
arguments( &self, open_token: arena::Box<'alloc, Token>, mut arguments: arena::Box<'alloc, Arguments<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Arguments<'alloc>>1602     pub fn arguments(
1603         &self,
1604         open_token: arena::Box<'alloc, Token>,
1605         mut arguments: arena::Box<'alloc, Arguments<'alloc>>,
1606         close_token: arena::Box<'alloc, Token>,
1607     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1608         arguments.loc.set_range(open_token.loc, close_token.loc);
1609         arguments
1610     }
1611 
1612     // ArgumentList : AssignmentExpression
1613     // ArgumentList : ArgumentList `,` AssignmentExpression
arguments_append( &self, mut arguments: arena::Box<'alloc, Arguments<'alloc>>, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Arguments<'alloc>>1614     pub fn arguments_append(
1615         &self,
1616         mut arguments: arena::Box<'alloc, Arguments<'alloc>>,
1617         expression: arena::Box<'alloc, Expression<'alloc>>,
1618     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1619         self.push(&mut arguments.args, Argument::Expression(expression));
1620         arguments
1621     }
1622 
1623     // ArgumentList : `...` AssignmentExpression
1624     // ArgumentList : ArgumentList `,` `...` AssignmentExpression
arguments_append_spread( &self, mut arguments: arena::Box<'alloc, Arguments<'alloc>>, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Arguments<'alloc>>1625     pub fn arguments_append_spread(
1626         &self,
1627         mut arguments: arena::Box<'alloc, Arguments<'alloc>>,
1628         expression: arena::Box<'alloc, Expression<'alloc>>,
1629     ) -> arena::Box<'alloc, Arguments<'alloc>> {
1630         self.push(&mut arguments.args, Argument::SpreadElement(expression));
1631         arguments
1632     }
1633 
1634     // UpdateExpression : LeftHandSideExpression `++`
post_increment_expr( &self, operand: arena::Box<'alloc, Expression<'alloc>>, operator_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1635     pub fn post_increment_expr(
1636         &self,
1637         operand: arena::Box<'alloc, Expression<'alloc>>,
1638         operator_token: arena::Box<'alloc, Token>,
1639     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1640         let operand = self.expression_to_simple_assignment_target(operand)?;
1641         let operand_loc = operand.get_loc();
1642         Ok(self.alloc_with(|| Expression::UpdateExpression {
1643             is_prefix: false,
1644             operator: UpdateOperator::Increment {
1645                 loc: operator_token.loc,
1646             },
1647             operand,
1648             loc: SourceLocation::from_parts(operand_loc, operator_token.loc),
1649         }))
1650     }
1651 
1652     // UpdateExpression : LeftHandSideExpression `--`
post_decrement_expr( &self, operand: arena::Box<'alloc, Expression<'alloc>>, operator_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1653     pub fn post_decrement_expr(
1654         &self,
1655         operand: arena::Box<'alloc, Expression<'alloc>>,
1656         operator_token: arena::Box<'alloc, Token>,
1657     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1658         let operand = self.expression_to_simple_assignment_target(operand)?;
1659         let operand_loc = operand.get_loc();
1660         Ok(self.alloc_with(|| Expression::UpdateExpression {
1661             is_prefix: false,
1662             operator: UpdateOperator::Decrement {
1663                 loc: operator_token.loc,
1664             },
1665             operand,
1666             loc: SourceLocation::from_parts(operand_loc, operator_token.loc),
1667         }))
1668     }
1669 
1670     // UpdateExpression : `++` UnaryExpression
pre_increment_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1671     pub fn pre_increment_expr(
1672         &self,
1673         operator_token: arena::Box<'alloc, Token>,
1674         operand: arena::Box<'alloc, Expression<'alloc>>,
1675     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1676         let operand = self.expression_to_simple_assignment_target(operand)?;
1677         let operand_loc = operand.get_loc();
1678         Ok(self.alloc_with(|| Expression::UpdateExpression {
1679             is_prefix: true,
1680             operator: UpdateOperator::Increment {
1681                 loc: operator_token.loc,
1682             },
1683             operand,
1684             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1685         }))
1686     }
1687 
1688     // UpdateExpression : `--` UnaryExpression
pre_decrement_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>1689     pub fn pre_decrement_expr(
1690         &self,
1691         operator_token: arena::Box<'alloc, Token>,
1692         operand: arena::Box<'alloc, Expression<'alloc>>,
1693     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
1694         let operand = self.expression_to_simple_assignment_target(operand)?;
1695         let operand_loc = operand.get_loc();
1696         Ok(self.alloc_with(|| Expression::UpdateExpression {
1697             is_prefix: true,
1698             operator: UpdateOperator::Decrement {
1699                 loc: operator_token.loc,
1700             },
1701             operand,
1702             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1703         }))
1704     }
1705 
1706     // UnaryExpression : `delete` UnaryExpression
delete_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1707     pub fn delete_expr(
1708         &self,
1709         operator_token: arena::Box<'alloc, Token>,
1710         operand: arena::Box<'alloc, Expression<'alloc>>,
1711     ) -> arena::Box<'alloc, Expression<'alloc>> {
1712         let operand_loc = operand.get_loc();
1713         self.alloc_with(|| Expression::UnaryExpression {
1714             operator: UnaryOperator::Delete {
1715                 loc: operator_token.loc,
1716             },
1717             operand,
1718             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1719         })
1720     }
1721 
1722     // UnaryExpression : `void` UnaryExpression
void_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1723     pub fn void_expr(
1724         &self,
1725         operator_token: arena::Box<'alloc, Token>,
1726         operand: arena::Box<'alloc, Expression<'alloc>>,
1727     ) -> arena::Box<'alloc, Expression<'alloc>> {
1728         let operand_loc = operand.get_loc();
1729         self.alloc_with(|| Expression::UnaryExpression {
1730             operator: UnaryOperator::Void {
1731                 loc: operator_token.loc,
1732             },
1733             operand,
1734             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1735         })
1736     }
1737 
1738     // UnaryExpression : `typeof` UnaryExpression
typeof_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1739     pub fn typeof_expr(
1740         &self,
1741         operator_token: arena::Box<'alloc, Token>,
1742         operand: arena::Box<'alloc, Expression<'alloc>>,
1743     ) -> arena::Box<'alloc, Expression<'alloc>> {
1744         let operand_loc = operand.get_loc();
1745         self.alloc_with(|| Expression::UnaryExpression {
1746             operator: UnaryOperator::Typeof {
1747                 loc: operator_token.loc,
1748             },
1749             operand,
1750             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1751         })
1752     }
1753 
1754     // UnaryExpression : `+` UnaryExpression
unary_plus_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1755     pub fn unary_plus_expr(
1756         &self,
1757         operator_token: arena::Box<'alloc, Token>,
1758         operand: arena::Box<'alloc, Expression<'alloc>>,
1759     ) -> arena::Box<'alloc, Expression<'alloc>> {
1760         let operand_loc = operand.get_loc();
1761         self.alloc_with(|| Expression::UnaryExpression {
1762             operator: UnaryOperator::Plus {
1763                 loc: operator_token.loc,
1764             },
1765             operand,
1766             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1767         })
1768     }
1769 
1770     // UnaryExpression : `-` UnaryExpression
unary_minus_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1771     pub fn unary_minus_expr(
1772         &self,
1773         operator_token: arena::Box<'alloc, Token>,
1774         operand: arena::Box<'alloc, Expression<'alloc>>,
1775     ) -> arena::Box<'alloc, Expression<'alloc>> {
1776         let operand_loc = operand.get_loc();
1777         self.alloc_with(|| Expression::UnaryExpression {
1778             operator: UnaryOperator::Minus {
1779                 loc: operator_token.loc,
1780             },
1781             operand,
1782             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1783         })
1784     }
1785 
1786     // UnaryExpression : `~` UnaryExpression
bitwise_not_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1787     pub fn bitwise_not_expr(
1788         &self,
1789         operator_token: arena::Box<'alloc, Token>,
1790         operand: arena::Box<'alloc, Expression<'alloc>>,
1791     ) -> arena::Box<'alloc, Expression<'alloc>> {
1792         let operand_loc = operand.get_loc();
1793         self.alloc_with(|| Expression::UnaryExpression {
1794             operator: UnaryOperator::BitwiseNot {
1795                 loc: operator_token.loc,
1796             },
1797             operand,
1798             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1799         })
1800     }
1801 
1802     // UnaryExpression : `!` UnaryExpression
logical_not_expr( &self, operator_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1803     pub fn logical_not_expr(
1804         &self,
1805         operator_token: arena::Box<'alloc, Token>,
1806         operand: arena::Box<'alloc, Expression<'alloc>>,
1807     ) -> arena::Box<'alloc, Expression<'alloc>> {
1808         let operand_loc = operand.get_loc();
1809         self.alloc_with(|| Expression::UnaryExpression {
1810             operator: UnaryOperator::LogicalNot {
1811                 loc: operator_token.loc,
1812             },
1813             operand,
1814             loc: SourceLocation::from_parts(operator_token.loc, operand_loc),
1815         })
1816     }
1817 
equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1818     pub fn equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1819         BinaryOperator::Equals { loc: token.loc }
1820     }
not_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1821     pub fn not_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1822         BinaryOperator::NotEquals { loc: token.loc }
1823     }
strict_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1824     pub fn strict_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1825         BinaryOperator::StrictEquals { loc: token.loc }
1826     }
strict_not_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1827     pub fn strict_not_equals_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1828         BinaryOperator::StrictNotEquals { loc: token.loc }
1829     }
less_than_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1830     pub fn less_than_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1831         BinaryOperator::LessThan { loc: token.loc }
1832     }
less_than_or_equal_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1833     pub fn less_than_or_equal_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1834         BinaryOperator::LessThanOrEqual { loc: token.loc }
1835     }
greater_than_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1836     pub fn greater_than_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1837         BinaryOperator::GreaterThan { loc: token.loc }
1838     }
greater_than_or_equal_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1839     pub fn greater_than_or_equal_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1840         BinaryOperator::GreaterThanOrEqual { loc: token.loc }
1841     }
in_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1842     pub fn in_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1843         BinaryOperator::In { loc: token.loc }
1844     }
instanceof_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1845     pub fn instanceof_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1846         BinaryOperator::Instanceof { loc: token.loc }
1847     }
left_shift_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1848     pub fn left_shift_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1849         BinaryOperator::LeftShift { loc: token.loc }
1850     }
right_shift_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1851     pub fn right_shift_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1852         BinaryOperator::RightShift { loc: token.loc }
1853     }
right_shift_ext_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1854     pub fn right_shift_ext_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1855         BinaryOperator::RightShiftExt { loc: token.loc }
1856     }
add_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1857     pub fn add_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1858         BinaryOperator::Add { loc: token.loc }
1859     }
sub_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1860     pub fn sub_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1861         BinaryOperator::Sub { loc: token.loc }
1862     }
mul_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1863     pub fn mul_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1864         BinaryOperator::Mul { loc: token.loc }
1865     }
div_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1866     pub fn div_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1867         BinaryOperator::Div { loc: token.loc }
1868     }
mod_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1869     pub fn mod_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1870         BinaryOperator::Mod { loc: token.loc }
1871     }
pow_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1872     pub fn pow_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1873         BinaryOperator::Pow { loc: token.loc }
1874     }
comma_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1875     pub fn comma_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1876         BinaryOperator::Comma { loc: token.loc }
1877     }
coalesce_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1878     pub fn coalesce_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1879         BinaryOperator::Coalesce { loc: token.loc }
1880     }
logical_or_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1881     pub fn logical_or_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1882         BinaryOperator::LogicalOr { loc: token.loc }
1883     }
logical_and_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1884     pub fn logical_and_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1885         BinaryOperator::LogicalAnd { loc: token.loc }
1886     }
bitwise_or_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1887     pub fn bitwise_or_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1888         BinaryOperator::BitwiseOr { loc: token.loc }
1889     }
bitwise_xor_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1890     pub fn bitwise_xor_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1891         BinaryOperator::BitwiseXor { loc: token.loc }
1892     }
bitwise_and_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator1893     pub fn bitwise_and_op(&self, token: arena::Box<'alloc, Token>) -> BinaryOperator {
1894         BinaryOperator::BitwiseAnd { loc: token.loc }
1895     }
1896 
1897     // Due to limitations of the current parser generator,
1898     // MultiplicativeOperators and CompoundAssignmentOperators currently get
1899     // boxed.
box_op(&self, op: BinaryOperator) -> arena::Box<'alloc, BinaryOperator>1900     pub fn box_op(&self, op: BinaryOperator) -> arena::Box<'alloc, BinaryOperator> {
1901         self.alloc_with(|| op)
1902     }
1903 
1904     // MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
multiplicative_expr( &self, left: arena::Box<'alloc, Expression<'alloc>>, operator: arena::Box<'alloc, BinaryOperator>, right: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1905     pub fn multiplicative_expr(
1906         &self,
1907         left: arena::Box<'alloc, Expression<'alloc>>,
1908         operator: arena::Box<'alloc, BinaryOperator>,
1909         right: arena::Box<'alloc, Expression<'alloc>>,
1910     ) -> arena::Box<'alloc, Expression<'alloc>> {
1911         self.binary_expr(operator.unbox(), left, right)
1912     }
1913 
1914     // ExponentiationExpression : UpdateExpression `**` ExponentiationExpression
1915     // AdditiveExpression : AdditiveExpression `+` MultiplicativeExpression
1916     // AdditiveExpression : AdditiveExpression `-` MultiplicativeExpression
1917     // ShiftExpression : ShiftExpression `<<` AdditiveExpression
1918     // ShiftExpression : ShiftExpression `>>` AdditiveExpression
1919     // ShiftExpression : ShiftExpression `>>>` AdditiveExpression
1920     // RelationalExpression : RelationalExpression `<` ShiftExpression
1921     // RelationalExpression : RelationalExpression `>` ShiftExpression
1922     // RelationalExpression : RelationalExpression `<=` ShiftExpression
1923     // RelationalExpression : RelationalExpression `>=` ShiftExpression
1924     // RelationalExpression : RelationalExpression `instanceof` ShiftExpression
1925     // RelationalExpression : [+In] RelationalExpression `in` ShiftExpression
1926     // EqualityExpression : EqualityExpression `==` RelationalExpression
1927     // EqualityExpression : EqualityExpression `!=` RelationalExpression
1928     // EqualityExpression : EqualityExpression `===` RelationalExpression
1929     // EqualityExpression : EqualityExpression `!==` RelationalExpression
1930     // BitwiseANDExpression : BitwiseANDExpression `&` EqualityExpression
1931     // BitwiseXORExpression : BitwiseXORExpression `^` BitwiseANDExpression
1932     // BitwiseORExpression : BitwiseORExpression `|` BitwiseXORExpression
1933     // LogicalANDExpression : LogicalANDExpression `&&` BitwiseORExpression
1934     // LogicalORExpression : LogicalORExpression `||` LogicalANDExpression
binary_expr( &self, operator: BinaryOperator, left: arena::Box<'alloc, Expression<'alloc>>, right: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1935     pub fn binary_expr(
1936         &self,
1937         operator: BinaryOperator,
1938         left: arena::Box<'alloc, Expression<'alloc>>,
1939         right: arena::Box<'alloc, Expression<'alloc>>,
1940     ) -> arena::Box<'alloc, Expression<'alloc>> {
1941         let left_loc = left.get_loc();
1942         let right_loc = right.get_loc();
1943         self.alloc_with(|| Expression::BinaryExpression {
1944             operator,
1945             left,
1946             right,
1947             loc: SourceLocation::from_parts(left_loc, right_loc),
1948         })
1949     }
1950 
1951     // ConditionalExpression : LogicalORExpression `?` AssignmentExpression `:` AssignmentExpression
conditional_expr( &self, test: arena::Box<'alloc, Expression<'alloc>>, consequent: arena::Box<'alloc, Expression<'alloc>>, alternate: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>1952     pub fn conditional_expr(
1953         &self,
1954         test: arena::Box<'alloc, Expression<'alloc>>,
1955         consequent: arena::Box<'alloc, Expression<'alloc>>,
1956         alternate: arena::Box<'alloc, Expression<'alloc>>,
1957     ) -> arena::Box<'alloc, Expression<'alloc>> {
1958         let test_loc = test.get_loc();
1959         let alternate_loc = alternate.get_loc();
1960         self.alloc_with(|| Expression::ConditionalExpression {
1961             test,
1962             consequent,
1963             alternate,
1964             loc: SourceLocation::from_parts(test_loc, alternate_loc),
1965         })
1966     }
1967 
1968     /// Refine an *ArrayLiteral* into an *ArrayAssignmentPattern*.
array_expression_to_array_assignment_target( &self, mut elements: arena::Vec<'alloc, ArrayExpressionElement<'alloc>>, loc: SourceLocation, ) -> Result<'alloc, ArrayAssignmentTarget<'alloc>>1969     fn array_expression_to_array_assignment_target(
1970         &self,
1971         mut elements: arena::Vec<'alloc, ArrayExpressionElement<'alloc>>,
1972         loc: SourceLocation,
1973     ) -> Result<'alloc, ArrayAssignmentTarget<'alloc>> {
1974         let spread = self.pop_trailing_spread_element(&mut elements);
1975         let elements =
1976             self.collect_vec_from_results(elements.into_iter().map(|element| match element {
1977                 ArrayExpressionElement::SpreadElement(_) => {
1978                     Err(ParseError::NotImplemented("rest destructuring in array pattern").into())
1979                 }
1980                 ArrayExpressionElement::Expression(expression) => Ok(Some(
1981                     self.expression_to_assignment_target_maybe_default(expression)?,
1982                 )),
1983                 ArrayExpressionElement::Elision { .. } => Ok(None),
1984             }))?;
1985         let rest: Option<Result<'alloc, arena::Box<'alloc, AssignmentTarget<'alloc>>>> =
1986             spread.map(|expr| Ok(self.alloc(self.expression_to_assignment_target(expr)?)));
1987         let rest = rest.transpose()?;
1988         Ok(ArrayAssignmentTarget {
1989             elements,
1990             rest,
1991             loc,
1992         })
1993     }
1994 
object_property_to_assignment_target_property( &self, property: arena::Box<'alloc, ObjectProperty<'alloc>>, ) -> Result<'alloc, AssignmentTargetProperty<'alloc>>1995     fn object_property_to_assignment_target_property(
1996         &self,
1997         property: arena::Box<'alloc, ObjectProperty<'alloc>>,
1998     ) -> Result<'alloc, AssignmentTargetProperty<'alloc>> {
1999         Ok(match property.unbox() {
2000             ObjectProperty::NamedObjectProperty(NamedObjectProperty::MethodDefinition(_)) => {
2001                 return Err(ParseError::ObjectPatternWithMethod.into())
2002             }
2003 
2004             ObjectProperty::NamedObjectProperty(NamedObjectProperty::DataProperty(
2005                 DataProperty {
2006                     property_name,
2007                     expression,
2008                     loc,
2009                 },
2010             )) => AssignmentTargetProperty::AssignmentTargetPropertyProperty(
2011                 AssignmentTargetPropertyProperty {
2012                     name: property_name,
2013                     binding: self.expression_to_assignment_target_maybe_default(expression)?,
2014                     loc,
2015                 },
2016             ),
2017 
2018             ObjectProperty::ShorthandProperty(ShorthandProperty {
2019                 name: IdentifierExpression { name, loc },
2020                 ..
2021             }) => {
2022                 // TODO - support CoverInitializedName
2023                 AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(
2024                     AssignmentTargetPropertyIdentifier {
2025                         binding: AssignmentTargetIdentifier { name, loc },
2026                         init: None,
2027                         loc,
2028                     },
2029                 )
2030             }
2031 
2032             ObjectProperty::SpreadProperty(_expression) => {
2033                 return Err(ParseError::ObjectPatternWithNonFinalRest.into())
2034             }
2035         })
2036     }
2037 
2038     // Refine an *ObjectLiteral* into an *ObjectAssignmentPattern*.
object_expression_to_object_assignment_target( &self, mut properties: arena::Vec<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>>, loc: SourceLocation, ) -> Result<'alloc, ObjectAssignmentTarget<'alloc>>2039     fn object_expression_to_object_assignment_target(
2040         &self,
2041         mut properties: arena::Vec<'alloc, arena::Box<'alloc, ObjectProperty<'alloc>>>,
2042         loc: SourceLocation,
2043     ) -> Result<'alloc, ObjectAssignmentTarget<'alloc>> {
2044         let spread = self.pop_trailing_spread_property(&mut properties);
2045         let properties = self.collect_vec_from_results(
2046             properties
2047                 .into_iter()
2048                 .map(|p| self.object_property_to_assignment_target_property(p)),
2049         )?;
2050         let rest: Option<Result<'alloc, arena::Box<'alloc, AssignmentTarget<'alloc>>>> =
2051             spread.map(|expr| Ok(self.alloc(self.expression_to_assignment_target(expr)?)));
2052         let rest = rest.transpose()?;
2053         Ok(ObjectAssignmentTarget {
2054             properties,
2055             rest,
2056             loc,
2057         })
2058     }
2059 
expression_to_assignment_target_maybe_default( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, AssignmentTargetMaybeDefault<'alloc>>2060     fn expression_to_assignment_target_maybe_default(
2061         &self,
2062         expression: arena::Box<'alloc, Expression<'alloc>>,
2063     ) -> Result<'alloc, AssignmentTargetMaybeDefault<'alloc>> {
2064         Ok(match expression.unbox() {
2065             Expression::AssignmentExpression {
2066                 binding,
2067                 expression,
2068                 loc,
2069             } => AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(
2070                 AssignmentTargetWithDefault {
2071                     binding,
2072                     init: expression,
2073                     loc,
2074                 },
2075             ),
2076 
2077             other => AssignmentTargetMaybeDefault::AssignmentTarget(
2078                 self.expression_to_assignment_target(self.alloc_with(|| other))?,
2079             ),
2080         })
2081     }
2082 
expression_to_assignment_target( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, AssignmentTarget<'alloc>>2083     fn expression_to_assignment_target(
2084         &self,
2085         expression: arena::Box<'alloc, Expression<'alloc>>,
2086     ) -> Result<'alloc, AssignmentTarget<'alloc>> {
2087         Ok(match expression.unbox() {
2088             Expression::ArrayExpression(ArrayExpression { elements, loc }) => {
2089                 AssignmentTarget::AssignmentTargetPattern(
2090                     AssignmentTargetPattern::ArrayAssignmentTarget(
2091                         self.array_expression_to_array_assignment_target(elements, loc)?,
2092                     ),
2093                 )
2094             }
2095 
2096             Expression::ObjectExpression(ObjectExpression { properties, loc }) => {
2097                 AssignmentTarget::AssignmentTargetPattern(
2098                     AssignmentTargetPattern::ObjectAssignmentTarget(
2099                         self.object_expression_to_object_assignment_target(properties, loc)?,
2100                     ),
2101                 )
2102             }
2103 
2104             other => AssignmentTarget::SimpleAssignmentTarget(
2105                 self.expression_to_simple_assignment_target(self.alloc_with(|| other))?,
2106             ),
2107         })
2108     }
2109 
expression_to_simple_assignment_target( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, SimpleAssignmentTarget<'alloc>>2110     fn expression_to_simple_assignment_target(
2111         &self,
2112         expression: arena::Box<'alloc, Expression<'alloc>>,
2113     ) -> Result<'alloc, SimpleAssignmentTarget<'alloc>> {
2114         Ok(match expression.unbox() {
2115             // Static Semantics: AssignmentTargetType
2116             // https://tc39.es/ecma262/#sec-identifiers-static-semantics-assignmenttargettype
2117             Expression::IdentifierExpression(IdentifierExpression { name, loc }) => {
2118                 // IdentifierReference : Identifier
2119                 //
2120                 // 1. If this IdentifierReference is contained in strict mode
2121                 //    code and StringValue of Identifier is "eval" or
2122                 //    "arguments", return invalid.
2123                 if name.value == CommonSourceAtomSetIndices::arguments()
2124                     || name.value == CommonSourceAtomSetIndices::eval()
2125                 {
2126                     if self.is_strict()? {
2127                         return Err(ParseError::InvalidAssignmentTarget.into());
2128                     }
2129                 }
2130 
2131                 // 2. Return simple.
2132                 //
2133                 // IdentifierReference : yield
2134                 //
2135                 // 1. Return simple.
2136                 //
2137                 // IdentifierReference : await
2138                 //
2139                 // 1. Return simple.
2140                 SimpleAssignmentTarget::AssignmentTargetIdentifier(AssignmentTargetIdentifier {
2141                     name,
2142                     loc,
2143                 })
2144             }
2145 
2146             // Static Semantics: AssignmentTargetType
2147             // https://tc39.es/ecma262/#sec-static-semantics-static-semantics-assignmenttargettype
2148             //
2149             // MemberExpression :
2150             //   MemberExpression [ Expression ]
2151             //   MemberExpression . IdentifierName
2152             //   SuperProperty
2153             //
2154             // 1. Return simple.
2155             Expression::MemberExpression(MemberExpression::StaticMemberExpression(
2156                 StaticMemberExpression {
2157                     object,
2158                     property,
2159                     loc,
2160                 },
2161             )) => SimpleAssignmentTarget::MemberAssignmentTarget(
2162                 MemberAssignmentTarget::StaticMemberAssignmentTarget(
2163                     StaticMemberAssignmentTarget {
2164                         object,
2165                         property,
2166                         loc,
2167                     },
2168                 ),
2169             ),
2170             Expression::MemberExpression(MemberExpression::ComputedMemberExpression(
2171                 ComputedMemberExpression {
2172                     object,
2173                     expression,
2174                     loc,
2175                 },
2176             )) => SimpleAssignmentTarget::MemberAssignmentTarget(
2177                 MemberAssignmentTarget::ComputedMemberAssignmentTarget(
2178                     ComputedMemberAssignmentTarget {
2179                         object,
2180                         expression,
2181                         loc,
2182                     },
2183                 ),
2184             ),
2185             Expression::MemberExpression(MemberExpression::PrivateFieldExpression(
2186                 PrivateFieldExpression { object, field, loc },
2187             )) => SimpleAssignmentTarget::MemberAssignmentTarget(
2188                 MemberAssignmentTarget::PrivateFieldAssignmentTarget(
2189                     PrivateFieldAssignmentTarget { object, field, loc },
2190                 ),
2191             ),
2192 
2193             // Static Semantics: AssignmentTargetType
2194             // https://tc39.es/ecma262/#sec-static-semantics-static-semantics-assignmenttargettype
2195             //
2196             // CallExpression :
2197             //   CallExpression [ Expression ]
2198             //   CallExpression . IdentifierName
2199             //
2200             // 1. Return simple.
2201             Expression::CallExpression(CallExpression { .. }) => {
2202                 return Err(ParseError::NotImplemented(
2203                     "Assignment to CallExpression is allowed for non-strict mode.",
2204                 )
2205                 .into());
2206             }
2207 
2208             _ => {
2209                 return Err(ParseError::InvalidAssignmentTarget.into());
2210             }
2211         })
2212     }
2213 
2214     // AssignmentExpression : LeftHandSideExpression `=` AssignmentExpression
assignment_expr( &self, left_hand_side: arena::Box<'alloc, Expression<'alloc>>, value: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>2215     pub fn assignment_expr(
2216         &self,
2217         left_hand_side: arena::Box<'alloc, Expression<'alloc>>,
2218         value: arena::Box<'alloc, Expression<'alloc>>,
2219     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
2220         let target = self.expression_to_assignment_target(left_hand_side)?;
2221         let target_loc = target.get_loc();
2222         let value_loc = value.get_loc();
2223         Ok(self.alloc_with(|| Expression::AssignmentExpression {
2224             binding: target,
2225             expression: value,
2226             loc: SourceLocation::from_parts(target_loc, value_loc),
2227         }))
2228     }
2229 
add_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2230     pub fn add_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2231         CompoundAssignmentOperator::Add { loc: token.loc }
2232     }
sub_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2233     pub fn sub_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2234         CompoundAssignmentOperator::Sub { loc: token.loc }
2235     }
mul_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2236     pub fn mul_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2237         CompoundAssignmentOperator::Mul { loc: token.loc }
2238     }
div_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2239     pub fn div_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2240         CompoundAssignmentOperator::Div { loc: token.loc }
2241     }
mod_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2242     pub fn mod_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2243         CompoundAssignmentOperator::Mod { loc: token.loc }
2244     }
pow_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator2245     pub fn pow_assign_op(&self, token: arena::Box<'alloc, Token>) -> CompoundAssignmentOperator {
2246         CompoundAssignmentOperator::Pow { loc: token.loc }
2247     }
left_shift_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2248     pub fn left_shift_assign_op(
2249         &self,
2250         token: arena::Box<'alloc, Token>,
2251     ) -> CompoundAssignmentOperator {
2252         CompoundAssignmentOperator::LeftShift { loc: token.loc }
2253     }
right_shift_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2254     pub fn right_shift_assign_op(
2255         &self,
2256         token: arena::Box<'alloc, Token>,
2257     ) -> CompoundAssignmentOperator {
2258         CompoundAssignmentOperator::RightShift { loc: token.loc }
2259     }
right_shift_ext_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2260     pub fn right_shift_ext_assign_op(
2261         &self,
2262         token: arena::Box<'alloc, Token>,
2263     ) -> CompoundAssignmentOperator {
2264         CompoundAssignmentOperator::RightShiftExt { loc: token.loc }
2265     }
bitwise_or_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2266     pub fn bitwise_or_assign_op(
2267         &self,
2268         token: arena::Box<'alloc, Token>,
2269     ) -> CompoundAssignmentOperator {
2270         CompoundAssignmentOperator::Or { loc: token.loc }
2271     }
bitwise_xor_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2272     pub fn bitwise_xor_assign_op(
2273         &self,
2274         token: arena::Box<'alloc, Token>,
2275     ) -> CompoundAssignmentOperator {
2276         CompoundAssignmentOperator::Xor { loc: token.loc }
2277     }
bitwise_and_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2278     pub fn bitwise_and_assign_op(
2279         &self,
2280         token: arena::Box<'alloc, Token>,
2281     ) -> CompoundAssignmentOperator {
2282         CompoundAssignmentOperator::And { loc: token.loc }
2283     }
2284 
logical_or_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2285     pub fn logical_or_assign_op(
2286         &self,
2287         token: arena::Box<'alloc, Token>,
2288     ) -> CompoundAssignmentOperator {
2289         CompoundAssignmentOperator::LogicalOr { loc: token.loc }
2290     }
logical_and_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2291     pub fn logical_and_assign_op(
2292         &self,
2293         token: arena::Box<'alloc, Token>,
2294     ) -> CompoundAssignmentOperator {
2295         CompoundAssignmentOperator::LogicalAnd { loc: token.loc }
2296     }
coalesce_assign_op( &self, token: arena::Box<'alloc, Token>, ) -> CompoundAssignmentOperator2297     pub fn coalesce_assign_op(
2298         &self,
2299         token: arena::Box<'alloc, Token>,
2300     ) -> CompoundAssignmentOperator {
2301         CompoundAssignmentOperator::Coalesce { loc: token.loc }
2302     }
2303 
box_assign_op( &self, op: CompoundAssignmentOperator, ) -> arena::Box<'alloc, CompoundAssignmentOperator>2304     pub fn box_assign_op(
2305         &self,
2306         op: CompoundAssignmentOperator,
2307     ) -> arena::Box<'alloc, CompoundAssignmentOperator> {
2308         self.alloc_with(|| op)
2309     }
2310 
2311     // AssignmentExpression : LeftHandSideExpression AssignmentOperator AssignmentExpression
2312     // AssignmentExpression : LeftHandSideExpression LogicalAssignmentOperator AssignmentExpression
compound_assignment_expr( &self, left_hand_side: arena::Box<'alloc, Expression<'alloc>>, operator: arena::Box<'alloc, CompoundAssignmentOperator>, value: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>2313     pub fn compound_assignment_expr(
2314         &self,
2315         left_hand_side: arena::Box<'alloc, Expression<'alloc>>,
2316         operator: arena::Box<'alloc, CompoundAssignmentOperator>,
2317         value: arena::Box<'alloc, Expression<'alloc>>,
2318     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
2319         let target = self.expression_to_simple_assignment_target(left_hand_side)?;
2320         let target_loc = target.get_loc();
2321         let value_loc = value.get_loc();
2322         Ok(
2323             self.alloc_with(|| Expression::CompoundAssignmentExpression {
2324                 operator: operator.unbox(),
2325                 binding: target,
2326                 expression: value,
2327                 loc: SourceLocation::from_parts(target_loc, value_loc),
2328             }),
2329         )
2330     }
2331 
2332     // BlockStatement : Block
block_statement( &self, block: arena::Box<'alloc, Block<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>2333     pub fn block_statement(
2334         &self,
2335         block: arena::Box<'alloc, Block<'alloc>>,
2336     ) -> arena::Box<'alloc, Statement<'alloc>> {
2337         let loc = block.loc;
2338         self.alloc_with(|| Statement::BlockStatement {
2339             block: block.unbox(),
2340             loc,
2341         })
2342     }
2343 
2344     // Block : `{` StatementList? `}`
block( &mut self, open_token: arena::Box<'alloc, Token>, statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Block<'alloc>>>2345     pub fn block(
2346         &mut self,
2347         open_token: arena::Box<'alloc, Token>,
2348         statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
2349         close_token: arena::Box<'alloc, Token>,
2350     ) -> Result<'alloc, arena::Box<'alloc, Block<'alloc>>> {
2351         self.check_block_bindings(open_token.loc.start)?;
2352 
2353         Ok(self.alloc_with(|| Block {
2354             statements: match statements {
2355                 Some(statements) => statements.unbox(),
2356                 None => self.new_vec(),
2357             },
2358             declarations: None,
2359             loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
2360         }))
2361     }
2362 
2363     // Block : `{` StatementList? `}`
2364     // for Catch
catch_block( &mut self, open_token: arena::Box<'alloc, Token>, statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Block<'alloc>>2365     pub fn catch_block(
2366         &mut self,
2367         open_token: arena::Box<'alloc, Token>,
2368         statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
2369         close_token: arena::Box<'alloc, Token>,
2370     ) -> arena::Box<'alloc, Block<'alloc>> {
2371         // Early Error handling is done in Catch.
2372 
2373         self.alloc_with(|| Block {
2374             statements: match statements {
2375                 Some(statements) => statements.unbox(),
2376                 None => self.new_vec(),
2377             },
2378             declarations: None,
2379             loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
2380         })
2381     }
2382 
2383     // StatementList : StatementListItem
statement_list_single( &self, statement: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>2384     pub fn statement_list_single(
2385         &self,
2386         statement: arena::Box<'alloc, Statement<'alloc>>,
2387     ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>> {
2388         self.alloc_with(|| self.new_vec_single(statement.unbox()))
2389     }
2390 
2391     // StatementList : StatementList StatementListItem
statement_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>, statement: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>2392     pub fn statement_list_append(
2393         &self,
2394         mut list: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>,
2395         statement: arena::Box<'alloc, Statement<'alloc>>,
2396     ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>> {
2397         self.push(&mut list, statement.unbox());
2398         list
2399     }
2400 
2401     // LexicalDeclaration : LetOrConst BindingList `;`
lexical_declaration( &mut self, kind: arena::Box<'alloc, VariableDeclarationKind>, declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2402     pub fn lexical_declaration(
2403         &mut self,
2404         kind: arena::Box<'alloc, VariableDeclarationKind>,
2405         declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>,
2406     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2407         let binding_kind = match &*kind {
2408             VariableDeclarationKind::Let { .. } => BindingKind::Let,
2409             VariableDeclarationKind::Const { .. } => BindingKind::Const,
2410             _ => panic!("unexpected VariableDeclarationKind"),
2411         };
2412 
2413         self.context_metadata
2414             .mark_binding_kind(kind.get_loc().start, None, binding_kind);
2415 
2416         // 13.3.1.1 Static Semantics: Early Errors
2417         if let VariableDeclarationKind::Const { .. } = *kind {
2418             for v in declarators.iter() {
2419                 if v.init == None {
2420                     return Err(ParseError::NotImplemented(
2421                         "Missing initializer in a lexical binding.",
2422                     )
2423                     .into());
2424                 }
2425             }
2426         }
2427 
2428         let kind_loc = kind.get_loc();
2429         let declarator_loc = declarators
2430             .last()
2431             .expect("There should be at least one declarator")
2432             .get_loc();
2433         Ok(self.alloc_with(|| {
2434             Statement::VariableDeclarationStatement(VariableDeclaration {
2435                 kind: kind.unbox(),
2436                 declarators: declarators.unbox(),
2437                 loc: SourceLocation::from_parts(kind_loc, declarator_loc),
2438             })
2439         }))
2440     }
2441 
2442     // ForLexicalDeclaration : LetOrConst BindingList `;`
for_lexical_declaration( &mut self, kind: arena::Box<'alloc, VariableDeclarationKind>, declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>, ) -> Result<'alloc, arena::Box<'alloc, VariableDeclarationOrExpression<'alloc>>>2443     pub fn for_lexical_declaration(
2444         &mut self,
2445         kind: arena::Box<'alloc, VariableDeclarationKind>,
2446         declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>,
2447     ) -> Result<'alloc, arena::Box<'alloc, VariableDeclarationOrExpression<'alloc>>> {
2448         let binding_kind = match &*kind {
2449             VariableDeclarationKind::Let { .. } => BindingKind::Let,
2450             VariableDeclarationKind::Const { .. } => BindingKind::Const,
2451             _ => panic!("unexpected VariableDeclarationKind"),
2452         };
2453         self.context_metadata
2454             .mark_binding_kind(kind.get_loc().start, None, binding_kind);
2455 
2456         // 13.3.1.1 Static Semantics: Early Errors
2457         if let VariableDeclarationKind::Const { .. } = *kind {
2458             for v in declarators.iter() {
2459                 if v.init == None {
2460                     return Err(ParseError::NotImplemented(
2461                         "Missing initializer in a lexical binding.",
2462                     )
2463                     .into());
2464                 }
2465             }
2466         }
2467 
2468         let kind_loc = kind.get_loc();
2469         let declarator_loc = declarators
2470             .last()
2471             .expect("There should be at least one declarator")
2472             .get_loc();
2473         Ok(self.alloc_with(|| {
2474             VariableDeclarationOrExpression::VariableDeclaration(VariableDeclaration {
2475                 kind: kind.unbox(),
2476                 declarators: declarators.unbox(),
2477                 loc: SourceLocation::from_parts(kind_loc, declarator_loc),
2478             })
2479         }))
2480     }
2481 
2482     // LetOrConst : `let`
let_kind( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, VariableDeclarationKind>2483     pub fn let_kind(
2484         &self,
2485         token: arena::Box<'alloc, Token>,
2486     ) -> arena::Box<'alloc, VariableDeclarationKind> {
2487         self.alloc_with(|| VariableDeclarationKind::Let { loc: token.loc })
2488     }
2489 
2490     // LetOrConst : `const`
const_kind( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, VariableDeclarationKind>2491     pub fn const_kind(
2492         &self,
2493         token: arena::Box<'alloc, Token>,
2494     ) -> arena::Box<'alloc, VariableDeclarationKind> {
2495         self.alloc_with(|| VariableDeclarationKind::Const { loc: token.loc })
2496     }
2497 
2498     // VariableStatement : `var` VariableDeclarationList `;`
variable_statement( &mut self, var_token: arena::Box<'alloc, Token>, declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>, ) -> arena::Box<'alloc, Statement<'alloc>>2499     pub fn variable_statement(
2500         &mut self,
2501         var_token: arena::Box<'alloc, Token>,
2502         declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>,
2503     ) -> arena::Box<'alloc, Statement<'alloc>> {
2504         let var_loc = var_token.loc;
2505         let declarator_loc = declarators
2506             .last()
2507             .expect("There should be at least one declarator")
2508             .get_loc();
2509 
2510         self.context_metadata
2511             .mark_binding_kind(var_loc.start, None, BindingKind::Var);
2512 
2513         self.alloc_with(|| {
2514             Statement::VariableDeclarationStatement(VariableDeclaration {
2515                 kind: VariableDeclarationKind::Var { loc: var_loc },
2516                 declarators: declarators.unbox(),
2517                 loc: SourceLocation::from_parts(var_loc, declarator_loc),
2518             })
2519         })
2520     }
2521 
2522     // VariableDeclarationList : VariableDeclaration
2523     // BindingList : LexicalBinding
variable_declaration_list_single( &self, decl: arena::Box<'alloc, VariableDeclarator<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>2524     pub fn variable_declaration_list_single(
2525         &self,
2526         decl: arena::Box<'alloc, VariableDeclarator<'alloc>>,
2527     ) -> arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>> {
2528         self.alloc_with(|| self.new_vec_single(decl.unbox()))
2529     }
2530 
2531     // VariableDeclarationList : VariableDeclarationList `,` VariableDeclaration
2532     // BindingList : BindingList `,` LexicalBinding
variable_declaration_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>, decl: arena::Box<'alloc, VariableDeclarator<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>2533     pub fn variable_declaration_list_append(
2534         &self,
2535         mut list: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>,
2536         decl: arena::Box<'alloc, VariableDeclarator<'alloc>>,
2537     ) -> arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>> {
2538         self.push(&mut list, decl.unbox());
2539         list
2540     }
2541 
2542     // VariableDeclaration : BindingIdentifier Initializer?
2543     // VariableDeclaration : BindingPattern Initializer
variable_declaration( &self, binding: arena::Box<'alloc, Binding<'alloc>>, init: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, VariableDeclarator<'alloc>>2544     pub fn variable_declaration(
2545         &self,
2546         binding: arena::Box<'alloc, Binding<'alloc>>,
2547         init: Option<arena::Box<'alloc, Expression<'alloc>>>,
2548     ) -> arena::Box<'alloc, VariableDeclarator<'alloc>> {
2549         let binding_loc = binding.get_loc();
2550         let loc = match init {
2551             Some(ref init) => SourceLocation::from_parts(binding_loc, init.get_loc()),
2552             None => binding_loc,
2553         };
2554         self.alloc_with(|| VariableDeclarator {
2555             binding: binding.unbox(),
2556             init,
2557             loc,
2558         })
2559     }
2560 
2561     // ObjectBindingPattern : `{` `}`
2562     // ObjectBindingPattern : `{` BindingRestProperty `}`
2563     // ObjectBindingPattern : `{` BindingPropertyList `}`
2564     // ObjectBindingPattern : `{` BindingPropertyList `,` BindingRestProperty? `}`
object_binding_pattern( &self, open_token: arena::Box<'alloc, Token>, properties: arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>, rest: Option<arena::Box<'alloc, BindingIdentifier>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Binding<'alloc>>2565     pub fn object_binding_pattern(
2566         &self,
2567         open_token: arena::Box<'alloc, Token>,
2568         properties: arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>,
2569         rest: Option<arena::Box<'alloc, BindingIdentifier>>,
2570         close_token: arena::Box<'alloc, Token>,
2571     ) -> arena::Box<'alloc, Binding<'alloc>> {
2572         self.alloc_with(|| {
2573             Binding::BindingPattern(BindingPattern::ObjectBinding(ObjectBinding {
2574                 properties: properties.unbox(),
2575                 rest,
2576                 loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
2577             }))
2578         })
2579     }
2580 
binding_element_list_empty( &self, ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>2581     pub fn binding_element_list_empty(
2582         &self,
2583     ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>> {
2584         self.alloc_with(|| self.new_vec())
2585     }
2586 
2587     // ArrayBindingPattern : `[` Elision? BindingRestElement? `]`
2588     // ArrayBindingPattern : `[` BindingElementList `]`
2589     // ArrayBindingPattern : `[` BindingElementList `,` Elision? BindingRestElement? `]`
array_binding_pattern( &self, open_token: arena::Box<'alloc, Token>, mut elements: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, rest: Option<arena::Box<'alloc, Binding<'alloc>>>, close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Binding<'alloc>>2590     pub fn array_binding_pattern(
2591         &self,
2592         open_token: arena::Box<'alloc, Token>,
2593         mut elements: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>,
2594         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
2595         rest: Option<arena::Box<'alloc, Binding<'alloc>>>,
2596         close_token: arena::Box<'alloc, Token>,
2597     ) -> arena::Box<'alloc, Binding<'alloc>> {
2598         if let Some(elision) = elision {
2599             for _ in 0..elision.elements.len() {
2600                 self.push(&mut elements, None);
2601             }
2602         }
2603 
2604         self.alloc_with(|| {
2605             Binding::BindingPattern(BindingPattern::ArrayBinding(ArrayBinding {
2606                 elements: elements.unbox(),
2607                 rest,
2608                 loc: SourceLocation::from_parts(open_token.loc, close_token.loc),
2609             }))
2610         })
2611     }
2612 
binding_property_list_empty( &self, ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>2613     pub fn binding_property_list_empty(
2614         &self,
2615     ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>> {
2616         self.alloc_with(|| self.new_vec())
2617     }
2618 
2619     // BindingPropertyList : BindingProperty
binding_property_list_single( &self, property: arena::Box<'alloc, BindingProperty<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>2620     pub fn binding_property_list_single(
2621         &self,
2622         property: arena::Box<'alloc, BindingProperty<'alloc>>,
2623     ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>> {
2624         self.alloc_with(|| self.new_vec_single(property.unbox()))
2625     }
2626 
2627     // BindingPropertyList : BindingPropertyList `,` BindingProperty
binding_property_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>, property: arena::Box<'alloc, BindingProperty<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>2628     pub fn binding_property_list_append(
2629         &self,
2630         mut list: arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>>,
2631         property: arena::Box<'alloc, BindingProperty<'alloc>>,
2632     ) -> arena::Box<'alloc, arena::Vec<'alloc, BindingProperty<'alloc>>> {
2633         self.push(&mut list, property.unbox());
2634         list
2635     }
2636 
2637     // BindingElementList : BindingElementList `,` BindingElisionElement
binding_element_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>, mut element: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>2638     pub fn binding_element_list_append(
2639         &self,
2640         mut list: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>,
2641         mut element: arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>,
2642     ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>> {
2643         self.append(&mut list, &mut element);
2644         list
2645     }
2646 
2647     // BindingElisionElement : Elision? BindingElement
binding_elision_element( &self, elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>, element: arena::Box<'alloc, Parameter<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>>2648     pub fn binding_elision_element(
2649         &self,
2650         elision: Option<arena::Box<'alloc, ArrayExpression<'alloc>>>,
2651         element: arena::Box<'alloc, Parameter<'alloc>>,
2652     ) -> arena::Box<'alloc, arena::Vec<'alloc, Option<Parameter<'alloc>>>> {
2653         let elision_count = elision.map(|v| v.elements.len()).unwrap_or(0);
2654         let mut result = self.alloc_with(|| self.new_vec());
2655         for _ in 0..elision_count {
2656             self.push(&mut result, None);
2657         }
2658         self.push(&mut result, Some(element.unbox()));
2659         result
2660     }
2661 
2662     // BindingProperty : SingleNameBinding
binding_property_shorthand( &self, binding: arena::Box<'alloc, Parameter<'alloc>>, ) -> arena::Box<'alloc, BindingProperty<'alloc>>2663     pub fn binding_property_shorthand(
2664         &self,
2665         binding: arena::Box<'alloc, Parameter<'alloc>>,
2666     ) -> arena::Box<'alloc, BindingProperty<'alloc>> {
2667         // Previous parsing interpreted this as a Parameter. We need to take
2668         // all the pieces out of that box and put them in a new box.
2669         let (binding, init) = match binding.unbox() {
2670             Parameter::Binding(binding) => (binding, None),
2671             Parameter::BindingWithDefault(BindingWithDefault { binding, init, .. }) => {
2672                 (binding, Some(init.unbox()))
2673             }
2674         };
2675 
2676         let binding = match binding {
2677             Binding::BindingIdentifier(bi) => bi,
2678             _ => {
2679                 // The grammar ensures that the parser always passes a valid
2680                 // argument to this method.
2681                 panic!("invalid argument: binding_property_shorthand requires a Binding::BindingIdentifier");
2682             }
2683         };
2684 
2685         let loc = binding.loc;
2686 
2687         self.alloc_with(|| {
2688             BindingProperty::BindingPropertyIdentifier(BindingPropertyIdentifier {
2689                 binding,
2690                 init: init.map(|x| self.alloc_with(|| x)),
2691                 loc,
2692             })
2693         })
2694     }
2695 
2696     // BindingProperty : PropertyName `:` BindingElement
binding_property( &self, name: arena::Box<'alloc, PropertyName<'alloc>>, binding: arena::Box<'alloc, Parameter<'alloc>>, ) -> arena::Box<'alloc, BindingProperty<'alloc>>2697     pub fn binding_property(
2698         &self,
2699         name: arena::Box<'alloc, PropertyName<'alloc>>,
2700         binding: arena::Box<'alloc, Parameter<'alloc>>,
2701     ) -> arena::Box<'alloc, BindingProperty<'alloc>> {
2702         let name_loc = name.get_loc();
2703         let binding_loc = binding.get_loc();
2704         self.alloc_with(|| {
2705             BindingProperty::BindingPropertyProperty(BindingPropertyProperty {
2706                 name: name.unbox(),
2707                 binding: binding.unbox(),
2708                 loc: SourceLocation::from_parts(name_loc, binding_loc),
2709             })
2710         })
2711     }
2712 
2713     // BindingElement : BindingPattern Initializer?
binding_element_pattern( &self, binding: arena::Box<'alloc, Binding<'alloc>>, init: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, Parameter<'alloc>>2714     pub fn binding_element_pattern(
2715         &self,
2716         binding: arena::Box<'alloc, Binding<'alloc>>,
2717         init: Option<arena::Box<'alloc, Expression<'alloc>>>,
2718     ) -> arena::Box<'alloc, Parameter<'alloc>> {
2719         self.alloc_with(|| match init {
2720             None => Parameter::Binding(binding.unbox()),
2721             Some(init) => {
2722                 let binding_loc = binding.get_loc();
2723                 let init_loc = init.get_loc();
2724                 Parameter::BindingWithDefault(BindingWithDefault {
2725                     binding: binding.unbox(),
2726                     init,
2727                     loc: SourceLocation::from_parts(binding_loc, init_loc),
2728                 })
2729             }
2730         })
2731     }
2732 
2733     // SingleNameBinding : BindingIdentifier Initializer?
single_name_binding( &self, name: arena::Box<'alloc, BindingIdentifier>, init: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, Parameter<'alloc>>2734     pub fn single_name_binding(
2735         &self,
2736         name: arena::Box<'alloc, BindingIdentifier>,
2737         init: Option<arena::Box<'alloc, Expression<'alloc>>>,
2738     ) -> arena::Box<'alloc, Parameter<'alloc>> {
2739         let binding = Binding::BindingIdentifier(name.unbox());
2740         self.alloc_with(|| match init {
2741             None => Parameter::Binding(binding),
2742             Some(init) => {
2743                 let binding_loc = binding.get_loc();
2744                 let init_loc = init.get_loc();
2745                 Parameter::BindingWithDefault(BindingWithDefault {
2746                     binding,
2747                     init,
2748                     loc: SourceLocation::from_parts(binding_loc, init_loc),
2749                 })
2750             }
2751         })
2752     }
2753 
2754     // BindingRestElement : `...` BindingIdentifier
binding_rest_element( &self, name: arena::Box<'alloc, BindingIdentifier>, ) -> arena::Box<'alloc, Binding<'alloc>>2755     pub fn binding_rest_element(
2756         &self,
2757         name: arena::Box<'alloc, BindingIdentifier>,
2758     ) -> arena::Box<'alloc, Binding<'alloc>> {
2759         self.alloc_with(|| Binding::BindingIdentifier(name.unbox()))
2760     }
2761 
2762     // EmptyStatement : `;`
empty_statement( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Statement<'alloc>>2763     pub fn empty_statement(
2764         &self,
2765         token: arena::Box<'alloc, Token>,
2766     ) -> arena::Box<'alloc, Statement<'alloc>> {
2767         self.alloc_with(|| Statement::EmptyStatement { loc: token.loc })
2768     }
2769 
2770     // ExpressionStatement : [lookahead not in {'{', 'function', 'async', 'class', 'let'}] Expression `;`
expression_statement( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>2771     pub fn expression_statement(
2772         &self,
2773         expression: arena::Box<'alloc, Expression<'alloc>>,
2774     ) -> arena::Box<'alloc, Statement<'alloc>> {
2775         self.alloc_with(|| Statement::ExpressionStatement(expression))
2776     }
2777 
2778     // IfStatement : `if` `(` Expression `)` Statement `else` Statement
2779     // IfStatement : `if` `(` Expression `)` Statement
if_statement( &self, if_token: arena::Box<'alloc, Token>, test: arena::Box<'alloc, Expression<'alloc>>, consequent: arena::Box<'alloc, Statement<'alloc>>, alternate: Option<arena::Box<'alloc, Statement<'alloc>>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2780     pub fn if_statement(
2781         &self,
2782         if_token: arena::Box<'alloc, Token>,
2783         test: arena::Box<'alloc, Expression<'alloc>>,
2784         consequent: arena::Box<'alloc, Statement<'alloc>>,
2785         alternate: Option<arena::Box<'alloc, Statement<'alloc>>>,
2786     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2787         self.check_single_statement(consequent.get_loc().start)?;
2788         if let Some(ref stmt) = alternate {
2789             self.check_single_statement(stmt.get_loc().start)?;
2790         }
2791 
2792         let if_loc = if_token.loc;
2793         let loc = match alternate {
2794             Some(ref alternate) => SourceLocation::from_parts(if_loc, alternate.get_loc()),
2795             None => SourceLocation::from_parts(if_loc, consequent.get_loc()),
2796         };
2797         Ok(self.alloc_with(|| {
2798             Statement::IfStatement(IfStatement {
2799                 test,
2800                 consequent,
2801                 alternate,
2802                 loc,
2803             })
2804         }))
2805     }
2806 
2807     // Create BlockStatement from FunctionDeclaration, for the following:
2808     //
2809     // IfStatement : `if` `(` Expression `)` FunctionDeclaration `else` Statement
2810     // IfStatement : `if` `(` Expression `)` Statement `else` FunctionDeclaration
2811     // IfStatement : `if` `(` Expression `)` FunctionDeclaration `else` FunctionDeclaration
2812     // IfStatement : `if` `(` Expression `)` FunctionDeclaration
make_block_stmt_from_function_decl( &mut self, fun: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2813     pub fn make_block_stmt_from_function_decl(
2814         &mut self,
2815         fun: arena::Box<'alloc, Statement<'alloc>>,
2816     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2817         let fun_loc = fun.get_loc();
2818 
2819         // Annex B. FunctionDeclarations in IfStatement Statement Clauses
2820         // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses
2821         //
2822         // This production only applies when parsing non-strict code.
2823         if self.is_strict()? {
2824             return Err(ParseError::FunctionDeclInSingleStatement.into());
2825         }
2826 
2827         // Code matching this production is processed as if each matching
2828         // occurrence of FunctionDeclaration[?Yield, ?Await, ~Default] was the
2829         // sole StatementListItem of a BlockStatement occupying that position
2830         // in the source code. The semantics of such a synthetic BlockStatement
2831         // includes the web legacy compatibility semantics specified in B.3.3.
2832         self.check_block_bindings(fun_loc.start)?;
2833 
2834         Ok(self.alloc_with(|| Statement::BlockStatement {
2835             block: Block {
2836                 statements: self.new_vec_single(fun.unbox()),
2837                 declarations: None,
2838                 loc: fun_loc,
2839             },
2840             loc: fun_loc,
2841         }))
2842     }
2843 
is_strict(&self) -> Result<'alloc, bool>2844     fn is_strict(&self) -> Result<'alloc, bool> {
2845         Err(ParseError::NotImplemented("strict-mode-only early error is not yet supported").into())
2846     }
2847 
2848     // IterationStatement : `do` Statement `while` `(` Expression `)` `;`
do_while_statement( &mut self, do_token: arena::Box<'alloc, Token>, stmt: arena::Box<'alloc, Statement<'alloc>>, test: arena::Box<'alloc, Expression<'alloc>>, close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2849     pub fn do_while_statement(
2850         &mut self,
2851         do_token: arena::Box<'alloc, Token>,
2852         stmt: arena::Box<'alloc, Statement<'alloc>>,
2853         test: arena::Box<'alloc, Expression<'alloc>>,
2854         close_token: arena::Box<'alloc, Token>,
2855     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2856         self.check_single_statement(stmt.get_loc().start)?;
2857 
2858         self.context_metadata
2859             .pop_unlabelled_breaks_and_continues_from(do_token.loc.start);
2860         Ok(self.alloc_with(|| Statement::DoWhileStatement {
2861             block: stmt,
2862             test,
2863             loc: SourceLocation::from_parts(do_token.loc, close_token.loc),
2864         }))
2865     }
2866 
2867     // IterationStatement : `while` `(` Expression `)` Statement
while_statement( &mut self, while_token: arena::Box<'alloc, Token>, test: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2868     pub fn while_statement(
2869         &mut self,
2870         while_token: arena::Box<'alloc, Token>,
2871         test: arena::Box<'alloc, Expression<'alloc>>,
2872         stmt: arena::Box<'alloc, Statement<'alloc>>,
2873     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2874         self.check_single_statement(stmt.get_loc().start)?;
2875 
2876         let stmt_loc = stmt.get_loc();
2877         self.context_metadata
2878             .pop_unlabelled_breaks_and_continues_from(stmt_loc.start);
2879         Ok(self.alloc_with(|| Statement::WhileStatement {
2880             test,
2881             block: stmt,
2882             loc: SourceLocation::from_parts(while_token.loc, stmt_loc),
2883         }))
2884     }
2885 
2886     // IterationStatement : `for` `(` [lookahead != 'let'] Expression? `;` Expression? `;` Expression? `)` Statement
2887     // IterationStatement : `for` `(` `var` VariableDeclarationList `;` Expression? `;` Expression? `)` Statement
for_statement( &mut self, for_token: arena::Box<'alloc, Token>, init: Option<VariableDeclarationOrExpression<'alloc>>, test: Option<arena::Box<'alloc, Expression<'alloc>>>, update: Option<arena::Box<'alloc, Expression<'alloc>>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2888     pub fn for_statement(
2889         &mut self,
2890         for_token: arena::Box<'alloc, Token>,
2891         init: Option<VariableDeclarationOrExpression<'alloc>>,
2892         test: Option<arena::Box<'alloc, Expression<'alloc>>>,
2893         update: Option<arena::Box<'alloc, Expression<'alloc>>>,
2894         stmt: arena::Box<'alloc, Statement<'alloc>>,
2895     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2896         self.check_single_statement(stmt.get_loc().start)?;
2897         self.for_statement_common(for_token, init, test, update, stmt)
2898     }
2899 
2900     // IterationStatement : `for` `(` ForLexicalDeclaration Expression? `;` Expression? `)` Statement
for_statement_lexical( &mut self, for_token: arena::Box<'alloc, Token>, init: VariableDeclarationOrExpression<'alloc>, test: Option<arena::Box<'alloc, Expression<'alloc>>>, update: Option<arena::Box<'alloc, Expression<'alloc>>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2901     pub fn for_statement_lexical(
2902         &mut self,
2903         for_token: arena::Box<'alloc, Token>,
2904         init: VariableDeclarationOrExpression<'alloc>,
2905         test: Option<arena::Box<'alloc, Expression<'alloc>>>,
2906         update: Option<arena::Box<'alloc, Expression<'alloc>>>,
2907         stmt: arena::Box<'alloc, Statement<'alloc>>,
2908     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2909         self.check_single_statement(stmt.get_loc().start)?;
2910         let init_loc = &init.get_loc();
2911         self.check_lexical_for_bindings(init_loc.start, init_loc.end)?;
2912         self.for_statement_common(for_token, Some(init), test, update, stmt)
2913     }
2914 
for_statement_common( &mut self, for_token: arena::Box<'alloc, Token>, init: Option<VariableDeclarationOrExpression<'alloc>>, test: Option<arena::Box<'alloc, Expression<'alloc>>>, update: Option<arena::Box<'alloc, Expression<'alloc>>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2915     pub fn for_statement_common(
2916         &mut self,
2917         for_token: arena::Box<'alloc, Token>,
2918         init: Option<VariableDeclarationOrExpression<'alloc>>,
2919         test: Option<arena::Box<'alloc, Expression<'alloc>>>,
2920         update: Option<arena::Box<'alloc, Expression<'alloc>>>,
2921         stmt: arena::Box<'alloc, Statement<'alloc>>,
2922     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2923         let stmt_loc = stmt.get_loc();
2924         self.context_metadata
2925             .pop_unlabelled_breaks_and_continues_from(stmt_loc.start);
2926         Ok(self.alloc_with(|| Statement::ForStatement {
2927             init,
2928             test,
2929             update,
2930             block: stmt,
2931             loc: SourceLocation::from_parts(for_token.loc, stmt_loc),
2932         }))
2933     }
2934 
for_expression( &self, expr: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> Option<VariableDeclarationOrExpression<'alloc>>2935     pub fn for_expression(
2936         &self,
2937         expr: Option<arena::Box<'alloc, Expression<'alloc>>>,
2938     ) -> Option<VariableDeclarationOrExpression<'alloc>> {
2939         expr.map(|expr| VariableDeclarationOrExpression::Expression(expr))
2940     }
2941 
for_var_declaration( &mut self, var_token: arena::Box<'alloc, Token>, declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>, ) -> VariableDeclarationOrExpression<'alloc>2942     pub fn for_var_declaration(
2943         &mut self,
2944         var_token: arena::Box<'alloc, Token>,
2945         declarators: arena::Box<'alloc, arena::Vec<'alloc, VariableDeclarator<'alloc>>>,
2946     ) -> VariableDeclarationOrExpression<'alloc> {
2947         let var_loc = var_token.loc;
2948         let declarator_loc = declarators
2949             .last()
2950             .expect("There should be at least one declarator")
2951             .get_loc();
2952 
2953         self.context_metadata.mark_binding_kind(
2954             var_loc.start,
2955             Some(declarator_loc.end),
2956             BindingKind::Var,
2957         );
2958 
2959         VariableDeclarationOrExpression::VariableDeclaration(VariableDeclaration {
2960             kind: VariableDeclarationKind::Var { loc: var_loc },
2961             declarators: declarators.unbox(),
2962             loc: SourceLocation::from_parts(var_loc, declarator_loc),
2963         })
2964     }
2965 
unbox_for_lexical_declaration( &self, declaration: arena::Box<'alloc, VariableDeclarationOrExpression<'alloc>>, ) -> VariableDeclarationOrExpression<'alloc>2966     pub fn unbox_for_lexical_declaration(
2967         &self,
2968         declaration: arena::Box<'alloc, VariableDeclarationOrExpression<'alloc>>,
2969     ) -> VariableDeclarationOrExpression<'alloc> {
2970         declaration.unbox()
2971     }
2972 
2973     // IterationStatement : `for` `(` [lookahead != 'let'] LeftHandSideExpression `in` Expression `)` Statement
2974     // IterationStatement : `for` `(` `var` ForBinding `in` Expression `)` Statement
2975     //
2976     // Annex B: Initializers in ForIn Statement Heads
2977     // https://tc39.es/ecma262/#sec-initializers-in-forin-statement-heads
2978     //
2979     // IterationStatement :  `for` `(` `var` BindingIdentifier Initializer `in` Expression `)` Statement
for_in_statement( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2980     pub fn for_in_statement(
2981         &mut self,
2982         for_token: arena::Box<'alloc, Token>,
2983         left: VariableDeclarationOrAssignmentTarget<'alloc>,
2984         right: arena::Box<'alloc, Expression<'alloc>>,
2985         stmt: arena::Box<'alloc, Statement<'alloc>>,
2986     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2987         self.check_single_statement(stmt.get_loc().start)?;
2988         self.for_in_statement_common(for_token, left, right, stmt)
2989     }
2990 
2991     // IterationStatement : `for` `(` ForDeclaration `in` Expression `)` Statement
for_in_statement_lexical( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>2992     pub fn for_in_statement_lexical(
2993         &mut self,
2994         for_token: arena::Box<'alloc, Token>,
2995         left: VariableDeclarationOrAssignmentTarget<'alloc>,
2996         right: arena::Box<'alloc, Expression<'alloc>>,
2997         stmt: arena::Box<'alloc, Statement<'alloc>>,
2998     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
2999         self.check_single_statement(stmt.get_loc().start)?;
3000         let left_loc = &left.get_loc();
3001         self.check_lexical_for_bindings(left_loc.start, left_loc.end)?;
3002         self.for_in_statement_common(for_token, left, right, stmt)
3003     }
3004 
for_in_statement_common( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3005     pub fn for_in_statement_common(
3006         &mut self,
3007         for_token: arena::Box<'alloc, Token>,
3008         left: VariableDeclarationOrAssignmentTarget<'alloc>,
3009         right: arena::Box<'alloc, Expression<'alloc>>,
3010         stmt: arena::Box<'alloc, Statement<'alloc>>,
3011     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3012         let stmt_loc = stmt.get_loc();
3013         self.context_metadata
3014             .pop_unlabelled_breaks_and_continues_from(stmt_loc.start);
3015         Ok(self.alloc_with(|| Statement::ForInStatement {
3016             left,
3017             right,
3018             block: stmt,
3019             loc: SourceLocation::from_parts(for_token.loc, stmt_loc),
3020         }))
3021     }
3022 
for_in_or_of_var_declaration( &mut self, var_token: arena::Box<'alloc, Token>, binding: arena::Box<'alloc, Binding<'alloc>>, init: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> VariableDeclarationOrAssignmentTarget<'alloc>3023     pub fn for_in_or_of_var_declaration(
3024         &mut self,
3025         var_token: arena::Box<'alloc, Token>,
3026         binding: arena::Box<'alloc, Binding<'alloc>>,
3027         init: Option<arena::Box<'alloc, Expression<'alloc>>>,
3028     ) -> VariableDeclarationOrAssignmentTarget<'alloc> {
3029         let var_loc = var_token.loc;
3030         let binding_loc = binding.get_loc();
3031         let decl_loc = match init {
3032             Some(ref init) => SourceLocation::from_parts(binding_loc, init.get_loc()),
3033             None => binding_loc,
3034         };
3035 
3036         self.context_metadata.mark_binding_kind(
3037             binding_loc.start,
3038             Some(binding_loc.end),
3039             BindingKind::Var,
3040         );
3041 
3042         VariableDeclarationOrAssignmentTarget::VariableDeclaration(VariableDeclaration {
3043             kind: VariableDeclarationKind::Var { loc: var_loc },
3044             declarators: self.new_vec_single(VariableDeclarator {
3045                 binding: binding.unbox(),
3046                 init,
3047                 loc: decl_loc,
3048             }),
3049             loc: SourceLocation::from_parts(var_loc, decl_loc),
3050         })
3051     }
3052 
for_assignment_target( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>>3053     pub fn for_assignment_target(
3054         &self,
3055         expression: arena::Box<'alloc, Expression<'alloc>>,
3056     ) -> Result<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>> {
3057         Ok(VariableDeclarationOrAssignmentTarget::AssignmentTarget(
3058             self.expression_to_assignment_target(expression)?,
3059         ))
3060     }
3061 
unbox_for_declaration( &self, declaration: arena::Box<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>>, ) -> VariableDeclarationOrAssignmentTarget<'alloc>3062     pub fn unbox_for_declaration(
3063         &self,
3064         declaration: arena::Box<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>>,
3065     ) -> VariableDeclarationOrAssignmentTarget<'alloc> {
3066         declaration.unbox()
3067     }
3068 
3069     // IterationStatement : `for` `(` [lookahead != 'let'] LeftHandSideExpression `of` AssignmentExpression `)` Statement
3070     // IterationStatement : `for` `(` `var` ForBinding `of` AssignmentExpression `)` Statement
for_of_statement( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3071     pub fn for_of_statement(
3072         &mut self,
3073         for_token: arena::Box<'alloc, Token>,
3074         left: VariableDeclarationOrAssignmentTarget<'alloc>,
3075         right: arena::Box<'alloc, Expression<'alloc>>,
3076         stmt: arena::Box<'alloc, Statement<'alloc>>,
3077     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3078         self.check_single_statement(stmt.get_loc().start)?;
3079         self.for_of_statement_common(for_token, left, right, stmt)
3080     }
3081 
3082     // IterationStatement : `for` `(` ForDeclaration `of` AssignmentExpression `)` Statement
for_of_statement_lexical( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3083     pub fn for_of_statement_lexical(
3084         &mut self,
3085         for_token: arena::Box<'alloc, Token>,
3086         left: VariableDeclarationOrAssignmentTarget<'alloc>,
3087         right: arena::Box<'alloc, Expression<'alloc>>,
3088         stmt: arena::Box<'alloc, Statement<'alloc>>,
3089     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3090         self.check_single_statement(stmt.get_loc().start)?;
3091         let left_loc = &left.get_loc();
3092         self.check_lexical_for_bindings(left_loc.start, left_loc.end)?;
3093         self.for_of_statement_common(for_token, left, right, stmt)
3094     }
3095 
for_of_statement_common( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget<'alloc>, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3096     pub fn for_of_statement_common(
3097         &mut self,
3098         for_token: arena::Box<'alloc, Token>,
3099         left: VariableDeclarationOrAssignmentTarget<'alloc>,
3100         right: arena::Box<'alloc, Expression<'alloc>>,
3101         stmt: arena::Box<'alloc, Statement<'alloc>>,
3102     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3103         let stmt_loc = stmt.get_loc();
3104         self.context_metadata
3105             .pop_unlabelled_breaks_and_continues_from(stmt_loc.start);
3106         Ok(self.alloc_with(|| Statement::ForOfStatement {
3107             left,
3108             right,
3109             block: stmt,
3110             loc: SourceLocation::from_parts(for_token.loc, stmt_loc),
3111         }))
3112     }
3113 
3114     // IterationStatement : `for` `await` `(` [lookahead != 'let'] LeftHandSideExpression `of` AssignmentExpression `)` Statement
3115     // IterationStatement : `for` `await` `(` `var` ForBinding `of` AssignmentExpression `)` Statement
for_await_of_statement( &self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3116     pub fn for_await_of_statement(
3117         &self,
3118         for_token: arena::Box<'alloc, Token>,
3119         left: VariableDeclarationOrAssignmentTarget,
3120         right: arena::Box<'alloc, Expression<'alloc>>,
3121         stmt: arena::Box<'alloc, Statement<'alloc>>,
3122     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3123         self.check_single_statement(stmt.get_loc().start)?;
3124         self.for_await_of_statement_common(for_token, left, right, stmt)
3125     }
3126 
3127     // IterationStatement : `for` `await` `(` ForDeclaration `of` AssignmentExpression `)` Statement
for_await_of_statement_lexical( &mut self, for_token: arena::Box<'alloc, Token>, left: VariableDeclarationOrAssignmentTarget, right: arena::Box<'alloc, Expression<'alloc>>, stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3128     pub fn for_await_of_statement_lexical(
3129         &mut self,
3130         for_token: arena::Box<'alloc, Token>,
3131         left: VariableDeclarationOrAssignmentTarget,
3132         right: arena::Box<'alloc, Expression<'alloc>>,
3133         stmt: arena::Box<'alloc, Statement<'alloc>>,
3134     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3135         self.check_single_statement(stmt.get_loc().start)?;
3136         let left_loc = &left.get_loc();
3137         self.check_lexical_for_bindings(left_loc.start, left_loc.end)?;
3138         self.for_await_of_statement_common(for_token, left, right, stmt)
3139     }
3140 
for_await_of_statement_common( &self, _for_token: arena::Box<'alloc, Token>, _left: VariableDeclarationOrAssignmentTarget, _right: arena::Box<'alloc, Expression<'alloc>>, _stmt: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3141     pub fn for_await_of_statement_common(
3142         &self,
3143         _for_token: arena::Box<'alloc, Token>,
3144         _left: VariableDeclarationOrAssignmentTarget,
3145         _right: arena::Box<'alloc, Expression<'alloc>>,
3146         _stmt: arena::Box<'alloc, Statement<'alloc>>,
3147     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3148         Err(ParseError::NotImplemented("for await statement (missing from AST)").into())
3149     }
3150 
3151     // ForDeclaration : LetOrConst ForBinding => ForDeclaration($0, $1)
for_declaration( &mut self, kind: arena::Box<'alloc, VariableDeclarationKind>, binding: arena::Box<'alloc, Binding<'alloc>>, ) -> arena::Box<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>>3152     pub fn for_declaration(
3153         &mut self,
3154         kind: arena::Box<'alloc, VariableDeclarationKind>,
3155         binding: arena::Box<'alloc, Binding<'alloc>>,
3156     ) -> arena::Box<'alloc, VariableDeclarationOrAssignmentTarget<'alloc>> {
3157         let binding_kind = match &*kind {
3158             VariableDeclarationKind::Let { .. } => BindingKind::Let,
3159             VariableDeclarationKind::Const { .. } => BindingKind::Const,
3160             _ => panic!("unexpected VariableDeclarationKind"),
3161         };
3162 
3163         self.context_metadata
3164             .mark_binding_kind(kind.get_loc().start, None, binding_kind);
3165 
3166         let kind_loc = kind.get_loc();
3167         let binding_loc = binding.get_loc();
3168         self.alloc_with(|| {
3169             VariableDeclarationOrAssignmentTarget::VariableDeclaration(VariableDeclaration {
3170                 kind: kind.unbox(),
3171                 declarators: self.new_vec_single(VariableDeclarator {
3172                     binding: binding.unbox(),
3173                     init: None,
3174                     loc: binding_loc,
3175                 }),
3176                 loc: SourceLocation::from_parts(kind_loc, binding_loc),
3177             })
3178         })
3179     }
3180 
3181     // CatchParameter : BindingIdentifier
3182     // ForBinding : BindingIdentifier
3183     // LexicalBinding : BindingIdentifier Initializer?
3184     // VariableDeclaration : BindingIdentifier Initializer?
binding_identifier_to_binding( &self, identifier: arena::Box<'alloc, BindingIdentifier>, ) -> arena::Box<'alloc, Binding<'alloc>>3185     pub fn binding_identifier_to_binding(
3186         &self,
3187         identifier: arena::Box<'alloc, BindingIdentifier>,
3188     ) -> arena::Box<'alloc, Binding<'alloc>> {
3189         self.alloc_with(|| Binding::BindingIdentifier(identifier.unbox()))
3190     }
3191 
3192     // ContinueStatement : `continue` `;`
3193     // ContinueStatement : `continue` LabelIdentifier `;`
continue_statement( &mut self, continue_token: arena::Box<'alloc, Token>, label: Option<arena::Box<'alloc, Label>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3194     pub fn continue_statement(
3195         &mut self,
3196         continue_token: arena::Box<'alloc, Token>,
3197         label: Option<arena::Box<'alloc, Label>>,
3198     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3199         let info = match label {
3200             Some(ref label) => {
3201                 // Label is used both for LabelledStatement and for labelled
3202                 // ContinueStatements. A label will be noted in the context metadata
3203                 // whenever we hit a label, as is the case for BreakStatements. These
3204                 // bindings are not necessary, and are at the end of the bindings stack.
3205                 // To keep things clean, we will pop the last element (the label we just
3206                 // added) off the stack.
3207                 let index = self
3208                     .context_metadata
3209                     .find_first_label(continue_token.loc.start);
3210                 self.context_metadata.pop_labels_from(index);
3211 
3212                 ControlInfo::new_continue(continue_token.loc.start, Some(label.value))
3213             }
3214             None => ControlInfo::new_continue(continue_token.loc.start, None),
3215         };
3216 
3217         self.context_metadata.push_break_or_continue(info);
3218 
3219         let continue_loc = continue_token.loc;
3220         let loc = match label {
3221             Some(ref label) => SourceLocation::from_parts(continue_loc, label.loc),
3222             None => continue_loc,
3223         };
3224         Ok(self.alloc_with(|| Statement::ContinueStatement {
3225             label: label.map(|boxed| boxed.unbox()),
3226             loc,
3227         }))
3228     }
3229 
3230     // BreakStatement : `break` `;`
3231     // BreakStatement : `break` LabelIdentifier `;`
break_statement( &mut self, break_token: arena::Box<'alloc, Token>, label: Option<arena::Box<'alloc, Label>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3232     pub fn break_statement(
3233         &mut self,
3234         break_token: arena::Box<'alloc, Token>,
3235         label: Option<arena::Box<'alloc, Label>>,
3236     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3237         let info = match label {
3238             Some(ref label) => {
3239                 // Label is used both for LabelledStatement and for labelled
3240                 // BreakStatements. A label will be noted in the context metadata
3241                 // whenever we hit a label, as is the case for BreakStatements. These
3242                 // bindings are not necessary, and are at the end of the bindings stack.
3243                 // To keep things clean, we will pop the last element (the label we just
3244                 // added) off the stack.
3245                 let index = self.context_metadata.find_first_label(label.loc.start);
3246                 self.context_metadata.pop_labels_from(index);
3247 
3248                 ControlInfo::new_break(break_token.loc.start, Some(label.value))
3249             }
3250             None => ControlInfo::new_break(break_token.loc.start, None),
3251         };
3252 
3253         self.context_metadata.push_break_or_continue(info);
3254         let break_loc = break_token.loc;
3255         let loc = match label {
3256             Some(ref label) => SourceLocation::from_parts(break_loc, label.loc),
3257             None => break_loc,
3258         };
3259         Ok(self.alloc_with(|| Statement::BreakStatement {
3260             label: label.map(|boxed| boxed.unbox()),
3261             loc,
3262         }))
3263     }
3264 
3265     // ReturnStatement : `return` `;`
3266     // ReturnStatement : `return` Expression `;`
return_statement( &self, return_token: arena::Box<'alloc, Token>, expression: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, Statement<'alloc>>3267     pub fn return_statement(
3268         &self,
3269         return_token: arena::Box<'alloc, Token>,
3270         expression: Option<arena::Box<'alloc, Expression<'alloc>>>,
3271     ) -> arena::Box<'alloc, Statement<'alloc>> {
3272         let return_loc = return_token.loc;
3273         let loc = match expression {
3274             Some(ref expression) => SourceLocation::from_parts(return_loc, expression.get_loc()),
3275             None => return_loc,
3276         };
3277         self.alloc_with(|| Statement::ReturnStatement { expression, loc })
3278     }
3279 
3280     // WithStatement : `with` `(` Expression `)` Statement
with_statement( &self, with_token: arena::Box<'alloc, Token>, object: arena::Box<'alloc, Expression<'alloc>>, body: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>3281     pub fn with_statement(
3282         &self,
3283         with_token: arena::Box<'alloc, Token>,
3284         object: arena::Box<'alloc, Expression<'alloc>>,
3285         body: arena::Box<'alloc, Statement<'alloc>>,
3286     ) -> arena::Box<'alloc, Statement<'alloc>> {
3287         let body_loc = body.get_loc();
3288         self.alloc_with(|| Statement::WithStatement {
3289             object,
3290             body,
3291             loc: SourceLocation::from_parts(with_token.loc, body_loc),
3292         })
3293     }
3294 
3295     // SwitchStatement : `switch` `(` Expression `)` CaseBlock
switch_statement( &self, switch_token: arena::Box<'alloc, Token>, discriminant_expr: arena::Box<'alloc, Expression<'alloc>>, mut cases: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>3296     pub fn switch_statement(
3297         &self,
3298         switch_token: arena::Box<'alloc, Token>,
3299         discriminant_expr: arena::Box<'alloc, Expression<'alloc>>,
3300         mut cases: arena::Box<'alloc, Statement<'alloc>>,
3301     ) -> arena::Box<'alloc, Statement<'alloc>> {
3302         match &mut *cases {
3303             Statement::SwitchStatement {
3304                 discriminant, loc, ..
3305             } => {
3306                 *discriminant = discriminant_expr;
3307                 (*loc).start = switch_token.loc.start;
3308             }
3309             Statement::SwitchStatementWithDefault {
3310                 discriminant, loc, ..
3311             } => {
3312                 *discriminant = discriminant_expr;
3313                 (*loc).start = switch_token.loc.start;
3314             }
3315             _ => {
3316                 // The grammar ensures that the parser always passes a valid
3317                 // argument to this method.
3318                 panic!("invalid argument: argument 2 must be a SwitchStatement");
3319             }
3320         }
3321         cases
3322     }
3323 
3324     // CaseBlock : `{` CaseClauses? `}`
case_block( &mut self, open_token: arena::Box<'alloc, Token>, cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>, close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3325     pub fn case_block(
3326         &mut self,
3327         open_token: arena::Box<'alloc, Token>,
3328         cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>,
3329         close_token: arena::Box<'alloc, Token>,
3330     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3331         self.check_case_block_binding(open_token.loc.start)?;
3332 
3333         Ok(self.alloc_with(|| Statement::SwitchStatement {
3334             // This will be overwritten once the enclosing switch statement
3335             // gets parsed.
3336             discriminant: self.alloc_with(|| Expression::LiteralNullExpression {
3337                 loc: SourceLocation::default(),
3338             }),
3339             cases: match cases {
3340                 None => self.new_vec(),
3341                 Some(boxed) => boxed.unbox(),
3342             },
3343             // `start` of this will be overwritten once the enclosing switch
3344             // statement gets parsed.
3345             loc: close_token.loc,
3346         }))
3347     }
3348 
3349     // CaseBlock : `{` CaseClauses DefaultClause CaseClauses `}`
case_block_with_default( &mut self, open_token: arena::Box<'alloc, Token>, pre_default_cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>, default_case: arena::Box<'alloc, SwitchDefault<'alloc>>, post_default_cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>, close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3350     pub fn case_block_with_default(
3351         &mut self,
3352         open_token: arena::Box<'alloc, Token>,
3353         pre_default_cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>,
3354         default_case: arena::Box<'alloc, SwitchDefault<'alloc>>,
3355         post_default_cases: Option<arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>>,
3356         close_token: arena::Box<'alloc, Token>,
3357     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3358         self.check_case_block_binding(open_token.loc.start)?;
3359 
3360         Ok(self.alloc_with(|| Statement::SwitchStatementWithDefault {
3361             // This will be overwritten once the enclosing switch statement
3362             // gets parsed.
3363             discriminant: self.alloc_with(|| Expression::LiteralNullExpression {
3364                 loc: SourceLocation::default(),
3365             }),
3366             pre_default_cases: match pre_default_cases {
3367                 None => self.new_vec(),
3368                 Some(boxed) => boxed.unbox(),
3369             },
3370             default_case: default_case.unbox(),
3371             post_default_cases: match post_default_cases {
3372                 None => self.new_vec(),
3373                 Some(boxed) => boxed.unbox(),
3374             },
3375             // `start` of this will be overwritten once the enclosing switch
3376             // statement gets parsed.
3377             loc: close_token.loc,
3378         }))
3379     }
3380 
3381     // CaseClauses : CaseClause
case_clauses_single( &self, case: arena::Box<'alloc, SwitchCase<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>3382     pub fn case_clauses_single(
3383         &self,
3384         case: arena::Box<'alloc, SwitchCase<'alloc>>,
3385     ) -> arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>> {
3386         self.alloc_with(|| self.new_vec_single(case.unbox()))
3387     }
3388 
3389     // CaseClauses : CaseClauses CaseClause
case_clauses_append( &self, mut cases: arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>, case: arena::Box<'alloc, SwitchCase<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>3390     pub fn case_clauses_append(
3391         &self,
3392         mut cases: arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>>,
3393         case: arena::Box<'alloc, SwitchCase<'alloc>>,
3394     ) -> arena::Box<'alloc, arena::Vec<'alloc, SwitchCase<'alloc>>> {
3395         self.push(&mut cases, case.unbox());
3396         cases
3397     }
3398 
3399     // CaseClause : `case` Expression `:` StatementList
case_clause( &self, case_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, colon_token: arena::Box<'alloc, Token>, statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, ) -> arena::Box<'alloc, SwitchCase<'alloc>>3400     pub fn case_clause(
3401         &self,
3402         case_token: arena::Box<'alloc, Token>,
3403         expression: arena::Box<'alloc, Expression<'alloc>>,
3404         colon_token: arena::Box<'alloc, Token>,
3405         statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
3406     ) -> arena::Box<'alloc, SwitchCase<'alloc>> {
3407         let case_loc = case_token.loc;
3408         if let Some(statements) = statements {
3409             let statement_loc = statements
3410                 .last()
3411                 .expect("There should be at least one statement")
3412                 .get_loc();
3413 
3414             self.alloc_with(|| SwitchCase {
3415                 test: expression,
3416                 consequent: statements.unbox(),
3417                 loc: SourceLocation::from_parts(case_loc, statement_loc),
3418             })
3419         } else {
3420             self.alloc_with(|| SwitchCase {
3421                 test: expression,
3422                 consequent: self.new_vec(),
3423                 loc: SourceLocation::from_parts(case_loc, colon_token.loc),
3424             })
3425         }
3426     }
3427 
3428     // DefaultClause : `default` `:` StatementList
default_clause( &self, default_token: arena::Box<'alloc, Token>, colon_token: arena::Box<'alloc, Token>, statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, ) -> arena::Box<'alloc, SwitchDefault<'alloc>>3429     pub fn default_clause(
3430         &self,
3431         default_token: arena::Box<'alloc, Token>,
3432         colon_token: arena::Box<'alloc, Token>,
3433         statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
3434     ) -> arena::Box<'alloc, SwitchDefault<'alloc>> {
3435         let default_loc = default_token.loc;
3436         if let Some(statements) = statements {
3437             let statement_loc = statements
3438                 .last()
3439                 .expect("There should be at least one statement")
3440                 .get_loc();
3441 
3442             self.alloc_with(|| SwitchDefault {
3443                 consequent: statements.unbox(),
3444                 loc: SourceLocation::from_parts(default_loc, statement_loc),
3445             })
3446         } else {
3447             self.alloc_with(|| SwitchDefault {
3448                 consequent: self.new_vec(),
3449                 loc: SourceLocation::from_parts(default_loc, colon_token.loc),
3450             })
3451         }
3452     }
3453 
3454     // LabelledStatement : LabelIdentifier `:` LabelledItem
labelled_statement( &mut self, label: arena::Box<'alloc, Label>, body: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>>3455     pub fn labelled_statement(
3456         &mut self,
3457         label: arena::Box<'alloc, Label>,
3458         body: arena::Box<'alloc, Statement<'alloc>>,
3459     ) -> Result<'alloc, arena::Box<'alloc, Statement<'alloc>>> {
3460         let label_loc = label.loc;
3461         let body_loc = body.get_loc();
3462         self.mark_labelled_statement(&label, &body);
3463         self.check_labelled_statement(label.value, label_loc.start, body_loc.start)?;
3464         Ok(self.alloc_with(|| Statement::LabelledStatement {
3465             label: label.unbox(),
3466             body,
3467             loc: SourceLocation::from_parts(label_loc, body_loc),
3468         }))
3469     }
3470 
3471     // ThrowStatement : `throw` Expression `;`
throw_statement( &self, throw_token: arena::Box<'alloc, Token>, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>3472     pub fn throw_statement(
3473         &self,
3474         throw_token: arena::Box<'alloc, Token>,
3475         expression: arena::Box<'alloc, Expression<'alloc>>,
3476     ) -> arena::Box<'alloc, Statement<'alloc>> {
3477         let expression_loc = expression.get_loc();
3478         self.alloc_with(|| Statement::ThrowStatement {
3479             expression,
3480             loc: SourceLocation::from_parts(throw_token.loc, expression_loc),
3481         })
3482     }
3483 
3484     // TryStatement : `try` Block Catch
3485     // TryStatement : `try` Block Finally
3486     // TryStatement : `try` Block Catch Finally
try_statement( &self, try_token: arena::Box<'alloc, Token>, body: arena::Box<'alloc, Block<'alloc>>, catch_clause: Option<arena::Box<'alloc, CatchClause<'alloc>>>, finally_block: Option<arena::Box<'alloc, Block<'alloc>>>, ) -> arena::Box<'alloc, Statement<'alloc>>3487     pub fn try_statement(
3488         &self,
3489         try_token: arena::Box<'alloc, Token>,
3490         body: arena::Box<'alloc, Block<'alloc>>,
3491         catch_clause: Option<arena::Box<'alloc, CatchClause<'alloc>>>,
3492         finally_block: Option<arena::Box<'alloc, Block<'alloc>>>,
3493     ) -> arena::Box<'alloc, Statement<'alloc>> {
3494         let try_loc = try_token.loc;
3495         match (catch_clause, finally_block) {
3496             (Some(catch_clause), None) => {
3497                 let catch_clause_loc = catch_clause.loc;
3498                 self.alloc_with(|| Statement::TryCatchStatement {
3499                     body: body.unbox(),
3500                     catch_clause: catch_clause.unbox(),
3501                     loc: SourceLocation::from_parts(try_loc, catch_clause_loc),
3502                 })
3503             }
3504             (catch_clause, Some(finally_block)) => {
3505                 let finally_block_loc = finally_block.loc;
3506                 self.alloc_with(|| Statement::TryFinallyStatement {
3507                     body: body.unbox(),
3508                     catch_clause: catch_clause.map(|boxed| boxed.unbox()),
3509                     finalizer: finally_block.unbox(),
3510                     loc: SourceLocation::from_parts(try_loc, finally_block_loc),
3511                 })
3512             }
3513             _ => {
3514                 // The grammar won't accept a bare try-block, so the parser always
3515                 // a catch clause, a finally block, or both.
3516                 panic!("invalid argument: try_statement requires a catch or finally block");
3517             }
3518         }
3519     }
3520 
3521     // Catch : `catch` `(` CatchParameter `)` Block
catch( &mut self, catch_token: arena::Box<'alloc, Token>, binding: arena::Box<'alloc, Binding<'alloc>>, body: arena::Box<'alloc, Block<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, CatchClause<'alloc>>>3522     pub fn catch(
3523         &mut self,
3524         catch_token: arena::Box<'alloc, Token>,
3525         binding: arena::Box<'alloc, Binding<'alloc>>,
3526         body: arena::Box<'alloc, Block<'alloc>>,
3527     ) -> Result<'alloc, arena::Box<'alloc, CatchClause<'alloc>>> {
3528         let catch_loc = catch_token.loc;
3529         let body_loc = body.loc;
3530 
3531         let is_simple = match &*binding {
3532             Binding::BindingIdentifier(_) => true,
3533             _ => false,
3534         };
3535 
3536         let bindings_loc = &binding.get_loc();
3537         self.check_catch_bindings(is_simple, bindings_loc.start, bindings_loc.end)?;
3538 
3539         Ok(self.alloc_with(|| CatchClause {
3540             binding: Some(binding),
3541             body: body.unbox(),
3542             loc: SourceLocation::from_parts(catch_loc, body_loc),
3543         }))
3544     }
3545 
3546     // Catch : `catch` `(` CatchParameter `)` Block
catch_no_param( &mut self, catch_token: arena::Box<'alloc, Token>, body: arena::Box<'alloc, Block<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, CatchClause<'alloc>>>3547     pub fn catch_no_param(
3548         &mut self,
3549         catch_token: arena::Box<'alloc, Token>,
3550         body: arena::Box<'alloc, Block<'alloc>>,
3551     ) -> Result<'alloc, arena::Box<'alloc, CatchClause<'alloc>>> {
3552         let catch_loc = catch_token.loc;
3553         let body_loc = body.loc;
3554 
3555         self.check_catch_no_param_bindings(catch_loc.start)?;
3556 
3557         Ok(self.alloc_with(|| CatchClause {
3558             binding: None,
3559             body: body.unbox(),
3560             loc: SourceLocation::from_parts(catch_loc, body_loc),
3561         }))
3562     }
3563 
3564     // DebuggerStatement : `debugger` `;`
debugger_statement( &self, token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, Statement<'alloc>>3565     pub fn debugger_statement(
3566         &self,
3567         token: arena::Box<'alloc, Token>,
3568     ) -> arena::Box<'alloc, Statement<'alloc>> {
3569         self.alloc_with(|| Statement::DebuggerStatement { loc: token.loc })
3570     }
3571 
function_decl(&mut self, f: Function<'alloc>) -> arena::Box<'alloc, Statement<'alloc>>3572     pub fn function_decl(&mut self, f: Function<'alloc>) -> arena::Box<'alloc, Statement<'alloc>> {
3573         self.context_metadata
3574             .mark_binding_kind(f.loc.start, None, BindingKind::Function);
3575 
3576         self.alloc_with(|| Statement::FunctionDeclaration(f))
3577     }
3578 
async_or_generator_decl( &mut self, f: Function<'alloc>, ) -> arena::Box<'alloc, Statement<'alloc>>3579     pub fn async_or_generator_decl(
3580         &mut self,
3581         f: Function<'alloc>,
3582     ) -> arena::Box<'alloc, Statement<'alloc>> {
3583         self.context_metadata
3584             .mark_binding_kind(f.loc.start, None, BindingKind::AsyncOrGenerator);
3585 
3586         self.alloc_with(|| Statement::FunctionDeclaration(f))
3587     }
3588 
function_expr(&mut self, f: Function<'alloc>) -> arena::Box<'alloc, Expression<'alloc>>3589     pub fn function_expr(&mut self, f: Function<'alloc>) -> arena::Box<'alloc, Expression<'alloc>> {
3590         let index = self.context_metadata.find_first_binding(f.loc.start);
3591         self.context_metadata.pop_bindings_from(index);
3592 
3593         let label_index = self.context_metadata.find_first_label(f.loc.start);
3594         self.context_metadata.pop_labels_from(label_index);
3595 
3596         self.alloc_with(|| Expression::FunctionExpression(f))
3597     }
3598 
3599     // FunctionDeclaration : `function` BindingIdentifier `(` FormalParameters `)` `{` FunctionBody `}`
3600     // FunctionDeclaration : [+Default] `function` `(` FormalParameters `)` `{` FunctionBody `}`
3601     // FunctionExpression : `function` BindingIdentifier? `(` FormalParameters `)` `{` FunctionBody `}`
function( &mut self, function_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, Function<'alloc>>3602     pub fn function(
3603         &mut self,
3604         function_token: arena::Box<'alloc, Token>,
3605         name: Option<arena::Box<'alloc, BindingIdentifier>>,
3606         param_open_token: arena::Box<'alloc, Token>,
3607         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3608         param_close_token: arena::Box<'alloc, Token>,
3609         body_open_token: arena::Box<'alloc, Token>,
3610         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3611         body_close_token: arena::Box<'alloc, Token>,
3612     ) -> Result<'alloc, Function<'alloc>> {
3613         let param_open_loc = param_open_token.loc;
3614         let param_close_loc = param_close_token.loc;
3615         let body_close_loc = body_close_token.loc;
3616 
3617         let is_simple = Self::is_params_simple(&params);
3618         self.check_function_bindings(is_simple, param_open_loc.start, param_close_loc.end)?;
3619 
3620         params.loc.set_range(param_open_loc, param_close_loc);
3621         body.loc.set_range(body_open_token.loc, body_close_loc);
3622 
3623         Ok(Function {
3624             name: name.map(|b| b.unbox()),
3625             is_async: false,
3626             is_generator: false,
3627             params: params.unbox(),
3628             body: body.unbox(),
3629             loc: SourceLocation::from_parts(function_token.loc, body_close_loc),
3630         })
3631     }
3632 
3633     // AsyncFunctionDeclaration : `async` `function` BindingIdentifier `(` FormalParameters `)` `{` AsyncFunctionBody `}`
3634     // AsyncFunctionDeclaration : [+Default] `async` `function` `(` FormalParameters `)` `{` AsyncFunctionBody `}`
3635     // AsyncFunctionExpression : `async` `function` `(` FormalParameters `)` `{` AsyncFunctionBody `}`
async_function( &mut self, async_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, Function<'alloc>>3636     pub fn async_function(
3637         &mut self,
3638         async_token: arena::Box<'alloc, Token>,
3639         name: Option<arena::Box<'alloc, BindingIdentifier>>,
3640         param_open_token: arena::Box<'alloc, Token>,
3641         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3642         param_close_token: arena::Box<'alloc, Token>,
3643         body_open_token: arena::Box<'alloc, Token>,
3644         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3645         body_close_token: arena::Box<'alloc, Token>,
3646     ) -> Result<'alloc, Function<'alloc>> {
3647         let param_open_loc = param_open_token.loc;
3648         let param_close_loc = param_close_token.loc;
3649         let body_close_loc = body_close_token.loc;
3650 
3651         let is_simple = Self::is_params_simple(&params);
3652         self.check_function_bindings(is_simple, param_open_loc.start, param_close_loc.end)?;
3653 
3654         params.loc.set_range(param_open_loc, param_close_loc);
3655         body.loc.set_range(body_open_token.loc, body_close_loc);
3656 
3657         Ok(Function {
3658             name: name.map(|b| b.unbox()),
3659             is_async: true,
3660             is_generator: false,
3661             params: params.unbox(),
3662             body: body.unbox(),
3663             loc: SourceLocation::from_parts(async_token.loc, body_close_loc),
3664         })
3665     }
3666 
3667     // GeneratorDeclaration : `function` `*` BindingIdentifier `(` FormalParameters `)` `{` GeneratorBody `}`
3668     // GeneratorDeclaration : [+Default] `function` `*` `(` FormalParameters `)` `{` GeneratorBody `}`
3669     // GeneratorExpression : `function` `*` BindingIdentifier? `(` FormalParameters `)` `{` GeneratorBody `}`
generator( &mut self, function_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, Function<'alloc>>3670     pub fn generator(
3671         &mut self,
3672         function_token: arena::Box<'alloc, Token>,
3673         name: Option<arena::Box<'alloc, BindingIdentifier>>,
3674         param_open_token: arena::Box<'alloc, Token>,
3675         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3676         param_close_token: arena::Box<'alloc, Token>,
3677         body_open_token: arena::Box<'alloc, Token>,
3678         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3679         body_close_token: arena::Box<'alloc, Token>,
3680     ) -> Result<'alloc, Function<'alloc>> {
3681         let param_open_loc = param_open_token.loc;
3682         let param_close_loc = param_close_token.loc;
3683         let body_close_loc = body_close_token.loc;
3684 
3685         let is_simple = Self::is_params_simple(&params);
3686         self.check_function_bindings(is_simple, param_open_loc.start, param_close_loc.end)?;
3687 
3688         params.loc.set_range(param_open_loc, param_close_loc);
3689         body.loc.set_range(body_open_token.loc, body_close_loc);
3690 
3691         Ok(Function {
3692             name: name.map(|b| b.unbox()),
3693             is_async: false,
3694             is_generator: true,
3695             params: params.unbox(),
3696             body: body.unbox(),
3697             loc: SourceLocation::from_parts(function_token.loc, body_close_loc),
3698         })
3699     }
3700 
3701     // AsyncGeneratorDeclaration : `async` `function` `*` BindingIdentifier `(` FormalParameters `)` `{` AsyncGeneratorBody `}`
3702     // AsyncGeneratorDeclaration : [+Default] `async` `function` `*` `(` FormalParameters `)` `{` AsyncGeneratorBody `}`
3703     // AsyncGeneratorExpression : `async` `function` `*` BindingIdentifier? `(` FormalParameters `)` `{` AsyncGeneratorBody `}`
async_generator( &mut self, async_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, Function<'alloc>>3704     pub fn async_generator(
3705         &mut self,
3706         async_token: arena::Box<'alloc, Token>,
3707         name: Option<arena::Box<'alloc, BindingIdentifier>>,
3708         param_open_token: arena::Box<'alloc, Token>,
3709         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3710         param_close_token: arena::Box<'alloc, Token>,
3711         body_open_token: arena::Box<'alloc, Token>,
3712         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3713         body_close_token: arena::Box<'alloc, Token>,
3714     ) -> Result<'alloc, Function<'alloc>> {
3715         let param_open_loc = param_open_token.loc;
3716         let param_close_loc = param_close_token.loc;
3717         let body_close_loc = body_close_token.loc;
3718 
3719         let is_simple = Self::is_params_simple(&params);
3720         self.check_function_bindings(is_simple, param_open_loc.start, param_close_loc.end)?;
3721 
3722         params.loc.set_range(param_open_loc, param_close_loc);
3723         body.loc.set_range(body_open_token.loc, body_close_loc);
3724 
3725         Ok(Function {
3726             name: name.map(|b| b.unbox()),
3727             is_async: true,
3728             is_generator: true,
3729             params: params.unbox(),
3730             body: body.unbox(),
3731             loc: SourceLocation::from_parts(async_token.loc, body_close_loc),
3732         })
3733     }
3734 
3735     // UniqueFormalParameters : FormalParameters
unique_formal_parameters( &self, parameters: arena::Box<'alloc, FormalParameters<'alloc>>, ) -> arena::Box<'alloc, FormalParameters<'alloc>>3736     pub fn unique_formal_parameters(
3737         &self,
3738         parameters: arena::Box<'alloc, FormalParameters<'alloc>>,
3739     ) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3740         parameters
3741     }
3742 
3743     // FormalParameters : [empty]
empty_formal_parameters(&self) -> arena::Box<'alloc, FormalParameters<'alloc>>3744     pub fn empty_formal_parameters(&self) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3745         self.alloc_with(|| FormalParameters {
3746             items: self.new_vec(),
3747             rest: None,
3748             // This will be overwritten once the enclosing function gets parsed.
3749             loc: SourceLocation::default(),
3750         })
3751     }
3752 
3753     // FormalParameters : FunctionRestParameter
3754     // FormalParameters : FormalParameterList `,` FunctionRestParameter
with_rest_parameter( &self, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, rest: arena::Box<'alloc, Binding<'alloc>>, ) -> arena::Box<'alloc, FormalParameters<'alloc>>3755     pub fn with_rest_parameter(
3756         &self,
3757         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3758         rest: arena::Box<'alloc, Binding<'alloc>>,
3759     ) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3760         params.rest = Some(rest.unbox());
3761         params
3762     }
3763 
3764     // FormalParameterList : FormalParameter
formal_parameter_list_single( &self, parameter: arena::Box<'alloc, Parameter<'alloc>>, ) -> arena::Box<'alloc, FormalParameters<'alloc>>3765     pub fn formal_parameter_list_single(
3766         &self,
3767         parameter: arena::Box<'alloc, Parameter<'alloc>>,
3768     ) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3769         self.alloc_with(|| FormalParameters {
3770             items: self.new_vec_single(parameter.unbox()),
3771             rest: None,
3772             // This will be overwritten once the enclosing function gets parsed.
3773             loc: SourceLocation::default(),
3774         })
3775     }
3776 
3777     // FormalParameterList : FormalParameterList "," FormalParameter
formal_parameter_list_append( &self, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, next_param: arena::Box<'alloc, Parameter<'alloc>>, ) -> arena::Box<'alloc, FormalParameters<'alloc>>3778     pub fn formal_parameter_list_append(
3779         &self,
3780         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3781         next_param: arena::Box<'alloc, Parameter<'alloc>>,
3782     ) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3783         self.push(&mut params.items, next_param.unbox());
3784         params
3785     }
3786 
3787     // FunctionBody : FunctionStatementList
function_body( &self, statements: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>, ) -> arena::Box<'alloc, FunctionBody<'alloc>>3788     pub fn function_body(
3789         &self,
3790         statements: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>,
3791     ) -> arena::Box<'alloc, FunctionBody<'alloc>> {
3792         // TODO: Directives
3793         self.alloc_with(|| FunctionBody {
3794             directives: self.new_vec(),
3795             statements: statements.unbox(),
3796             // This will be overwritten once the enclosing function gets parsed.
3797             loc: SourceLocation::default(),
3798         })
3799     }
3800 
3801     // FunctionStatementList : StatementList?
function_statement_list( &self, statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>3802     pub fn function_statement_list(
3803         &self,
3804         statements: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
3805     ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>> {
3806         match statements {
3807             Some(statements) => statements,
3808             None => self.alloc_with(|| self.new_vec()),
3809         }
3810     }
3811 
3812     // ArrowFunction : ArrowParameters `=>` ConciseBody
arrow_function( &mut self, params: arena::Box<'alloc, FormalParameters<'alloc>>, body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>3813     pub fn arrow_function(
3814         &mut self,
3815         params: arena::Box<'alloc, FormalParameters<'alloc>>,
3816         body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>,
3817     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
3818         self.check_unique_function_bindings(params.loc.start, params.loc.end)?;
3819 
3820         let params_loc = params.loc;
3821         let body_loc = body.get_loc();
3822 
3823         Ok(self.alloc_with(|| Expression::ArrowExpression {
3824             is_async: false,
3825             params: params.unbox(),
3826             body: body.unbox(),
3827             loc: SourceLocation::from_parts(params_loc, body_loc),
3828         }))
3829     }
3830 
3831     // ArrowParameters : BindingIdentifier
arrow_parameters_bare( &mut self, identifier: arena::Box<'alloc, BindingIdentifier>, ) -> arena::Box<'alloc, FormalParameters<'alloc>>3832     pub fn arrow_parameters_bare(
3833         &mut self,
3834         identifier: arena::Box<'alloc, BindingIdentifier>,
3835     ) -> arena::Box<'alloc, FormalParameters<'alloc>> {
3836         let loc = identifier.loc;
3837         self.alloc_with(|| FormalParameters {
3838             items: self.new_vec_single(Parameter::Binding(Binding::BindingIdentifier(
3839                 identifier.unbox(),
3840             ))),
3841             rest: None,
3842             loc,
3843         })
3844     }
3845 
3846     // ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
uncover_arrow_parameters( &self, covered: arena::Box<'alloc, CoverParenthesized<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, FormalParameters<'alloc>>>3847     pub fn uncover_arrow_parameters(
3848         &self,
3849         covered: arena::Box<'alloc, CoverParenthesized<'alloc>>,
3850     ) -> Result<'alloc, arena::Box<'alloc, FormalParameters<'alloc>>> {
3851         Ok(match covered.unbox() {
3852             CoverParenthesized::Expression { expression, loc } => self.alloc(FormalParameters {
3853                 items: self.expression_to_parameter_list(expression)?,
3854                 rest: None,
3855                 loc,
3856             }),
3857             CoverParenthesized::Parameters(parameters) => parameters,
3858         })
3859     }
3860 
3861     // ConciseBody : [lookahead != `{`] AssignmentExpression
concise_body_expression( &self, expression: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, ArrowExpressionBody<'alloc>>3862     pub fn concise_body_expression(
3863         &self,
3864         expression: arena::Box<'alloc, Expression<'alloc>>,
3865     ) -> arena::Box<'alloc, ArrowExpressionBody<'alloc>> {
3866         self.alloc_with(|| ArrowExpressionBody::Expression(expression))
3867     }
3868 
3869     // ConciseBody : `{` FunctionBody `}`
concise_body_block( &self, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, ArrowExpressionBody<'alloc>>3870     pub fn concise_body_block(
3871         &self,
3872         body_open_token: arena::Box<'alloc, Token>,
3873         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3874         body_close_token: arena::Box<'alloc, Token>,
3875     ) -> arena::Box<'alloc, ArrowExpressionBody<'alloc>> {
3876         body.loc
3877             .set_range(body_open_token.loc, body_close_token.loc);
3878         self.alloc_with(|| ArrowExpressionBody::FunctionBody(body.unbox()))
3879     }
3880 
3881     // MethodDefinition : ClassElementName `(` UniqueFormalParameters `)` `{` FunctionBody `}`
method_definition( &mut self, name: arena::Box<'alloc, ClassElementName<'alloc>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>>3882     pub fn method_definition(
3883         &mut self,
3884         name: arena::Box<'alloc, ClassElementName<'alloc>>,
3885         param_open_token: arena::Box<'alloc, Token>,
3886         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3887         param_close_token: arena::Box<'alloc, Token>,
3888         body_open_token: arena::Box<'alloc, Token>,
3889         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3890         body_close_token: arena::Box<'alloc, Token>,
3891     ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>> {
3892         let name_loc = name.get_loc();
3893         let param_open_loc = param_open_token.loc;
3894         let param_close_loc = param_close_token.loc;
3895         let body_close_loc = body_close_token.loc;
3896 
3897         self.check_unique_function_bindings(param_open_loc.start, param_close_loc.end)?;
3898 
3899         params.loc.set_range(param_open_loc, param_close_loc);
3900         body.loc.set_range(body_open_token.loc, body_close_loc);
3901 
3902         Ok(self.alloc_with(|| {
3903             MethodDefinition::Method(Method {
3904                 name: name.unbox(),
3905                 is_async: false,
3906                 is_generator: false,
3907                 params: params.unbox(),
3908                 body: body.unbox(),
3909                 loc: SourceLocation::from_parts(name_loc, body_close_loc),
3910             })
3911         }))
3912     }
3913 
3914     // MethodDefinition : `get` ClassElementName `(` `)` `{` FunctionBody `}`
getter( &self, get_token: arena::Box<'alloc, Token>, name: arena::Box<'alloc, ClassElementName<'alloc>>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, MethodDefinition<'alloc>>3915     pub fn getter(
3916         &self,
3917         get_token: arena::Box<'alloc, Token>,
3918         name: arena::Box<'alloc, ClassElementName<'alloc>>,
3919         body_open_token: arena::Box<'alloc, Token>,
3920         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3921         body_close_token: arena::Box<'alloc, Token>,
3922     ) -> arena::Box<'alloc, MethodDefinition<'alloc>> {
3923         let body_close_loc = body_close_token.loc;
3924         body.loc.set_range(body_open_token.loc, body_close_loc);
3925         self.alloc_with(|| {
3926             MethodDefinition::Getter(Getter {
3927                 property_name: name.unbox(),
3928                 body: body.unbox(),
3929                 loc: SourceLocation::from_parts(get_token.loc, body_close_loc),
3930             })
3931         })
3932     }
3933 
3934     // MethodDefinition : `set` ClassElementName `(` PropertySetParameterList `)` `{` FunctionBody `}`
setter( &mut self, set_token: arena::Box<'alloc, Token>, name: arena::Box<'alloc, ClassElementName<'alloc>>, param_open_token: arena::Box<'alloc, Token>, mut parameter: arena::Box<'alloc, Parameter<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>>3935     pub fn setter(
3936         &mut self,
3937         set_token: arena::Box<'alloc, Token>,
3938         name: arena::Box<'alloc, ClassElementName<'alloc>>,
3939         param_open_token: arena::Box<'alloc, Token>,
3940         mut parameter: arena::Box<'alloc, Parameter<'alloc>>,
3941         param_close_token: arena::Box<'alloc, Token>,
3942         body_open_token: arena::Box<'alloc, Token>,
3943         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3944         body_close_token: arena::Box<'alloc, Token>,
3945     ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>> {
3946         let param_open_loc = param_open_token.loc;
3947         let param_close_loc = param_close_token.loc;
3948         let body_close_loc = body_close_token.loc;
3949 
3950         // A setter only has one parameter, but it can be a destructuring
3951         // pattern, so it is still possible to flunk this check.
3952         self.check_unique_function_bindings(param_open_loc.start, param_close_loc.end)?;
3953 
3954         parameter.set_loc(param_open_loc, param_close_loc);
3955         body.loc.set_range(body_open_token.loc, body_close_loc);
3956         Ok(self.alloc_with(|| {
3957             MethodDefinition::Setter(Setter {
3958                 property_name: name.unbox(),
3959                 param: parameter.unbox(),
3960                 body: body.unbox(),
3961                 loc: SourceLocation::from_parts(set_token.loc, body_close_loc),
3962             })
3963         }))
3964     }
3965 
3966     // GeneratorMethod : `*` ClassElementName `(` UniqueFormalParameters `)` `{` GeneratorBody `}`
generator_method( &mut self, generator_token: arena::Box<'alloc, Token>, name: arena::Box<'alloc, ClassElementName<'alloc>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>>3967     pub fn generator_method(
3968         &mut self,
3969         generator_token: arena::Box<'alloc, Token>,
3970         name: arena::Box<'alloc, ClassElementName<'alloc>>,
3971         param_open_token: arena::Box<'alloc, Token>,
3972         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
3973         param_close_token: arena::Box<'alloc, Token>,
3974         body_open_token: arena::Box<'alloc, Token>,
3975         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
3976         body_close_token: arena::Box<'alloc, Token>,
3977     ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>> {
3978         let param_open_loc = param_open_token.loc;
3979         let param_close_loc = param_close_token.loc;
3980         let body_close_loc = body_close_token.loc;
3981 
3982         self.check_unique_function_bindings(param_open_loc.start, param_close_loc.end)?;
3983 
3984         params.loc.set_range(param_open_loc, param_close_loc);
3985         body.loc.set_range(body_open_token.loc, body_close_loc);
3986 
3987         Ok(self.alloc_with(|| {
3988             MethodDefinition::Method(Method {
3989                 name: name.unbox(),
3990                 is_async: false,
3991                 is_generator: true,
3992                 params: params.unbox(),
3993                 body: body.unbox(),
3994                 loc: SourceLocation::from_parts(generator_token.loc, body_close_loc),
3995             })
3996         }))
3997     }
3998 
3999     // YieldExpression : `yield`
4000     // YieldExpression : `yield` AssignmentExpression
yield_expr( &self, yield_token: arena::Box<'alloc, Token>, operand: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, Expression<'alloc>>4001     pub fn yield_expr(
4002         &self,
4003         yield_token: arena::Box<'alloc, Token>,
4004         operand: Option<arena::Box<'alloc, Expression<'alloc>>>,
4005     ) -> arena::Box<'alloc, Expression<'alloc>> {
4006         let yield_loc = yield_token.loc;
4007         let loc = match operand {
4008             Some(ref operand) => SourceLocation::from_parts(yield_loc, operand.get_loc()),
4009             None => yield_loc,
4010         };
4011 
4012         self.alloc_with(|| Expression::YieldExpression {
4013             expression: operand,
4014             loc,
4015         })
4016     }
4017 
4018     // YieldExpression : `yield` `*` AssignmentExpression
yield_star_expr( &self, yield_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>4019     pub fn yield_star_expr(
4020         &self,
4021         yield_token: arena::Box<'alloc, Token>,
4022         operand: arena::Box<'alloc, Expression<'alloc>>,
4023     ) -> arena::Box<'alloc, Expression<'alloc>> {
4024         let yield_loc = yield_token.loc;
4025         let operand_loc = operand.get_loc();
4026         self.alloc_with(|| Expression::YieldGeneratorExpression {
4027             expression: operand,
4028             loc: SourceLocation::from_parts(yield_loc, operand_loc),
4029         })
4030     }
4031 
4032     // AsyncGeneratorMethod ::= "async" "*" ClassElementName "(" UniqueFormalParameters ")" "{" AsyncGeneratorBody "}"
async_generator_method( &mut self, async_token: arena::Box<'alloc, Token>, name: arena::Box<'alloc, ClassElementName<'alloc>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>>4033     pub fn async_generator_method(
4034         &mut self,
4035         async_token: arena::Box<'alloc, Token>,
4036         name: arena::Box<'alloc, ClassElementName<'alloc>>,
4037         param_open_token: arena::Box<'alloc, Token>,
4038         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
4039         param_close_token: arena::Box<'alloc, Token>,
4040         body_open_token: arena::Box<'alloc, Token>,
4041         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
4042         body_close_token: arena::Box<'alloc, Token>,
4043     ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>> {
4044         let param_open_loc = param_open_token.loc;
4045         let param_close_loc = param_close_token.loc;
4046         let body_close_loc = body_close_token.loc;
4047 
4048         self.check_unique_function_bindings(param_open_loc.start, param_close_loc.end)?;
4049 
4050         params.loc.set_range(param_open_loc, param_close_loc);
4051         body.loc.set_range(body_open_token.loc, body_close_loc);
4052 
4053         Ok(self.alloc_with(|| {
4054             MethodDefinition::Method(Method {
4055                 name: name.unbox(),
4056                 is_async: true,
4057                 is_generator: true,
4058                 params: params.unbox(),
4059                 body: body.unbox(),
4060                 loc: SourceLocation::from_parts(async_token.loc, body_close_loc),
4061             })
4062         }))
4063     }
4064 
4065     // ClassDeclaration : `class` BindingIdentifier ClassTail
4066     // ClassDeclaration : `class` ClassTail
class_declaration( &mut self, class_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, tail: arena::Box<'alloc, ClassExpression<'alloc>>, ) -> arena::Box<'alloc, Statement<'alloc>>4067     pub fn class_declaration(
4068         &mut self,
4069         class_token: arena::Box<'alloc, Token>,
4070         name: Option<arena::Box<'alloc, BindingIdentifier>>,
4071         tail: arena::Box<'alloc, ClassExpression<'alloc>>,
4072     ) -> arena::Box<'alloc, Statement<'alloc>> {
4073         let class_loc = class_token.loc;
4074 
4075         self.context_metadata
4076             .mark_binding_kind(class_loc.start, None, BindingKind::Class);
4077 
4078         let tail = tail.unbox();
4079         let tail_loc = tail.loc;
4080         self.alloc_with(|| {
4081             Statement::ClassDeclaration(ClassDeclaration {
4082                 name: match name {
4083                     None => {
4084                         let loc = SourceLocation::new(class_loc.end, class_loc.end);
4085                         BindingIdentifier {
4086                             name: Identifier {
4087                                 value: CommonSourceAtomSetIndices::default(),
4088                                 loc,
4089                             },
4090                             loc,
4091                         }
4092                     }
4093                     Some(bi) => bi.unbox(),
4094                 },
4095                 super_: tail.super_,
4096                 elements: tail.elements,
4097                 loc: SourceLocation::from_parts(class_loc, tail_loc),
4098             })
4099         })
4100     }
4101 
4102     // ClassExpression : `class` BindingIdentifier? ClassTail
class_expression( &mut self, class_token: arena::Box<'alloc, Token>, name: Option<arena::Box<'alloc, BindingIdentifier>>, mut tail: arena::Box<'alloc, ClassExpression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>4103     pub fn class_expression(
4104         &mut self,
4105         class_token: arena::Box<'alloc, Token>,
4106         name: Option<arena::Box<'alloc, BindingIdentifier>>,
4107         mut tail: arena::Box<'alloc, ClassExpression<'alloc>>,
4108     ) -> arena::Box<'alloc, Expression<'alloc>> {
4109         let offset = class_token.loc.start;
4110         let index = self.context_metadata.find_first_binding(offset);
4111         self.context_metadata.pop_bindings_from(index);
4112 
4113         tail.name = name.map(|boxed| boxed.unbox());
4114         tail.loc.start = class_token.loc.start;
4115         self.alloc_with(|| Expression::ClassExpression(tail.unbox()))
4116     }
4117 
4118     // ClassTail : ClassHeritage? `{` ClassBody? `}`
class_tail( &self, heritage: Option<arena::Box<'alloc, Expression<'alloc>>>, body: Option< arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>, >, body_close_token: arena::Box<'alloc, Token>, ) -> arena::Box<'alloc, ClassExpression<'alloc>>4119     pub fn class_tail(
4120         &self,
4121         heritage: Option<arena::Box<'alloc, Expression<'alloc>>>,
4122         body: Option<
4123             arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>,
4124         >,
4125         body_close_token: arena::Box<'alloc, Token>,
4126     ) -> arena::Box<'alloc, ClassExpression<'alloc>> {
4127         self.alloc_with(|| ClassExpression {
4128             name: None,
4129             super_: heritage,
4130             elements: match body {
4131                 None => self.new_vec(),
4132                 Some(boxed) => boxed.unbox(),
4133             },
4134             // `start` of this will be overwritten once the enclosing class
4135             // gets parsed.
4136             loc: body_close_token.loc,
4137         })
4138     }
4139 
4140     // ClassElementList : ClassElementList ClassElement
class_element_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>, mut element: arena::Box< 'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>, >, ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>4141     pub fn class_element_list_append(
4142         &self,
4143         mut list: arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>,
4144         mut element: arena::Box<
4145             'alloc,
4146             arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>,
4147         >,
4148     ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>> {
4149         self.append(&mut list, &mut element);
4150         list
4151     }
4152 
4153     // FieldDefinition : ClassElementName Initializer?
class_field_definition( &self, name: arena::Box<'alloc, ClassElementName<'alloc>>, init: Option<arena::Box<'alloc, Expression<'alloc>>>, ) -> arena::Box<'alloc, ClassElement<'alloc>>4154     pub fn class_field_definition(
4155         &self,
4156         name: arena::Box<'alloc, ClassElementName<'alloc>>,
4157         init: Option<arena::Box<'alloc, Expression<'alloc>>>,
4158     ) -> arena::Box<'alloc, ClassElement<'alloc>> {
4159         let name_loc = name.get_loc();
4160         let loc = match &init {
4161             None => name_loc,
4162             Some(expr) => SourceLocation::from_parts(name_loc, expr.get_loc()),
4163         };
4164         self.alloc_with(|| ClassElement::FieldDefinition {
4165             name: name.unbox(),
4166             init,
4167             loc,
4168         })
4169     }
4170 
4171     // ClassElementName : PropertyName
property_name_to_class_element_name( &self, name: arena::Box<'alloc, PropertyName<'alloc>>, ) -> arena::Box<'alloc, ClassElementName<'alloc>>4172     pub fn property_name_to_class_element_name(
4173         &self,
4174         name: arena::Box<'alloc, PropertyName<'alloc>>,
4175     ) -> arena::Box<'alloc, ClassElementName<'alloc>> {
4176         self.alloc_with(|| match name.unbox() {
4177             PropertyName::ComputedPropertyName(cpn) => ClassElementName::ComputedPropertyName(cpn),
4178             PropertyName::StaticPropertyName(spn) => ClassElementName::StaticPropertyName(spn),
4179             PropertyName::StaticNumericPropertyName(snpn) => {
4180                 ClassElementName::StaticNumericPropertyName(snpn)
4181             }
4182         })
4183     }
4184 
4185     // ClassElementName : PrivateIdentifier
class_element_name_private( &self, private_identifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, ClassElementName<'alloc>>>4186     pub fn class_element_name_private(
4187         &self,
4188         private_identifier: arena::Box<'alloc, Token>,
4189     ) -> Result<'alloc, arena::Box<'alloc, ClassElementName<'alloc>>> {
4190         let name = self.private_identifier(private_identifier)?;
4191         Ok(self.alloc_with(|| ClassElementName::PrivateFieldName(name)))
4192     }
4193 
4194     // ClassElement : MethodDefinition
class_element( &self, method: arena::Box<'alloc, MethodDefinition<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>4195     pub fn class_element(
4196         &self,
4197         method: arena::Box<'alloc, MethodDefinition<'alloc>>,
4198     ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>> {
4199         let loc = method.get_loc();
4200         self.class_element_to_vec(self.alloc_with(|| ClassElement::MethodDefinition {
4201             is_static: false,
4202             method: method.unbox(),
4203             loc,
4204         }))
4205     }
4206 
4207     // ClassElement : FieldDefinition `;`
class_element_to_vec( &self, element: arena::Box<'alloc, ClassElement<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>4208     pub fn class_element_to_vec(
4209         &self,
4210         element: arena::Box<'alloc, ClassElement<'alloc>>,
4211     ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>> {
4212         self.alloc_with(|| self.new_vec_single(element))
4213     }
4214 
4215     // ClassElement : `static` MethodDefinition
class_element_static( &self, static_token: arena::Box<'alloc, Token>, method: arena::Box<'alloc, MethodDefinition<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>4216     pub fn class_element_static(
4217         &self,
4218         static_token: arena::Box<'alloc, Token>,
4219         method: arena::Box<'alloc, MethodDefinition<'alloc>>,
4220     ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>> {
4221         let method_loc = method.get_loc();
4222         self.alloc_with(|| {
4223             self.new_vec_single(self.alloc_with(|| ClassElement::MethodDefinition {
4224                 is_static: true,
4225                 method: method.unbox(),
4226                 loc: SourceLocation::from_parts(static_token.loc, method_loc),
4227             }))
4228         })
4229     }
4230 
4231     // ClassElement : `static` MethodDefinition
class_element_static_field( &self, _static_token: arena::Box<'alloc, Token>, _field: arena::Box<'alloc, ClassElement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4232     pub fn class_element_static_field(
4233         &self,
4234         _static_token: arena::Box<'alloc, Token>,
4235         _field: arena::Box<'alloc, ClassElement<'alloc>>,
4236     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4237         Err(ParseError::NotImplemented("class static field").into())
4238     }
4239 
4240     // ClassElement : `;`
class_element_empty( &self, ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>>4241     pub fn class_element_empty(
4242         &self,
4243     ) -> arena::Box<'alloc, arena::Vec<'alloc, arena::Box<'alloc, ClassElement<'alloc>>>> {
4244         self.alloc_with(|| self.new_vec())
4245     }
4246 
4247     // AsyncMethod : `async` ClassElementName `(` UniqueFormalParameters `)` `{` AsyncFunctionBody `}`
async_method( &mut self, async_token: arena::Box<'alloc, Token>, name: arena::Box<'alloc, ClassElementName<'alloc>>, param_open_token: arena::Box<'alloc, Token>, mut params: arena::Box<'alloc, FormalParameters<'alloc>>, param_close_token: arena::Box<'alloc, Token>, body_open_token: arena::Box<'alloc, Token>, mut body: arena::Box<'alloc, FunctionBody<'alloc>>, body_close_token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>>4248     pub fn async_method(
4249         &mut self,
4250         async_token: arena::Box<'alloc, Token>,
4251         name: arena::Box<'alloc, ClassElementName<'alloc>>,
4252         param_open_token: arena::Box<'alloc, Token>,
4253         mut params: arena::Box<'alloc, FormalParameters<'alloc>>,
4254         param_close_token: arena::Box<'alloc, Token>,
4255         body_open_token: arena::Box<'alloc, Token>,
4256         mut body: arena::Box<'alloc, FunctionBody<'alloc>>,
4257         body_close_token: arena::Box<'alloc, Token>,
4258     ) -> Result<'alloc, arena::Box<'alloc, MethodDefinition<'alloc>>> {
4259         let param_open_loc = param_open_token.loc;
4260         let param_close_loc = param_close_token.loc;
4261         let body_close_loc = body_close_token.loc;
4262 
4263         self.check_unique_function_bindings(param_open_loc.start, param_close_loc.end)?;
4264 
4265         params.loc.set_range(param_open_loc, param_close_loc);
4266         body.loc.set_range(body_open_token.loc, body_close_loc);
4267 
4268         Ok(self.alloc_with(|| {
4269             MethodDefinition::Method(Method {
4270                 name: name.unbox(),
4271                 is_async: true,
4272                 is_generator: false,
4273                 params: params.unbox(),
4274                 body: body.unbox(),
4275                 loc: SourceLocation::from_parts(async_token.loc, body_close_loc),
4276             })
4277         }))
4278     }
4279 
4280     // AwaitExpression : `await` UnaryExpression
await_expr( &self, await_token: arena::Box<'alloc, Token>, operand: arena::Box<'alloc, Expression<'alloc>>, ) -> arena::Box<'alloc, Expression<'alloc>>4281     pub fn await_expr(
4282         &self,
4283         await_token: arena::Box<'alloc, Token>,
4284         operand: arena::Box<'alloc, Expression<'alloc>>,
4285     ) -> arena::Box<'alloc, Expression<'alloc>> {
4286         let operand_loc = operand.get_loc();
4287         self.alloc_with(|| Expression::AwaitExpression {
4288             expression: operand,
4289             loc: SourceLocation::from_parts(await_token.loc, operand_loc),
4290         })
4291     }
4292 
4293     // AsyncArrowFunction : `async` AsyncArrowBindingIdentifier `=>` AsyncConciseBody
4294     // AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead `=>` AsyncConciseBody
async_arrow_function_bare( &mut self, async_token: arena::Box<'alloc, Token>, identifier: arena::Box<'alloc, BindingIdentifier>, body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>4295     pub fn async_arrow_function_bare(
4296         &mut self,
4297         async_token: arena::Box<'alloc, Token>,
4298         identifier: arena::Box<'alloc, BindingIdentifier>,
4299         body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>,
4300     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
4301         let params = self.arrow_parameters_bare(identifier);
4302 
4303         self.check_unique_function_bindings(params.loc.start, params.loc.end)?;
4304 
4305         let body_loc = body.get_loc();
4306         Ok(self.alloc_with(|| Expression::ArrowExpression {
4307             is_async: true,
4308             params: params.unbox(),
4309             body: body.unbox(),
4310             loc: SourceLocation::from_parts(async_token.loc, body_loc),
4311         }))
4312     }
4313 
async_arrow_function( &mut self, params: arena::Box<'alloc, Expression<'alloc>>, body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>>4314     pub fn async_arrow_function(
4315         &mut self,
4316         params: arena::Box<'alloc, Expression<'alloc>>,
4317         body: arena::Box<'alloc, ArrowExpressionBody<'alloc>>,
4318     ) -> Result<'alloc, arena::Box<'alloc, Expression<'alloc>>> {
4319         let (params, call_loc) = self.async_arrow_parameters(params)?;
4320 
4321         self.check_unique_function_bindings(params.loc.start, params.loc.end)?;
4322 
4323         let body_loc = body.get_loc();
4324         Ok(self.alloc_with(|| Expression::ArrowExpression {
4325             is_async: true,
4326             params: params.unbox(),
4327             body: body.unbox(),
4328             loc: SourceLocation::from_parts(call_loc, body_loc),
4329         }))
4330     }
4331 
4332     // AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead `=>` AsyncConciseBody
4333     //
4334     // This is used to convert the Expression that is produced by parsing a CoverCallExpressionAndAsyncArrowHead
async_arrow_parameters( &self, call_expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, (arena::Box<'alloc, FormalParameters<'alloc>>, SourceLocation)>4335     fn async_arrow_parameters(
4336         &self,
4337         call_expression: arena::Box<'alloc, Expression<'alloc>>,
4338     ) -> Result<'alloc, (arena::Box<'alloc, FormalParameters<'alloc>>, SourceLocation)> {
4339         match call_expression.unbox() {
4340             Expression::CallExpression(CallExpression {
4341                 callee: ce,
4342                 arguments,
4343                 loc,
4344             }) => {
4345                 // Check that `callee` is `async`.
4346                 match ce {
4347                     ExpressionOrSuper::Expression(callee) => match callee.unbox() {
4348                         Expression::IdentifierExpression(IdentifierExpression { name, .. }) => {
4349                             if name.value != CommonSourceAtomSetIndices::async_() {
4350                                 // `foo(a, b) => {}`
4351                                 return Err(ParseError::ArrowHeadInvalid.into());
4352                             }
4353                         }
4354                         _ => {
4355                             // `obj.async() => {}`
4356                             return Err(ParseError::ArrowHeadInvalid.into());
4357                         }
4358                     },
4359 
4360                     ExpressionOrSuper::Super { .. } => {
4361                         // Can't happen: `super()` doesn't match
4362                         // CoverCallExpressionAndAsyncArrowHead.
4363                         return Err(ParseError::ArrowHeadInvalid.into());
4364                     }
4365                 }
4366 
4367                 Ok((self.arguments_to_parameter_list(arguments)?, loc))
4368             }
4369             _ => {
4370                 // The grammar ensures that the parser always passes
4371                 // a valid CallExpression to this function.
4372                 panic!("invalid argument");
4373             }
4374         }
4375     }
4376 
4377     // Script : ScriptBody?
script( &mut self, script: Option<arena::Box<'alloc, Script<'alloc>>>, ) -> Result<'alloc, arena::Box<'alloc, Script<'alloc>>>4378     pub fn script(
4379         &mut self,
4380         script: Option<arena::Box<'alloc, Script<'alloc>>>,
4381     ) -> Result<'alloc, arena::Box<'alloc, Script<'alloc>>> {
4382         self.check_script_bindings()?;
4383 
4384         Ok(match script {
4385             Some(script) => script,
4386             None => self.alloc_with(|| Script {
4387                 directives: self.new_vec(),
4388                 statements: self.new_vec(),
4389                 loc: SourceLocation::default(),
4390             }),
4391         })
4392     }
4393 
4394     // ScriptBody : StatementList
script_body( &self, statements: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>, ) -> arena::Box<'alloc, Script<'alloc>>4395     pub fn script_body(
4396         &self,
4397         statements: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>,
4398     ) -> arena::Box<'alloc, Script<'alloc>> {
4399         let loc = if statements.is_empty() {
4400             SourceLocation::default()
4401         } else {
4402             SourceLocation::from_parts(
4403                 statements.first().unwrap().get_loc(),
4404                 statements.last().unwrap().get_loc(),
4405             )
4406         };
4407 
4408         // TODO: directives
4409         self.alloc_with(|| Script {
4410             directives: self.new_vec(),
4411             statements: statements.unbox(),
4412             loc,
4413         })
4414     }
4415 
4416     // Module : ModuleBody?
module( &mut self, body: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>, ) -> Result<'alloc, arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>4417     pub fn module(
4418         &mut self,
4419         body: Option<arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>>,
4420     ) -> Result<'alloc, arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>> {
4421         self.check_module_bindings()?;
4422 
4423         Ok(body.unwrap_or_else(|| self.alloc_with(|| self.new_vec())))
4424     }
4425 
4426     // ModuleItemList : ModuleItem
module_item_list_single( &self, item: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>4427     pub fn module_item_list_single(
4428         &self,
4429         item: arena::Box<'alloc, Statement<'alloc>>,
4430     ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>> {
4431         self.alloc_with(|| self.new_vec_single(item.unbox()))
4432     }
4433 
4434     // ModuleItemList : ModuleItemList ModuleItem
module_item_list_append( &self, mut list: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>, item: arena::Box<'alloc, Statement<'alloc>>, ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>4435     pub fn module_item_list_append(
4436         &self,
4437         mut list: arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>>,
4438         item: arena::Box<'alloc, Statement<'alloc>>,
4439     ) -> arena::Box<'alloc, arena::Vec<'alloc, Statement<'alloc>>> {
4440         self.push(&mut list, item.unbox());
4441         list
4442     }
4443 
4444     // ImportDeclaration : `import` ImportClause FromClause `;`
4445     // ImportDeclaration : `import` ModuleSpecifier `;`
import_declaration( &self, _import_clause: Option<arena::Box<'alloc, Void>>, _module_specifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4446     pub fn import_declaration(
4447         &self,
4448         _import_clause: Option<arena::Box<'alloc, Void>>,
4449         _module_specifier: arena::Box<'alloc, Token>,
4450     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4451         Err(ParseError::NotImplemented("import").into())
4452     }
4453 
4454     // ImportClause : ImportedDefaultBinding
4455     // ImportClause : NameSpaceImport
4456     // ImportClause : NamedImports
4457     // ImportClause : ImportedDefaultBinding `,` NameSpaceImport
4458     // ImportClause : ImportedDefaultBinding `,` NamedImports
import_clause( &self, _default_binding: Option<arena::Box<'alloc, BindingIdentifier>>, _name_space_import: Option<arena::Box<'alloc, Void>>, _named_imports: Option<arena::Box<'alloc, Void>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4459     pub fn import_clause(
4460         &self,
4461         _default_binding: Option<arena::Box<'alloc, BindingIdentifier>>,
4462         _name_space_import: Option<arena::Box<'alloc, Void>>,
4463         _named_imports: Option<arena::Box<'alloc, Void>>,
4464     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4465         Err(ParseError::NotImplemented("import").into())
4466     }
4467 
4468     // NameSpaceImport : `*` `as` ImportedBinding
name_space_import( &self, _name: arena::Box<'alloc, BindingIdentifier>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4469     pub fn name_space_import(
4470         &self,
4471         _name: arena::Box<'alloc, BindingIdentifier>,
4472     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4473         Err(ParseError::NotImplemented("import").into())
4474     }
4475 
4476     // NamedImports : `{` `}`
imports_list_empty(&self) -> Result<'alloc, arena::Box<'alloc, Void>>4477     pub fn imports_list_empty(&self) -> Result<'alloc, arena::Box<'alloc, Void>> {
4478         Err(ParseError::NotImplemented("import").into())
4479     }
4480 
4481     // ImportsList : ImportSpecifier
4482     // ImportsList : ImportsList `,` ImportSpecifier
imports_list_append( &self, _list: arena::Box<'alloc, Void>, _item: arena::Box<'alloc, Void>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4483     pub fn imports_list_append(
4484         &self,
4485         _list: arena::Box<'alloc, Void>,
4486         _item: arena::Box<'alloc, Void>,
4487     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4488         Err(ParseError::NotImplemented("import").into())
4489     }
4490 
4491     // ImportSpecifier : ImportedBinding
import_specifier( &self, _name: arena::Box<'alloc, BindingIdentifier>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4492     pub fn import_specifier(
4493         &self,
4494         _name: arena::Box<'alloc, BindingIdentifier>,
4495     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4496         Err(ParseError::NotImplemented("import").into())
4497     }
4498 
4499     // ImportSpecifier : IdentifierName `as` ImportedBinding
import_specifier_renaming( &self, _original_name: arena::Box<'alloc, Token>, _local_name: arena::Box<'alloc, BindingIdentifier>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4500     pub fn import_specifier_renaming(
4501         &self,
4502         _original_name: arena::Box<'alloc, Token>,
4503         _local_name: arena::Box<'alloc, BindingIdentifier>,
4504     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4505         Err(ParseError::NotImplemented("import").into())
4506     }
4507 
4508     // ModuleSpecifier : StringLiteral
module_specifier( &self, _token: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Token>>4509     pub fn module_specifier(
4510         &self,
4511         _token: arena::Box<'alloc, Token>,
4512     ) -> Result<'alloc, arena::Box<'alloc, Token>> {
4513         Err(ParseError::NotImplemented("import").into())
4514     }
4515 
4516     // ExportDeclaration : `export` `*` FromClause `;`
export_all_from( &self, _module_specifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4517     pub fn export_all_from(
4518         &self,
4519         _module_specifier: arena::Box<'alloc, Token>,
4520     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4521         Err(ParseError::NotImplemented("export").into())
4522     }
4523 
4524     // ExportDeclaration : `export` ExportClause FromClause `;`
export_set_from( &self, _export_clause: arena::Box<'alloc, Void>, _module_specifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4525     pub fn export_set_from(
4526         &self,
4527         _export_clause: arena::Box<'alloc, Void>,
4528         _module_specifier: arena::Box<'alloc, Token>,
4529     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4530         Err(ParseError::NotImplemented("export").into())
4531     }
4532 
4533     // ExportDeclaration : `export` ExportClause `;`
export_set( &self, _export_clause: arena::Box<'alloc, Void>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4534     pub fn export_set(
4535         &self,
4536         _export_clause: arena::Box<'alloc, Void>,
4537     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4538         Err(ParseError::NotImplemented("export").into())
4539     }
4540 
4541     // ExportDeclaration : `export` VariableStatement
export_vars( &self, _statement: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4542     pub fn export_vars(
4543         &self,
4544         _statement: arena::Box<'alloc, Statement<'alloc>>,
4545     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4546         Err(ParseError::NotImplemented("export").into())
4547     }
4548 
4549     // ExportDeclaration : `export` Declaration
export_declaration( &self, _declaration: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4550     pub fn export_declaration(
4551         &self,
4552         _declaration: arena::Box<'alloc, Statement<'alloc>>,
4553     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4554         Err(ParseError::NotImplemented("export").into())
4555     }
4556 
4557     // ExportDeclaration : `export` `default` HoistableDeclaration
export_default_hoistable( &self, _declaration: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4558     pub fn export_default_hoistable(
4559         &self,
4560         _declaration: arena::Box<'alloc, Statement<'alloc>>,
4561     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4562         Err(ParseError::NotImplemented("export").into())
4563     }
4564 
4565     // ExportDeclaration : `export` `default` ClassDeclaration
export_default_class( &self, _class_declaration: arena::Box<'alloc, Statement<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4566     pub fn export_default_class(
4567         &self,
4568         _class_declaration: arena::Box<'alloc, Statement<'alloc>>,
4569     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4570         Err(ParseError::NotImplemented("export").into())
4571     }
4572 
4573     // ExportDeclaration : `export` `default` [lookahead <! {`function`, `async`, `class`}] AssignmentExpression `;`
export_default_value( &self, _expression: arena::Box<'alloc, Expression<'alloc>>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4574     pub fn export_default_value(
4575         &self,
4576         _expression: arena::Box<'alloc, Expression<'alloc>>,
4577     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4578         Err(ParseError::NotImplemented("export").into())
4579     }
4580 
4581     // ExportClause : `{` `}`
exports_list_empty(&self) -> Result<'alloc, arena::Box<'alloc, Void>>4582     pub fn exports_list_empty(&self) -> Result<'alloc, arena::Box<'alloc, Void>> {
4583         Err(ParseError::NotImplemented("export").into())
4584     }
4585 
4586     // ExportsList : ExportSpecifier
4587     // ExportsList : ExportsList `,` ExportSpecifier
exports_list_append( &self, _list: arena::Box<'alloc, Void>, _export_specifier: arena::Box<'alloc, Void>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4588     pub fn exports_list_append(
4589         &self,
4590         _list: arena::Box<'alloc, Void>,
4591         _export_specifier: arena::Box<'alloc, Void>,
4592     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4593         Err(ParseError::NotImplemented("export").into())
4594     }
4595 
4596     // ExportSpecifier : IdentifierName
export_specifier( &self, _identifier: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4597     pub fn export_specifier(
4598         &self,
4599         _identifier: arena::Box<'alloc, Token>,
4600     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4601         Err(ParseError::NotImplemented("export").into())
4602     }
4603 
4604     // ExportSpecifier : IdentifierName `as` IdentifierName
export_specifier_renaming( &self, _local_name: arena::Box<'alloc, Token>, _exported_name: arena::Box<'alloc, Token>, ) -> Result<'alloc, arena::Box<'alloc, Void>>4605     pub fn export_specifier_renaming(
4606         &self,
4607         _local_name: arena::Box<'alloc, Token>,
4608         _exported_name: arena::Box<'alloc, Token>,
4609     ) -> Result<'alloc, arena::Box<'alloc, Void>> {
4610         Err(ParseError::NotImplemented("export").into())
4611     }
4612 
4613     // Returns IsSimpleParameterList of `params`.
4614     //
4615     // NOTE: For Syntax-only parsing (NYI), the stack value for FormalParameters
4616     //       should contain this information.
is_params_simple(params: &FormalParameters<'alloc>) -> bool4617     fn is_params_simple(params: &FormalParameters<'alloc>) -> bool {
4618         for param in params.items.iter() {
4619             match param {
4620                 Parameter::Binding(Binding::BindingIdentifier(_)) => {}
4621                 _ => {
4622                     return false;
4623                 }
4624             }
4625         }
4626 
4627         if params.rest.is_some() {
4628             return false;
4629         }
4630 
4631         true
4632     }
4633 
mark_labelled_statement( &mut self, label: &arena::Box<'alloc, Label>, body: &Statement<'alloc>, )4634     fn mark_labelled_statement(
4635         &mut self,
4636         label: &arena::Box<'alloc, Label>,
4637         body: &Statement<'alloc>,
4638     ) {
4639         let start_label_offset = label.loc.start;
4640         let kind = match body {
4641             Statement::ForStatement { .. }
4642             | Statement::ForOfStatement { .. }
4643             | Statement::ForInStatement { .. }
4644             | Statement::WhileStatement { .. }
4645             | Statement::DoWhileStatement { .. } => LabelKind::Loop,
4646             Statement::LabelledStatement { .. } => LabelKind::LabelledLabel,
4647             Statement::FunctionDeclaration { .. } => LabelKind::Function,
4648             _ => LabelKind::Other,
4649         };
4650 
4651         self.context_metadata
4652             .mark_label_kind_at_offset(start_label_offset, kind);
4653     }
4654 }
4655 
4656 impl<'alloc> EarlyErrorChecker<'alloc> for AstBuilder<'alloc> {
context_metadata_mut(&mut self) -> &mut ContextMetadata4657     fn context_metadata_mut(&mut self) -> &mut ContextMetadata {
4658         &mut self.context_metadata
4659     }
context_metadata(&self) -> &ContextMetadata4660     fn context_metadata(&self) -> &ContextMetadata {
4661         &self.context_metadata
4662     }
atoms(&self) -> &Rc<RefCell<SourceAtomSet<'alloc>>>4663     fn atoms(&self) -> &Rc<RefCell<SourceAtomSet<'alloc>>> {
4664         &self.atoms
4665     }
4666 }
4667