mxmlextrema_as3parser/parser/
parser.rs

1use crate::ns::*;
2use maplit::hashmap;
3use std::{path::PathBuf, sync::{Arc, RwLock}};
4use lazy_regex::*;
5
6pub struct Parser<'input> {
7    tokenizer: Tokenizer<'input>,
8    previous_token: (Token, Location),
9    token: (Token, Location),
10    locations: Vec<Location>,
11    activations: Vec<ParserActivation>,
12    ignore_xml_whitespace: bool,
13    documentable_metadata: Vec<String>,
14    replace_included_content: Arc<RwLock<HashMap<PathBuf, String>>>,
15    expecting_token_error: bool,
16}
17
18impl<'input> Parser<'input> {
19    /// Constructs a parser.
20    pub fn new(compilation_unit: &'input Rc<CompilationUnit>, options: &ParserOptions) -> Self {
21        Self {
22            tokenizer: Tokenizer::new(compilation_unit, options),
23            previous_token: (Token::Eof, Location::with_offset(&compilation_unit, 0)),
24            token: (Token::Eof, Location::with_offset(&compilation_unit, 0)),
25            locations: vec![],
26            activations: vec![],
27            ignore_xml_whitespace: options.ignore_xml_whitespace,
28            documentable_metadata: options.documentable_metadata.clone(),
29            replace_included_content: options.replace_included_content.clone(),
30            expecting_token_error: false,
31        }
32    }
33
34    fn options(&self) -> ParserOptions {
35        ParserOptions {
36            ignore_xml_whitespace: self.ignore_xml_whitespace,
37            documentable_metadata: self.documentable_metadata.clone(),
38            replace_included_content: self.replace_included_content.clone(),
39            ..default()
40        }
41    }
42
43    fn compilation_unit(&self) -> &Rc<CompilationUnit> {
44        self.tokenizer.compilation_unit()
45    }
46
47    fn token_location(&self) -> Location {
48        self.token.1.clone()
49    }
50
51    fn mark_location(&mut self) {
52        self.locations.push(self.token.1.clone());
53    }
54
55    fn duplicate_location(&mut self) {
56        self.locations.push(self.locations.last().unwrap().clone());
57    }
58
59    fn push_location(&mut self, location: &Location) {
60        self.locations.push(location.clone());
61    }
62
63    fn pop_location(&mut self) -> Location {
64        self.locations.pop().unwrap().combine_with(self.previous_token.1.clone())
65    }
66
67    fn add_syntax_error(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
68        if self.compilation_unit().prevent_equal_offset_error(location) {
69            return;
70        }
71        self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(location, kind, arguments));
72    }
73
74    fn patch_syntax_error(&self, original: DiagnosticKind, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
75        if self.compilation_unit().diagnostics.borrow().is_empty() {
76            return;
77        }
78        if self.compilation_unit().diagnostics.borrow().last().unwrap().kind == original {
79            let loc = self.compilation_unit().diagnostics.borrow_mut().pop().unwrap().location();
80            self.compilation_unit().add_diagnostic(Diagnostic::new_syntax_error(&loc, kind, arguments));
81        }
82    }
83
84    /*
85    fn add_warning(&self, location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) {
86        if self.compilation_unit().prevent_equal_offset_warning(location) {
87            return;
88        }
89        self.compilation_unit().add_diagnostic(Diagnostic::new_warning(location, kind, arguments));
90    }
91    */
92
93    fn next(&mut self) {
94        self.previous_token = self.token.clone();
95        self.token = self.tokenizer.scan_ie_div();
96    }
97
98    fn next_ie_xml_tag(&mut self) {
99        self.previous_token = self.token.clone();
100        self.token = self.tokenizer.scan_ie_xml_tag();
101    }
102
103    fn next_ie_xml_content(&mut self) {
104        self.previous_token = self.token.clone();
105        self.token = self.tokenizer.scan_ie_xml_content();
106    }
107
108    fn peek(&self, token: Token) -> bool {
109        self.token.0 == token
110    }
111
112    fn peek_identifier(&self, reserved_words: bool) -> Option<(String, Location)> {
113        if let Token::Identifier(id) = self.token.0.clone() {
114            let location = self.token.1.clone();
115            Some((id, location))
116        } else {
117            if reserved_words {
118                if let Some(id) = self.token.0.reserved_word_name() {
119                    let location = self.token.1.clone();
120                    return Some((id, location));
121                }
122            }
123            None
124        }
125    }
126
127    fn peek_context_keyword(&self, name: &str) -> bool {
128        if let Token::Identifier(id) = self.token.0.clone() { id == name && self.token.1.character_count() == name.len() } else { false }
129    }
130
131    fn consume(&mut self, token: Token) -> bool {
132        if self.token.0 == token {
133            self.next();
134            true
135        } else {
136            false
137        }
138    }
139
140    fn consume_and_ie_xml_tag(&mut self, token: Token) -> bool {
141        if self.token.0 == token {
142            self.next_ie_xml_tag();
143            true
144        } else {
145            false
146        }
147    }
148
149    fn consume_and_ie_xml_content(&mut self, token: Token) -> bool {
150        if self.token.0 == token {
151            self.next_ie_xml_content();
152            true
153        } else {
154            false
155        }
156    }
157
158    fn consume_identifier(&mut self, reserved_words: bool) -> Option<(String, Location)> {
159        if let Token::Identifier(id) = self.token.0.clone() {
160            let location = self.token.1.clone();
161            self.next();
162            Some((id, location))
163        } else {
164            if reserved_words {
165                if let Some(id) = self.token.0.reserved_word_name() {
166                    let location = self.token.1.clone();
167                    self.next();
168                    return Some((id, location));
169                }
170            }
171            None
172        }
173    }
174
175    fn _consume_context_keyword(&mut self, name: &str) -> bool {
176        if let Token::Identifier(id) = self.token.0.clone() {
177            if id == name && self.token.1.character_count() == name.len() {
178                self.next();
179                true
180            } else {
181                false
182            }
183        } else {
184            false
185        }
186    }
187
188    fn expect(&mut self, token: Token) {
189        if self.token.0 != token {
190            self.expecting_token_error = true;
191            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
192            let expecting_identifier_name = token.is_identifier_name();
193            while self.token.0 != Token::Eof && (if expecting_identifier_name { self.token.0.is_identifier_name() } else { true }) {
194                self.next();
195                if self.token.0 == token {
196                    return;
197                }
198            }
199        } else {
200            self.expecting_token_error = false;
201            self.next();
202        }
203    }
204
205    /// Expects a token; but if it fails, does not skip any token.
206    fn non_greedy_expect(&mut self, token: Token) {
207        if self.token.0 != token {
208            self.expecting_token_error = true;
209            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
210        } else {
211            self.expecting_token_error = false;
212            self.next();
213        }
214    }
215
216    fn non_greedy_expect_virtual_semicolon(&mut self) {
217        self.expecting_token_error = false;
218        if !self.parse_semicolon() {
219            self.expecting_token_error = true;
220            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingEitherSemicolonOrNewLineHere, vec![]);
221        }
222    }
223
224    fn expect_and_ie_xml_tag(&mut self, token: Token) {
225        if self.token.0 != token {
226            self.expecting_token_error = true;
227            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
228            while self.token.0 != Token::Eof {
229                self.next_ie_xml_tag();
230                if self.token.0 == token {
231                    return;
232                }
233            }
234        } else {
235            self.expecting_token_error = false;
236            self.next_ie_xml_tag();
237        }
238    }
239
240    /// Expects a token; but if it fails, does not skip any token.
241    fn non_greedy_expect_and_ie_xml_tag(&mut self, token: Token) {
242        if self.token.0 != token {
243            self.expecting_token_error = true;
244            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
245        } else {
246            self.expecting_token_error = false;
247            self.next_ie_xml_tag();
248        }
249    }
250
251    fn expect_and_ie_xml_content(&mut self, token: Token) {
252        if self.token.0 != token {
253            self.expecting_token_error = true;
254            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
255            while self.token.0 != Token::Eof {
256                self.next_ie_xml_content();
257                if self.token.0 == token {
258                    return;
259                }
260            }
261        } else {
262            self.expecting_token_error = false;
263            self.next_ie_xml_content();
264        }
265    }
266
267    fn non_greedy_expect_and_ie_xml_content(&mut self, token: Token) {
268        if self.token.0 != token {
269            self.expecting_token_error = true;
270            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![token.clone(), self.token.0.clone()]);
271        } else {
272            self.expecting_token_error = false;
273            self.next_ie_xml_content();
274        }
275    }
276
277    fn expect_identifier(&mut self, reserved_words: bool) -> (String, Location) {
278        if let Token::Identifier(id) = self.token.0.clone() {
279            self.expecting_token_error = false;
280            let location = self.token.1.clone();
281            self.next();
282            (id, location)
283        } else {
284            if reserved_words {
285                if let Some(id) = self.token.0.reserved_word_name() {
286                    self.expecting_token_error = false;
287                    let location = self.token.1.clone();
288                    self.next();
289                    return (id, location);
290                }
291            }
292            self.expecting_token_error = true;
293            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
294            /*
295            while self.token.0 != Token::Eof && self.token.0.is_identifier_name() {
296                if let Some(id) = self.consume_identifier(reserved_words) {
297                    return id;
298                } else {
299                    self.next();
300                }
301            }
302            */
303            (INVALIDATED_IDENTIFIER.to_owned(), self.tokenizer.cursor_location())
304        }
305    }
306
307    fn _expect_context_keyword(&mut self, name: &str) {
308        if let Token::Identifier(id) = self.token.0.clone() {
309            if id == name && self.token.1.character_count() == name.len() {
310                self.expecting_token_error = false;
311                self.next();
312                return;
313            }
314        }
315        self.expecting_token_error = true;
316        self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![format!("'{name}'"), self.token.0.clone()]);
317        while self.token.0 != Token::Eof && self.token.0.is_identifier_name() {
318            if self._consume_context_keyword(name) {
319                return;
320            } else {
321                self.next();
322            }
323        }
324    }
325
326    fn non_greedy_expect_context_keyword(&mut self, name: &str) {
327        if let Token::Identifier(id) = self.token.0.clone() {
328            if id == name && self.token.1.character_count() == name.len() {
329                self.expecting_token_error = false;
330                self.next();
331                return;
332            }
333        }
334        self.expecting_token_error = true;
335        self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![format!("'{name}'"), self.token.0.clone()]);
336    }
337
338    /// Expects a greater-than symbol. If the facing token is not greater-than,
339    /// but starts with a greater-than symbol, the first character is shifted off
340    /// from the facing token.
341    fn _expect_type_parameters_gt(&mut self) {
342        self.expecting_token_error = false;
343        if !self.consume_type_parameters_gt() {
344            self.expecting_token_error = true;
345            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Gt, self.token.0.clone()]);
346            while self.token.0 != Token::Eof {
347                self.next();
348                if self.consume_type_parameters_gt() {
349                    return;
350                }
351            }
352        }
353    }
354
355    fn non_greedy_expect_type_parameters_gt(&mut self) {
356        self.expecting_token_error = false;
357        if !self.consume_type_parameters_gt() {
358            self.expecting_token_error = true;
359            self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Gt, self.token.0.clone()]);
360        }
361    }
362
363    /// Consumes a greater-than symbol. If the facing token is not greater-than,
364    /// but starts with a greater-than symbol, the first character is shifted off
365    /// from the facing token.
366    fn consume_type_parameters_gt(&mut self) -> bool {
367        match self.token.0 {
368            Token::Gt => {
369                self.next();
370                true
371            },
372            Token::Ge => {
373                self.token.0 = Token::Assign;
374                self.token.1.first_offset += 1;
375                true
376            },
377            Token::RightShift => {
378                self.token.0 = Token::Gt;
379                self.token.1.first_offset += 1;
380                true
381            },
382            Token::RightShiftAssign => {
383                self.token.0 = Token::Ge;
384                self.token.1.first_offset += 1;
385                true
386            },
387            Token::UnsignedRightShift => {
388                self.token.0 = Token::RightShift;
389                self.token.1.first_offset += 1;
390                true
391            },
392            Token::UnsignedRightShiftAssign => {
393                self.token.0 = Token::RightShiftAssign;
394                self.token.1.first_offset += 1;
395                true
396            },
397            _ => {
398                false
399            },
400        }
401    }
402
403    fn offending_token_is_inline_or_higher_indented(&self) -> bool {
404        if !self.previous_token.1.line_break(&self.token.1) {
405            return true;
406        }
407        let i1 = self.compilation_unit().get_line_indent(self.previous_token.1.first_line_number());
408        let i2 = self.compilation_unit().get_line_indent(self.token.1.first_line_number());
409        i2 > i1
410    }
411
412    pub fn expect_eof(&mut self) {
413        self.expect(Token::Eof)
414    }
415
416    fn create_invalidated_expression(&self, location: &Location) -> Rc<Expression> {
417        Rc::new(Expression::Invalidated(InvalidatedNode {
418            location: location.clone(),
419        }))
420    }
421
422    fn create_invalidated_directive(&self, location: &Location) -> Rc<Directive> {
423        Rc::new(Directive::Invalidated(InvalidatedNode {
424            location: location.clone(),
425        }))
426    }
427
428    pub fn parse_metadata(&mut self) -> (Vec<Attribute>, Option<Rc<Asdoc>>) {
429        let Some(exp) = self.parse_opt_expression(Default::default()) else {
430            return (vec![], self.parse_asdoc());
431        };
432        self.expect(Token::Eof);
433
434        match exp.to_metadata(self) {
435            Ok(Some(metadata)) => {
436                // For meta-data that are not one of certain Flex meta-data,
437                // delegate the respective ASDoc forward.
438                let mut new_metadata = Vec::<Attribute>::new();
439                let mut asdoc: Option<Rc<Asdoc>> = None;
440                for attr in &metadata {
441                    if let Attribute::Metadata(metadata) = attr {
442                        if !self.documentable_metadata.contains(&metadata.name.0) && metadata.asdoc.is_some() {
443                            new_metadata.push(Attribute::Metadata(Rc::new(Metadata {
444                                location: metadata.location.clone(),
445                                asdoc: None,
446                                name: metadata.name.clone(),
447                                entries: metadata.entries.clone(),
448                            })));
449                            asdoc = metadata.asdoc.clone();
450                        } else {
451                            new_metadata.push(attr.clone());
452                        }
453                    } else {
454                        new_metadata.push(attr.clone());
455                    }
456                }
457
458                (new_metadata, asdoc)
459            },
460            Ok(None) => {
461                self.add_syntax_error(&exp.location(), DiagnosticKind::UnallowedExpression, diagarg![]);
462                (vec![], None)
463            },
464            Err(MetadataRefineError1(MetadataRefineError::Syntax, loc)) => {
465                let asdoc = self.parse_asdoc();
466                self.add_syntax_error(&loc, DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]);
467                (vec![], asdoc)
468            },
469        }
470    }
471
472    pub fn parse_metadata_content(&mut self) -> Rc<Metadata> {
473        let loc1 = self.token.1.clone();
474        let Some(exp) = self.parse_opt_expression(Default::default()) else {
475            self.push_location(&loc1);
476            self.expect_identifier(false);
477            return Rc::new(Metadata {
478                location: self.pop_location(),
479                asdoc: None,
480                name: (INVALIDATED_IDENTIFIER.to_owned(), loc1),
481                entries: None,
482            });
483        };
484        self.expect(Token::Eof);
485
486        match self.refine_metadata(&exp, None) {
487            Ok(metadata) => {
488                metadata
489            },
490            Err(MetadataRefineError::Syntax) => {
491                self.push_location(&loc1);
492                self.add_syntax_error(&exp.location(), DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]);
493                Rc::new(Metadata {
494                    location: self.pop_location(),
495                    asdoc: None,
496                    name: (INVALIDATED_IDENTIFIER.to_owned(), loc1),
497                    entries: None,
498                })
499            },
500        }
501    }
502
503    pub fn parse_expression(&mut self, context: ParserExpressionContext) -> Rc<Expression> {
504        if let Some(exp) = self.parse_opt_expression(context) {
505            exp
506        } else {
507            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingExpression, diagarg![self.token.0.clone()]);
508            self.create_invalidated_expression(&self.tokenizer.cursor_location())
509        }
510    }
511
512    pub fn parse_opt_expression(&mut self, context: ParserExpressionContext) -> Option<Rc<Expression>> {
513        let exp: Option<Rc<Expression>> = self.parse_opt_start_expression(context.clone());
514
515        // Parse subexpressions
516        if let Some(exp) = exp {
517            return Some(self.parse_subexpressions(exp, context.clone()));
518        }
519        None
520    }
521
522    fn parse_subexpressions(&mut self, mut base: Rc<Expression>, context: ParserExpressionContext) -> Rc<Expression> {
523        loop {
524            if self.consume(Token::Dot) {
525                base = self.parse_dot_subexpression(base);
526            } else if self.consume(Token::OptionalChaining) {
527                base = self.parse_optional_chaining(base);
528            } else if self.peek(Token::SquareOpen) {
529                let asdoc = self.parse_asdoc();
530                self.next();
531                self.push_location(&base.location());
532                let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
533                self.non_greedy_expect(Token::SquareClose);
534                base = Rc::new(Expression::ComputedMember(ComputedMemberExpression {
535                    base, asdoc, key, location: self.pop_location()
536                }));
537            } else if self.consume(Token::Descendants) {
538                self.push_location(&base.location());
539                let id = self.parse_qualified_identifier();
540                base = Rc::new(Expression::Descendants(DescendantsExpression {
541                    location: self.pop_location(),
542                    base,
543                    identifier: id,
544                }));
545            } else if self.peek(Token::ParenOpen) {
546                self.push_location(&base.location());
547                let arguments = self.parse_arguments();
548                base = Rc::new(Expression::Call(CallExpression {
549                    location: self.pop_location(),
550                    base,
551                    arguments,
552                }));
553            } else if self.peek(Token::Increment) && !self.previous_token.1.line_break(&self.token.1) {
554                self.push_location(&base.location());
555                self.next();
556                base = Rc::new(Expression::Unary(UnaryExpression {
557                    location: self.pop_location(),
558                    expression: base,
559                    operator: Operator::PostIncrement,
560                }));
561            } else if self.peek(Token::Decrement) && !self.previous_token.1.line_break(&self.token.1) {
562                self.push_location(&base.location());
563                self.next();
564                base = Rc::new(Expression::Unary(UnaryExpression {
565                    location: self.pop_location(),
566                    expression: base,
567                    operator: Operator::PostDecrement,
568                }));
569            } else if self.peek(Token::Exclamation) && !self.previous_token.1.line_break(&self.token.1) {
570                self.push_location(&base.location());
571                self.next();
572                base = Rc::new(Expression::Unary(UnaryExpression {
573                    location: self.pop_location(),
574                    expression: base, operator: Operator::NonNull,
575                }));
576            // `not in`
577            } else if self.token.0 == Token::Not && context.allow_in && context.min_precedence.includes(&OperatorPrecedence::Relational) && !self.previous_token.1.line_break(&self.token.1) {
578                self.push_location(&base.location());
579                self.next();
580                self.non_greedy_expect(Token::In);
581                base = self.parse_binary_operator(base, Operator::NotIn, OperatorPrecedence::Relational.add(1).unwrap(), context.clone());
582            // ConditionalExpression
583            } else if self.peek(Token::Question) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) {
584                self.push_location(&base.location());
585                self.next();
586                let consequent = self.parse_expression(ParserExpressionContext {
587                    min_precedence: OperatorPrecedence::AssignmentAndOther,
588                    ..context.clone()
589                });
590                let mut alternative = self.create_invalidated_expression(&self.tokenizer.cursor_location());
591                self.non_greedy_expect(Token::Colon);
592                if !self.expecting_token_error {
593                    alternative = self.parse_expression(ParserExpressionContext {
594                        min_precedence: OperatorPrecedence::AssignmentAndOther,
595                        ..context.clone()
596                    });
597                }
598                base = Rc::new(Expression::Conditional(ConditionalExpression {
599                    location: self.pop_location(),
600                    test: base, consequent, alternative,
601                }));
602            } else if let Some(binary_operator) = self.check_binary_operator(context.clone()) {
603                let BinaryOperator(operator, required_precedence, _) = binary_operator;
604                if context.min_precedence.includes(&required_precedence) {
605                    self.next();
606                    base = self.parse_binary_operator(base, operator, binary_operator.right_precedence(), context.clone());
607                } else {
608                    break;
609                }
610            // AssignmentExpression
611            } else if self.peek(Token::Assign) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) && context.allow_assignment {
612                self.push_location(&base.location());
613                self.next();
614                let left = base.clone();
615                if !left.is_valid_assignment_left_hand_side() {
616                    self.add_syntax_error(&left.location(), DiagnosticKind::MalformedDestructuring, vec![])
617                }
618                let right = self.parse_expression(ParserExpressionContext {
619                    min_precedence: OperatorPrecedence::AssignmentAndOther,
620                    ..context.clone()
621                });
622                base = Rc::new(Expression::Assignment(AssignmentExpression {
623                    location: self.pop_location(),
624                    left, compound: None, right,
625                }));
626            // CompoundAssignment and LogicalAssignment
627            } else if let Some(compound) = self.token.0.compound_assignment() {
628                if context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) && context.allow_assignment {
629                    self.push_location(&base.location());
630                    self.next();
631                    let left = base.clone();
632                    let right = self.parse_expression(ParserExpressionContext {
633                        min_precedence: OperatorPrecedence::AssignmentAndOther,
634                        ..context.clone()
635                    });
636                    base = Rc::new(Expression::Assignment(AssignmentExpression {
637                        location: self.pop_location(),
638                        left, compound: Some(compound), right,
639                    }));
640                } else {
641                    break;
642                }
643            } else if self.peek(Token::Comma) && context.min_precedence.includes(&OperatorPrecedence::List) {
644                self.push_location(&base.location());
645                self.next();
646                let right = self.parse_expression(ParserExpressionContext {
647                    min_precedence: OperatorPrecedence::AssignmentAndOther,
648                    ..context.clone()
649                });
650                base = Rc::new(Expression::Sequence(SequenceExpression {
651                    location: self.pop_location(),
652                    left: base, right,
653                }));
654            } else {
655                break;
656            }
657        }
658
659        base
660    }
661
662    fn parse_binary_operator(&mut self, base: Rc<Expression>, mut operator: Operator, right_precedence: OperatorPrecedence, context: ParserExpressionContext) -> Rc<Expression> {
663        // The left operand of a null-coalescing operation must not be
664        // a logical AND, XOR or OR operation.
665        if operator == Operator::NullCoalescing {
666            if let Expression::Unary(UnaryExpression { expression, operator, .. }) = base.as_ref() {
667                if [Operator::LogicalAnd, Operator::LogicalXor, Operator::LogicalOr].contains(&operator) {
668                    self.add_syntax_error(&expression.location(), DiagnosticKind::IllegalNullishCoalescingLeftOperand, vec![]);
669                }
670            }
671        }
672
673        if operator == Operator::Is && self.consume(Token::Not) {
674            operator = Operator::IsNot;
675        }
676
677        self.push_location(&base.location());
678        let right = self.parse_expression(ParserExpressionContext {
679            min_precedence: right_precedence,
680            ..context
681        });
682        Rc::new(Expression::Binary(BinaryExpression {
683            location: self.pop_location(),
684            left: base, operator, right,
685        }))
686    }
687
688    fn check_binary_operator(&self, context: ParserExpressionContext) -> Option<BinaryOperator> {
689        if let Some(operator) = self.token.0.to_binary_operator() {
690            if operator == Operator::In && !context.allow_in {
691                return None;
692            }
693            BinaryOperator::try_from(operator).ok()
694        } else {
695            None
696        }
697    }
698
699    fn parse_optional_chaining(&mut self, base: Rc<Expression>) -> Rc<Expression> {
700        self.push_location(&base.location());
701        self.duplicate_location();
702        let mut operation = Rc::new(Expression::OptionalChainingPlaceholder(OptionalChainingPlaceholder {
703            location: base.location(),
704        }));
705        if self.peek(Token::ParenOpen) {
706            let arguments: Vec<Rc<Expression>> = self.parse_arguments();
707            if arguments.len() == 1 && self.peek(Token::ColonColon) {
708                self.duplicate_location();
709                let ql = self.pop_location();
710                let q = Rc::new(Expression::Paren(ParenExpression {
711                    location: ql.clone(),
712                    expression: arguments[0].clone(),
713                }));
714                let identifier = self.finish_qualified_identifier(false, ql, q);
715                operation = Rc::new(Expression::Member(MemberExpression {
716                    location: self.pop_location(),
717                    base: operation,
718                    identifier,
719                }));
720            } else {
721                operation = Rc::new(Expression::Call(CallExpression {
722                    location: self.pop_location(),
723                    base: operation, arguments
724                }));
725            }
726        } else if self.peek(Token::SquareOpen) {
727            let asdoc = self.parse_asdoc();
728            self.next();
729            let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
730            self.non_greedy_expect(Token::SquareClose);
731            operation = Rc::new(Expression::ComputedMember(ComputedMemberExpression {
732                location: self.pop_location(),
733                base: operation, asdoc, key,
734            }));
735        } else {
736            let identifier = self.parse_qualified_identifier();
737            operation = Rc::new(Expression::Member(MemberExpression {
738                location: self.pop_location(),
739                base: operation, identifier
740            }));
741        }
742
743        // Parse postfix subexpressions
744        operation = self.parse_optional_chaining_subexpressions(operation);
745
746        Rc::new(Expression::OptionalChaining(OptionalChainingExpression {
747            location: self.pop_location(),
748            base, expression: operation,
749        }))
750    }
751
752    fn parse_optional_chaining_subexpressions(&mut self, mut base: Rc<Expression>) -> Rc<Expression> {
753        loop {
754            if self.consume(Token::Dot) {
755                base = self.parse_dot_subexpression(base);
756            } else if self.consume(Token::OptionalChaining) {
757                base = self.parse_optional_chaining(base);
758            } else if self.peek(Token::SquareOpen) {
759                self.next();
760                self.push_location(&base.location());
761                let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
762                self.non_greedy_expect(Token::SquareClose);
763                base = Rc::new(Expression::ComputedMember(ComputedMemberExpression {
764                    base, asdoc: None, key, location: self.pop_location()
765                }));
766            } else if self.consume(Token::Descendants) {
767                self.push_location(&base.location());
768                let id = self.parse_qualified_identifier();
769                base = Rc::new(Expression::Descendants(DescendantsExpression {
770                    location: self.pop_location(),
771                    base,
772                    identifier: id,
773                }));
774            } else if self.peek(Token::ParenOpen) {
775                self.push_location(&base.location());
776                let arguments = self.parse_arguments();
777                base = Rc::new(Expression::Call(CallExpression {
778                    location: self.pop_location(),
779                    base,
780                    arguments,
781                }));
782            } else if self.peek(Token::Exclamation) && !self.previous_token.1.line_break(&self.token.1) {
783                self.push_location(&base.location());
784                self.next();
785                base = Rc::new(Expression::Unary(UnaryExpression {
786                    location: self.pop_location(),
787                    expression: base, operator: Operator::NonNull,
788                }));
789            } else {
790                break;
791            }
792        }
793
794        base
795    }
796
797    fn parse_dot_subexpression(&mut self, base: Rc<Expression>) -> Rc<Expression> {
798        self.push_location(&base.location());
799        if self.peek(Token::ParenOpen) {
800            let paren_location = self.token_location();
801            let paren_exp = self.parse_paren_list_expression();
802            if !matches!(paren_exp.as_ref(), Expression::Sequence(_)) && self.peek(Token::ColonColon) {
803                let q = Rc::new(Expression::Paren(ParenExpression {
804                    location: paren_location.clone(),
805                    expression: paren_exp.clone(),
806                }));
807                let id = self.finish_qualified_identifier(false, paren_location, q);
808                Rc::new(Expression::Member(MemberExpression {
809                    location: self.pop_location(),
810                    base, identifier: id
811                }))
812            } else {
813                Rc::new(Expression::Filter(FilterExpression {
814                    location: self.pop_location(),
815                    base, test: paren_exp
816                }))
817            }
818        } else if self.consume(Token::Lt) {
819            let mut arguments = vec![];
820            arguments.push(self.parse_type_expression());
821            while self.consume(Token::Comma) {
822                arguments.push(self.parse_type_expression());
823            }
824            self.non_greedy_expect_type_parameters_gt();
825            Rc::new(Expression::WithTypeArguments(ApplyTypeExpression {
826                location: self.pop_location(),
827                base, arguments
828            }))
829        } else {
830            let id = self.parse_qualified_identifier();
831            Rc::new(Expression::Member(MemberExpression {
832                location: self.pop_location(),
833                base, identifier: id
834            }))
835        }
836    }
837
838    /// Ensures a parameter list consists of zero or more required parameters followed by
839    /// zero or more optional parameters optionally followed by a rest parameter.
840    fn validate_parameter_list(&mut self, params: Vec<(ParameterKind, Location)>) {
841        let mut least_kind = ParameterKind::Required; 
842        let mut has_rest = false;
843        for (param_kind, param_loc) in params {
844            if !least_kind.may_be_followed_by(param_kind) {
845                self.add_syntax_error(&param_loc, DiagnosticKind::WrongParameterPosition, vec![]);
846            }
847            least_kind = param_kind;
848            if param_kind == ParameterKind::Rest && has_rest {
849                self.add_syntax_error(&param_loc, DiagnosticKind::DuplicateRestParameter, vec![]);
850            }
851            has_rest = param_kind == ParameterKind::Rest;
852        }
853    }
854
855    fn parse_opt_start_expression(&mut self, context: ParserExpressionContext) -> Option<Rc<Expression>> {
856        if let Token::Identifier(id) = self.token.0.clone() {
857            let id_location = self.token_location();
858            self.next();
859            Some(self.parse_expression_starting_with_identifier((id, id_location)))
860        } else if self.peek(Token::Null) {
861            self.mark_location();
862            self.next();
863            Some(Rc::new(Expression::NullLiteral(NullLiteral {
864                location: self.pop_location(),
865            })))
866        } else if self.peek(Token::False) {
867            self.mark_location();
868            self.next();
869            Some(Rc::new(Expression::BooleanLiteral(BooleanLiteral {
870                location: self.pop_location(),
871                value: false,
872            })))
873        } else if self.peek(Token::True) {
874            self.mark_location();
875            self.next();
876            Some(Rc::new(Expression::BooleanLiteral(BooleanLiteral {
877                location: self.pop_location(),
878                value: true,
879            })))
880        } else if let Token::Number(n, suffix) = self.token.0.clone() {
881            self.mark_location();
882            self.next();
883            Some(Rc::new(Expression::NumericLiteral(NumericLiteral {
884                location: self.pop_location(),
885                value: n,
886                suffix,
887            })))
888        } else if let Token::String(ref s) = self.token.0.clone() {
889            self.mark_location();
890            self.next();
891            Some(Rc::new(Expression::StringLiteral(StringLiteral {
892                location: self.pop_location(),
893                value: s.clone(),
894            })))
895        } else if self.peek(Token::This) {
896            self.mark_location();
897            self.next();
898            Some(Rc::new(Expression::ThisLiteral(ThisLiteral {
899                location: self.pop_location(),
900            })))
901        } else if self.peek(Token::Div) || self.peek(Token::DivideAssign) {
902            self.mark_location();
903            self.token = self.tokenizer.scan_regexp_literal(self.token.1.clone(), if self.peek(Token::DivideAssign) { "=".into() } else { "".into() });
904            let Token::RegExp { ref body, ref flags } = self.token.0.clone() else {
905                panic!();
906            };
907            self.next();
908            Some(Rc::new(Expression::RegExpLiteral(RegExpLiteral {
909                location: self.pop_location(),
910                body: body.clone(), flags: flags.clone(),
911            })))
912        // `@`
913        } else if self.peek(Token::Attribute) {
914            self.mark_location();
915            let id = self.parse_qualified_identifier();
916            Some(Rc::new(Expression::QualifiedIdentifier(id)))
917        // Parentheses
918        } else if self.peek(Token::ParenOpen) {
919            Some(self.parse_paren_list_expr_or_qual_id())
920        // XMLList, XMLElement, XMLMarkup
921        } else if self.peek(Token::Lt) {
922            if let Some(token) = self.tokenizer.scan_xml_markup(self.token_location()) {
923                self.token = token;
924            }
925            let start = self.token_location();
926            if let Token::XmlMarkup(content) = &self.token.0.clone() {
927                self.mark_location();
928                self.next();
929                Some(Rc::new(Expression::XmlMarkup(XmlMarkupExpression {
930                    location: self.pop_location(),
931                    markup: content.clone(),
932                })))
933            } else {
934                Some(self.parse_xml_element_or_xml_list(start))
935            }
936        // ArrayInitializer
937        } else if self.peek(Token::SquareOpen) {
938            Some(self.parse_array_initializer())
939        // NewExpression
940        } else if self.peek(Token::New) && context.min_precedence.includes(&OperatorPrecedence::Unary) {
941            let start = self.token_location();
942            self.next();
943            Some(self.parse_new_expression(start))
944        } else if self.peek(Token::BlockOpen) {
945            Some(self.parse_object_initializer())
946        } else if self.peek(Token::Function) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) {
947            Some(self.parse_function_expression(context.clone()))
948        // SuperExpression
949        } else if self.peek(Token::Super) && context.min_precedence.includes(&OperatorPrecedence::Postfix) {
950            Some(self.parse_super_expression_followed_by_property_operator())
951        // AwaitExpression
952        } else if self.peek(Token::Await) && context.min_precedence.includes(&OperatorPrecedence::Unary) {
953            self.mark_location();
954            let operator_token = self.token.clone();
955            self.next();
956            let base = self.parse_expression(ParserExpressionContext {
957                allow_in: true,
958                min_precedence: OperatorPrecedence::Unary,
959                ..default()
960            });
961            if let Some(activation) = self.activations.last_mut() {
962                activation.uses_await = true;
963            } else {
964                self.add_syntax_error(&operator_token.1, DiagnosticKind::NotAllowedHere, diagarg![operator_token.0]);
965            }
966            Some(Rc::new(Expression::Unary(UnaryExpression {
967                location: self.pop_location(),
968                expression: base, operator: Operator::Await,
969            })))
970        // YieldExpression
971        } else if self.peek(Token::Yield) && context.min_precedence.includes(&OperatorPrecedence::AssignmentAndOther) {
972            self.mark_location();
973            let operator_token = self.token.clone();
974            self.next();
975            let base = self.parse_expression(ParserExpressionContext {
976                allow_in: true,
977                min_precedence: OperatorPrecedence::AssignmentAndOther,
978                ..default()
979            });
980            if let Some(activation) = self.activations.last_mut() {
981                activation.uses_yield = true;
982            } else {
983                self.add_syntax_error(&operator_token.1, DiagnosticKind::NotAllowedHere, diagarg![operator_token.0]);
984            }
985            Some(Rc::new(Expression::Unary(UnaryExpression {
986                location: self.pop_location(),
987                expression: base, operator: Operator::Yield,
988            })))
989        // Miscellaneous prefix unary expressions
990        } else if let Some((operator, subexp_precedence)) = self.check_prefix_operator() {
991            if context.min_precedence.includes(&OperatorPrecedence::Unary) {
992                self.mark_location();
993                self.next();
994                let base = self.parse_expression(ParserExpressionContext { min_precedence: subexp_precedence, ..default() });
995                Some(Rc::new(Expression::Unary(UnaryExpression {
996                    location: self.pop_location(),
997                    expression: base, operator,
998                })))
999            } else {
1000                None
1001            }
1002        // ImportMeta
1003        } else if self.peek(Token::Import) && context.min_precedence.includes(&OperatorPrecedence::Postfix) {
1004            self.mark_location();
1005            self.next();
1006            self.non_greedy_expect(Token::Dot);
1007            self.non_greedy_expect_context_keyword("meta");
1008            Some(Rc::new(Expression::ImportMeta(ImportMeta {
1009                location: self.pop_location(),
1010            })))
1011        // QualifiedIdentifier
1012        } else if
1013                self.peek(Token::Times)
1014            ||  self.peek(Token::Public) || self.peek(Token::Private)
1015            ||  self.peek(Token::Protected) || self.peek(Token::Internal) {
1016            let id = self.parse_qualified_identifier();
1017            Some(Rc::new(Expression::QualifiedIdentifier(id)))
1018        } else {
1019            None
1020        }
1021    }
1022
1023    fn parse_expression_starting_with_identifier(&mut self, id: (String, Location)) -> Rc<Expression> {
1024        let id_location = id.1.clone();
1025        let id = id.0;
1026
1027        /*
1028        // EmbedExpression
1029        if self.peek(Token::BlockOpen) && id == "embed" && self.previous_token.1.character_count() == "embed".len() {
1030            return self.finish_embed_expression(id_location);
1031        }
1032        */
1033
1034        let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier {
1035            location: id_location.clone(),
1036            attribute: false,
1037            qualifier: None,
1038            id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1039        }));
1040        if self.peek(Token::ColonColon) {
1041            self.push_location(&id_location.clone());
1042            let ql = self.pop_location();
1043            let id = self.finish_qualified_identifier(false, ql, id);
1044            Rc::new(Expression::QualifiedIdentifier(id))
1045        } else {
1046            id
1047        }
1048    }
1049
1050    fn check_prefix_operator(&self) -> Option<(Operator, OperatorPrecedence)> {
1051        match self.token.0 {
1052            Token::Delete => Some((Operator::Delete, OperatorPrecedence::Postfix)),
1053            Token::Void => Some((Operator::Void, OperatorPrecedence::Unary)),
1054            Token::Typeof => Some((Operator::Typeof, OperatorPrecedence::Unary)),
1055            Token::Increment => Some((Operator::PreIncrement, OperatorPrecedence::Postfix)),
1056            Token::Decrement => Some((Operator::PreDecrement, OperatorPrecedence::Postfix)),
1057            Token::Plus => Some((Operator::Positive, OperatorPrecedence::Unary)),
1058            Token::Minus => Some((Operator::Negative, OperatorPrecedence::Unary)),
1059            Token::Tilde => Some((Operator::BitwiseNot, OperatorPrecedence::Unary)),
1060            Token::Exclamation => Some((Operator::LogicalNot, OperatorPrecedence::Unary)),
1061            _ => None,
1062        }
1063    }
1064
1065    fn parse_function_expression(&mut self, context: ParserExpressionContext) -> Rc<Expression> {
1066        self.mark_location();
1067        self.next();
1068        let mut name = None;
1069        if let Token::Identifier(id) = self.token.0.clone() {
1070            name = Some((id, self.token.1.clone()));
1071            self.next();
1072        }
1073        let common = self.parse_function_common(true, ParserDirectiveContext::Default, context.allow_in);
1074        Rc::new(Expression::Function(FunctionExpression {
1075            location: self.pop_location(),
1076            name,
1077            common,
1078        }))
1079    }
1080
1081    fn parse_function_common(&mut self, function_expr: bool, block_context: ParserDirectiveContext, allow_in: bool) -> Rc<FunctionCommon> {
1082        self.mark_location();
1083        self.duplicate_location();
1084        let mut this_parameter: Option<Rc<ThisParameter>> = None;
1085        let mut params: Vec<Rc<Parameter>> = vec![];
1086        let mut return_annotation = Some(self.create_invalidated_expression(&self.tokenizer.cursor_location()));
1087        self.non_greedy_expect(Token::ParenOpen);
1088        if !self.expecting_token_error {
1089            if !self.peek(Token::ParenClose) {
1090                if self.peek(Token::This) {
1091                    self.mark_location();
1092                    self.next();
1093                    let mut type_annotation = self.create_invalidated_expression(&self.tokenizer.cursor_location());
1094                    self.expect(Token::Colon);
1095                    if !self.expecting_token_error
1096                    {
1097                        type_annotation = self.parse_type_expression();
1098                    }
1099                    this_parameter = Some(Rc::new(ThisParameter {
1100                        location: self.pop_location(),
1101                        type_annotation,
1102                    }));
1103                } else {
1104                    params.push(self.parse_parameter());
1105                }
1106                while self.consume(Token::Comma) {
1107                    params.push(self.parse_parameter());
1108                }
1109            }
1110            self.non_greedy_expect(Token::ParenClose);
1111            if !self.expecting_token_error {
1112                return_annotation = if self.consume(Token::Colon) { Some(self.parse_type_expression()) } else { None };
1113            }
1114            self.validate_parameter_list(params.iter().map(|p| (p.kind, p.location.clone())).collect::<Vec<_>>());
1115        }
1116
1117        let signature_location = self.pop_location();
1118
1119        // Enter activation
1120        self.activations.push(ParserActivation::new());
1121
1122        // Body
1123        let body = if self.peek(Token::BlockOpen) {
1124            Some(FunctionBody::Block(Rc::new(self.parse_block(block_context))))
1125        } else if !(self.offending_token_is_inline_or_higher_indented() || self.peek(Token::ParenOpen)) {
1126            None
1127        } else {
1128            self.parse_opt_expression(ParserExpressionContext {
1129                allow_in,
1130                min_precedence: OperatorPrecedence::AssignmentAndOther,
1131                ..default()
1132            }).map(|e| FunctionBody::Expression(e))
1133        };
1134
1135        // Body is required by function expressions
1136        if body.is_none() && function_expr {
1137            self.non_greedy_expect(Token::BlockOpen);
1138        }
1139
1140        // Exit activation
1141        let activation = self.activations.pop().unwrap();
1142
1143        Rc::new(FunctionCommon {
1144            location: self.pop_location(),
1145            contains_await: activation.uses_await,
1146            contains_yield: activation.uses_yield,
1147            signature: FunctionSignature {
1148                location: signature_location,
1149                this_parameter,
1150                parameters: params,
1151                result_type: return_annotation,
1152            },
1153            body,
1154        })
1155    }
1156
1157    fn parse_parameter(&mut self) -> Rc<Parameter> {
1158        self.mark_location();
1159        let rest = self.consume(Token::Ellipsis);
1160        let binding: Rc<VariableBinding> = Rc::new(self.parse_variable_binding(true));
1161        let has_initializer = binding.initializer.is_some();
1162        let location = self.pop_location();
1163        if rest && has_initializer {
1164            self.add_syntax_error(&location.clone(), DiagnosticKind::MalformedRestParameter, vec![]);
1165        }
1166        Rc::new(Parameter {
1167            location,
1168            destructuring: binding.destructuring.clone(),
1169            default_value: binding.initializer.clone(),
1170            kind: if rest {
1171                ParameterKind::Rest
1172            } else if has_initializer {
1173                ParameterKind::Optional
1174            } else {
1175                ParameterKind::Required
1176            },
1177        })
1178    }
1179
1180    fn parse_object_initializer(&mut self) -> Rc<Expression> {
1181        self.mark_location();
1182        self.non_greedy_expect(Token::BlockOpen);
1183        let mut fields: Vec<Rc<InitializerField>> = vec![];
1184        while !self.peek(Token::BlockClose) {
1185            fields.push(self.parse_field());
1186            if !self.consume(Token::Comma) {
1187                break;
1188            }
1189        }
1190        self.non_greedy_expect(Token::BlockClose);
1191
1192        Rc::new(Expression::ObjectInitializer(ObjectInitializer {
1193            location: self.pop_location(),
1194            fields,
1195        }))
1196    }
1197
1198    fn parse_field(&mut self) -> Rc<InitializerField> {
1199        if self.peek(Token::Ellipsis) {
1200            self.mark_location();
1201            self.next();
1202            let subexp = self.parse_expression(ParserExpressionContext {
1203                allow_in: true,
1204                min_precedence: OperatorPrecedence::AssignmentAndOther,
1205                ..default()
1206            });
1207            return Rc::new(InitializerField::Rest((subexp, self.pop_location())));
1208        }
1209
1210        let name = self.parse_field_name();
1211
1212        let mut value = None;
1213
1214        if self.consume(Token::Colon) {
1215            value = Some(self.parse_expression(ParserExpressionContext {
1216                allow_in: true,
1217                min_precedence: OperatorPrecedence::AssignmentAndOther,
1218                ..default()
1219            }));
1220        } else if !matches!(name.0, FieldName::Identifier(_)) {
1221            self.non_greedy_expect(Token::Colon);
1222        }
1223
1224        Rc::new(InitializerField::Field {
1225            name,
1226            value,
1227        })
1228    }
1229
1230    fn parse_field_name(&mut self) -> (FieldName, Location) {
1231        if let Token::String(value) = &self.token.0.clone() {
1232            let location = self.token_location();
1233            self.next();
1234            (FieldName::StringLiteral(Rc::new(Expression::StringLiteral(StringLiteral {
1235                location: location.clone(),
1236                value: value.clone(),
1237            }))), location)
1238        } else if let Token::Number(value, suffix) = &self.token.0.clone() {
1239            let location = self.token_location();
1240            self.next();
1241            (FieldName::NumericLiteral(Rc::new(Expression::NumericLiteral(NumericLiteral {
1242                location: location.clone(),
1243                value: value.clone(),
1244                suffix: *suffix,
1245            }))), location)
1246        } else if self.peek(Token::SquareOpen) {
1247            self.mark_location();
1248            self.next();
1249            let key_expr = self.parse_expression(ParserExpressionContext {
1250                allow_in: true,
1251                min_precedence: OperatorPrecedence::List,
1252                ..default()
1253            });
1254            self.non_greedy_expect(Token::SquareClose);
1255            let location = self.pop_location();
1256            (FieldName::Brackets(key_expr), location)
1257        } else {
1258            let id = self.parse_non_attribute_qualified_identifier();
1259            let l = id.location.clone();
1260            (FieldName::Identifier(id), l)
1261        }
1262    }
1263
1264    fn parse_new_expression(&mut self, start: Location) -> Rc<Expression> {
1265        self.push_location(&start);
1266        if self.consume(Token::Lt) {
1267            let element_type = self.parse_type_expression();
1268            self.non_greedy_expect_type_parameters_gt();
1269            let mut elements: Vec<Element> = vec![];
1270            self.non_greedy_expect(Token::SquareOpen);
1271            if !self.expecting_token_error {
1272                while !self.peek(Token::SquareClose) {
1273                    if self.peek(Token::Ellipsis) {
1274                        self.mark_location();
1275                        self.next();
1276                        elements.push(Element::Rest((self.parse_expression(ParserExpressionContext {
1277                            allow_in: true,
1278                            min_precedence: OperatorPrecedence::AssignmentAndOther,
1279                            ..default()
1280                        }), self.pop_location())));
1281                    } else {
1282                        elements.push(Element::Expression(self.parse_expression(ParserExpressionContext {
1283                            allow_in: true,
1284                            min_precedence: OperatorPrecedence::AssignmentAndOther,
1285                            ..default()
1286                        })));
1287                    }
1288                    if !self.consume(Token::Comma) {
1289                        break;
1290                    }
1291                }
1292                self.non_greedy_expect(Token::SquareClose);
1293            }
1294            Rc::new(Expression::VectorLiteral(VectorLiteral {
1295                location: self.pop_location(),
1296                element_type,
1297                elements,
1298            }))
1299        } else {
1300            let base = self.parse_new_subexpression();
1301            let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None };
1302            Rc::new(Expression::New(NewExpression {
1303                location: self.pop_location(),
1304                base, arguments,
1305            }))
1306        }
1307    }
1308
1309    fn parse_new_expression_start(&mut self) -> Rc<Expression> {
1310        if self.peek(Token::New) {
1311            let start = self.token_location();
1312            self.next();
1313            self.parse_new_expression(start)
1314        } else if self.peek(Token::Super) {
1315            self.parse_super_expression_followed_by_property_operator()
1316        } else {
1317            self.parse_primary_expression()
1318        }
1319    }
1320
1321    fn parse_super_expression_followed_by_property_operator(&mut self) -> Rc<Expression> {
1322        self.mark_location();
1323        self.duplicate_location();
1324        self.next();
1325        let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None };
1326        let super_expr = Rc::new(Expression::Super(SuperExpression {
1327            location: self.pop_location(),
1328            object: arguments,
1329        }));
1330
1331        if self.consume(Token::SquareOpen) {
1332            let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
1333            self.non_greedy_expect(Token::SquareClose);
1334            Rc::new(Expression::ComputedMember(ComputedMemberExpression {
1335                location: self.pop_location(),
1336                base: super_expr, asdoc: None, key,
1337            }))
1338        } else {
1339            self.non_greedy_expect(Token::Dot);
1340            let identifier = self.parse_qualified_identifier();
1341            Rc::new(Expression::Member(MemberExpression {
1342                location: self.pop_location(),
1343                base: super_expr, identifier,
1344            }))
1345        }
1346    }
1347
1348    fn parse_arguments(&mut self) -> Vec<Rc<Expression>> {
1349        self.non_greedy_expect(Token::ParenOpen);
1350        let mut arguments = vec![];
1351        if !self.peek(Token::ParenClose) {
1352            arguments.push(self.parse_expression(ParserExpressionContext {
1353                allow_in: true,
1354                min_precedence: OperatorPrecedence::AssignmentAndOther,
1355                ..default()
1356            }));
1357            while self.consume(Token::Comma) {
1358                arguments.push(self.parse_expression(ParserExpressionContext {
1359                    allow_in: true,
1360                    min_precedence: OperatorPrecedence::AssignmentAndOther,
1361                    ..default()
1362                }));
1363            }
1364        }
1365        self.non_greedy_expect(Token::ParenClose);
1366        arguments
1367    }
1368
1369    fn parse_new_subexpression(&mut self) -> Rc<Expression> {
1370        let mut base = self.parse_new_expression_start();
1371        loop {
1372            if self.consume(Token::SquareOpen) {
1373                self.push_location(&base.location());
1374                let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
1375                self.non_greedy_expect(Token::SquareClose);
1376                base = Rc::new(Expression::ComputedMember(ComputedMemberExpression {
1377                    location: self.pop_location(),
1378                    base, asdoc: None, key,
1379                }));
1380            } else if self.consume(Token::Dot) {
1381                self.push_location(&base.location());
1382                if self.consume(Token::Lt) {
1383                    let mut arguments = vec![];
1384                    arguments.push(self.parse_type_expression());
1385                    while self.consume(Token::Comma) {
1386                        arguments.push(self.parse_type_expression());
1387                    }
1388                    self.non_greedy_expect_type_parameters_gt();
1389                    base = Rc::new(Expression::WithTypeArguments(ApplyTypeExpression {
1390                        location: self.pop_location(),
1391                        base, arguments
1392                    }));
1393                } else {
1394                    let identifier = self.parse_qualified_identifier();
1395                    base = Rc::new(Expression::Member(MemberExpression {
1396                        location: self.pop_location(),
1397                        base, identifier,
1398                    }));
1399                }
1400            } else {
1401                break;
1402            }
1403        }
1404        base
1405    }
1406
1407    fn parse_primary_expression(&mut self) -> Rc<Expression> {
1408        if let Token::Identifier(id) = self.token.0.clone() {
1409            let id_location = self.token_location();
1410            self.next();
1411
1412            /*
1413            // EmbedExpression
1414            if self.peek(Token::BlockOpen) && id == "embed" && self.previous_token.1.character_count() == "embed".len() {
1415                return self.finish_embed_expression(id_location);
1416            }
1417            */
1418
1419            let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier {
1420                location: id_location.clone(),
1421                attribute: false,
1422                qualifier: None,
1423                id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1424            }));
1425            if self.peek(Token::ColonColon) {
1426                self.push_location(&id_location.clone());
1427                let ql = self.pop_location();
1428                let id = self.finish_qualified_identifier(false, ql, id);
1429                Rc::new(Expression::QualifiedIdentifier(id))
1430            } else {
1431                id
1432            }
1433        } else if self.peek(Token::Null) {
1434            self.mark_location();
1435            self.next();
1436            Rc::new(Expression::NullLiteral(NullLiteral {
1437                location: self.pop_location(),
1438            }))
1439        } else if self.peek(Token::False) {
1440            self.mark_location();
1441            self.next();
1442            Rc::new(Expression::BooleanLiteral(BooleanLiteral {
1443                location: self.pop_location(),
1444                value: false,
1445            }))
1446        } else if self.peek(Token::True) {
1447            self.mark_location();
1448            self.next();
1449            Rc::new(Expression::BooleanLiteral(BooleanLiteral {
1450                location: self.pop_location(),
1451                value: true,
1452            }))
1453        } else if let Token::Number(n, suffix) = self.token.0.clone() {
1454            self.mark_location();
1455            self.next();
1456            Rc::new(Expression::NumericLiteral(NumericLiteral {
1457                location: self.pop_location(),
1458                value: n,
1459                suffix,
1460            }))
1461        } else if let Token::String(ref s) = self.token.0.clone() {
1462            self.mark_location();
1463            self.next();
1464            Rc::new(Expression::StringLiteral(StringLiteral {
1465                location: self.pop_location(),
1466                value: s.clone(),
1467            }))
1468        } else if self.peek(Token::This) {
1469            self.mark_location();
1470            self.next();
1471            Rc::new(Expression::ThisLiteral(ThisLiteral {
1472                location: self.pop_location(),
1473            }))
1474        } else if self.peek(Token::Div) || self.peek(Token::DivideAssign) {
1475            self.mark_location();
1476            self.token = self.tokenizer.scan_regexp_literal(self.token.1.clone(), if self.peek(Token::DivideAssign) { "=".into() } else { "".into() });
1477            let Token::RegExp { ref body, ref flags } = self.token.0.clone() else {
1478                panic!();
1479            };
1480            self.next();
1481            Rc::new(Expression::RegExpLiteral(RegExpLiteral {
1482                location: self.pop_location(),
1483                body: body.clone(), flags: flags.clone(),
1484            }))
1485        // `@`
1486        } else if self.peek(Token::Attribute) {
1487            self.mark_location();
1488            let id = self.parse_qualified_identifier();
1489            Rc::new(Expression::QualifiedIdentifier(id))
1490        // Parentheses
1491        } else if self.peek(Token::ParenOpen) {
1492            return self.parse_paren_list_expr_or_qual_id();
1493        // XMLList, XMLElement, XMLMarkup
1494        } else if self.peek(Token::Lt) {
1495            if let Some(token) = self.tokenizer.scan_xml_markup(self.token_location()) {
1496                self.token = token;
1497            }
1498            let start = self.token_location();
1499            if let Token::XmlMarkup(content) = &self.token.0.clone() {
1500                self.mark_location();
1501                self.next();
1502                Rc::new(Expression::XmlMarkup(XmlMarkupExpression {
1503                    location: self.pop_location(),
1504                    markup: content.clone(),
1505                }))
1506            } else {
1507                self.parse_xml_element_or_xml_list(start)
1508            }
1509        // ArrayInitializer
1510        } else if self.peek(Token::SquareOpen) {
1511            self.parse_array_initializer()
1512        } else if self.peek(Token::BlockOpen) {
1513            self.parse_object_initializer()
1514        // QualifiedIdentifier
1515        } else if
1516                self.peek(Token::Times)
1517            ||  self.peek(Token::Public) || self.peek(Token::Private)
1518            ||  self.peek(Token::Protected) || self.peek(Token::Internal) {
1519            let id = self.parse_qualified_identifier();
1520            Rc::new(Expression::QualifiedIdentifier(id))
1521        } else {
1522            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingExpression, diagarg![self.token.0.clone()]);
1523            self.create_invalidated_expression(&self.tokenizer.cursor_location())
1524        }
1525    }
1526
1527    /*
1528    fn finish_embed_expression(&mut self, start: Location) -> Rc<Expression> {
1529        self.push_location(&start);
1530        let descriptor = self.parse_object_initializer().clone();
1531        let Expression::ObjectInitializer(descriptor) = descriptor.as_ref() else {
1532            panic!();
1533        };
1534        return Rc::new(Expression::Embed(EmbedExpression {
1535            location: self.pop_location(),
1536            description: descriptor.clone(),
1537        }));
1538    }
1539    */
1540
1541    fn parse_array_initializer(&mut self) -> Rc<Expression> {
1542        self.mark_location();
1543
1544        let asdoc = self.parse_asdoc();
1545
1546        self.non_greedy_expect(Token::SquareOpen);
1547
1548        let mut elements: Vec<Element> = vec![];
1549
1550        while !self.peek(Token::SquareClose) {
1551            let mut ellipses = false;
1552            while self.consume(Token::Comma) {
1553                elements.push(Element::Elision);
1554                ellipses = true;
1555            }
1556            if !ellipses  {
1557                if self.peek(Token::Ellipsis) {
1558                    self.mark_location();
1559                    self.next();
1560                    elements.push(Element::Rest((self.parse_expression(ParserExpressionContext {
1561                        allow_in: true,
1562                        min_precedence: OperatorPrecedence::AssignmentAndOther,
1563                        ..default()
1564                    }), self.pop_location())));
1565                } else {
1566                    elements.push(Element::Expression(self.parse_expression(ParserExpressionContext {
1567                        allow_in: true,
1568                        min_precedence: OperatorPrecedence::AssignmentAndOther,
1569                        ..default()
1570                    })));
1571                }
1572            }
1573            if !self.consume(Token::Comma) {
1574                break;
1575            }
1576        }
1577        self.non_greedy_expect(Token::SquareClose);
1578        Rc::new(Expression::ArrayLiteral(ArrayLiteral {
1579            location: self.pop_location(),
1580            asdoc,
1581            elements,
1582        }))
1583    }
1584
1585    fn parse_xml_element_or_xml_list(&mut self, start: Location) -> Rc<Expression> {
1586        self.next_ie_xml_tag();
1587        if self.consume_and_ie_xml_content(Token::Gt) {
1588            self.push_location(&start);
1589            let content = self.parse_xml_content();
1590            self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash);
1591            self.non_greedy_expect(Token::Gt);
1592            return Rc::new(Expression::XmlList(XmlListExpression {
1593                location: self.pop_location(),
1594                content,
1595            }));
1596        }
1597
1598        self.push_location(&start);
1599        let element = Rc::new(self.parse_xml_element(start, true));
1600        return Rc::new(Expression::Xml(XmlExpression {
1601            location: self.pop_location(),
1602            element,
1603        }));
1604    }
1605
1606    /// Parses XMLElement starting from its XMLTagContent.
1607    fn parse_xml_element(&mut self, start: Location, ends_at_ie_div: bool) -> XmlElement {
1608        self.push_location(&start);
1609        let name = self.parse_xml_tag_name();
1610        let mut attributes: Vec<Rc<XmlAttribute>> = vec![];
1611        let mut attribute_expression: Option<Rc<Expression>> = None;
1612        while self.consume_and_ie_xml_tag(Token::XmlWhitespace) {
1613            if self.consume(Token::BlockOpen) {
1614                let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() });
1615                self.expect_and_ie_xml_tag(Token::BlockClose);
1616                attribute_expression = Some(expr);
1617                self.consume_and_ie_xml_tag(Token::XmlWhitespace);
1618                break;
1619            } else if matches!(self.token.0, Token::XmlName(_)) {
1620                self.mark_location();
1621                let name = self.parse_xml_name();
1622                self.consume_and_ie_xml_tag(Token::XmlWhitespace);
1623                self.non_greedy_expect_and_ie_xml_tag(Token::Assign);
1624                let mut value = XmlAttributeValue::Value(("".into(), self.token.1.clone()));
1625                if !self.expecting_token_error {
1626                    self.consume_and_ie_xml_tag(Token::XmlWhitespace);
1627                    if self.consume(Token::BlockOpen) {
1628                        let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() });
1629                        self.expect_and_ie_xml_tag(Token::BlockClose);
1630                        value = XmlAttributeValue::Expression(expr);
1631                    } else {
1632                        value = XmlAttributeValue::Value(self.parse_xml_attribute_value());
1633                    }
1634                }
1635                attributes.push(Rc::new(XmlAttribute {
1636                    location: self.pop_location(),
1637                    name, value
1638                }));
1639            } else {
1640                break;
1641            }
1642        }
1643
1644        let mut content: Option<Vec<Rc<XmlContent>>> = None;
1645        let mut closing_name: Option<XmlTagName> = None;
1646
1647        let is_empty;
1648
1649        if ends_at_ie_div {
1650            is_empty = self.consume(Token::XmlSlashGt);
1651        } else {
1652            is_empty = self.consume_and_ie_xml_content(Token::XmlSlashGt);
1653        }
1654
1655        if !is_empty {
1656            self.expect_and_ie_xml_content(Token::Gt);
1657            content = Some(self.parse_xml_content());
1658            self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash);
1659            closing_name = Some(self.parse_xml_tag_name());
1660            self.consume_and_ie_xml_tag(Token::XmlWhitespace);
1661            if ends_at_ie_div {
1662                self.non_greedy_expect(Token::Gt);
1663            } else {
1664                self.non_greedy_expect_and_ie_xml_content(Token::Gt);
1665            }
1666        }
1667
1668        XmlElement {
1669            location: self.pop_location(),
1670            name,
1671            attributes,
1672            attribute_expression,
1673            content,
1674            closing_name,
1675        }
1676    }
1677    
1678    fn parse_xml_attribute_value(&mut self) -> (String, Location) {
1679        if let Token::XmlAttributeValue(value) = self.token.0.clone() {
1680            let location = self.token_location();
1681            self.next_ie_xml_tag();
1682            return (value, location);
1683        } else {
1684            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingXmlAttributeValue, diagarg![self.token.0.clone()]);
1685            ("".into(), self.tokenizer.cursor_location())
1686        }
1687    }
1688
1689    fn parse_xml_tag_name(&mut self) -> XmlTagName {
1690        if self.consume(Token::BlockOpen) {
1691            let expr = self.parse_expression(ParserExpressionContext {
1692                allow_in: true,
1693                min_precedence: OperatorPrecedence::AssignmentAndOther,
1694                ..default()
1695            });
1696            self.expect_and_ie_xml_tag(Token::BlockClose);
1697            XmlTagName::Expression(expr)
1698        } else {
1699            XmlTagName::Name(self.parse_xml_name())
1700        }
1701    }
1702
1703    fn parse_xml_name(&mut self) -> (String, Location) {
1704        if let Token::XmlName(name) = self.token.0.clone() {
1705            let name_location = self.token_location();
1706            self.next_ie_xml_tag();
1707            return (name, name_location);
1708        } else {
1709            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingXmlName, diagarg![self.token.0.clone()]);
1710            (INVALIDATED_IDENTIFIER.into(), self.tokenizer.cursor_location())
1711        }
1712    }
1713
1714    /// Parses XMLContent until a `</` token.
1715    fn parse_xml_content(&mut self) -> Vec<Rc<XmlContent>> {
1716        let mut content = vec![];
1717        while !self.peek(Token::XmlLtSlash) {
1718            if self.consume(Token::BlockOpen) {
1719                let expr = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::AssignmentAndOther, ..default() });
1720                self.expect_and_ie_xml_content(Token::BlockClose);
1721                content.push(Rc::new(XmlContent::Expression(expr)));
1722            } else if let Token::XmlMarkup(markup) = self.token.0.clone() {
1723                let location = self.token_location();
1724                self.next_ie_xml_content();
1725                content.push(Rc::new(XmlContent::Markup((markup, location))));
1726            } else if let Token::XmlText(text) = self.token.0.clone() {
1727                if self.tokenizer.characters().reached_end() {
1728                    self.expect_and_ie_xml_content(Token::XmlLtSlash);
1729                    break;
1730                }
1731                let location = self.token_location();
1732                self.next_ie_xml_content();
1733                content.push(Rc::new(XmlContent::Characters((text, location))));
1734            } else if self.consume_and_ie_xml_tag(Token::Lt) {
1735                let start = self.token_location();
1736                let element = self.parse_xml_element(start, false);
1737                content.push(Rc::new(XmlContent::Element(Rc::new(element))));
1738            } else if self.peek(Token::Eof) {
1739                break;
1740            } else {
1741                self.expect_and_ie_xml_content(Token::XmlLtSlash);
1742            }
1743        }
1744        content
1745    }
1746
1747    fn finish_paren_list_expr_or_qual_id(&mut self, start: Location, left: Rc<Expression>) -> Rc<Expression> {
1748        if self.peek(Token::ColonColon) && !matches!(left.as_ref(), Expression::Sequence(_)) {
1749            self.push_location(&start);
1750            let ql = self.pop_location();
1751            let left = Rc::new(Expression::Paren(ParenExpression {
1752                location: ql.clone(),
1753                expression: left,
1754            }));
1755            let id = self.finish_qualified_identifier(false, ql, left);
1756            return Rc::new(Expression::QualifiedIdentifier(id));
1757        }
1758        self.push_location(&start);
1759        return Rc::new(Expression::Paren(ParenExpression {
1760            location: self.pop_location(),
1761            expression: left,
1762        }));
1763    }
1764
1765    /// Parses either a ParenListExpression, (), or a QualifiedIdentifier
1766    fn parse_paren_list_expr_or_qual_id(&mut self) -> Rc<Expression> {
1767        let start = self.token_location();
1768        self.non_greedy_expect(Token::ParenOpen);
1769
1770        let expr = self.parse_expression(ParserExpressionContext {
1771            min_precedence: OperatorPrecedence::List,
1772            allow_in: true,
1773            ..default()
1774        });
1775
1776        self.non_greedy_expect(Token::ParenClose);
1777        self.finish_paren_list_expr_or_qual_id(start, expr)
1778    }
1779
1780    fn parse_opt_reserved_namespace(&mut self) -> Option<Rc<Expression>> {
1781        let loc = self.token.1.clone();
1782        if self.consume(Token::Public) {
1783            Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Public(loc))))
1784        } else if self.consume(Token::Private) {
1785            Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Private(loc))))
1786        } else if self.consume(Token::Protected) {
1787            Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Protected(loc))))
1788        } else if self.consume(Token::Internal) {
1789            Some(Rc::new(Expression::ReservedNamespace(ReservedNamespaceExpression::Internal(loc))))
1790        } else {
1791            None
1792        }
1793    }
1794
1795    fn parse_qualified_identifier(&mut self) -> QualifiedIdentifier {
1796        self.mark_location();
1797
1798        let attribute = self.consume(Token::Attribute);
1799        if attribute && self.peek(Token::SquareOpen) {
1800            let brackets = self.parse_brackets();
1801            return QualifiedIdentifier {
1802                location: self.pop_location(),
1803                attribute,
1804                qualifier: None,
1805                id: QualifiedIdentifierIdentifier::Brackets(brackets),
1806            };
1807        }
1808
1809        // public, private, protected, internal
1810        if let Some(qual) = self.parse_opt_reserved_namespace() {
1811            if self.peek(Token::ColonColon) {
1812                let ql = self.pop_location();
1813                return self.finish_qualified_identifier(attribute, ql, qual);
1814            } else {
1815                let id = QualifiedIdentifier {
1816                    location: self.pop_location(),
1817                    attribute,
1818                    qualifier: None,
1819                    id: QualifiedIdentifierIdentifier::Id((qual.to_reserved_namespace_string().unwrap(), qual.location())),
1820                };
1821                return id;
1822            }
1823        }
1824
1825        let mut id: Option<String> = None;
1826
1827        // IdentifierName
1828        if let Token::Identifier(id_1) = self.token.0.clone() {
1829            id = Some(id_1);
1830        } else {
1831            if let Some(id_1) = self.token.0.reserved_word_name() {
1832                id = Some(id_1);
1833            } else if self.peek(Token::Times) {
1834                id = Some("*".to_owned());
1835            }
1836        }
1837
1838        if let Some(id) = id {
1839            let id_location = self.token_location();
1840            self.next();
1841            if self.peek(Token::ColonColon) {
1842                let id = QualifiedIdentifier {
1843                    location: id_location.clone(),
1844                    attribute: false,
1845                    qualifier: None,
1846                    id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1847                };
1848                let id = Rc::new(Expression::QualifiedIdentifier(id));
1849                let ql = self.pop_location();
1850                return self.finish_qualified_identifier(attribute, ql, id);
1851            } else {
1852                let id = QualifiedIdentifier {
1853                    location: self.pop_location(),
1854                    attribute,
1855                    qualifier: None,
1856                    id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1857                };
1858                return id;
1859            }
1860        }
1861
1862        // (q)::x
1863        if self.peek(Token::ParenOpen) {
1864            let qual = self.parse_paren_expression();
1865            let ql = self.pop_location();
1866            let qual = Rc::new(Expression::Paren(ParenExpression {
1867                location: ql.clone(),
1868                expression: qual,
1869            }));
1870            return self.finish_qualified_identifier(attribute, ql, qual);
1871        }
1872
1873        self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
1874        QualifiedIdentifier {
1875            location: self.pop_location(),
1876            attribute: false,
1877            qualifier: None,
1878            id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())),
1879        }
1880    }
1881
1882    fn parse_non_attribute_qualified_identifier(&mut self) -> QualifiedIdentifier {
1883        self.mark_location();
1884
1885        let attribute = false;
1886
1887        // public, private, protected, internal
1888        if let Some(qual) = self.parse_opt_reserved_namespace() {
1889            if self.peek(Token::ColonColon) {
1890                let ql = self.pop_location();
1891                return self.finish_qualified_identifier(attribute, ql, qual);
1892            } else {
1893                let id = QualifiedIdentifier {
1894                    location: self.pop_location(),
1895                    attribute,
1896                    qualifier: None,
1897                    id: QualifiedIdentifierIdentifier::Id((qual.to_reserved_namespace_string().unwrap(), qual.location())),
1898                };
1899                return id;
1900            }
1901        }
1902
1903        let mut id: Option<String> = None;
1904
1905        // IdentifierName
1906        if let Token::Identifier(id_1) = self.token.0.clone() {
1907            id = Some(id_1);
1908        } else {
1909            if let Some(id_1) = self.token.0.reserved_word_name() {
1910                id = Some(id_1);
1911            } else if self.peek(Token::Times) {
1912                id = Some("*".to_owned());
1913            }
1914        }
1915
1916        if let Some(id) = id {
1917            let id_location = self.token_location();
1918            self.next();
1919            if self.peek(Token::ColonColon) {
1920                let id = QualifiedIdentifier {
1921                    location: id_location.clone(),
1922                    attribute: false,
1923                    qualifier: None,
1924                    id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1925                };
1926                let id = Rc::new(Expression::QualifiedIdentifier(id));
1927                let ql = self.pop_location();
1928                return self.finish_qualified_identifier(attribute, ql, id);
1929            } else {
1930                let id = QualifiedIdentifier {
1931                    location: self.pop_location(),
1932                    attribute,
1933                    qualifier: None,
1934                    id: QualifiedIdentifierIdentifier::Id((id, id_location.clone())),
1935                };
1936                return id;
1937            }
1938        }
1939
1940        // (q)::x
1941        if self.peek(Token::ParenOpen) {
1942            let qual = self.parse_paren_expression();
1943            let ql = self.pop_location();
1944            let qual = Rc::new(Expression::Paren(ParenExpression {
1945                location: ql.clone(),
1946                expression: qual,
1947            }));
1948            return self.finish_qualified_identifier(attribute, ql, qual);
1949        }
1950
1951        self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
1952        QualifiedIdentifier {
1953            location: self.pop_location(),
1954            attribute: false,
1955            qualifier: None,
1956            id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())),
1957        }
1958    }
1959
1960    /// Expects a colon-colon and finishes a qualified identifier.
1961    fn finish_qualified_identifier(&mut self, attribute: bool, start_location: Location, qual: Rc<Expression>) -> QualifiedIdentifier {
1962        self.push_location(&start_location);
1963        self.non_greedy_expect(Token::ColonColon);
1964
1965        // `::` may be followed by one of { IdentifierName, `*`, Brackets }
1966
1967        // IdentifierName
1968        if let Some(id) = self.consume_identifier(true) {
1969            QualifiedIdentifier {
1970                location: self.pop_location(),
1971                attribute,
1972                qualifier: Some(qual),
1973                id: QualifiedIdentifierIdentifier::Id(id),
1974            }
1975        // `*`
1976        } else if self.peek(Token::Times) {
1977            let id_location = self.token_location();
1978            self.next();
1979            QualifiedIdentifier {
1980                location: self.pop_location(),
1981                attribute,
1982                qualifier: Some(qual),
1983                id: QualifiedIdentifierIdentifier::Id(("*".into(), id_location)),
1984            }
1985        // Brackets
1986        } else if self.peek(Token::SquareOpen) {
1987            let brackets = self.parse_brackets();
1988            QualifiedIdentifier {
1989                location: self.pop_location(),
1990                attribute,
1991                qualifier: Some(qual),
1992                id: QualifiedIdentifierIdentifier::Brackets(brackets),
1993            }
1994        } else {
1995            self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingIdentifier, diagarg![self.token.0.clone()]);
1996            QualifiedIdentifier {
1997                location: self.pop_location(),
1998                attribute,
1999                qualifier: Some(qual),
2000                id: QualifiedIdentifierIdentifier::Id(("".into(), self.tokenizer.cursor_location())),
2001            }
2002        }
2003    }
2004
2005    fn parse_brackets(&mut self) -> Rc<Expression> {
2006        self.non_greedy_expect(Token::SquareOpen);
2007        let expr = self.parse_expression(ParserExpressionContext {
2008            min_precedence: OperatorPrecedence::List,
2009            allow_in: true,
2010            ..default()
2011        });
2012        self.non_greedy_expect(Token::SquareClose);
2013        expr
2014    }
2015
2016    fn parse_paren_expression(&mut self) -> Rc<Expression> {
2017        self.non_greedy_expect(Token::ParenOpen);
2018        let expr = self.parse_expression(ParserExpressionContext {
2019            min_precedence: OperatorPrecedence::AssignmentAndOther,
2020            allow_in: true,
2021            ..default()
2022        });
2023        self.non_greedy_expect(Token::ParenClose);
2024        expr
2025    }
2026
2027    fn parse_paren_list_expression(&mut self) -> Rc<Expression> {
2028        self.non_greedy_expect(Token::ParenOpen);
2029        let expr = self.parse_expression(ParserExpressionContext {
2030            min_precedence: OperatorPrecedence::List,
2031            allow_in: true,
2032            ..default()
2033        });
2034        self.non_greedy_expect(Token::ParenClose);
2035        expr
2036    }
2037
2038    fn parse_typed_destructuring(&mut self) -> TypedDestructuring {
2039        self.mark_location();
2040        let destructuring: Rc<Expression>;
2041        if self.peek(Token::BlockOpen) {
2042            destructuring = self.parse_object_initializer();
2043        } else if self.peek(Token::SquareOpen) {
2044            destructuring = self.parse_array_initializer();
2045        } else {
2046            let id = self.expect_identifier(true);
2047            let id = QualifiedIdentifier {
2048                location: id.1.clone(),
2049                attribute: false,
2050                qualifier: None,
2051                id: QualifiedIdentifierIdentifier::Id(id.clone()),
2052            };
2053            destructuring = Rc::new(Expression::QualifiedIdentifier(id));
2054        }
2055        if !destructuring.is_valid_destructuring() {
2056            self.add_syntax_error(&destructuring.location(), DiagnosticKind::MalformedDestructuring, vec![])
2057        }
2058        let type_annotation = if self.consume(Token::Colon) { Some(self.parse_type_expression()) } else { None };
2059        TypedDestructuring {
2060            location: self.pop_location(),
2061            destructuring,
2062            type_annotation,
2063        }
2064    }
2065
2066    pub fn parse_type_expression(&mut self) -> Rc<Expression> {
2067        let mut base = self.parse_type_expression_start();
2068
2069        loop {
2070            if self.consume(Token::Dot) {
2071                base = self.parse_dot_subexpression(base);
2072            } else {
2073                break;
2074            }
2075        }
2076
2077        base
2078    }
2079
2080    fn parse_type_expression_start(&mut self) -> Rc<Expression> {
2081        // Parenthesized
2082        if self.peek(Token::ParenOpen) {
2083            self.mark_location();
2084            let expression = self.parse_type_expression();
2085            Rc::new(Expression::Paren(ParenExpression {
2086                location: self.pop_location(),
2087                expression,
2088            }))
2089        }
2090        // `function`
2091        else if self.peek(Token::Function) {
2092            self.parse_function_type_expression()
2093        // `void`
2094        } else if self.peek(Token::Void) {
2095            self.mark_location();
2096            self.next();
2097            Rc::new(Expression::VoidType(VoidTypeExpression {
2098                location: self.pop_location(),
2099            }))
2100        // [T]
2101        // [T1, T2, ...Tn]
2102        } else if self.peek(Token::SquareOpen) {
2103            let mut elements = vec![];
2104            self.mark_location();
2105            self.next();
2106            elements.push(self.parse_type_expression());
2107            if self.consume(Token::SquareClose) {
2108                Rc::new(Expression::ArrayType(ArrayTypeExpression {
2109                    location: self.pop_location(),
2110                    expression: elements[0].clone(),
2111                }))
2112            } else {
2113                self.non_greedy_expect(Token::Comma);
2114                elements.push(self.parse_type_expression());
2115                while self.consume(Token::Comma) {
2116                    if self.peek(Token::SquareClose) {
2117                        break;
2118                    }
2119                    elements.push(self.parse_type_expression());
2120                }
2121                self.non_greedy_expect(Token::SquareClose);
2122                Rc::new(Expression::TupleType(TupleTypeExpression {
2123                    location: self.pop_location(),
2124                    expressions: elements,
2125                }))
2126            }
2127        } else if self.peek(Token::Times) {
2128            let location = self.token_location();
2129            self.next();
2130            Rc::new(Expression::AnyType(AnyTypeExpression {
2131                location,
2132            }))
2133        // Identifier
2134        } else {
2135            let id = self.parse_qualified_identifier();
2136            Rc::new(Expression::QualifiedIdentifier(id))
2137        }
2138    }
2139
2140    fn parse_function_type_expression(&mut self) -> Rc<Expression> {
2141        self.mark_location();
2142        self.next();
2143
2144        let mut parameters = vec![];
2145        self.non_greedy_expect(Token::ParenOpen);
2146        if !self.expecting_token_error {
2147            if !self.peek(Token::ParenClose) {
2148                parameters.push(self.parse_function_type_parameter());
2149                while self.consume(Token::Comma) {
2150                    parameters.push(self.parse_function_type_parameter());
2151                }
2152            }
2153            self.non_greedy_expect(Token::ParenClose);
2154            self.validate_parameter_list(parameters.iter().map(|p| (p.kind, p.location.clone())).collect::<Vec<_>>());
2155        }
2156
2157        let mut result_type = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2158        self.non_greedy_expect(Token::Colon);
2159        if !self.expecting_token_error {
2160            result_type = self.parse_type_expression();
2161        }
2162        Rc::new(Expression::FunctionType(FunctionTypeExpression {
2163            location: self.pop_location(),
2164            parameters,
2165            result_type: Some(result_type),
2166        }))
2167    }
2168
2169    fn parse_function_type_parameter(&mut self) -> Rc<FunctionTypeParameter> {
2170        self.mark_location();
2171        let rest = self.consume(Token::Ellipsis);
2172        let type_expression: Option<Rc<Expression>> = if rest && self.peek(Token::ParenClose) {
2173            None
2174        } else {
2175            Some(self.parse_type_expression())
2176        };
2177        let optional = !rest && self.consume(Token::Assign);
2178        let location = self.pop_location();
2179        Rc::new(FunctionTypeParameter {
2180            location,
2181            type_expression,
2182            kind: if rest {
2183                ParameterKind::Rest
2184            } else if optional {
2185                ParameterKind::Optional
2186            } else {
2187                ParameterKind::Required
2188            },
2189        })
2190    }
2191
2192    fn parse_variable_binding(&mut self, allow_in: bool) -> VariableBinding {
2193        let destructuring = self.parse_typed_destructuring();
2194        let initializer = if self.consume(Token::Assign) {
2195            Some(self.parse_expression(ParserExpressionContext {
2196                allow_in,
2197                min_precedence: OperatorPrecedence::AssignmentAndOther,
2198                ..default()
2199            }))
2200        } else {
2201            None
2202        };
2203        VariableBinding {
2204            destructuring,
2205            initializer,
2206        }
2207    }
2208
2209    fn parse_semicolon(&mut self) -> bool {
2210        self.consume(Token::Semicolon) || self.peek(Token::BlockClose) || self.previous_token.1.line_break(&self.token.1)
2211    }
2212
2213    fn parse_substatement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2214        self.parse_statement(context)
2215    }
2216
2217    fn parse_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2218        // ExpressionStatement or LabeledStatement
2219        if let Token::Identifier(id) = &self.token.0.clone() {
2220            let id = (id.clone(), self.token_location());
2221            self.next();
2222            self.parse_statement_starting_with_identifier(context, id)
2223        // SuperStatement or ExpressionStatement with `super`
2224        } else if self.peek(Token::Super) {
2225            self.mark_location();
2226            self.next();
2227            let arguments = if self.peek(Token::ParenOpen) { Some(self.parse_arguments()) } else { None };
2228            let mut semicolon = false;
2229            if arguments.is_some() {
2230                semicolon = self.parse_semicolon();
2231            }
2232            if !semicolon && (self.peek(Token::Dot) || self.peek(Token::SquareOpen)) {
2233                if !(self.peek(Token::Dot) || self.peek(Token::SquareOpen)) {
2234                    self.non_greedy_expect(Token::Dot);
2235                }
2236                self.duplicate_location();
2237                // ExpressionStatement (`super`...)
2238                let mut expr = Rc::new(Expression::Super(SuperExpression {
2239                    location: self.pop_location(),
2240                    object: arguments,
2241                }));
2242                expr = self.parse_subexpressions(expr, ParserExpressionContext {
2243                    allow_in: true,
2244                    min_precedence: OperatorPrecedence::List,
2245                    ..default()
2246                });
2247                let semicolon = self.parse_semicolon();
2248                (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
2249                    location: self.pop_location(),
2250                    expression: expr,
2251                })), semicolon)
2252            } else {
2253                // SuperStatement
2254                let node = Rc::new(Directive::SuperStatement(SuperStatement {
2255                    location: self.pop_location(),
2256                    arguments: arguments.unwrap(),
2257                }));
2258
2259                // Check whether super statement is allowed here
2260                let allowed_here;
2261                if context.may_contain_super_statement() {
2262                    allowed_here = !context.super_statement_found();
2263                    context.set_super_statement_found(true);
2264                } else {
2265                    allowed_here = false;
2266                }
2267
2268                if !allowed_here {
2269                    self.add_syntax_error(&node.location(), DiagnosticKind::NotAllowedHere, diagarg![Token::Super]);
2270                }
2271
2272                (node, semicolon)
2273            }
2274        // EmptyStatement
2275        } else if self.peek(Token::Semicolon) {
2276            self.mark_location();
2277            self.next();
2278            (Rc::new(Directive::EmptyStatement(EmptyStatement {
2279                location: self.pop_location(),
2280            })), true)
2281        // Block
2282        } else if self.peek(Token::BlockOpen) {
2283            let context = if context.is_top_level_or_package() || context.is_type_block() {
2284                context.clone()
2285            } else {
2286                context.override_control_context(true, ParserControlFlowContext {
2287                    breakable: true,
2288                    iteration: false,
2289                })
2290            };
2291            let block = self.parse_block(context);
2292            (Rc::new(Directive::Block(block)), true)
2293        // IfStatement
2294        } else if self.peek(Token::If) {
2295            self.parse_if_statement(context)
2296        // SwitchStatement
2297        // `switch type`
2298        } else if self.peek(Token::Switch) {
2299            self.parse_switch_statement(context)
2300        // DoStatement
2301        } else if self.peek(Token::Do) {
2302            self.parse_do_statement(context)
2303        // WhileStatement
2304        } else if self.peek(Token::While) {
2305            self.parse_while_statement(context)
2306        // ForStatement
2307        // `for..in`
2308        // `for each`
2309        } else if self.peek(Token::For) {
2310            self.parse_for_statement(context)
2311        // WithStatement
2312        } else if self.peek(Token::With) {
2313            self.parse_with_statement(context)
2314        // BreakStatement
2315        } else if self.peek(Token::Break) {
2316            self.parse_break_statement(context)
2317        // ContinueStatement
2318        } else if self.peek(Token::Continue) {
2319            self.parse_continue_statement(context)
2320        // ReturnStatement
2321        } else if self.peek(Token::Return) {
2322            self.parse_return_statement(context)
2323        // ThrowStatement
2324        } else if self.peek(Token::Throw) {
2325            self.parse_throw_statement(context)
2326        // TryStatement
2327        } else if self.peek(Token::Try) {
2328            self.parse_try_statement(context)
2329        // `default xml namespace = expression`
2330        } else if self.peek(Token::Default) {
2331            self.parse_default_xml_namespace_statement()
2332        // ExpressionStatement
2333        } else {
2334            self.mark_location();
2335
2336            // Store offset for patching error
2337            let i = self.tokenizer.characters().index();
2338
2339            let exp = self.parse_expression(ParserExpressionContext {
2340                allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2341            });
2342
2343            // Patch error
2344            if i == self.tokenizer.characters().index() {
2345                self.patch_syntax_error(DiagnosticKind::ExpectingExpression, DiagnosticKind::ExpectingStatement, diagarg![self.token.0.clone()]);
2346            }
2347
2348            let semicolon = if exp.is_invalidated() {
2349                self.next();
2350                true
2351            } else { self.parse_semicolon() };
2352            (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
2353                location: self.pop_location(),
2354                expression: exp,
2355            })), semicolon)
2356        }
2357    }
2358
2359    fn parse_statement_starting_with_identifier(&mut self, context: ParserDirectiveContext, id: (String, Location)) -> (Rc<Directive>, bool) {
2360        self.push_location(&id.1);
2361        let id_location = id.1.clone();
2362
2363        // LabeledStatement
2364        if self.consume(Token::Colon) {
2365            let (substatement, semicolon) = self.parse_substatement(context.put_label(id.0.clone()));
2366            let labeled = Rc::new(Directive::LabeledStatement(LabeledStatement {
2367                location: self.pop_location(),
2368                label: id.clone(),
2369                substatement,
2370            }));
2371            return (labeled, semicolon);
2372        }
2373
2374        let mut exp: Rc<Expression>;
2375
2376        /*
2377        // EmbedExpression
2378        if self.peek(Token::BlockOpen) && id.0 == "embed" && self.previous_token.1.character_count() == "embed".len() {
2379            exp = self.finish_embed_expression(id_location);
2380        } else {
2381        */
2382        {
2383            let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier {
2384                location: id_location.clone(),
2385                attribute: false,
2386                qualifier: None,
2387                id: QualifiedIdentifierIdentifier::Id(id.clone()),
2388            }));
2389            if self.peek(Token::ColonColon) {
2390                self.push_location(&id_location.clone());
2391                let ql = self.pop_location();
2392                let id = self.finish_qualified_identifier(false, ql, id);
2393                exp = Rc::new(Expression::QualifiedIdentifier(id));
2394            } else {
2395                exp = id;
2396            }
2397        }
2398
2399        exp = self.parse_subexpressions(exp, ParserExpressionContext {
2400            allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2401        });
2402        let semicolon = self.parse_semicolon();
2403        (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
2404            location: self.pop_location(),
2405            expression: exp,
2406        })), semicolon)
2407    }
2408
2409    fn parse_qualified_identifier_statement_or_config(&mut self, context: ParserDirectiveContext, id: (String, Location), asdoc: Option<Rc<Asdoc>>) -> (Rc<Directive>, bool) {
2410        self.push_location(&id.1);
2411        let id_location = id.1.clone();
2412        let id = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier {
2413            location: id_location.clone(),
2414            attribute: false,
2415            qualifier: None,
2416            id: QualifiedIdentifierIdentifier::Id(id.clone()),
2417        }));
2418        self.push_location(&id_location.clone());
2419        let ql = self.pop_location();
2420        let id = self.finish_qualified_identifier(false, ql, id);
2421        let mut exp = Rc::new(Expression::QualifiedIdentifier(id));
2422        exp = self.parse_subexpressions(exp, ParserExpressionContext {
2423            allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2424        });
2425
2426        // Parse CONFIG::VAR_NAME
2427        if let Some(result) = self.parse_opt_config(&exp, asdoc.clone(), context.clone()) {
2428            return result;
2429        }
2430
2431        let semicolon = self.parse_semicolon();
2432        (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
2433            location: self.pop_location(),
2434            expression: exp,
2435        })), semicolon)
2436    }
2437
2438    fn parse_opt_config(&mut self, exp: &Rc<Expression>, asdoc: Option<Rc<Asdoc>>, context: ParserDirectiveContext) -> Option<(Rc<Directive>, bool)> {
2439        if self.peek_annotatable_directive_identifier_name() {
2440            match exp.to_configuration_identifier(self) {
2441                Ok(Some((q, constant_name, metadata))) => {
2442                    self.push_location(&exp.location());
2443                    let mut context = AnnotatableContext {
2444                        start_location: exp.location(),
2445                        asdoc: self.parse_asdoc().or(asdoc),
2446                        attributes: metadata,
2447                        context,
2448                        directive_context_keyword: None,
2449                    };
2450                    self.parse_attribute_keywords_or_expressions(&mut context);
2451                    let (directive, semicolon) = self.parse_annotatable_directive(context);
2452                    return Some((Rc::new(Directive::ConfigurationDirective(ConfigurationDirective {
2453                        location: self.pop_location(),
2454                        namespace: q,
2455                        constant_name,
2456                        directive,
2457                    })), semicolon));
2458                },
2459                Ok(None) => {},
2460                Err(MetadataRefineError1(MetadataRefineError::Syntax, loc)) => {
2461                    self.add_syntax_error(&loc, DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]);
2462                },
2463            }
2464        }
2465        if self.peek(Token::BlockOpen) {
2466            if let Some((q, constant_name)) = exp.to_configuration_identifier_no_metadata() {
2467                self.push_location(&exp.location());
2468                let block = self.parse_block(context);
2469                return Some((Rc::new(Directive::ConfigurationDirective(ConfigurationDirective {
2470                    location: self.pop_location(),
2471                    namespace: q,
2472                    constant_name,
2473                    directive: Rc::new(Directive::Block(block)),
2474                })), true));
2475            }
2476        }
2477        None
2478    }
2479
2480    fn parse_block(&mut self, context: ParserDirectiveContext) -> Block {
2481        self.mark_location();
2482        self.non_greedy_expect(Token::BlockOpen);
2483        let mut directives = vec![];
2484        if !self.expecting_token_error {
2485            let mut semicolon = false;
2486            while !self.peek(Token::BlockClose) && !self.peek(Token::Eof) {
2487                if !directives.is_empty() && !semicolon {
2488                    self.non_greedy_expect_virtual_semicolon();
2489                }
2490                let (directive, semicolon_1) = self.parse_directive(context.clone());
2491                directives.push(directive);
2492                semicolon = semicolon_1;
2493            }
2494            self.non_greedy_expect(Token::BlockClose);
2495        }
2496        Block { 
2497            location: self.pop_location(),
2498            directives,
2499        }
2500    }
2501
2502    fn parse_if_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2503        let context = context.override_control_context(true, ParserControlFlowContext {
2504            breakable: true,
2505            iteration: false,
2506        });
2507        self.mark_location();
2508        self.next();
2509        let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2510        let mut consequent: Rc<Directive> = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2511        let mut alternative: Option<Rc<Directive>> = None;
2512        let semicolon;
2513        self.non_greedy_expect(Token::ParenOpen);
2514        if self.expecting_token_error {
2515            semicolon = self.parse_semicolon();
2516        } else {
2517            test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2518            consequent = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2519            self.non_greedy_expect(Token::ParenClose);
2520            if self.expecting_token_error {
2521                semicolon = self.parse_semicolon();
2522            } else {
2523                let (consequent_1, semicolon_1) = self.parse_substatement(context.clone());
2524                consequent = consequent_1;
2525                if self.peek(Token::Else) {
2526                    if !semicolon_1 {
2527                        self.non_greedy_expect_virtual_semicolon();
2528                    }
2529                    self.next();
2530                    let (alternative_2, semicolon_2) = self.parse_substatement(context.clone());
2531                    alternative = Some(alternative_2);
2532                    semicolon = semicolon_2;
2533                } else {
2534                    semicolon = semicolon_1;
2535                }
2536            }
2537        }
2538        (Rc::new(Directive::IfStatement(IfStatement {
2539            location: self.pop_location(),
2540            test, consequent, alternative,
2541        })), semicolon)
2542    }
2543
2544    fn parse_switch_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2545        self.mark_location();
2546        self.next();
2547        if self.peek_context_keyword("type") {
2548            self.forbid_line_break_before_token();
2549            self.next();
2550            return self.parse_switch_type_statement(context);
2551        }
2552        let context = context.override_control_context(false, ParserControlFlowContext {
2553            breakable: true,
2554            iteration: false,
2555        });
2556        let mut discriminant = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2557        let mut cases: Vec<Case> = vec![];
2558        self.non_greedy_expect(Token::ParenOpen);
2559        if !self.expecting_token_error {
2560            discriminant = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2561            self.non_greedy_expect(Token::ParenClose);
2562            if !self.expecting_token_error {
2563                self.non_greedy_expect(Token::BlockOpen);
2564                if !self.expecting_token_error {
2565                    cases = self.parse_case_elements(context);
2566                    self.non_greedy_expect(Token::BlockClose);
2567                }
2568            }
2569        }
2570        (Rc::new(Directive::SwitchStatement(SwitchStatement {
2571            location: self.pop_location(),
2572            discriminant, cases,
2573        })), true)
2574    }
2575
2576    fn parse_case_elements(&mut self, context: ParserDirectiveContext) -> Vec<Case> {
2577        let mut cases = vec![];
2578        let mut semicolon = false;
2579        while !self.peek(Token::BlockClose) {
2580            if !cases.is_empty() && !semicolon {
2581                self.non_greedy_expect_virtual_semicolon();
2582            }
2583            if !(self.peek(Token::Case) || self.peek(Token::Default)) {
2584                break;
2585            }
2586            self.mark_location();
2587            let mut labels = vec![];
2588            loop {
2589                if self.peek(Token::Case) {
2590                    self.mark_location();
2591                    self.next();
2592                    let exp = self.parse_expression(ParserExpressionContext {
2593                        allow_in: true,
2594                        min_precedence: OperatorPrecedence::List,
2595                        ..default()
2596                    });
2597                    self.non_greedy_expect(Token::Colon);
2598                    labels.push(CaseLabel::Case((exp, self.pop_location())));
2599                } else if self.peek(Token::Default) {
2600                    self.mark_location();
2601                    self.next();
2602                    self.non_greedy_expect(Token::Colon);
2603                    labels.push(CaseLabel::Default(self.pop_location()));
2604                } else {
2605                    break;
2606                }
2607            }
2608            let mut directives = vec![];
2609            semicolon = false;
2610            while !(self.peek(Token::BlockClose) || self.peek(Token::Case) || self.peek(Token::Default)) {
2611                if !directives.is_empty() && !semicolon {
2612                    self.non_greedy_expect_virtual_semicolon();
2613                }
2614                let (directive, semicolon_1) = self.parse_directive(context.clone());
2615                directives.push(directive);
2616                semicolon = semicolon_1;
2617            }
2618            cases.push(Case {
2619                location: self.pop_location(),
2620                labels,
2621                directives,
2622            });
2623        }
2624        cases
2625    }
2626
2627    fn parse_switch_type_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2628        let context = context.override_control_context(true, ParserControlFlowContext {
2629            breakable: true,
2630            iteration: false,
2631        });
2632        let mut discriminant = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2633        let mut cases: Vec<TypeCase> = vec![];
2634        self.non_greedy_expect(Token::ParenOpen);
2635        if !self.expecting_token_error {
2636            discriminant = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2637            self.non_greedy_expect(Token::ParenClose);
2638            if !self.expecting_token_error {
2639                self.non_greedy_expect(Token::BlockOpen);
2640                if !self.expecting_token_error {
2641                    cases = self.parse_type_case_elements(context);
2642                    self.non_greedy_expect(Token::BlockClose);
2643                }
2644            }
2645        }
2646        (Rc::new(Directive::SwitchTypeStatement(SwitchTypeStatement {
2647            location: self.pop_location(),
2648            discriminant, cases,
2649        })), true)
2650    }
2651
2652    fn parse_type_case_elements(&mut self, context: ParserDirectiveContext) -> Vec<TypeCase> {
2653        let mut cases = vec![];
2654        while !self.peek(Token::BlockClose) && !self.peek(Token::Eof) {
2655            if self.peek(Token::Default) {
2656                self.mark_location();
2657                self.next();
2658                let block = Rc::new(self.parse_block(context.clone()));
2659                cases.push(TypeCase {
2660                    location: self.pop_location(),
2661                    parameter: None,
2662                    block,
2663                });
2664            } else {
2665                self.mark_location();
2666                self.non_greedy_expect(Token::Case);
2667                if !self.expecting_token_error {
2668                    self.non_greedy_expect(Token::ParenOpen);
2669                    if !self.expecting_token_error {
2670                        let parameter = Some(self.parse_typed_destructuring());
2671                        self.non_greedy_expect(Token::ParenClose);
2672                        if !self.expecting_token_error {
2673                            let block = Rc::new(self.parse_block(context.clone()));
2674                            cases.push(TypeCase {
2675                                location: self.pop_location(),
2676                                parameter,
2677                                block,
2678                            });
2679                        }
2680                    }
2681                }
2682            }
2683        }
2684        cases
2685    }
2686
2687    fn parse_do_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2688        let context = context.override_control_context(false, ParserControlFlowContext {
2689            breakable: true,
2690            iteration: true,
2691        });
2692        self.mark_location();
2693        self.next();
2694
2695        // Body
2696        let (body, semicolon_1) = self.parse_substatement(context);
2697        if !semicolon_1 {
2698            self.non_greedy_expect_virtual_semicolon();
2699        }
2700
2701        let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2702        self.non_greedy_expect(Token::While);
2703        if !self.expecting_token_error {
2704            test = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2705            self.non_greedy_expect(Token::ParenOpen);
2706            if !self.expecting_token_error {
2707                test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2708                self.non_greedy_expect(Token::ParenClose);
2709            }
2710        }
2711
2712        let semicolon = self.parse_semicolon();
2713        (Rc::new(Directive::DoStatement(DoStatement {
2714            location: self.pop_location(),
2715            body, test,
2716        })), semicolon)
2717    }
2718
2719    fn parse_while_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2720        let context = context.override_control_context(false, ParserControlFlowContext {
2721            breakable: true,
2722            iteration: true,
2723        });
2724        self.mark_location();
2725        self.next();
2726
2727        // Test
2728        let mut test = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2729        let mut body = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2730        let semicolon: bool;
2731        self.non_greedy_expect(Token::ParenOpen);
2732        if !self.expecting_token_error {
2733            test = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2734            body = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2735            self.non_greedy_expect(Token::ParenClose);
2736            if !self.expecting_token_error {
2737                let (body_1, semicolon_1) = self.parse_substatement(context);
2738                body = body_1;
2739                semicolon = semicolon_1;
2740            } else {
2741                semicolon = self.parse_semicolon();
2742            }
2743        } else {
2744            semicolon = self.parse_semicolon();
2745        }
2746
2747        (Rc::new(Directive::WhileStatement(WhileStatement {
2748            location: self.pop_location(),
2749            test, body,
2750        })), semicolon)
2751    }
2752
2753    /// Parses `for`, `for..in` or `for each`.
2754    fn parse_for_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2755        let context = context.override_control_context(false, ParserControlFlowContext {
2756            breakable: true,
2757            iteration: true,
2758        });
2759        self.mark_location();
2760        self.next();
2761
2762        // `for each`
2763        if self.peek_context_keyword("each") {
2764            self.forbid_line_break_before_token();
2765            self.next();
2766            return self.parse_for_each_statement(context);
2767        }
2768
2769        self.non_greedy_expect(Token::ParenOpen);
2770        if self.expecting_token_error {
2771            let body = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2772            let semicolon = self.parse_semicolon();
2773            return (Rc::new(Directive::ForStatement(ForStatement {
2774                location: self.pop_location(),
2775                init: None, test: None, update: None, body,
2776            })), semicolon);
2777        }
2778
2779        let init_variable = if self.peek(Token::Var) || self.peek(Token::Const) {
2780            Some(self.parse_simple_variable_definition(false))
2781        } else {
2782            None
2783        };
2784
2785        if init_variable.is_some() && self.consume(Token::In) {
2786            return self.parse_for_in_statement_with_left_variable(context, init_variable.unwrap());
2787        }
2788
2789        let mut init_exp = if init_variable.is_none() && !self.peek(Token::Semicolon) {
2790            self.parse_opt_expression(ParserExpressionContext {
2791                allow_in: false,
2792                min_precedence: OperatorPrecedence::Postfix,
2793                ..default()
2794            })
2795        } else {
2796            None
2797        };
2798
2799        if init_exp.is_some() && self.consume(Token::In) {
2800            return self.parse_for_in_statement_with_left_exp(context, init_exp.unwrap());
2801        }
2802
2803        if init_exp.is_none() && init_variable.is_none() && !self.peek(Token::Semicolon) {
2804            init_exp = Some(self.parse_expression(ParserExpressionContext {
2805                allow_in: false, min_precedence: OperatorPrecedence::List, ..default()
2806            }));
2807        } else if let Some(exp) = init_exp.as_ref() {
2808            init_exp = Some(self.parse_subexpressions(exp.clone(), ParserExpressionContext {
2809                allow_in: false, min_precedence: OperatorPrecedence::List, ..default()
2810            }));
2811        }
2812
2813        let init = if let Some(exp) = init_exp.as_ref() {
2814            Some(ForInitializer::Expression(exp.clone()))
2815        } else if let Some(variable) = init_variable.as_ref() {
2816            Some(ForInitializer::VariableDefinition(Rc::new(variable.clone())))
2817        } else {
2818            None
2819        };
2820
2821        self.non_greedy_expect(Token::Semicolon);
2822        let test = if self.peek(Token::Semicolon) {
2823            None
2824        } else {
2825            Some(self.parse_expression(ParserExpressionContext {
2826                allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2827            }))
2828        };
2829        self.non_greedy_expect(Token::Semicolon);
2830        let update = if self.peek(Token::ParenClose) {
2831            None
2832        } else {
2833            Some(self.parse_expression(ParserExpressionContext {
2834                allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2835            }))
2836        };
2837        self.non_greedy_expect(Token::ParenClose);
2838
2839        // Body
2840        let (body, semicolon) = self.parse_substatement(context);
2841
2842        (Rc::new(Directive::ForStatement(ForStatement {
2843            location: self.pop_location(),
2844            init, test, update, body,
2845        })), semicolon)
2846    }
2847
2848    fn parse_for_each_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2849        self.non_greedy_expect(Token::ParenOpen);
2850        if self.expecting_token_error {
2851            let left = ForInBinding::Expression(self.create_invalidated_expression(&self.tokenizer.cursor_location()));
2852            let right = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2853            let body = self.create_invalidated_directive(&self.tokenizer.cursor_location());
2854            let semicolon = self.parse_semicolon();
2855            return (Rc::new(Directive::ForInStatement(ForInStatement {
2856                location: self.pop_location(),
2857                each: true, left, right, body,
2858            })), semicolon);
2859        }
2860
2861        let left = if self.peek(Token::Var) || self.peek(Token::Const) {
2862            self.mark_location();
2863            let kind = (if self.peek(Token::Var) { VariableDefinitionKind::Var } else { VariableDefinitionKind::Const }, self.token_location());
2864            self.next();
2865            let binding = self.parse_variable_binding(false);
2866            if let Some(init) = &binding.initializer {
2867                self.add_syntax_error(&init.location(), DiagnosticKind::IllegalForInInitializer, vec![]);
2868            }
2869            ForInBinding::VariableDefinition(Rc::new(SimpleVariableDefinition {
2870                location: self.pop_location(),
2871                kind,
2872                bindings: vec![Rc::new(binding)],
2873            }))
2874        } else {
2875            ForInBinding::Expression(self.parse_expression(ParserExpressionContext {
2876                allow_in: false, min_precedence: OperatorPrecedence::Postfix, ..default()
2877            }))
2878        };
2879        let mut right = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2880        self.non_greedy_expect(Token::In);
2881        if !self.expecting_token_error {
2882            right = self.parse_expression(ParserExpressionContext {
2883                allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2884            });
2885        }
2886        self.non_greedy_expect(Token::ParenClose);
2887
2888        // Body
2889        let (body, semicolon) = self.parse_substatement(context);
2890
2891        (Rc::new(Directive::ForInStatement(ForInStatement {
2892            location: self.pop_location(),
2893            each: true, left, right, body,
2894        })), semicolon)
2895    }
2896
2897    fn parse_for_in_statement_with_left_variable(&mut self, context: ParserDirectiveContext, left: SimpleVariableDefinition) -> (Rc<Directive>, bool) {
2898        let variable_binding = left.bindings[0].clone();
2899
2900        if let Some(init) = &variable_binding.initializer {
2901            self.add_syntax_error(&init.location(), DiagnosticKind::IllegalForInInitializer, vec![]);
2902        }
2903
2904        if left.bindings.len() > 1 {
2905            self.add_syntax_error(&left.kind.1.clone(), DiagnosticKind::MultipleForInBindings, vec![]);
2906        }
2907
2908        let right = self.parse_expression(ParserExpressionContext {
2909            allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2910        });
2911        self.non_greedy_expect(Token::ParenClose);
2912
2913        // Body
2914        let (body, semicolon) = self.parse_substatement(context);
2915
2916        (Rc::new(Directive::ForInStatement(ForInStatement {
2917            location: self.pop_location(),
2918            each: false, left: ForInBinding::VariableDefinition(Rc::new(left)), right, body,
2919        })), semicolon)
2920    }
2921
2922    fn parse_for_in_statement_with_left_exp(&mut self, context: ParserDirectiveContext, left: Rc<Expression>) -> (Rc<Directive>, bool) {
2923        let right = self.parse_expression(ParserExpressionContext {
2924            allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
2925        });
2926        self.non_greedy_expect(Token::ParenClose);
2927
2928        // Body
2929        let (body, semicolon) = self.parse_substatement(context);
2930
2931        (Rc::new(Directive::ForInStatement(ForInStatement {
2932            location: self.pop_location(),
2933            each: false, left: ForInBinding::Expression(left), right, body,
2934        })), semicolon)
2935    }
2936
2937    fn parse_simple_variable_definition(&mut self, allow_in: bool) -> SimpleVariableDefinition {
2938        self.mark_location();
2939        let kind: VariableDefinitionKind;
2940        let kind_location = self.token_location();
2941        if self.consume(Token::Const) {
2942            kind = VariableDefinitionKind::Const;
2943        } else {
2944            self.non_greedy_expect(Token::Var);
2945            kind = VariableDefinitionKind::Var;
2946        }
2947        let mut bindings = vec![Rc::new(self.parse_variable_binding(allow_in))];
2948        while self.consume(Token::Comma) {
2949            bindings.push(Rc::new(self.parse_variable_binding(allow_in)));
2950        }
2951        SimpleVariableDefinition {
2952            location: self.pop_location(),
2953            kind: (kind, kind_location),
2954            bindings,
2955        }
2956    }
2957
2958    fn parse_with_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2959        let context = context.override_control_context(true, ParserControlFlowContext {
2960            breakable: true,
2961            iteration: false,
2962        });
2963        self.mark_location();
2964        self.next();
2965
2966        let mut object = self.create_invalidated_expression(&self.tokenizer.cursor_location());
2967        self.non_greedy_expect(Token::ParenOpen);
2968        if !self.expecting_token_error {
2969            object = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
2970        }
2971        self.non_greedy_expect(Token::ParenClose);
2972
2973        // Body
2974        let (body, semicolon) = self.parse_substatement(context);
2975
2976        (Rc::new(Directive::WithStatement(WithStatement {
2977            location: self.pop_location(),
2978            object, body,
2979        })), semicolon)
2980    }
2981
2982    fn parse_break_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
2983        self.mark_location();
2984        self.next();
2985
2986        let label = if self.previous_token.1.line_break(&self.token.1) { None } else { self.consume_identifier(false) };
2987        let label_location = label.clone().map(|label| label.1.clone());
2988        let label = label.map(|label| label.0.clone());
2989
2990        let semicolon = self.parse_semicolon();
2991
2992        let node = Rc::new(Directive::BreakStatement(BreakStatement {
2993            location: self.pop_location(),
2994            label: label.clone().map(|l| (l.clone(), label_location.clone().unwrap())),
2995        }));
2996
2997        if label.is_some() && !context.is_label_defined(label.clone().unwrap()) {
2998            self.add_syntax_error(&label_location.unwrap(), DiagnosticKind::UndefinedLabel, diagarg![label.clone().unwrap()]);
2999        } else if !context.is_break_allowed(label) {
3000            self.add_syntax_error(&node.location(), DiagnosticKind::IllegalBreak, vec![]);
3001        }
3002
3003        (node, semicolon)
3004    }
3005
3006    fn parse_continue_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3007        self.mark_location();
3008        self.next();
3009
3010        let label = if self.previous_token.1.line_break(&self.token.1) { None } else { self.consume_identifier(false) };
3011        let label_location = label.clone().map(|label| label.1.clone());
3012        let label = label.map(|label| label.0.clone());
3013
3014        let semicolon = self.parse_semicolon();
3015
3016        let node = Rc::new(Directive::ContinueStatement(ContinueStatement {
3017            location: self.pop_location(),
3018            label: label.clone().map(|l| (l.clone(), label_location.clone().unwrap())),
3019        }));
3020
3021        if label.is_some() && !context.is_label_defined(label.clone().unwrap()) {
3022            self.add_syntax_error(&label_location.unwrap(), DiagnosticKind::UndefinedLabel, diagarg![label.clone().unwrap()]);
3023        } else if !context.is_continue_allowed(label) {
3024            self.add_syntax_error(&node.location(), DiagnosticKind::IllegalContinue, vec![]);
3025        }
3026
3027        (node, semicolon)
3028    }
3029
3030    fn parse_return_statement(&mut self, _context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3031        self.mark_location();
3032        self.next();
3033
3034        let expression = if self.previous_token.1.line_break(&self.token.1) { None } else {
3035            self.parse_opt_expression(ParserExpressionContext {
3036                allow_in: true,
3037                min_precedence: OperatorPrecedence::List,
3038                ..default()
3039            })
3040        };
3041
3042        let semicolon = self.parse_semicolon();
3043
3044        let node = Rc::new(Directive::ReturnStatement(ReturnStatement {
3045            location: self.pop_location(),
3046            expression,
3047        }));
3048
3049        (node, semicolon)
3050    }
3051
3052    fn parse_throw_statement(&mut self, _context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3053        self.mark_location();
3054        self.next();
3055
3056        let line_break = self.previous_token.1.line_break(&self.token.1);
3057
3058        let expression = self.parse_expression(ParserExpressionContext {
3059            allow_in: true,
3060            min_precedence: OperatorPrecedence::List,
3061            ..default()
3062        });
3063
3064        if line_break {
3065            self.add_syntax_error(&expression.location(), DiagnosticKind::ExpressionMustNotFollowLineBreak, vec![]);
3066        }
3067
3068        let semicolon = self.parse_semicolon();
3069
3070        let node = Rc::new(Directive::ThrowStatement(ThrowStatement {
3071            location: self.pop_location(),
3072            expression,
3073        }));
3074
3075        (node, semicolon)
3076    }
3077
3078    fn parse_try_statement(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3079        self.mark_location();
3080        self.next();
3081        let context = context.clone_control();
3082        let block = Rc::new(self.parse_block(context.clone()));
3083        let mut catch_clauses: Vec<CatchClause> = vec![];
3084        let mut finally_clause: Option<FinallyClause> = None;
3085        let mut found_catch = false;
3086        loop {
3087            if self.peek(Token::Catch) {
3088                found_catch = true;
3089                self.mark_location();
3090                self.next();
3091                self.non_greedy_expect(Token::ParenOpen);
3092                if !self.expecting_token_error {
3093                    let parameter = self.parse_typed_destructuring();
3094                    self.non_greedy_expect(Token::ParenClose);
3095                    if !self.expecting_token_error {
3096                        let block = Rc::new(self.parse_block(context.clone()));
3097                        catch_clauses.push(CatchClause {
3098                            location: self.pop_location(),
3099                            parameter,
3100                            block,
3101                        });
3102                    }
3103                }
3104            } else if self.peek(Token::Finally) {
3105                self.mark_location();
3106                self.next();
3107                let block = Rc::new(self.parse_block(context.clone()));
3108                finally_clause = Some(FinallyClause {
3109                    location: self.pop_location(),
3110                    block,
3111                });
3112                break;
3113            } else {
3114                break;
3115            }
3116        }
3117        if !found_catch && finally_clause.is_none() {
3118            self.non_greedy_expect(Token::Catch);
3119        }
3120
3121        let node = Rc::new(Directive::TryStatement(TryStatement {
3122            location: self.pop_location(),
3123            block, catch_clauses, finally_clause,
3124        }));
3125
3126        (node, true)
3127    }
3128
3129    fn parse_default_xml_namespace_statement(&mut self) -> (Rc<Directive>, bool) {
3130        self.mark_location();
3131        self.next();
3132
3133        let mut expression = self.create_invalidated_expression(&self.tokenizer.cursor_location());
3134        self.forbid_line_break_before_token();
3135        self.non_greedy_expect_context_keyword("xml");
3136        if !self.expecting_token_error {
3137            expression = self.create_invalidated_expression(&self.tokenizer.cursor_location());
3138            self.forbid_line_break_before_token();
3139            self.non_greedy_expect_context_keyword("namespace");
3140            if !self.expecting_token_error {
3141                expression = self.create_invalidated_expression(&self.tokenizer.cursor_location());
3142                self.non_greedy_expect(Token::Assign);
3143
3144                if !self.expecting_token_error {
3145                    expression = self.parse_expression(ParserExpressionContext {
3146                        allow_in: true,
3147                        allow_assignment: false,
3148                        min_precedence: OperatorPrecedence::AssignmentAndOther,
3149                        ..default()
3150                    });
3151                }
3152            }
3153        }
3154
3155        let semicolon = self.parse_semicolon();
3156
3157        let node = Rc::new(Directive::DefaultXmlNamespaceStatement(DefaultXmlNamespaceStatement {
3158            location: self.pop_location(),
3159            right: expression,
3160        }));
3161
3162        (node, semicolon)
3163    }
3164
3165    fn forbid_line_break_before_token(&mut self) {
3166        if self.previous_token.1.line_break(&self.token.1) {
3167            self.add_syntax_error(&self.token.1.clone(), DiagnosticKind::TokenMustNotFollowLineBreak, vec![]);
3168        }
3169    }
3170
3171    fn parse_directive(&mut self, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3172        let asdoc: Option<Rc<Asdoc>> = if self.peek(Token::SquareOpen) { None } else { self.parse_asdoc() };
3173        // ConfigurationDirective or Statement
3174        if let Token::Identifier(id) = &self.token.0 {
3175            let id = (id.clone(), self.token_location());
3176            self.next();
3177
3178            if id.0 == "include" && id.1.character_count() == "include".len() && matches!(self.token.0, Token::String(_)) && !self.previous_token.1.line_break(&self.token.1) {
3179                return self.parse_include_directive(context, id.1);
3180            }
3181
3182            // Labeled statement
3183            if self.consume(Token::Colon) {
3184                self.push_location(&id.1);
3185                let (substatement, semicolon) = self.parse_substatement(context.put_label(id.0.clone()));
3186                let labeled = Rc::new(Directive::LabeledStatement(LabeledStatement {
3187                    location: self.pop_location(),
3188                    label: id.clone(),
3189                    substatement,
3190                }));
3191                return (labeled, semicolon);
3192            }
3193
3194            // If there is a line break or offending token is "::",
3195            // do not proceed into parsing an expression attribute or annotatble directive.
3196            let eligible_attribute_or_directive
3197                =  !self.previous_token.1.line_break(&self.token.1)
3198                && !(matches!(self.token.0, Token::ColonColon));
3199
3200            if eligible_attribute_or_directive && (self.peek_annotatable_directive_identifier_name() || self.lookbehind_is_annotatable_directive_identifier_name()) {
3201                let mut context1: AnnotatableContext;
3202
3203                if ["enum", "type", "namespace"].contains(&id.0.as_ref())
3204                && id.1.character_count() == id.0.len()
3205                && self.token.0.is_identifier_name() {
3206                    context1 = AnnotatableContext {
3207                        start_location: id.1.clone(),
3208                        asdoc,
3209                        attributes: vec![],
3210                        context: context.clone(),
3211                        directive_context_keyword: Some(id.clone()),
3212                    };
3213                    // self.parse_attribute_keywords_or_expressions(&mut context);
3214                } else {
3215                    let mut first_attr_expr = self.parse_expression_starting_with_identifier(id);
3216                    first_attr_expr = self.parse_subexpressions(first_attr_expr, ParserExpressionContext {
3217                        allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
3218                    });
3219
3220                    // Do not proceed into parsing an annotatable directive
3221                    // if there is a line break after an expression attribute,
3222                    // or if the offending token is not an identifier name,
3223                    // or if the expression attribute is not a valid access modifier.
3224                    if !first_attr_expr.valid_access_modifier() || self.previous_token.1.line_break(&self.token.1) || !(matches!(self.token.0, Token::Identifier(_)) || self.token.0.is_reserved_word()) {
3225                        self.push_location(&first_attr_expr.location());
3226
3227                        // Parse CONFIG::VAR_NAME
3228                        if let Some(result) = self.parse_opt_config(&first_attr_expr, asdoc.clone(), context.clone()) {
3229                            return result;
3230                        }
3231
3232                        let semicolon = self.parse_semicolon();
3233                        return (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
3234                            location: self.pop_location(),
3235                            expression: first_attr_expr,
3236                        })), semicolon);
3237                    }
3238
3239                    let first_attr = self.keyword_or_expression_attribute_from_expression(&first_attr_expr);
3240
3241                    context1 = AnnotatableContext {
3242                        start_location: first_attr.location(),
3243                        asdoc,
3244                        attributes: vec![first_attr],
3245                        context: context.clone(),
3246                        directive_context_keyword: None,
3247                    };
3248                    self.parse_attribute_keywords_or_expressions(&mut context1);
3249                }
3250                return self.parse_annotatable_directive(context1);
3251            } else if self.peek(Token::ColonColon) {
3252                self.parse_qualified_identifier_statement_or_config(context, id, asdoc)
3253            } else {
3254                self.parse_statement_starting_with_identifier(context, id)
3255            }
3256        } else if self.peek(Token::Import) {
3257            self.parse_import_directive_or_expression_statement(context)
3258        } else if self.peek(Token::SquareOpen) {
3259            self.mark_location();
3260            let exp = self.parse_expression(ParserExpressionContext {
3261                allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
3262            });
3263            if self.peek_annotatable_directive_identifier_name() {
3264                match exp.to_metadata(self) {
3265                    Ok(Some(metadata)) => {
3266                        let mut context = AnnotatableContext {
3267                            start_location: self.pop_location(),
3268                            asdoc: self.parse_asdoc(),
3269                            attributes: metadata,
3270                            context: context.clone(),
3271                            directive_context_keyword: None,
3272                        };
3273                        self.parse_attribute_keywords_or_expressions(&mut context);
3274                        return self.parse_annotatable_directive(context);
3275                    },
3276                    Ok(None) => {},
3277                    Err(MetadataRefineError1(MetadataRefineError::Syntax, loc)) => {
3278                        self.add_syntax_error(&loc, DiagnosticKind::UnrecognizedMetadataSyntax, diagarg![]);
3279                    },
3280                }
3281            }
3282            let semicolon = self.parse_semicolon();
3283            (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
3284                location: self.pop_location(),
3285                expression: exp,
3286            })), semicolon)
3287        } else if self.peek(Token::Public) || self.peek(Token::Private) || self.peek(Token::Protected)
3288        || self.peek(Token::Internal) || self.peek(Token::Var) || self.peek(Token::Const)
3289        || self.peek(Token::Function) || self.peek(Token::Class) || self.peek(Token::Interface) {
3290            let is_public = self.peek(Token::Public);
3291            let rns = self.parse_opt_reserved_namespace();
3292            let mut attributes: Vec<Attribute> = vec![];
3293            if let Some(rns) = rns {
3294                // The public += ns.*; directive
3295                if self.peek(Token::AddAssign) && is_public {
3296                    return self.parse_package_concat_directive(&rns.location(), context);
3297                }
3298
3299                // Do not proceed into parsing an annotatable directive
3300                // if there is a "::" token.
3301                if matches!(self.token.0, Token::ColonColon) {
3302                    self.push_location(&rns.location());
3303                    let rns = Rc::new(Expression::QualifiedIdentifier(self.finish_qualified_identifier(false, rns.location(), rns)));
3304                    let rns = self.parse_subexpressions(rns, ParserExpressionContext {
3305                        allow_in: true, min_precedence: OperatorPrecedence::List, ..default()
3306                    });
3307                    let semicolon = self.parse_semicolon();
3308                    return (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
3309                        location: self.pop_location(),
3310                        expression: rns,
3311                    })), semicolon);
3312                }
3313                attributes.push(rns.to_reserved_namespace_attribute().unwrap());
3314            }
3315            let mut context = AnnotatableContext {
3316                start_location: self.token_location(),
3317                asdoc,
3318                attributes,
3319                context: context.clone(),
3320                directive_context_keyword: None,
3321            };
3322            self.parse_attribute_keywords_or_expressions(&mut context);
3323            return self.parse_annotatable_directive(context);
3324        } else if self.peek(Token::Use) {
3325            self.parse_use_namespace_directive()
3326        } else {
3327            let i = self.tokenizer.characters().index();
3328            let r = self.parse_statement(context);
3329            if i == self.tokenizer.characters().index() {
3330                self.patch_syntax_error(DiagnosticKind::ExpectingStatement, DiagnosticKind::ExpectingDirective, diagarg![self.token.0.clone()]);
3331            }
3332            r
3333        }
3334    }
3335
3336    fn parse_directives(&mut self, context: ParserDirectiveContext) -> Vec<Rc<Directive>> {
3337        let mut directives = vec![];
3338        let mut semicolon = false;
3339        while !self.peek(Token::Eof) {
3340            if !directives.is_empty() && !semicolon {
3341                self.non_greedy_expect_virtual_semicolon();
3342            }
3343            let (directive, semicolon_1) = self.parse_directive(context.clone());
3344            directives.push(directive);
3345            semicolon = semicolon_1;
3346        }
3347        directives
3348    }
3349
3350    fn parse_expression_attribute(&mut self, id: &(String, Location)) -> Rc<Expression> {
3351        let mut result = Rc::new(Expression::QualifiedIdentifier(QualifiedIdentifier {
3352            location: id.1.clone(),
3353            attribute: false,
3354            qualifier: None,
3355            id: QualifiedIdentifierIdentifier::Id(id.clone()),
3356        }));
3357        loop {
3358            if self.peek(Token::Dot) {
3359                self.push_location(&result.location());
3360                self.next();
3361                let id = self.parse_qualified_identifier();
3362                result = Rc::new(Expression::Member(MemberExpression {
3363                    location: self.pop_location(),
3364                    base: result, identifier: id
3365                }));
3366            } else if self.consume(Token::SquareOpen) {
3367                self.push_location(&result.location());
3368                let key = self.parse_expression(ParserExpressionContext { allow_in: true, min_precedence: OperatorPrecedence::List, ..default() });
3369                self.non_greedy_expect(Token::SquareClose);
3370                result = Rc::new(Expression::ComputedMember(ComputedMemberExpression {
3371                    base: result, asdoc: None, key, location: self.pop_location()
3372                }));
3373            } else {
3374                break;
3375            }
3376        }
3377        result
3378    }
3379
3380    fn report_modifier_errors(&self, context: &AnnotatableContext) {
3381        let mut i = 0usize;
3382        while i < context.attributes.len() {
3383            let a = &context.attributes[i];
3384            if Attribute::has(&context.attributes[..i], &a) {
3385                self.add_syntax_error(&a.location(), DiagnosticKind::DuplicateAttribute, diagarg![]);
3386            }
3387            if Attribute::is_duplicate_access_modifier(&context.attributes[..i], &a) {
3388                self.add_syntax_error(&a.location(), DiagnosticKind::DuplicateAccessModifier, diagarg![]);
3389            }
3390            i += 1;
3391        }
3392    }
3393
3394    fn parse_annotatable_directive(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
3395        if self.peek(Token::Var) || self.peek(Token::Const) {
3396            self.report_modifier_errors(&context);
3397            self.parse_variable_definition(context)
3398        } else if self.consume(Token::Function) {
3399            self.report_modifier_errors(&context);
3400            self.parse_function_definition(context)
3401        } else if self.consume(Token::Class) {
3402            self.report_modifier_errors(&context);
3403            self.parse_class_definition(context)
3404        } else if context.has_directive_context_keyword("enum") {
3405            self.report_modifier_errors(&context);
3406            self.parse_enum_definition(context)
3407        } else if context.has_directive_context_keyword("namespace") {
3408            self.report_modifier_errors(&context);
3409            self.parse_namespace_definition(context)
3410        } else if self.consume(Token::Interface) {
3411            self.report_modifier_errors(&context);
3412            self.parse_interface_definition(context)
3413        } else if context.has_directive_context_keyword("type") {
3414            self.report_modifier_errors(&context);
3415            self.parse_type_definition(context)
3416        } else {
3417            // In case there is a series of inline modifiers,
3418            // report semicolon error between each.
3419            let mut i = 0usize;
3420            let mut error = false;
3421            while i < context.attributes.len() {
3422                if !context.attributes[i].is_metadata() {
3423                    let loc1 = context.attributes[i].location();
3424                    if i + 1 < context.attributes.len() {
3425                        let loc2 = context.attributes[i + 1].location();
3426                        if !loc1.line_break(&loc2) {
3427                            self.add_syntax_error(&loc2, DiagnosticKind::ExpectingEitherSemicolonOrNewLineHere, vec![]);
3428                            error = true;
3429                        }
3430                    }
3431                }
3432                i += 1;
3433            }
3434
3435            if !error {
3436                self.add_syntax_error(&self.token_location(), DiagnosticKind::ExpectingDirectiveKeyword, diagarg![self.token.0.clone()]);
3437            }
3438            self.push_location(&context.start_location);
3439            let loc = self.pop_location();
3440            (self.create_invalidated_directive(&loc), true)
3441        }
3442    }
3443
3444    pub(crate) fn refine_metadata(&self, exp: &Rc<Expression>, asdoc: Option<Rc<Asdoc>>) -> Result<Rc<Metadata>, MetadataRefineError> {
3445        if let Expression::Call(CallExpression { base, arguments, .. }) = exp.as_ref() {
3446            let Ok(name) = self.refine_metadata_name(base) else {
3447                return Err(MetadataRefineError::Syntax);
3448            };
3449            Ok(Rc::new(Metadata {
3450                location: exp.location(),
3451                asdoc,
3452                name,
3453                entries: Some(self.refine_metadata_entries(arguments)?),
3454            }))
3455        } else {
3456            if let Ok(name) = self.refine_metadata_name(exp) {
3457                Ok(Rc::new(Metadata {
3458                    location: exp.location(),
3459                    asdoc,
3460                    name,
3461                    entries: None,
3462                }))
3463            } else {
3464                Err(MetadataRefineError::Syntax)
3465            }
3466        }
3467    }
3468
3469    fn refine_metadata_name(&self, exp: &Rc<Expression>) -> Result<(String, Location), MetadataRefineError> {
3470        if let Expression::QualifiedIdentifier(id) = exp.as_ref() {
3471            if id.attribute {
3472                return Err(MetadataRefineError::Syntax);
3473            }
3474            let qual = id.qualifier.as_ref().and_then(|q| q.to_identifier_name().map(|n| n.0));
3475            if id.qualifier.is_some() && qual.is_none() {
3476                return Err(MetadataRefineError::Syntax);
3477            }
3478            if let QualifiedIdentifierIdentifier::Id((s, _)) = &id.id {
3479                if s == "*" { Err(MetadataRefineError::Syntax) } else { Ok((if let Some(q) = qual { format!("{q}::{s}") } else { s.to_string() }, exp.location())) }
3480            } else {
3481                Err(MetadataRefineError::Syntax)
3482            }
3483        } else {
3484            Err(MetadataRefineError::Syntax)
3485        }
3486    }
3487
3488    fn refine_metadata_entries(&self, list: &Vec<Rc<Expression>>) -> Result<Vec<Rc<MetadataEntry>>, MetadataRefineError> {
3489        let mut r = Vec::<Rc<MetadataEntry>>::new();
3490        for entry in list {
3491            r.push(self.refine_metadata_entry(&entry)?);
3492        }
3493        Ok(r)
3494    }
3495
3496    fn refine_metadata_entry(&self, exp: &Rc<Expression>) -> Result<Rc<MetadataEntry>, MetadataRefineError> {
3497        match exp.as_ref() {
3498            Expression::Assignment(AssignmentExpression { compound, left, right, location }) => {
3499                if compound.is_some() {
3500                    return Err(MetadataRefineError::Syntax);
3501                }
3502                let key = self.refine_metadata_name(left)?;
3503                if matches!(right.as_ref(), Expression::QualifiedIdentifier(_)) {
3504                    return Err(MetadataRefineError::Syntax);
3505                }
3506                let value = self.refine_metadata_value(right)?;
3507                Ok(Rc::new(MetadataEntry {
3508                    location: location.clone(),
3509                    key: Some(key),
3510                    value: Rc::new(value),
3511                }))
3512            },
3513            _ => {
3514                let value = self.refine_metadata_value(exp)?;
3515                Ok(Rc::new(MetadataEntry {
3516                    location: exp.location(),
3517                    key: None,
3518                    value: Rc::new(value),
3519                }))
3520            },
3521        }
3522    }
3523
3524    fn refine_metadata_value(&self, exp: &Rc<Expression>) -> Result<MetadataValue, MetadataRefineError> {
3525        match exp.as_ref() {
3526            Expression::QualifiedIdentifier(_) => {
3527                let name = self.refine_metadata_name(&exp)?;
3528                Ok(MetadataValue::IdentifierString(name))
3529            },
3530            Expression::StringLiteral(StringLiteral { value, .. }) => Ok(MetadataValue::String((value.clone(), exp.location()))),
3531            _ => Err(MetadataRefineError::Syntax),
3532        }
3533    }
3534
3535    fn parse_package_concat_directive(&mut self, start: &Location, context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3536        self.push_location(start);
3537        self.next();
3538        let mut package_name: Vec<(String, Location)> = vec![self.expect_identifier(false)];
3539        let mut import_specifier = ImportSpecifier::Wildcard(self.token_location());
3540
3541        if !self.peek(Token::Dot) {
3542            self.non_greedy_expect(Token::Dot);
3543        }
3544
3545        while self.consume(Token::Dot) {
3546            if self.peek(Token::Times) {
3547                import_specifier = ImportSpecifier::Wildcard(self.token_location());
3548                self.next();
3549                break;
3550            } else if self.peek(Token::Power) {
3551                import_specifier = ImportSpecifier::Recursive(self.token_location());
3552                self.next();
3553                break;
3554            } else {
3555                let id1 = self.expect_identifier(true);
3556                if !self.peek(Token::Dot) {
3557                    import_specifier = ImportSpecifier::Identifier(id1.clone());
3558                    break;
3559                } else {
3560                    package_name.push(id1.clone());
3561                }
3562            }
3563        }
3564
3565        let semicolon = self.parse_semicolon();
3566
3567        let node = Rc::new(Directive::PackageConcatDirective(PackageConcatDirective {
3568            location: self.pop_location(),
3569            package_name,
3570            import_specifier,
3571        }));
3572
3573        if !(matches!(context, ParserDirectiveContext::PackageBlock)) {
3574            self.add_syntax_error(&node.location(), DiagnosticKind::UnexpectedDirective, vec![]);
3575        }
3576
3577        (node, semicolon)
3578    }
3579
3580    fn parse_import_directive_or_expression_statement(&mut self, _context: ParserDirectiveContext) -> (Rc<Directive>, bool) {
3581        self.mark_location();
3582        self.next();
3583        if self.consume(Token::Dot) {
3584            self.duplicate_location();
3585            self.non_greedy_expect_context_keyword("meta");
3586            let mut expression = Rc::new(Expression::ImportMeta(ImportMeta {
3587                location: self.pop_location(),
3588            }));
3589            expression = self.parse_subexpressions(expression, ParserExpressionContext {
3590                allow_in: true,
3591                min_precedence: OperatorPrecedence::List,
3592                ..default()
3593            });
3594            let semicolon = self.parse_semicolon();
3595            (Rc::new(Directive::ExpressionStatement(ExpressionStatement {
3596                location: self.pop_location(),
3597                expression,
3598            })), semicolon)
3599        } else {
3600            let mut alias: Option<(String, Location)> = None;
3601            let mut package_name: Vec<(String, Location)> = vec![];
3602            let mut import_specifier = ImportSpecifier::Wildcard(self.token_location());
3603            let id1 = self.expect_identifier(false);
3604            if self.consume(Token::Assign) {
3605                alias = Some(id1.clone());
3606                package_name.push(self.expect_identifier(false));
3607            } else {
3608                package_name.push(id1);
3609            }
3610    
3611            if !self.peek(Token::Dot) {
3612                self.non_greedy_expect(Token::Dot);
3613            }
3614    
3615            while self.consume(Token::Dot) {
3616                if self.peek(Token::Times) {
3617                    import_specifier = ImportSpecifier::Wildcard(self.token_location());
3618                    self.next();
3619                    break;
3620                } else if self.peek(Token::Power) {
3621                    import_specifier = ImportSpecifier::Recursive(self.token_location());
3622                    self.next();
3623                    break;
3624                } else {
3625                    let id1 = self.expect_identifier(true);
3626                    if !self.peek(Token::Dot) {
3627                        import_specifier = ImportSpecifier::Identifier(id1.clone());
3628                        break;
3629                    } else {
3630                        package_name.push(id1.clone());
3631                    }
3632                }
3633            }
3634    
3635            let semicolon = self.parse_semicolon();
3636    
3637            let node = Rc::new(Directive::ImportDirective(ImportDirective {
3638                location: self.pop_location(),
3639                alias,
3640                package_name,
3641                import_specifier,
3642            }));
3643    
3644            (node, semicolon)
3645        }
3646    }
3647
3648    fn parse_include_directive(&mut self, context: ParserDirectiveContext, start: Location) -> (Rc<Directive>, bool) {
3649        self.push_location(&start);
3650        let source_path_location = self.token_location();
3651        let Token::String(source) = &self.token.0.clone() else {
3652            panic!();
3653        };
3654        let source = source.clone();
3655        self.next();
3656        let semicolon = self.parse_semicolon();
3657
3658        let mut nested_compilation_unit: Option<Rc<CompilationUnit>> = None;
3659
3660        // Select origin file path
3661        let origin_file_path = if let Some(file_path) = self.tokenizer.compilation_unit().file_path.clone() {
3662            Some(file_path)
3663        } else {
3664            std::env::current_dir().ok().map(|d| d.to_string_lossy().into_owned())
3665        };
3666
3667        // Resolve source
3668        if let Some(origin_file_path) = origin_file_path {
3669            let sub_flex_file_path = realhydroper_path::FlexPath::from_n_native([origin_file_path.as_ref(), "..", source.as_ref()]);
3670            let sub_file_path = sub_flex_file_path.to_string();
3671
3672            if !sub_flex_file_path.has_extension(".include.as") {
3673                self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::UnexpectedIncludeExtension, vec![]);
3674
3675                // Use a placeholder compilation unit
3676                nested_compilation_unit = Some(CompilationUnit::new(None, "".into()));
3677            } else if self.tokenizer.compilation_unit().include_directive_is_circular(&sub_file_path) {
3678                self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::CircularIncludeDirective, vec![]);
3679
3680                // Use a placeholder compilation unit
3681                nested_compilation_unit = Some(CompilationUnit::new(None, "".into()));
3682            } else {
3683                // Read included content
3684
3685                let sub_file_path_pathbuf = realhydroper_path::normalize_path(&sub_flex_file_path.to_path_buf());
3686                let mut replaced = false;
3687
3688                for (replace_file_path, content) in self.replace_included_content.read().unwrap().iter() {
3689                    if sub_file_path_pathbuf == realhydroper_path::normalize_path(&replace_file_path) {
3690                        nested_compilation_unit = Some(CompilationUnit::new(Some(sub_file_path_pathbuf.to_str().unwrap().to_owned()), content.clone()));
3691                        replaced = true;
3692                        break;
3693                    }
3694                }
3695
3696                if !replaced {
3697                    if let Ok(content) = std::fs::read_to_string(&sub_file_path) {
3698                        nested_compilation_unit = Some(CompilationUnit::new(Some(sub_file_path_pathbuf.to_str().unwrap().to_owned()), content));
3699                    } else {
3700                        self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::FailedToIncludeFile, vec![]);
3701
3702                        // Use a placeholder compilation unit
3703                        nested_compilation_unit = Some(CompilationUnit::new(None, "".into()));
3704                    }
3705                }
3706            }
3707        } else {
3708            self.add_syntax_error(&source_path_location.clone(), DiagnosticKind::ParentSourceIsNotAFile, vec![]);
3709
3710            // Use a placeholder compilation unit
3711            nested_compilation_unit = Some(CompilationUnit::new(None, "".into()));
3712        }
3713
3714        let nested_compilation_unit = nested_compilation_unit.unwrap();
3715
3716        // Inherit compiler options
3717        nested_compilation_unit.set_compiler_options(self.tokenizer.compilation_unit().compiler_options());
3718
3719        // Add sub compilation unit to super compilation unit
3720        self.tokenizer.compilation_unit().add_nested_compilation_unit(nested_compilation_unit.clone());
3721
3722        // Parse directives from replacement source
3723        let (nested_packages, nested_directives) = parse_include_directive_source(nested_compilation_unit.clone(), context, self.options());
3724
3725        // Delegate sub compilation unit errors to super compilation unit
3726        if nested_compilation_unit.invalidated() {
3727            self.tokenizer.compilation_unit().invalidated.set(true);
3728        }
3729
3730        let node = Rc::new(Directive::IncludeDirective(IncludeDirective {
3731            location: self.pop_location(),
3732            source,
3733            nested_packages,
3734            nested_directives,
3735            nested_compilation_unit: nested_compilation_unit.clone(),
3736        }));
3737
3738        (node, semicolon)
3739    }
3740
3741    fn parse_use_namespace_directive(&mut self) -> (Rc<Directive>, bool) {
3742        self.mark_location();
3743        self.next();
3744        let mut expression = self.create_invalidated_expression(&self.tokenizer.cursor_location());
3745        self.non_greedy_expect_context_keyword("namespace");
3746        if !self.expecting_token_error {
3747            expression = self.parse_expression(ParserExpressionContext {
3748                min_precedence: OperatorPrecedence::List,
3749                ..default()
3750            });
3751        }
3752        let semicolon = self.parse_semicolon();
3753
3754        let node = Rc::new(Directive::UseNamespaceDirective(UseNamespaceDirective {
3755            location: self.pop_location(),
3756            expression,
3757        }));
3758
3759        (node, semicolon)
3760    }
3761
3762    fn parse_variable_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
3763        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
3764        let has_static = Attribute::find_static(&attributes).is_some();
3765        self.push_location(&start_location);
3766        let kind_location = self.token_location();
3767        let kind = if self.consume(Token::Const) {
3768            VariableDefinitionKind::Const
3769        } else {
3770            self.non_greedy_expect(Token::Var);
3771            VariableDefinitionKind::Var
3772        };
3773        let mut bindings = vec![Rc::new(self.parse_variable_binding(true))];
3774        while self.consume(Token::Comma) {
3775            bindings.push(Rc::new(self.parse_variable_binding(true)));
3776        }
3777
3778        // Forbid destructuring bindings in enumerations.
3779        if !has_static && matches!(context, ParserDirectiveContext::EnumBlock) {
3780            if kind != VariableDefinitionKind::Const {
3781                self.add_syntax_error(&kind_location, DiagnosticKind::EnumMembersMustBeConst, diagarg![]);
3782            }
3783            for binding in &bindings {
3784                let malformed = !matches!(binding.destructuring.destructuring.as_ref(), Expression::QualifiedIdentifier(_))
3785                    || binding.destructuring.type_annotation.is_some();
3786                if malformed {
3787                    self.add_syntax_error(&binding.location(), DiagnosticKind::MalformedEnumMember, diagarg![]);
3788                }
3789            }
3790        }
3791
3792        for a in &attributes {
3793            if a.is_metadata() {
3794                continue;
3795            }
3796            match a {
3797                Attribute::Static(_) => {
3798                    if !context.is_type_block() {
3799                        // Unallowed attribute
3800                        self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3801                    }
3802                },
3803                Attribute::Expression(_) |
3804                Attribute::Public(_) |
3805                Attribute::Private(_) |
3806                Attribute::Protected(_) |
3807                Attribute::Internal(_) => {
3808                    self.verify_visibility(&a, &context);
3809                },
3810                _ => {
3811                    // Unallowed attribute
3812                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3813                },
3814            }
3815        }
3816
3817        let semicolon = self.parse_semicolon();
3818        let node = Rc::new(Directive::VariableDefinition(VariableDefinition {
3819            location: self.pop_location(),
3820            asdoc,
3821            attributes,
3822            kind: (kind, kind_location),
3823            bindings,
3824        }));
3825
3826        (node, semicolon)
3827    }
3828
3829    fn parse_function_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
3830        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
3831        let has_native = Attribute::find_native(&attributes).is_some();
3832        let has_abstract = Attribute::find_abstract(&attributes).is_some();
3833        self.push_location(&start_location);
3834        let mut name = self.expect_identifier(true);
3835        let mut getter = false;
3836        let mut setter = false;
3837        if self.peek_identifier(true).is_some() {
3838            getter = Token::is_context_keyword(&self.previous_token, "get");
3839            setter = Token::is_context_keyword(&self.previous_token, "set");
3840            if getter || setter {
3841                name = self.expect_identifier(true);
3842            }
3843        }
3844        let constructor = !getter && !setter && context.function_name_is_constructor(&name);
3845        let name = if getter {
3846            FunctionName::Getter(name)
3847        } else if setter {
3848            FunctionName::Setter(name)
3849        } else if constructor {
3850            FunctionName::Constructor(name)
3851        } else {
3852            FunctionName::Identifier(name)
3853        };
3854        let block_context = if constructor {
3855            ParserDirectiveContext::ConstructorBlock { super_statement_found: Rc::new(Cell::new(false)) }
3856        } else {
3857            ParserDirectiveContext::Default
3858        };
3859        let common = self.parse_function_common(false, block_context, true);
3860        let semicolon = if common.has_block_body() { true } else { self.parse_semicolon() };
3861
3862        /*
3863        if constructor && common.signature.result_type.is_some() {
3864            self.add_syntax_error(&name.location(), DiagnosticKind::ConstructorMustNotSpecifyResultType, diagarg![]);
3865        }
3866        */
3867
3868        // Not all kinds of functions may be generators.
3869        if common.contains_yield && (constructor || getter || setter) {
3870            self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMayNotBeGenerator, diagarg![]);
3871        }
3872
3873        // Not all kinds of functions may be asynchronous.
3874        if common.contains_await && (constructor || getter || setter) {
3875            self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMayNotBeAsynchronous, diagarg![]);
3876        }
3877
3878        let interface_method = matches!(context, ParserDirectiveContext::InterfaceBlock);
3879
3880        // Body verification.
3881        //
3882        // Note that interface methods must never have a body unlike in Java.
3883        if (interface_method || has_native || has_abstract) && common.body.is_some() {
3884            self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustNotContainBody, diagarg![]);
3885        } else if !(interface_method || has_native || has_abstract) && common.body.is_none() {
3886            self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustContainBody, diagarg![]);
3887        }
3888
3889        // Interface methods must not contain any annotations except for meta-data.
3890        if !attributes.is_empty() && interface_method {
3891            if !attributes.last().unwrap().is_metadata() {
3892                self.add_syntax_error(&name.location(), DiagnosticKind::FunctionMustNotContainAnnotations, diagarg![]);
3893            }
3894        }
3895
3896        for a in &attributes {
3897            if a.is_metadata() {
3898                continue;
3899            }
3900            match a {
3901                Attribute::Static(_) => {
3902                    if !context.is_type_block() {
3903                        // Unallowed attribute
3904                        self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3905                    }
3906                },
3907                Attribute::Final(_) |
3908                Attribute::Override(_) |
3909                Attribute::Abstract(_) => {
3910                    if !context.is_type_block() || constructor {
3911                        // Unallowed attribute
3912                        self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3913                    }
3914                },
3915
3916                Attribute::Native(_) => {},
3917
3918                Attribute::Expression(_) |
3919                Attribute::Public(_) |
3920                Attribute::Private(_) |
3921                Attribute::Protected(_) |
3922                Attribute::Internal(_) => {
3923                    self.verify_visibility(&a, &context);
3924                },
3925                _ => {
3926                    // Unallowed attribute
3927                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3928                },
3929            }
3930        }
3931
3932        let node = Rc::new(Directive::FunctionDefinition(FunctionDefinition {
3933            location: self.pop_location(),
3934            asdoc,
3935            attributes,
3936            name: name.clone(),
3937            common,
3938        }));
3939
3940        (node, semicolon)
3941    }
3942
3943    fn parse_class_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
3944        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
3945        self.push_location(&start_location);
3946        let name = self.expect_identifier(true);
3947        let type_parameters = self.parse_type_parameters_opt();
3948        let mut extends_clause: Option<Rc<Expression>> = None;
3949        if self.consume(Token::Extends) {
3950            extends_clause = Some(self.parse_type_expression());
3951        }
3952        let mut implements_clause: Option<Vec<Rc<Expression>>> = None;
3953        if self.consume(Token::Implements) {
3954            implements_clause = Some(self.parse_type_expression_list());
3955        }
3956        let block = Rc::new(self.parse_block(ParserDirectiveContext::ClassBlock {
3957            name: name.0.clone(),
3958        }));
3959
3960        for a in &attributes {
3961            if a.is_metadata() {
3962                continue;
3963            }
3964            match a {
3965                Attribute::Static(_) => {},
3966                Attribute::Final(_) => {},
3967                Attribute::Dynamic(_) => {},
3968                Attribute::Abstract(_) => {},
3969
3970                Attribute::Expression(_) |
3971                Attribute::Public(_) |
3972                Attribute::Private(_) |
3973                Attribute::Protected(_) |
3974                Attribute::Internal(_) => {
3975                    self.verify_visibility(&a, &context);
3976                },
3977                _ => {
3978                    // Unallowed attribute
3979                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
3980                },
3981            }
3982        }
3983
3984        // Nested classes not allowed
3985        if !context.is_top_level_or_package() {
3986            self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]);
3987        }
3988
3989        let node = Rc::new(Directive::ClassDefinition(ClassDefinition {
3990            location: self.pop_location(),
3991            asdoc,
3992            attributes,
3993            name: name.clone(),
3994            type_parameters,
3995            extends_clause,
3996            implements_clause,
3997            block,
3998        }));
3999
4000        (node, true)
4001    }
4002
4003    fn parse_enum_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
4004        let AnnotatableContext { start_location, asdoc, mut attributes, context, .. } = context;
4005        self.push_location(&start_location);
4006        let name = self.expect_identifier(true);
4007        let mut as_clause: Option<Rc<Expression>> = None;
4008        if self.consume(Token::As) {
4009            as_clause = Some(self.parse_type_expression());
4010        }
4011        let block = Rc::new(self.parse_block(ParserDirectiveContext::EnumBlock));
4012
4013        for a in &attributes {
4014            if a.is_metadata() {
4015                continue;
4016            }
4017            match a {
4018                Attribute::Expression(_) |
4019                Attribute::Public(_) |
4020                Attribute::Private(_) |
4021                Attribute::Protected(_) |
4022                Attribute::Internal(_) => {
4023                    self.verify_visibility(&a, &context);
4024                },
4025                _ => {
4026                    // Unallowed attribute
4027                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
4028                },
4029            }
4030        }
4031
4032        // Nested classes not allowed
4033        if !context.is_top_level_or_package() {
4034            self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]);
4035        }
4036
4037        let mut is_set = false;
4038        let metadata = Attribute::find_metadata(&attributes);
4039        for metadata in metadata {
4040            if metadata.name.0 == "Set" {
4041                is_set = true;
4042                Attribute::remove_metadata(&mut attributes, &metadata);
4043            }
4044        }
4045
4046        let node = Rc::new(Directive::EnumDefinition(EnumDefinition {
4047            location: self.pop_location(),
4048            asdoc,
4049            attributes,
4050            is_set,
4051            name: name.clone(),
4052            as_clause,
4053            block,
4054        }));
4055
4056        (node, true)
4057    }
4058
4059    fn parse_interface_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
4060        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
4061        self.push_location(&start_location);
4062        let name = self.expect_identifier(true);
4063        let type_parameters = self.parse_type_parameters_opt();
4064        let mut extends_clause: Option<Vec<Rc<Expression>>> = None;
4065        if self.consume(Token::Extends) {
4066            extends_clause = Some(self.parse_type_expression_list());
4067        }
4068        let block = Rc::new(self.parse_block(ParserDirectiveContext::InterfaceBlock));
4069
4070        // Interface block must only contain function definitions
4071        for directive in block.directives.iter() {
4072            if !(matches!(directive.as_ref(), Directive::FunctionDefinition(_))) {
4073                self.add_syntax_error(&directive.location(), DiagnosticKind::UnexpectedDirective, diagarg![]);
4074            }
4075        }
4076
4077        for a in &attributes {
4078            if a.is_metadata() {
4079                continue;
4080            }
4081            match a {
4082                Attribute::Expression(_) |
4083                Attribute::Public(_) |
4084                Attribute::Private(_) |
4085                Attribute::Protected(_) |
4086                Attribute::Internal(_) => {
4087                    self.verify_visibility(&a, &context);
4088                },
4089                _ => {
4090                    // Unallowed attribute
4091                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
4092                },
4093            }
4094        }
4095
4096        // Nested classes not allowed
4097        if !context.is_top_level_or_package() {
4098            self.add_syntax_error(&name.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]);
4099        }
4100
4101        let node = Rc::new(Directive::InterfaceDefinition(InterfaceDefinition {
4102            location: self.pop_location(),
4103            asdoc,
4104            attributes,
4105            name: name.clone(),
4106            type_parameters,
4107            extends_clause,
4108            block,
4109        }));
4110
4111        (node, true)
4112    }
4113
4114    fn parse_type_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
4115        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
4116        self.push_location(&start_location);
4117        let left = self.expect_identifier(true);
4118        let mut right = self.create_invalidated_expression(&self.tokenizer.cursor_location());
4119        self.non_greedy_expect(Token::Assign);
4120        if !self.expecting_token_error {
4121            right = self.parse_type_expression();
4122        }
4123
4124        for a in &attributes {
4125            if a.is_metadata() {
4126                continue;
4127            }
4128            match a {
4129                Attribute::Expression(_) |
4130                Attribute::Public(_) |
4131                Attribute::Private(_) |
4132                Attribute::Protected(_) |
4133                Attribute::Internal(_) => {
4134                    self.verify_visibility(&a, &context);
4135                },
4136                _ => {
4137                    // Unallowed attribute
4138                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
4139                },
4140            }
4141        }
4142
4143        // Nested classes not allowed
4144        if !context.is_top_level_or_package() {
4145            self.add_syntax_error(&left.1, DiagnosticKind::NestedClassesNotAllowed, diagarg![]);
4146        }
4147
4148        let semicolon = self.parse_semicolon();
4149
4150        let node = Rc::new(Directive::TypeDefinition(TypeDefinition {
4151            location: self.pop_location(),
4152            asdoc,
4153            attributes,
4154            left: left.clone(),
4155            right,
4156        }));
4157
4158        (node, semicolon)
4159    }
4160
4161    fn parse_namespace_definition(&mut self, context: AnnotatableContext) -> (Rc<Directive>, bool) {
4162        let AnnotatableContext { start_location, asdoc, attributes, context, .. } = context;
4163        self.push_location(&start_location);
4164        let left = self.expect_identifier(true);
4165        let mut right: Option<Rc<Expression>> = None;
4166        if self.consume(Token::Assign) {
4167            right = Some(self.parse_expression(ParserExpressionContext {
4168                min_precedence: OperatorPrecedence::AssignmentAndOther,
4169                ..default()
4170            }));
4171        }
4172
4173        for a in &attributes {
4174            if a.is_metadata() {
4175                continue;
4176            }
4177            match a {
4178                Attribute::Expression(_) |
4179                Attribute::Public(_) |
4180                Attribute::Private(_) |
4181                Attribute::Protected(_) |
4182                Attribute::Internal(_) => {
4183                    self.verify_visibility(&a, &context);
4184                },
4185                Attribute::Static(_) => {},
4186                _ => {
4187                    // Unallowed attribute
4188                    self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
4189                },
4190            }
4191        }
4192
4193        let semicolon = self.parse_semicolon();
4194
4195        let node = Rc::new(Directive::NamespaceDefinition(NamespaceDefinition {
4196            location: self.pop_location(),
4197            asdoc,
4198            attributes,
4199            left: left.clone(),
4200            right,
4201        }));
4202
4203        (node, semicolon)
4204    }
4205
4206    fn parse_type_expression_list(&mut self) -> Vec<Rc<Expression>> {
4207        let mut list = vec![self.parse_type_expression()];
4208        while self.consume(Token::Comma) {
4209            list.push(self.parse_type_expression());
4210        }
4211        list
4212    }
4213
4214    fn verify_visibility(&self, a: &Attribute, context: &ParserDirectiveContext) {
4215        let mut unallowed = false;
4216        match a {
4217            Attribute::Expression(_) => {},
4218            Attribute::Public(_) => {},
4219            Attribute::Private(_) |
4220            Attribute::Protected(_) => {
4221                if !context.is_type_block() {
4222                    unallowed = true;
4223                }
4224            },
4225            Attribute::Internal(_) => {},
4226            _ => {}
4227        }
4228        if unallowed {
4229            // Unallowed attribute
4230            self.add_syntax_error(&a.location(), DiagnosticKind::UnallowedAttribute, diagarg![]);
4231        }
4232    }
4233    
4234    fn parse_type_parameters_opt(&mut self) -> Option<Vec<Rc<TypeParameter>>> {
4235        if !self.consume(Token::Dot) {
4236            return None;
4237        }
4238        let mut list: Vec<Rc<TypeParameter>> = vec![];
4239        self.non_greedy_expect(Token::Lt);
4240        if !self.expecting_token_error {
4241            list.push(self.parse_type_parameter());
4242            while self.consume(Token::Comma) {
4243                list.push(self.parse_type_parameter());
4244            }
4245            self.non_greedy_expect_type_parameters_gt();
4246        }
4247        Some(list)
4248    }
4249    
4250    fn parse_type_parameter(&mut self) -> Rc<TypeParameter> {
4251        self.mark_location();
4252        let name = self.expect_identifier(false);
4253        Rc::new(TypeParameter {
4254            location: self.pop_location(),
4255            name,
4256        })
4257    }
4258
4259    fn keyword_or_expression_attribute_from_expression(&self, expr: &Rc<Expression>) -> Attribute {
4260        match expr.as_ref() {
4261            Expression::QualifiedIdentifier(id) => {
4262                if id.qualifier.is_some() || id.attribute {
4263                    return Attribute::Expression(expr.clone());
4264                }
4265                match &id.id {
4266                    QualifiedIdentifierIdentifier::Id((id, location)) => {
4267                        if let Some(attr) = Attribute::from_identifier_name(&id, &location) {
4268                            return attr;
4269                        }
4270                        Attribute::Expression(expr.clone())
4271                    },
4272                    _ => Attribute::Expression(expr.clone()),
4273                }
4274            },
4275            _ => Attribute::Expression(expr.clone()),
4276        }
4277    }
4278
4279    fn keyword_attribute_from_previous_token(&self) -> Option<Attribute> {
4280        self.previous_token.0.to_attribute(&self.previous_token.1)
4281    }
4282
4283    fn _keyword_or_expression_attribute_from_previous_token(&mut self) -> Option<Attribute> {
4284        if let Some(a) = self.keyword_attribute_from_previous_token() {
4285            return Some(a);
4286        }
4287        match &self.previous_token.0 {
4288            Token::Identifier(id) => Some(Attribute::Expression(self.parse_expression_attribute(&(id.clone(), self.previous_token.1.clone())))),
4289            _ => None,
4290        }
4291    }
4292
4293    fn parse_keyword_or_expression_attribute(&mut self) -> Option<Attribute> {
4294        if let Some(a) = self.token.0.to_attribute(&self.token.1) {
4295            self.next();
4296            return Some(a);
4297        }
4298        match &self.token.0 {
4299            Token::Identifier(_) => {
4300                let id = self.expect_identifier(false);
4301                Some(Attribute::Expression(self.parse_expression_attribute(&id)))
4302            },
4303            _ => None,
4304        }
4305    }
4306
4307    fn peek_annotatable_directive_identifier_name(&self) -> bool {
4308        if self.token.0.to_attribute(&self.token.1).is_some() {
4309            return true;
4310        }
4311        match self.token.0 {
4312            Token::Identifier(_) => true,
4313            Token::Var |
4314            Token::Const |
4315            Token::Function |
4316            Token::Class |
4317            Token::Interface => true,
4318            _ => false,
4319        }
4320    }
4321
4322    fn lookbehind_is_annotatable_directive_identifier_name(&self) -> bool {
4323        self.keyword_attribute_from_previous_token().is_some()
4324        || matches!(&self.previous_token.0, Token::Identifier(_))
4325        || Token::is_context_keyword(&self.previous_token, "enum")
4326        || Token::is_context_keyword(&self.previous_token, "type")
4327        || Token::is_context_keyword(&self.previous_token, "namespace")
4328    }
4329
4330    fn parse_attribute_keywords_or_expressions(&mut self, context: &mut AnnotatableContext) {
4331        if context.directive_context_keyword.is_some() {
4332            unreachable!();
4333        }
4334        loop {
4335            if let Some(a) = self.parse_keyword_or_expression_attribute() {
4336                if let Attribute::Expression(e) = &a {
4337                    let id = e.to_identifier_name();
4338                    if let Some(id) = id {
4339                        if ["enum", "type", "namespace"].contains(&id.0.as_ref()) {
4340                            context.directive_context_keyword = Some(id);
4341                            break;
4342                        }
4343                    }
4344                }
4345                let last_attribute_is_identifier = context.attributes.last().map_or(false, |a| !a.is_metadata());
4346                if last_attribute_is_identifier {
4347                    self.forbid_line_break_before_token();
4348                }
4349                context.attributes.push(a);
4350                // self.next();
4351            } else {
4352                if let Some(id) = self.peek_identifier(false) {
4353                    self.forbid_line_break_before_token();
4354                    if ["enum", "type", "namespace"].contains(&id.0.as_ref()) {
4355                        self.next();
4356                        context.directive_context_keyword = Some(id);
4357                    }
4358                }
4359                break;
4360            }
4361        }
4362        // For meta-data that are not one of certain Flex meta-data,
4363        // delegate the respective ASDoc to the annotatable directive.
4364        let mut new_attributes = Vec::<Attribute>::new();
4365        for attr in &context.attributes {
4366            if let Attribute::Metadata(metadata) = attr {
4367                if !self.documentable_metadata.contains(&metadata.name.0) && metadata.asdoc.is_some() {
4368                    new_attributes.push(Attribute::Metadata(Rc::new(Metadata {
4369                        location: metadata.location.clone(),
4370                        asdoc: None,
4371                        name: metadata.name.clone(),
4372                        entries: metadata.entries.clone(),
4373                    })));
4374                    context.asdoc = metadata.asdoc.clone();
4375                } else {
4376                    new_attributes.push(attr.clone());
4377                }
4378            } else {
4379                new_attributes.push(attr.clone());
4380            }
4381        }
4382        context.attributes = new_attributes;
4383    }
4384
4385    pub fn parse_package_definition(&mut self) -> Rc<PackageDefinition> {
4386        self.mark_location();
4387        let asdoc = self.parse_asdoc();
4388        self.non_greedy_expect(Token::Package);
4389        let mut name = vec![];
4390        if let Some(name1) = self.consume_identifier(false) {
4391            name.push(name1.clone());
4392            while self.consume(Token::Dot) {
4393                name.push(self.expect_identifier(true));
4394            }
4395        }
4396        let block = Rc::new(self.parse_block(ParserDirectiveContext::PackageBlock));
4397        Rc::new(PackageDefinition {
4398            location: self.pop_location(),
4399            asdoc,
4400            name,
4401            block,
4402        })
4403    }
4404
4405    pub fn parse_program(&mut self) -> Rc<Program> {
4406        self.mark_location();
4407        let just_eof = self.peek(Token::Eof);
4408        let mut packages = vec![];
4409        while self.peek(Token::Package) {
4410            packages.push(self.parse_package_definition());
4411        }
4412        let directives = self.parse_directives(ParserDirectiveContext::TopLevel);
4413        Rc::new(Program {
4414            location: if just_eof {
4415                self.pop_location();
4416                self.token.1.clone()
4417            } else {
4418                self.pop_location()
4419            },
4420            packages,
4421            directives,
4422        })
4423    }
4424
4425    pub fn parse_asdoc(&mut self) -> Option<Rc<Asdoc>> {
4426        let comments = self.compilation_unit().comments.borrow();
4427        let last_comment = comments.last().map(|last_comment| last_comment.clone());
4428        drop(comments);
4429        last_comment.and_then(|comment| {
4430            if comment.is_asdoc(&self.token.1) {
4431                self.compilation_unit().comments_mut().pop();
4432                let location = comment.location();
4433                let comment_prefix_length: usize = 3;
4434                let location1 = Location::with_offsets(self.compilation_unit(), location.first_offset + comment_prefix_length, location.last_offset - 2);
4435                let content = &comment.content.borrow()[1..];
4436                let (main_body, tags) = self.parse_asdoc_content(&location1, content);
4437                Some(Rc::new(Asdoc {
4438                    location,
4439                    main_body,
4440                    tags,
4441                }))
4442            } else {
4443                None
4444            }
4445        })
4446    }
4447
4448    fn parse_asdoc_content(&mut self, location: &Location, content: &str) -> (Option<(String, Location)>, Vec<(AsdocTag, Location)>) {
4449        let lines = self.split_asdoc_lines(location, content);
4450
4451        let mut main_body: Option<(String, Location)> = None;
4452        let mut tags: Vec<(AsdocTag, Location)> = vec![];
4453        let mut i = 0;
4454        let line_count = lines.len();
4455
4456        let mut building_content_tag_name: Option<(String, Location)> = None;
4457        let mut building_content: Vec<(String, Location)> = vec![];
4458        let mut inside_code_block = false;
4459        let mut backticks: Option<Regex> = None;
4460
4461        while i < line_count {
4462            let line = &lines[i];
4463            let tag = if inside_code_block { None } else {
4464                regex_captures!(r"^([\s\t]*\@)([^\s\t]+)(.*)", &line.content)
4465            };
4466            if let Some((_, tag_prefix, tag_name, tag_content)) = tag {
4467                self.parse_asdoc_tag_or_main_body(
4468                    &mut building_content_tag_name,
4469                    &mut building_content,
4470                    &mut main_body,
4471                    &mut tags,
4472                );
4473                if let Some((_, new_backticks)) = regex_captures!(r"^[\s\t]*(```(?:`*|$))", &tag_content) {
4474                    inside_code_block = true;
4475                    backticks = Some(Regex::new(&(r"^[\s\t]*".to_owned() + &new_backticks.to_owned() + &r"(?:$)?".to_owned())).unwrap());
4476                }
4477                let tag_name_location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + tag_prefix.len() - 1, line.location.first_offset() + tag_prefix.len() + tag_name.len());
4478                building_content_tag_name = Some((tag_name.into(), tag_name_location));
4479                let tag_content_location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + tag_prefix.len() + tag_name.len(), line.location.last_offset());
4480                building_content.push((tag_content.into(), tag_content_location));
4481
4482                if ["private", "inheritDoc"].contains(&tag_name) {
4483                    self.parse_asdoc_tag_or_main_body(
4484                        &mut building_content_tag_name,
4485                        &mut building_content,
4486                        &mut main_body,
4487                        &mut tags,
4488                    );
4489                    building_content_tag_name = None;
4490                    building_content.clear();
4491                }
4492            } else {
4493                if inside_code_block {
4494                    if backticks.as_ref().unwrap().is_match(&line.content) {
4495                        inside_code_block = false;
4496                    }
4497                } else {
4498                    if let Some((_, new_backticks)) = regex_captures!(r"^[\s\t]*(```(?:`*|$))", &line.content) {
4499                        inside_code_block = true;
4500                        backticks = Some(Regex::new(&(r"^[\s\t]*".to_owned() + &new_backticks.to_owned() + &r"([^`]|$)".to_owned())).unwrap());
4501                    }
4502                }
4503                building_content.push((line.content.clone(), line.location.clone()));
4504            }
4505            i += 1;
4506        }
4507
4508        self.parse_asdoc_tag_or_main_body(
4509            &mut building_content_tag_name,
4510            &mut building_content,
4511            &mut main_body,
4512            &mut tags,
4513        );
4514
4515        (main_body, tags)
4516    }
4517
4518    fn split_asdoc_lines(&mut self, location: &Location, content: &str) -> Vec<ParserAsdocLine> {
4519        let mut builder = String::new();
4520        let mut lines = vec![];
4521        let mut _line_number = location.first_line_number();
4522        let mut index = location.first_offset();
4523        let mut line_first_offset = index;
4524        let mut characters = content.chars();
4525        while let Some(ch) = characters.next() {
4526            if CharacterValidator::is_line_terminator(ch) {
4527                lines.push(ParserAsdocLine {
4528                    content: builder,
4529                    location: Location::with_offsets(self.compilation_unit(), line_first_offset, index),
4530                });
4531                index += ch.len_utf8();
4532                // <CR><LF> sequence
4533                if ch == '\r' && characters.clone().next().unwrap_or('\x00') == '\n' {
4534                    index += '\n'.len_utf8();
4535                    characters.next();
4536                }
4537                builder = String::new();
4538                _line_number += 1;
4539                line_first_offset = index;
4540            } else {
4541                builder.push(ch);
4542                index += ch.len_utf8();
4543            }
4544        }
4545        lines.push(ParserAsdocLine {
4546            content: builder,
4547            location: Location::with_offsets(self.compilation_unit(), line_first_offset, index),
4548        });
4549        for line in &mut lines {
4550            let line_content = line.content.to_owned();
4551            let prefix = regex_captures!(r"^\s*(\*\s?)", &line_content);
4552            if let Some((prefix, _)) = prefix {
4553                line.content = line.content[prefix.len()..].to_owned();
4554                line.location = Location::with_offsets(self.compilation_unit(), line.location.first_offset() + prefix.len(), line.location.last_offset());
4555            }
4556        }
4557
4558        lines
4559    }
4560
4561    fn parse_asdoc_tag_or_main_body(
4562        &self,
4563        building_content_tag_name: &mut Option<(String, Location)>,
4564        building_content: &mut Vec<(String, Location)>,
4565        main_body: &mut Option<(String, Location)>,
4566        tags: &mut Vec<(AsdocTag, Location)>
4567    ) {
4568        if let Some((tag_name, ref tag_location)) = building_content_tag_name.as_ref() {
4569            match tag_name.as_ref() {
4570                // @author Author text
4571                "author" => {
4572                    let (content, location) = join_asdoc_content(building_content);
4573                    // Content must be non empty
4574                    if regex_is_match!(r"^\s*$", &content) {
4575                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4576                    }
4577                    let location = tag_location.combine_with(location);
4578                    tags.push((AsdocTag::Author(content), location));
4579                },
4580
4581                // @copy reference
4582                "copy" => {
4583                    let (content, c_location) = join_asdoc_content(building_content);
4584                    let location = tag_location.combine_with(c_location.clone());
4585                    let reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]);
4586                    if let Some(reference) = self.parse_asdoc_reference(&content, &reference_loc, &tag_location, &tag_name) {
4587                        tags.push((AsdocTag::Copy(reference), location));
4588                    }
4589                },
4590
4591                // @created Date text
4592                "created" => {
4593                    let (content, location) = join_asdoc_content(building_content);
4594                    // Content must be non empty
4595                    if regex_is_match!(r"^\s*$", &content) {
4596                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4597                    }
4598                    let location = tag_location.combine_with(location);
4599                    tags.push((AsdocTag::Created(content), location));
4600                },
4601
4602                // @default value
4603                "default" => {
4604                    let (reference, location) = join_asdoc_content(building_content);
4605                    let location = tag_location.combine_with(location);
4606                    tags.push((AsdocTag::Default(reference), location));
4607                },
4608
4609                // @deprecated
4610                "deprecated" => {
4611                    let (text, location) = join_asdoc_content(building_content);
4612                    let location = tag_location.combine_with(location);
4613
4614                    let mut message: Option<String> = None;
4615
4616                    if !regex_is_match!(r"^\s*$", &text) {
4617                        message = Some(text.clone());
4618                    }
4619
4620                    tags.push((AsdocTag::Deprecated { message }, location));
4621                },
4622
4623                // @eventType typeOrConstant
4624                "eventType" => {
4625                    let (_, c_location) = join_asdoc_content(building_content);
4626                    let location = tag_location.combine_with(c_location.clone());
4627                    let reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]);
4628                    let parser_options = ParserOptions {
4629                        byte_range: Some((reference_loc.first_offset(), reference_loc.last_offset())),
4630                        ..self.options()
4631                    };
4632                    let exp = ParserFacade(self.compilation_unit(), parser_options).parse_expression();
4633                    tags.push((AsdocTag::EventType(exp), location));
4634                },
4635
4636                // @example text
4637                "example" => {
4638                    let (text, location) = join_asdoc_content(building_content);
4639                    let location = tag_location.combine_with(location);
4640                    tags.push((AsdocTag::Example(text), location));
4641                },
4642
4643                // @inheritDoc
4644                "inheritDoc" => {
4645                    let (text, location) = join_asdoc_content(building_content);
4646                    let location = tag_location.combine_with(location);
4647
4648                    // Content must be empty
4649                    if !regex_is_match!(r"^\s*$", &text) {
4650                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4651                    }
4652
4653                    tags.push((AsdocTag::InheritDoc, location));
4654                },
4655
4656                // @internal text
4657                "internal" => {
4658                    let (text, location) = join_asdoc_content(building_content);
4659                    let location = tag_location.combine_with(location);
4660
4661                    // Content must be non empty
4662                    if regex_is_match!(r"^\s*$", &text) {
4663                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4664                    }
4665
4666                    tags.push((AsdocTag::Internal(text), location));
4667                },
4668
4669                // @langversion text
4670                "langversion" => {
4671                    let (text, location) = join_asdoc_content(building_content);
4672                    let location = tag_location.combine_with(location);
4673
4674                    // Content must be non empty
4675                    if regex_is_match!(r"^\s*$", &text) {
4676                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4677                    }
4678
4679                    tags.push((AsdocTag::Langversion(text), location));
4680                },
4681
4682                // @param paramName description
4683                "param" => {
4684                    let (content, location) = join_asdoc_content(building_content);
4685                    let location = tag_location.combine_with(location);
4686
4687                    if let Some((_, name, description)) = regex_captures!(r"(?x) ([^\s]+) (.*)", &content) {
4688                        tags.push((AsdocTag::Param { name: name.into(), description: description.trim_start().into() }, location));
4689                    } else {
4690                        tags.push((AsdocTag::Param { name: content, description: "".into() }, location));
4691                    }
4692                },
4693
4694                // @playerversion text
4695                "playerversion" => {
4696                    let (text, location) = join_asdoc_content(building_content);
4697                    let location = tag_location.combine_with(location);
4698
4699                    // Content must be non empty
4700                    if regex_is_match!(r"^\s*$", &text) {
4701                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4702                    }
4703
4704                    tags.push((AsdocTag::Playerversion(text), location));
4705                },
4706
4707                // @private
4708                "private" => {
4709                    let (text, location) = join_asdoc_content(building_content);
4710                    let location = tag_location.combine_with(location);
4711
4712                    // Content must be empty
4713                    if !regex_is_match!(r"^\s*$", &text) {
4714                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4715                    }
4716
4717                    tags.push((AsdocTag::Private, location));
4718                },
4719
4720                // @productversion text
4721                "productversion" => {
4722                    let (text, location) = join_asdoc_content(building_content);
4723                    let location = tag_location.combine_with(location);
4724
4725                    // Content must be non empty
4726                    if regex_is_match!(r"^\s*$", &text) {
4727                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4728                    }
4729
4730                    tags.push((AsdocTag::Productversion(text), location));
4731                },
4732
4733                // @return text
4734                "return" => {
4735                    let (text, location) = join_asdoc_content(building_content);
4736                    let location = tag_location.combine_with(location);
4737                    tags.push((AsdocTag::Return(text), location));
4738                },
4739
4740                // @see reference [displayText]
4741                "see" => {
4742                    let (content, c_location) = join_asdoc_content(building_content);
4743                    let location = tag_location.combine_with(c_location.clone());
4744                    let reference: String;
4745                    let display_text: Option<String>;
4746                    let mut reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]);
4747                    if let Some((_, reference_1, display_text_1)) = regex_captures!(r"(?x) ([^\s]+) (.*)", &content) {
4748                        reference = reference_1.to_owned();
4749                        reference_loc = Location::with_offsets(self.compilation_unit(), reference_loc.first_offset(), reference_loc.first_offset() + reference.len());
4750                        display_text = Some(display_text_1.trim().to_owned());
4751                    } else {
4752                        reference = content;
4753                        display_text = None;
4754                    }
4755                    if let Some(reference) = self.parse_asdoc_reference(&reference, &reference_loc, &tag_location, &tag_name) {
4756                        tags.push((AsdocTag::See { reference, display_text }, location));
4757                    }
4758                },
4759
4760                // @throws className description
4761                "throws" => {
4762                    let (class_name_and_description, c_location) = join_asdoc_content(building_content);
4763                    let location = tag_location.combine_with(c_location.clone());
4764
4765                    let class_name_and_description = regex_captures!(r"^([^\s]+)(\s.*)?", &class_name_and_description);
4766
4767                    if let Some((_, class_name, description)) = class_name_and_description {
4768                        let description = description.trim().to_owned();
4769                        let description = if description.is_empty() {
4770                            None
4771                        } else {
4772                            Some(description)
4773                        };
4774                        let mut reference_loc = c_location.shift_whitespace(&self.compilation_unit().text()[c_location.first_offset()..c_location.last_offset()]);
4775                        reference_loc = Location::with_offsets(self.compilation_unit(), reference_loc.first_offset(), reference_loc.first_offset() + class_name.len());
4776                        let parser_options = ParserOptions {
4777                            byte_range: Some((reference_loc.first_offset(), reference_loc.last_offset())),
4778                            ..self.options()
4779                        };
4780                        let exp = ParserFacade(self.compilation_unit(), parser_options).parse_type_expression();
4781                        tags.push((AsdocTag::Throws { class_reference: exp, description }, location));
4782                    } else {
4783                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4784                    }
4785                },
4786
4787                // @version Version text
4788                "version" => {
4789                    let (content, location) = join_asdoc_content(building_content);
4790                    // Content must be non empty
4791                    if regex_is_match!(r"^\s*$", &content) {
4792                        self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.clone()]);
4793                    }
4794                    let location = tag_location.combine_with(location);
4795                    tags.push((AsdocTag::Version(content), location));
4796                },
4797
4798                // Unrecognized tag
4799                _ => {
4800                    self.add_syntax_error(&tag_location, DiagnosticKind::UnrecognizedAsdocTag, diagarg![tag_name.clone()]);
4801                },
4802            }
4803        } else if !building_content.is_empty() {
4804            let content = join_asdoc_content(building_content);
4805            if !content.0.is_empty() {
4806                *main_body = Some(content);
4807            }
4808        }
4809
4810        *building_content_tag_name = None;
4811        building_content.clear();
4812    }
4813
4814    fn parse_asdoc_reference(&self, reference: &str, reference_loc: &Location, tag_location: &Location, tag_name: &str) -> Option<Rc<AsdocReference>> {
4815        let split: Vec<&str> = reference.split("#").collect();
4816        if split.len() > 2 {
4817            self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.to_owned()]);
4818            return None;
4819        }
4820        let mut base: Option<Rc<Expression>> = None;
4821        let base_text: String = split[0].to_owned();
4822        let instance_property_text: Option<(String, Location)> = split.get(1).and_then(|&f| if f.is_empty() { None } else {
4823            Some((f.to_owned(), Location::with_offsets(self.compilation_unit(), reference_loc.first_offset() + base_text.len() + 1, reference_loc.last_offset())))
4824        });
4825
4826        if !base_text.is_empty() {
4827            let parser_options = ParserOptions {
4828                byte_range: Some((reference_loc.first_offset(), reference_loc.first_offset() + base_text.len())),
4829                ..self.options()
4830            };
4831            let exp = ParserFacade(self.compilation_unit(), parser_options).parse_expression();
4832            base = Some(exp);
4833        }
4834
4835        let mut instance_property: Option<Rc<QualifiedIdentifier>> = None;
4836        if let Some(text) = instance_property_text {
4837            let parser_options = ParserOptions {
4838                byte_range: Some((text.1.first_offset(), text.1.last_offset())),
4839                ..self.options()
4840            };
4841            let exp = ParserFacade(self.compilation_unit(), parser_options).parse_qualified_identifier();
4842            instance_property = Some(Rc::new(exp));
4843        }
4844
4845        if base.is_none() && instance_property.is_none() {
4846            self.add_syntax_error(&tag_location, DiagnosticKind::FailedParsingAsdocTag, diagarg![tag_name.to_owned()]);
4847            return None;
4848        }
4849        Some(Rc::new(AsdocReference { base, instance_property, }))
4850    }
4851
4852    /// Parses MXMLElement starting from its XMLTagContent.
4853    fn parse_mxml_element(&mut self, start: Location, namespace: &Rc<MxmlNamespace>, encoding: &mut String) -> MxmlElement {
4854        self.push_location(&start);
4855        let namespace = Rc::new(MxmlNamespace::new(Some(namespace)));
4856        let name = self.parse_xml_name();
4857        let mut attributes: Vec<Rc<MxmlAttribute>> = vec![];
4858        let mut plain_attributes: Vec<PlainMxmlAttribute> = vec![];
4859        while self.consume_and_ie_xml_tag(Token::XmlWhitespace) {
4860            if matches!(self.token.0, Token::XmlName(_)) {
4861                self.mark_location();
4862                let name = self.parse_xml_name();
4863                self.consume_and_ie_xml_tag(Token::XmlWhitespace);
4864                self.non_greedy_expect_and_ie_xml_tag(Token::Assign);
4865                let mut value = ("".into(), self.token.1.clone());
4866                if !self.expecting_token_error {
4867                    self.consume_and_ie_xml_tag(Token::XmlWhitespace);
4868                    value = self.parse_xml_attribute_value();
4869                }
4870                let attrib = PlainMxmlAttribute {
4871                    location: self.pop_location(),
4872                    name,
4873                    value,
4874                };
4875                self.process_mxml_xmlns_attribute(&mut attributes, &attrib, &namespace);
4876                plain_attributes.push(attrib);
4877            } else {
4878                break;
4879            }
4880        }
4881
4882        for attrib in &plain_attributes {
4883            self.process_mxml_attribute(&mut attributes, &attrib, &namespace);
4884        }
4885
4886        let name = self.process_mxml_tag_name(name, &namespace);
4887
4888        let mut content: Option<Vec<Rc<MxmlContent>>> = None;
4889        let mut closing_name: Option<MxmlTagName> = None;
4890
4891        let is_empty = self.consume_and_ie_xml_content(Token::XmlSlashGt);
4892
4893        if !is_empty {
4894            self.expect_and_ie_xml_content(Token::Gt);
4895            content = Some(self.parse_mxml_content(false, &namespace, encoding));
4896            self.non_greedy_expect_and_ie_xml_tag(Token::XmlLtSlash);
4897            let name_1 = self.parse_xml_name();
4898            let closing_name_1 = self.process_mxml_tag_name(name_1, &namespace);
4899            if let Ok(equal) = name.equals_name(&closing_name_1, &namespace) {
4900                if !equal {
4901                    self.add_syntax_error(&closing_name_1.location, DiagnosticKind::XmlClosingTagNameMustBeEquals, diagarg![name.to_string(&namespace)]);
4902                }
4903            }
4904            closing_name = Some(closing_name_1);
4905            self.consume_and_ie_xml_tag(Token::XmlWhitespace);
4906            self.non_greedy_expect_and_ie_xml_content(Token::Gt);
4907        }
4908
4909        if let Some(content) = content.as_mut() {
4910            self.filter_mxml_whitespace_out(content);
4911        }
4912
4913        MxmlElement {
4914            location: self.pop_location(),
4915            name,
4916            attributes,
4917            content,
4918            closing_name,
4919            namespace,
4920        }
4921    }
4922
4923    /// Filters whitespace chunks out of a content list when
4924    /// they include at least one child element.
4925    fn filter_mxml_whitespace_out(&self, content: &mut Vec<Rc<MxmlContent>>) {
4926        if !self.ignore_xml_whitespace {
4927            return;
4928        }
4929        let mut inc_el = false;
4930        for node in content.iter() {
4931            inc_el = matches!(node.as_ref(), MxmlContent::Element(_));
4932            if inc_el {
4933                break;
4934            }
4935        }
4936        if inc_el {
4937            let mut indices: Vec<usize> = vec![];
4938            for i in 0..content.len() {
4939                let MxmlContent::Characters((ch, _)) = content[i].as_ref() else {
4940                    continue;
4941                };
4942                if ch.trim().is_empty() {
4943                    indices.push(i);
4944                }
4945            }
4946            for i in indices.iter().rev() {
4947                content.remove(*i);
4948            }
4949        }
4950    }
4951
4952    fn process_mxml_xmlns_attribute(&mut self, output: &mut Vec<Rc<MxmlAttribute>>, attribute: &PlainMxmlAttribute, namespace: &Rc<MxmlNamespace>) {
4953        // xml="uri"
4954        if attribute.name.0 == "xmlns" {
4955            let attribute_value = unescape_xml(&attribute.value.0);
4956            namespace.set(MxmlNamespace::DEFAULT_NAMESPACE, &attribute_value);
4957            output.push(Rc::new(MxmlAttribute {
4958                location: attribute.location.clone(),
4959                name: MxmlName {
4960                    location: attribute.name.1.clone(),
4961                    prefix: None,
4962                    name: "xmlns".into(),
4963                },
4964                value: (attribute_value, attribute.value.1.clone()),
4965                xmlns: true,
4966            }));
4967        // xmlns:prefix="uri"
4968        } else if attribute.name.0.starts_with("xmlns:") {
4969            let attribute_value = unescape_xml(&attribute.value.0);
4970            namespace.set(&attribute.name.0[6..], &attribute_value);
4971            if attribute.name.0[6..].find(':').is_some() {
4972                self.add_syntax_error(&attribute.name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]);
4973            }
4974            output.push(Rc::new(MxmlAttribute {
4975                location: attribute.location.clone(),
4976                name: MxmlName {
4977                    location: attribute.name.1.clone(),
4978                    prefix: Some("xmlns".into()),
4979                    name: attribute.name.0[6..].to_owned(),
4980                },
4981                value: (attribute_value, attribute.value.1.clone()),
4982                xmlns: true,
4983            }));
4984        }
4985    }
4986
4987    fn process_mxml_attribute(&mut self, output: &mut Vec<Rc<MxmlAttribute>>, attribute: &PlainMxmlAttribute, namespace: &Rc<MxmlNamespace>) {
4988        // attrib="value"
4989        if !(attribute.name.0 == "xmlns" || attribute.name.0.starts_with("xmlns:")) {
4990            let attribute_value = unescape_xml(&attribute.value.0);
4991            let split = attribute.name.0.split(':').collect::<Vec<_>>();
4992            if split.len() > 2 {
4993                self.add_syntax_error(&attribute.name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]);
4994            }
4995            let prefix: Option<String> = if split.len() > 1 {
4996                Some(split[split.len() - 2].to_owned())
4997            } else {
4998                None
4999            };
5000            let name = split.last().unwrap();
5001            let attrib = Rc::new(MxmlAttribute {
5002                location: attribute.location.clone(),
5003                name: MxmlName {
5004                    location: attribute.name.1.clone(),
5005                    prefix,
5006                    name: (*name).to_owned(),
5007                },
5008                value: (attribute_value, attribute.value.1.clone()),
5009                xmlns: false,
5010            });
5011            match attrib.name.resolve_prefix(namespace) {
5012                Ok(_) => {
5013                    for prev_attrib in output.iter() {
5014                        if prev_attrib.name.equals_name(&attrib.name, namespace).unwrap_or(false) {
5015                            self.add_syntax_error(&attrib.name.location, DiagnosticKind::RedefiningXmlAttribute, diagarg![attrib.name.name.clone()]);
5016                        }
5017                    }
5018                },
5019                Err(MxmlNameError::PrefixNotDefined(prefix)) => {
5020                    self.add_syntax_error(&attrib.name.location, DiagnosticKind::XmlPrefixNotDefined, diagarg![prefix]);
5021                },
5022            }
5023            output.push(attrib);
5024        }
5025    }
5026
5027    fn process_mxml_tag_name(&mut self, name: (String, Location), namespace: &Rc<MxmlNamespace>) -> MxmlTagName {
5028        let split = name.0.split(':').collect::<Vec<_>>();
5029        if split.len() > 2 {
5030            self.add_syntax_error(&name.1, DiagnosticKind::XmlNameAtMostOneColon, vec![]);
5031        }
5032        let prefix: Option<String> = if split.len() > 1 {
5033            Some(split[split.len() - 2].to_owned())
5034        } else {
5035            None
5036        };
5037        let name_str = split.last().unwrap();
5038        let name = MxmlTagName {
5039            location: name.1.clone(),
5040            prefix,
5041            name: (*name_str).to_owned(),
5042        };
5043        match name.resolve_prefix(namespace) {
5044            Ok(_) => {},
5045            Err(MxmlNameError::PrefixNotDefined(prefix)) => {
5046                self.add_syntax_error(&name.location, DiagnosticKind::XmlPrefixNotDefined, diagarg![prefix]);
5047            },
5048        }
5049        name
5050    }
5051
5052    /// Parses XMLContent until either the `</` token or end-of-file.
5053    fn parse_mxml_content(&mut self, until_eof: bool, namespace: &Rc<MxmlNamespace>, encoding: &mut String) -> Vec<Rc<MxmlContent>> {
5054        let mut content = vec![];
5055        while if until_eof { self.tokenizer.characters().has_remaining() } else { !self.peek(Token::XmlLtSlash) } {
5056            if let Token::XmlMarkup(markup) = self.token.0.clone() {
5057                let location = self.token_location();
5058                self.next_ie_xml_content();
5059                // XMLCDATA
5060                if markup.starts_with("<![CDATA[") {
5061                    content.push(Rc::new(MxmlContent::CData((markup, location))));
5062                // XMLComment
5063                } else if markup.starts_with("<!--") {
5064                    content.push(Rc::new(MxmlContent::Comment((markup, location))));
5065                // XMLPI
5066                } else {
5067                    let mut pi_characters = CharacterReader::from(&markup[2..(markup.len() - 2)]);
5068                    let mut name = String::new();
5069                    if CharacterValidator::is_xml_name_start(pi_characters.peek_or_zero()) {
5070                        name.push(pi_characters.next_or_zero());
5071                        while CharacterValidator::is_xml_name_part(pi_characters.peek_or_zero()) {
5072                            name.push(pi_characters.next_or_zero());
5073                        }
5074                    }
5075                    let mut data = String::new();
5076                    while pi_characters.has_remaining() {
5077                        data.push(pi_characters.next_or_zero());
5078                    }
5079
5080                    let i = location.first_offset() + 2 + name.len();
5081                    let j = decrease_last_offset(i, location.last_offset(), 2);
5082
5083                    let errors = process_xml_pi(self.compilation_unit(), (i, j), &name, encoding);
5084                    for error in errors.iter() {
5085                        match error {
5086                            XmlPiError::UnknownAttribute(name) => {
5087                                self.add_syntax_error(&location, DiagnosticKind::XmlPiUnknownAttribute, diagarg![name.clone()]);
5088                            },
5089                            XmlPiError::Version => {
5090                                self.add_syntax_error(&location, DiagnosticKind::XmlPiVersion, vec![]);
5091                            },
5092                            XmlPiError::Encoding => {
5093                                self.add_syntax_error(&location, DiagnosticKind::XmlPiEncoding, vec![]);
5094                            },
5095                        }
5096                    }
5097                    content.push(Rc::new(MxmlContent::ProcessingInstruction {
5098                        location,
5099                        name,
5100                        data: if data.is_empty() { None } else { Some(data) },
5101                    }));
5102                }
5103            } else if let Token::XmlText(text) = self.token.0.clone() {
5104                let location = self.token_location();
5105                self.next_ie_xml_content();
5106                content.push(Rc::new(MxmlContent::Characters((unescape_xml(&text), location))));
5107            } else if self.consume_and_ie_xml_tag(Token::Lt) {
5108                let start = self.token_location();
5109                let element = self.parse_mxml_element(start, namespace, encoding);
5110                content.push(Rc::new(MxmlContent::Element(Rc::new(element))));
5111            } else if !until_eof {
5112                self.non_greedy_expect_and_ie_xml_content(Token::XmlLtSlash);
5113                if !self.tokenizer.characters().has_remaining() {
5114                    break;
5115                }
5116            } else if self.peek(Token::XmlLtSlash) {
5117                self.add_syntax_error(&self.token_location(), DiagnosticKind::Expecting, diagarg![Token::Eof, self.token.0.clone()]);
5118                self.next_ie_xml_tag();
5119                let _ = self.parse_xml_name();
5120                self.consume_and_ie_xml_tag(Token::XmlWhitespace);
5121                self.non_greedy_expect_and_ie_xml_content(Token::Gt);
5122            }
5123        }
5124        content
5125    }
5126
5127    fn parse_mxml(&mut self) -> Rc<Mxml> {
5128        self.mark_location();
5129        let ns = Rc::new(MxmlNamespace::new(None));
5130        let mut encoding = "utf-8".to_owned();
5131        let mut content = self.parse_mxml_content(true, &ns, &mut encoding);
5132        self.filter_mxml_whitespace_out(&mut content);
5133
5134        let mut element_count = 0usize;
5135        let mut character_count = 0usize;
5136
5137        for node in content.iter() {
5138            match node.as_ref() {
5139                MxmlContent::Characters(_) |
5140                MxmlContent::CData(_) => {
5141                    character_count += 1;
5142                },
5143                MxmlContent::Element(_) => {
5144                    element_count += 1;
5145                },
5146                _ => {},
5147            }
5148        }
5149        let location = self.pop_location();
5150        if element_count != 1 || character_count != 0 {
5151            self.add_syntax_error(&location, DiagnosticKind::XmlMustConsistOfExactly1Element, vec![]);
5152        }
5153        Rc::new(Mxml {
5154            location,
5155            version: XmlVersion::Version10,
5156            encoding,
5157            content,
5158        })
5159    }
5160}
5161
5162fn parse_include_directive_source(nested_compilation_unit: Rc<CompilationUnit>, context: ParserDirectiveContext, options: ParserOptions) -> (Vec<Rc<PackageDefinition>>, Vec<Rc<Directive>>) {
5163    let mut parser = Parser::new(&nested_compilation_unit, &options);
5164    parser.next();
5165    let mut packages = vec![];
5166    if matches!(context, ParserDirectiveContext::TopLevel) {
5167        while parser.peek(Token::Package) {
5168            packages.push(parser.parse_package_definition());
5169        }
5170    }
5171    (packages, parser.parse_directives(context))
5172}
5173
5174fn join_asdoc_content(content: &Vec<(String, Location)>) -> (String, Location) {
5175    // Ignore first empty lines
5176    let mut i = 0usize;
5177    for content1 in content.iter() {
5178        if content1.0.trim().is_empty() {
5179            i += 1;
5180        } else {
5181            break;
5182        }
5183    }
5184
5185    // Ignore last empty lines
5186    let mut j = content.len();
5187    for content1 in content.iter().rev() {
5188        if content1.0.trim().is_empty() {
5189            j -= 1;
5190        } else {
5191            break;
5192        }
5193    }
5194
5195    if i > j {
5196        i = j;
5197    }
5198
5199    let s: Vec<String> = content[i..j].iter().map(|c| c.0.clone()).collect();
5200    let s = s.join("\n").trim().to_owned();
5201    let location = if i == j {
5202        content[i].1.clone()
5203    } else {
5204        content[i].1.combine_with(content[i..j].last().unwrap().1.clone())
5205    };
5206    (s, location)
5207}
5208
5209fn process_xml_pi(cu: &Rc<CompilationUnit>, byte_range: (usize, usize), name: &str, encoding: &mut String) -> Vec<XmlPiError> {
5210    if name != "xml" {
5211        return vec![];
5212    }
5213    let mut parser = Parser::new(&cu, &ParserOptions {
5214        byte_range: Some(byte_range),
5215        ..default()
5216    });
5217    let mut errors = Vec::<XmlPiError>::new();
5218    parser.next_ie_xml_tag();
5219    while parser.consume_and_ie_xml_tag(Token::XmlWhitespace) {
5220        if matches!(parser.token.0, Token::XmlName(_)) {
5221            let name = parser.parse_xml_name();
5222            parser.consume_and_ie_xml_tag(Token::XmlWhitespace);
5223            parser.expect_and_ie_xml_tag(Token::Assign);
5224            parser.consume_and_ie_xml_tag(Token::XmlWhitespace);
5225            let value = parser.parse_xml_attribute_value();
5226            match name.0.as_ref() {
5227                "version" => {
5228                    if value.0 != "1.0" {
5229                        errors.push(XmlPiError::Version);
5230                    }
5231                },
5232                "encoding" => {
5233                    let v = value.0.to_lowercase();
5234                    if ["utf-8", "utf-16"].contains(&v.as_str()) {
5235                        *encoding = v;
5236                    } else {
5237                        errors.push(XmlPiError::Encoding);
5238                    }
5239                },
5240                _ => {
5241                    errors.push(XmlPiError::UnknownAttribute(name.0.clone()));
5242                },
5243            }
5244        } else {
5245            break;
5246        }
5247    }
5248    parser.expect_eof();
5249    errors
5250}
5251
5252enum XmlPiError {
5253    UnknownAttribute(String),
5254    Version,
5255    Encoding,
5256}
5257
5258struct ParserAsdocLine {
5259    content: String,
5260    location: Location,
5261}
5262
5263#[derive(Clone)]
5264struct ParserActivation {
5265    uses_yield: bool,
5266    uses_await: bool,
5267}
5268
5269impl ParserActivation {
5270    pub fn new() -> Self {
5271        Self {
5272            uses_yield: false,
5273            uses_await: false,
5274        }
5275    }
5276}
5277
5278#[derive(Clone)]
5279struct AnnotatableContext {
5280    start_location: Location,
5281    asdoc: Option<Rc<Asdoc>>,
5282    attributes: Vec<Attribute>,
5283    context: ParserDirectiveContext,
5284    /// Previous token as a directive context keyword.
5285    directive_context_keyword: Option<(String, Location)>,
5286}
5287
5288impl AnnotatableContext {
5289    pub fn has_directive_context_keyword(&self, name: &str) -> bool {
5290        if let Some((ref k, _)) = self.directive_context_keyword {
5291            k == name
5292        } else {
5293            false
5294        }
5295    }
5296}
5297
5298struct PlainMxmlAttribute {
5299    pub location: Location,
5300    pub name: (String, Location),
5301    pub value: (String, Location),
5302}
5303
5304/// A simplified interface for executing the parser.
5305pub struct ParserFacade<'input>(pub &'input Rc<CompilationUnit>, pub ParserOptions);
5306
5307pub struct ParserOptions {
5308    /// For MXML, indicates whether to ignore XML whitespace chunks when at
5309    /// least one element appears. Default: true.
5310    pub ignore_xml_whitespace: bool,
5311    /// Indicates the range of characters that shall be parsed,
5312    /// the first and last byte indices respectively.
5313    pub byte_range: Option<(usize, usize)>,
5314    /// Indicates the set of meta-data that are documentable through ASDoc comments.
5315    /// Defaults to \[`Event`, `SkinState`\].
5316    pub documentable_metadata: Vec<String>,
5317    /// Replaces the content of specific files included from the base compilation unit,
5318    /// such as these in AS3 `include` directives. The key of this hash map
5319    /// is a file path, and the value is the new file content.
5320    ///
5321    /// This option is useful for implementing language servers.
5322    pub replace_included_content: Arc<RwLock<HashMap<PathBuf, String>>>,
5323}
5324
5325impl Default for ParserOptions {
5326    fn default() -> Self {
5327        Self {
5328            ignore_xml_whitespace: true,
5329            byte_range: None,
5330            documentable_metadata: vec!["Event".into(), "SkinState".into()],
5331            replace_included_content: Arc::new(RwLock::new(hashmap![])),
5332        }
5333    }
5334}
5335
5336impl<'input> ParserFacade<'input> {
5337    fn create_parser(&self) -> Parser<'input> {
5338        Parser::new(self.0, &self.1)
5339    }
5340
5341    /// Parses `Program` until end-of-file.
5342    pub fn parse_program(&self) -> Rc<Program> {
5343        let mut parser = self.create_parser();
5344        parser.next();
5345        parser.parse_program()
5346    }
5347
5348    /// Parses `ListExpression^allowIn` and expects end-of-file.
5349    pub fn parse_expression(&self) -> Rc<Expression> {
5350        let mut parser = self.create_parser();
5351        parser.next();
5352        let exp = parser.parse_expression(ParserExpressionContext {
5353            ..default()
5354        });
5355        parser.expect_eof();
5356        exp
5357    }
5358
5359    /// Parses a qualified identifier and expects end-of-file.
5360    pub fn parse_qualified_identifier(&self) -> QualifiedIdentifier {
5361        let mut parser = self.create_parser();
5362        parser.next();
5363        let exp = parser.parse_qualified_identifier();
5364        parser.expect_eof();
5365        exp
5366    }
5367
5368    /// Parses `TypeExpression` and expects end-of-file.
5369    pub fn parse_type_expression(&self) -> Rc<Expression> {
5370        let mut parser = self.create_parser();
5371        parser.next();
5372        let exp = parser.parse_type_expression();
5373        parser.expect_eof();
5374        exp
5375    }
5376
5377    /// Parses `Directives` until end-of-file.
5378    pub fn parse_directives(&self, context: ParserDirectiveContext) -> Vec<Rc<Directive>> {
5379        let mut parser = self.create_parser();
5380        parser.next();
5381        parser.parse_directives(context)
5382    }
5383
5384    /// Parses `Mxml` until end-of-file.
5385    pub fn parse_mxml(&self) -> Rc<Mxml> {
5386        let mut parser = self.create_parser();
5387        parser.next_ie_xml_content();
5388        parser.parse_mxml()
5389    }
5390
5391    /// Parses a sequence of zero or meta data and an ASDoc comment.
5392    pub fn parse_metadata(&self) -> (Vec<Attribute>, Option<Rc<Asdoc>>) {
5393        let mut parser = self.create_parser();
5394        parser.next();
5395        parser.parse_metadata()
5396    }
5397
5398    /// Parses the content inside the square brackets (`[ ... ]`) of a meta data.
5399    pub fn parse_metadata_content(&self) -> Rc<Metadata> {
5400        let mut parser = self.create_parser();
5401        parser.next();
5402        parser.parse_metadata_content()
5403    }
5404}