1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
use std::rc::Rc;
use bitflags::bitflags;
use serde::{Serialize, Deserialize};
use crate::*;

#[derive(Clone, Serialize, Deserialize)]
pub struct QualifiedIdentifier {
    pub attribute: bool,
    pub qualifier: Option<Rc<Expression>>,
    pub name: IdentifierOrBrackets,
}

impl QualifiedIdentifier {
    /// Converts the qualified identifier to an Identifier token.
    pub fn to_identifier(&self) -> Option<(String, Location)> {
        if self.attribute || self.qualifier.is_some() {
            return None;
        }
        if let IdentifierOrBrackets::Id(id, location) = &self.name {
            if id != "*" { Some((id.clone(), location.clone())) } else { None }
        } else {
            None
        }
    }

    /// Converts the qualified identifier to a meta data name.
    pub fn to_metadata_name(&self) -> Option<(String, Location)> {
        if self.attribute {
            return None;
        }

        // `[q::Metadata]`
        if let Some(q) = self.qualifier.as_ref() {
            let q = q.to_identifier()?;
            let n = if let IdentifierOrBrackets::Id(id, location) = &self.name {
                if id != "*" { Some((id.clone(), location.clone())) } else { None }
            } else {
                None
            }?;
            return Some((format!("{}::{}", q.0, n.0), q.1.combine_with(n.1)));
        }

        // `[Metadata]`
        if let IdentifierOrBrackets::Id(id, location) = &self.name {
            if id != "*" { Some((id.clone(), location.clone())) } else { None }
        } else {
            None
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct NonAttributeQualifiedIdentifier {
    pub qualifier: Option<Rc<Expression>>,
    pub name: IdentifierOrBrackets,
}



impl NonAttributeQualifiedIdentifier {
    /// Converts the qualified identifier to an Identifier token.
    pub fn to_identifier(&self) -> Option<(String, Location)> {
        if self.qualifier.is_some() {
            return None;
        }
        if let IdentifierOrBrackets::Id(id, location) = &self.name {
            if id != "*" { Some((id.clone(), location.clone())) } else { None }
        } else {
            None
        }
    }

    /// Converts the qualified identifier to an Identifier token or a wildcard (`*`) token.
    pub fn to_identifier_or_wildcard(&self) -> Option<(String, Location)> {
        if self.qualifier.is_some() {
            return None;
        }
        if let IdentifierOrBrackets::Id(id, location) = &self.name {
            Some((id.clone(), location.clone()))
        } else {
            None
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum IdentifierOrBrackets {
    Id(String, Location),
    Brackets(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Expression {
    pub location: Location,
    pub kind: ExpressionKind,
}

impl Expression {
    pub(crate) fn to_modifier(&self) -> Option<Modifiers> {
        let id = self.to_identifier()?;
        if id.1.character_count() != id.0.len() {
            return None;
        }
        match id.0.as_ref() {
            "override" => Some(Modifiers::OVERRIDE),
            "final" => Some(Modifiers::FINAL),
            "dynamic" => Some(Modifiers::DYNAMIC),
            "native" => Some(Modifiers::NATIVE),
            "static" => Some(Modifiers::STATIC),
            _ => None,
        }
    }

    pub(crate) fn to_metadata_key(&self) -> Option<(String, Location)> {
        if let ExpressionKind::Id(id) = &self.kind {
            id.to_metadata_name()
        } else {
            None
        }
    }

    pub(crate) fn to_metadata_value(&self) -> Option<(String, Location)> {
        if let ExpressionKind::Id(id) = &self.kind {
            id.to_metadata_name()
        } else if let ExpressionKind::String(value) = &self.kind {
            Some((value.clone(), self.location.clone()))
        } else {
            None
        }
    }

    pub(crate) fn to_identifier(&self) -> Option<(String, Location)> {
        if let ExpressionKind::Id(id) = &self.kind {
            id.to_identifier()
        } else {
            None
        }
    }

    pub(crate) fn list_metadata_expressions(self: &Rc<Self>) -> Option<Vec<Rc<Self>>> {
        match &self.kind {
            ExpressionKind::ArrayInitializer { .. } => Some(vec![Rc::clone(self)]),
            ExpressionKind::BracketsMember { base, .. } => {
                let mut result = base.list_metadata_expressions()?;
                result.push(Rc::clone(&self));
                Some(result)
            },
            _ => None,
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ExpressionKind {
    Null,
    Boolean(bool),
    Numeric(f64),
    String(String),
    This,
    RegExp {
        body: String,
        flags: String,
    },
    Id(QualifiedIdentifier),
    XmlMarkup(String),
    XmlElement(XmlElement),
    XmlList(Vec<XmlElementContent>),
    ReservedNamespace(ReservedNamespace),
    /// `()`. Used solely internally for arrow functions.
    EmptyParen,
    Paren(Rc<Expression>),
    /// Present as part of an array initializer only.
    /// This expression is not valid in other contexts.
    Rest(Rc<Expression>),
    ArrayInitializer {
        asdoc: Option<AsDoc>,

        /// Element sequence possibly containing `Rest`s and ellisions.
        elements: Vec<Option<Rc<Expression>>>,
    },
    /// `new <T> []`
    VectorInitializer {
        element_type: Rc<TypeExpression>,
        /// Element sequence possibly containing `Rest`s.
        elements: Vec<Rc<Expression>>,
    },
    ObjectInitializer {
        fields: Vec<Rc<ObjectField>>,
    },
    Function {
        name: Option<(String, Location)>,
        common: Rc<FunctionCommon>,
    },
    ArrowFunction(Rc<FunctionCommon>),
    Super(Option<Vec<Rc<Expression>>>),
    New {
        base: Rc<Expression>,
        arguments: Option<Vec<Rc<Expression>>>,
    },
    /// The `o.x` expression.
    DotMember {
        base: Rc<Expression>,
        id: QualifiedIdentifier,
    },
    /// The `o[k]` expression.
    BracketsMember {
        base: Rc<Expression>,
        asdoc: Option<AsDoc>,
        key: Rc<Expression>,
    },
    /// `base.<T1, Tn>`
    WithTypeArguments {
        base: Rc<Expression>,
        arguments: Vec<Rc<TypeExpression>>,
    },
    /// The `o.(condition)` expression.
    Filter {
        base: Rc<Expression>,
        condition: Rc<Expression>,
    },
    /// The `o..x` expression.
    Descendants {
        base: Rc<Expression>,
        id: QualifiedIdentifier,
    },
    Call {
        base: Rc<Expression>,
        arguments: Vec<Rc<Expression>>,
    },
    Unary {
        base: Rc<Expression>,
        operator: Operator,
    },
    Binary {
        left: Rc<Expression>,
        operator: Operator,
        right: Rc<Expression>,
    },
    Conditional {
        test: Rc<Expression>,
        consequent: Rc<Expression>,
        alternative: Rc<Expression>,
    },
    Assignment {
        left: AssignmentLeft,
        compound: Option<Operator>,
        right: Rc<Expression>,
    },
    /// The `x, y` expression.
    Sequence(Rc<Expression>, Rc<Expression>),

    /// Expression used internally only. It is used for parsing
    /// arrow functions with typed parameters and return annotation.
    WithTypeAnnotation {
        base: Rc<Expression>,
        type_annotation: Rc<TypeExpression>,
    },

    Embed {
        source: String,
        type_annotation: Option<Rc<TypeExpression>>,
    },

    /// Expression containing an optional chaining operator.
    OptionalChaining {
        base: Rc<Expression>,
        /// Postfix operators that execute if the base is not `null`
        /// and not `undefined`. The topmost node in this field is
        /// [`ExpressionKind::OptionalChainingHost`], which holds
        /// a non-null and not-undefined value.
        operations: Rc<Expression>,
    },

    /// The topmost expression from which postfix operators
    /// follow in an [`ExpressionKind::OptionalChaining`] expression
    /// inside the `operations` field.
    OptionalChainingHost,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum AssignmentLeft {
    Expression(Rc<Expression>),
    Destructuring(Rc<Destructuring>),
}

impl AssignmentLeft {
    pub fn location(&self) -> Location {
        match self {
            Self::Expression(exp) => exp.location.clone(),
            Self::Destructuring(destr) => destr.location.clone(),
        }
    }

    pub(crate) fn to_metadata_key(&self) -> Option<(String, Location)> {
        match self {
            Self::Expression(exp) => exp.to_metadata_key(),
            _ => None,
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum XmlElementContent {
    Expression(Rc<Expression>),
    Markup(String, Location),
    Text(String, Location),
    Element(XmlElement),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct XmlElement {
    pub location: Location,
    pub opening_tag_name: XmlTagName,
    pub attributes: Vec<XmlAttributeOrExpression>,
    pub content: Vec<XmlElementContent>,
    pub closing_tag_name: Option<XmlTagName>,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum XmlTagName {
    Name((String, Location)),
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub enum XmlAttributeOrExpression {
    Attribute(XmlAttribute),
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct XmlAttribute {
    pub name: (String, Location),
    pub value: XmlAttributeValueOrExpression,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum XmlAttributeValueOrExpression {
    Value(String),
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ReservedNamespace {
    Public,
    Private,
    Protected,
    Internal,
}

impl ToString for ReservedNamespace {
    fn to_string(&self) -> String {
        match self {
            Self::Public => "public".into(),
            Self::Private => "private".into(),
            Self::Protected => "protected".into(),
            Self::Internal => "internal".into(),
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ObjectField {
    Field {
        key: (ObjectKey, Location),
        /// Used when parsing an object initializer as a destructuring pattern.
        /// This is the result of consuming the `!` punctuator.
        #[doc(hidden)]
        destructuring_non_null: bool,
        /// If `None`, this is a shorthand field.
        value: Option<Rc<Expression>>,
    },
    Rest(Rc<Expression>, Location),
}

impl ObjectField {
    pub fn location(&self) -> Location {
        match self {
            Self::Field { key, value, .. } => {
                if let Some(value) = value {
                    key.1.combine_with(value.location.clone())
                } else {
                    key.1.clone()
                }
            },
            Self::Rest(_, location) => location.clone(),
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ObjectKey {
    Id(NonAttributeQualifiedIdentifier),
    String(String, Location),
    Number(f64, Location),
    Brackets(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Destructuring {
    pub location: Location,
    pub kind: DestructuringKind,
    /// Indicates whether the pattern asserts that the
    /// destructuring base is not any of `undefined` and `null`.
    /// The patterns use the `!` punctuator to indicate this behavior.
    pub non_null: bool,
    pub type_annotation: Option<Rc<TypeExpression>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum DestructuringKind {
    Binding {
        name: (String, Location),
    },
    Record(Vec<Rc<RecordDestructuringField>>),
    Array(Vec<Option<ArrayDestructuringItem>>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RecordDestructuringField {
    pub location: Location,
    pub key: (ObjectKey, Location),
    pub non_null: bool,
    pub alias: Option<Rc<Destructuring>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ArrayDestructuringItem {
    Pattern(Rc<Destructuring>),
    Rest(Rc<Destructuring>, Location),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TypeExpression {
    pub location: Location,
    pub kind: TypeExpressionKind,
}

impl TypeExpression {
    pub(crate) fn to_function_type_param(&self) -> Option<FunctionTypeParam> {
        match &self.kind {
            TypeExpressionKind::Id(id) => {
                if let Some(name) = id.to_identifier() {
                    Some(FunctionTypeParam {
                        kind: FunctionParamKind::Required,
                        name,
                        type_annotation: None,
                    })
                } else {
                    None
                }
            },
            TypeExpressionKind::Nullable(subexp) => {
                match &subexp.kind {
                    TypeExpressionKind::Id(id) => {
                        if let Some(name) = id.to_identifier() {
                            Some(FunctionTypeParam {
                                kind: FunctionParamKind::Optional,
                                name,
                                type_annotation: None,
                            })
                        } else {
                            None
                        }
                    },
                    _ => None,
                }
            },
            _ => None,
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum TypeExpressionKind {
    Id(NonAttributeQualifiedIdentifier),
    DotMember {
        base: Rc<TypeExpression>,
        id: NonAttributeQualifiedIdentifier,
    },
    Tuple(Vec<Rc<TypeExpression>>),
    Record(Vec<Rc<RecordTypeField>>),
    /// `(x)`
    Paren(Rc<TypeExpression>),
    /// `*`
    Any,
    Void,
    Never,
    Undefined,
    Nullable(Rc<TypeExpression>),
    NonNullable(Rc<TypeExpression>),
    Function {
        params: Vec<FunctionTypeParam>,
        return_annotation: Rc<TypeExpression>,
    },
    StringLiteral(String),
    NumericLiteral(f64),
    /// `|`
    Union(Vec<Rc<TypeExpression>>),
    /// `&`
    Complement(Rc<TypeExpression>, Rc<TypeExpression>),
    /// `base.<T1, Tn>`
    WithTypeArguments {
        base: Rc<TypeExpression>,
        arguments: Vec<Rc<TypeExpression>>,
    },
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FunctionTypeParam {
    pub kind: FunctionParamKind,
    pub name: (String, Location),
    pub type_annotation: Option<Rc<TypeExpression>>,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[repr(u32)]
pub enum FunctionParamKind {
    Required = 1,
    Optional = 2,
    Rest = 3,
}

impl FunctionParamKind {
    pub fn may_be_followed_by(&self, other: Self) -> bool {
        (*self as u32) <= (other as u32)
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RecordTypeField {
    pub asdoc: Option<AsDoc>,
    pub readonly: bool,
    pub key: (ObjectKey, Location),
    pub nullability: FieldNullability,
    pub type_annotation: Rc<TypeExpression>,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum FieldNullability {
    Unspecified,
    NonNullable,
    Nullable,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Statement {
    pub location: Location,
    pub kind: StatementKind,
}

impl Statement {
    pub(crate) fn extract_asdoc(&self) -> Option<AsDoc> {
        if let StatementKind::Expression { asdoc, expression: _ } = &self.kind {
            asdoc.clone()
        } else {
            None
        }
    }

    pub(crate) fn to_identifier(&self) -> Option<(String, Location)> {
        if let StatementKind::Expression { expression, .. } = &self.kind {
            if let ExpressionKind::Id(id) = &expression.kind {
                id.to_identifier()
            } else {
                None
            }
        } else {
            None
        }
    }

    pub(crate) fn to_identifier_or_reserved_namespace(&self) -> Option<Rc<Expression>> {
        if let StatementKind::Expression { expression, .. } = &self.kind {
            if matches!(expression.kind, ExpressionKind::ReservedNamespace(_)) || self.to_identifier().is_some() {
                Some(Rc::clone(expression))
            } else {
                None
            }
        } else {
            None
        }
    }

    pub(crate) fn list_metadata_expressions(&self) -> Option<Vec<Rc<Expression>>> {
        if let StatementKind::Expression { expression, .. } = &self.kind {
            expression.list_metadata_expressions()
        } else {
            None
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum StatementKind {
    Empty,
    Super(Vec<Rc<Expression>>),
    Block(Rc<Block>),
    If {
        condition: Rc<Expression>,
        consequent: Rc<Statement>,
        alternative: Option<Rc<Statement>>,
    },
    Switch {
        discriminant: Rc<Expression>,
        cases: Vec<SwitchCase>,
    },
    SwitchType {
        discriminant: Rc<Expression>,
        cases: Vec<SwitchTypeCase>,
    },
    Do {
        body: Rc<Statement>,
        test: Rc<Expression>,
    },
    While {
        test: Rc<Expression>,
        body: Rc<Statement>,
    },
    For {
        init: Option<ForInit>,
        test: Option<Rc<Expression>>,
        update: Option<Rc<Expression>>,
        body: Rc<Statement>,
    },
    ForIn {
        each: bool,
        left: ForInLeft,
        right: Rc<Expression>,
        body: Rc<Statement>,
    },
    With {
        object: Rc<Expression>,
        body: Rc<Statement>,
    },
    Continue {
        label: Option<String>,
    },
    Break {
        label: Option<String>,
    },
    Return {
        expression: Option<Rc<Expression>>,
    },
    Throw {
        expression: Rc<Expression>,
    },
    Try {
        block: Rc<Block>,
        catch_clauses: Vec<CatchClause>,
        finally_clause: Option<FinallyClause>,
    },
    Expression {
        asdoc: Option<AsDoc>,
        expression: Rc<Expression>,
    },
    Labeled {
        label: (String, Location),
        statement: Rc<Statement>,
    },
    DefaultXmlNamespace(Rc<Expression>),
    SimpleVariableDeclaration(SimpleVariableDeclaration),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct CatchClause {
    pub pattern: Rc<Destructuring>,
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FinallyClause(pub Rc<Block>);

#[derive(Clone, Serialize, Deserialize)]
pub enum ForInit {
    Variable(SimpleVariableDeclaration),
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ForInLeft {
    Variable(VariableKind, VariableBinding),
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct SimpleVariableDeclaration {
    pub kind: (VariableKind, Location),
    pub bindings: Vec<VariableBinding>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct VariableBinding {
    pub pattern: Rc<Destructuring>,
    pub init: Option<Rc<Expression>>,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum VariableKind {
    Var,
    Const,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct SwitchCase {
    pub expression: Option<Rc<Expression>>,
    pub consequent: Vec<Rc<Directive>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct SwitchTypeCase {
    pub pattern: Option<Rc<Destructuring>>,
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Block(pub Vec<Rc<Directive>>);

#[derive(Clone, Serialize, Deserialize)]
pub struct Directive {
    pub location: Location,
    pub kind: DirectiveKind,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum DirectiveKind {
    Statement(Rc<Statement>),
    Include(Rc<IncludeDirective>),
    Import(Rc<ImportDirective>),
    UseNamespace(Rc<Expression>),
    VariableDefinition(Rc<VariableDefinition>),
    FunctionDefinition(Rc<FunctionDefinition>),
    ConstructorDefinition(Rc<ConstructorDefinition>),
    GetterDefinition(Rc<GetterDefinition>),
    SetterDefinition(Rc<SetterDefinition>),
    TypeDefinition(Rc<TypeDefinition>),
    ClassDefinition(Rc<ClassDefinition>),
    EnumDefinition(Rc<EnumDefinition>),
    InterfaceDefinition(Rc<InterfaceDefinition>),
    NamespaceDefinition(Rc<NamespaceDefinition>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ClassDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub generics: Generics,
    pub extends_clause: Option<Rc<TypeExpression>>,
    pub implements_clause: Option<Vec<Rc<TypeExpression>>>,
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct InterfaceDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub generics: Generics,
    pub extends_clause: Option<Vec<Rc<TypeExpression>>>,
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct EnumDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct NamespaceDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub left: (String, Location),
    pub right: Option<Rc<Expression>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct IncludeDirective {
    pub source: String,
    pub replaced_by: Vec<Rc<Directive>>,
    #[serde(skip)]
    pub replaced_by_source: Rc<Source>,
}

/// An import directive.
/// 
/// If it is an alias with a wildcard import item,
/// it is a package alias that opens the public namespace
/// and aliases it.
/// 
/// If it is an alias with a package recursive import item,
/// it is a package set alias that opens the public namespace of
/// all the respective packages and aliases them into a namespace set.
#[derive(Clone, Serialize, Deserialize)]
pub struct ImportDirective {
    pub alias: Option<(String, Location)>,
    pub package_name: Vec<(String, Location)>,
    pub import_item: (ImportItem, Location),
}

#[derive(Clone, Serialize, Deserialize)]
pub enum ImportItem {
    Wildcard,
    /// `**`
    Recursive,
    Name(String, Location),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct VariableDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub kind: VariableKind,
    pub bindings: Vec<VariableBinding>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FunctionDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub generics: Generics,
    pub common: Rc<FunctionCommon>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ConstructorDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub common: Rc<FunctionCommon>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct GetterDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub common: Rc<FunctionCommon>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct SetterDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub name: (String, Location),
    pub common: Rc<FunctionCommon>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TypeDefinition {
    pub asdoc: Option<AsDoc>,
    pub annotations: Annotations,
    pub left: (String, Location),
    pub generics: Generics,
    pub right: Rc<TypeExpression>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Annotations {
    pub metadata: Vec<Rc<Metadata>>,
    pub modifiers: Modifiers,
    pub access_modifier: Option<Rc<Expression>>,
}

bitflags! {
    #[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
    pub struct Modifiers: u32 {
        const OVERRIDE  = 0b00000001;
        const FINAL     = 0b00000010;
        const DYNAMIC   = 0b00000100;
        const NATIVE    = 0b00001000;
        const STATIC    = 0b00010000;
    }
}

impl Default for Modifiers {
    fn default() -> Self {
        Self::empty()
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Metadata {
    pub asdoc: Option<AsDoc>,
    pub location: Location,
    /// The metadata name. The metadata name may contain a single `::` delimiter.
    pub name: (String, Location),
    pub entries: Vec<MetadataEntry>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct MetadataEntry {
    pub key: Option<(String, Location)>,
    pub value: (String, Location),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Generics {
    pub params: Option<Vec<Rc<GenericParam>>>,
    pub where_clause: Option<GenericsWhere>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct GenericParam {
    pub location: Location,
    pub name: (String, Location),
    pub constraints: Vec<Rc<TypeExpression>>,
    pub default_type: Option<Rc<TypeExpression>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct GenericsWhere {
    pub constraints: Vec<GenericsWhereConstraint>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct GenericsWhereConstraint {
    pub name: (String, Location),
    pub constraints: Vec<Rc<TypeExpression>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FunctionCommon {
    pub flags: FunctionFlags,
    pub params: Vec<FunctionParam>,
    pub return_annotation: Option<Rc<TypeExpression>>,
    pub body: Option<FunctionBody>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FunctionParam {
    pub location: Location,
    pub kind: FunctionParamKind,
    pub binding: VariableBinding,
}

bitflags! {
    #[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
    pub struct FunctionFlags: u32 {
        const AWAIT     = 0b00000001;
        const YIELD     = 0b00000010;
    }
}

impl Default for FunctionFlags {
    fn default() -> Self {
        Self::empty()
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum FunctionBody {
    Block(Rc<Block>),
    /// The function body is allowed to be an expression
    /// in arrow functions.
    Expression(Rc<Expression>),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct AsDoc {
    pub main_body: String,
    pub tags: Vec<AsDocTag>,
}

#[derive(Clone, Serialize, Deserialize)]
pub enum AsDocTag {
    Copy(String),
    Default(String),
    EventType(Rc<Expression>),
    Example(String),
    ExampleText(String),
    InheritDoc,
    Internal(String),
    Param {
        name: String,
        description: String,
    },
    Private,
    Return(String),
    See {
        reference: String,
        display_text: Option<String>,
    },
    Throws {
        class_reference: Rc<TypeExpression>,
        description: Option<String>,
    },
}

#[derive(Clone, Serialize, Deserialize)]
pub struct PackageDefinition {
    pub asdoc: Option<AsDoc>,
    pub location: Location,
    pub id: Vec<(String, Location)>,
    pub block: Rc<Block>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Program {
    pub location: Location,
    pub packages: Vec<Rc<PackageDefinition>>,
    pub directives: Vec<Rc<Directive>>,
}