garden-lang-parser 0.19.0

A live programming language.
Documentation
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
//! Syntax tree definitions for Garden.

use rustc_hash::FxHashMap;

use std::{
    fmt::Display,
    path::{Path, PathBuf},
    rc::Rc,
};

use crate::position::Position;

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct TypeName {
    pub name: String,
}

impl TypeName {
    pub fn is_no_value(&self) -> bool {
        self.name == "NoValue"
    }
}

impl std::fmt::Debug for TypeName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}
impl Display for TypeName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl From<&str> for TypeName {
    fn from(s: &str) -> Self {
        TypeName { name: s.to_owned() }
    }
}

impl From<&String> for TypeName {
    fn from(s: &String) -> Self {
        TypeName { name: s.to_owned() }
    }
}

#[derive(Clone, Eq)]
pub struct TypeSymbol {
    pub name: TypeName,
    pub position: Position,
    pub id: SyntaxId,
}

/// Only consider the name when comparing type symbols. This is
/// important when the runtime checks values have the same type.
impl PartialEq for TypeSymbol {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl std::fmt::Debug for TypeSymbol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if std::env::var("VERBOSE").is_ok() {
            f.debug_struct("TypeSymbol")
                .field("name", &self.name)
                .field("position", &self.position)
                .field("id", &self.id)
                .finish()
        } else {
            write!(f, "TypeSymbol\"{}\"", self.name.name)
        }
    }
}

impl Display for TypeSymbol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl TypeSymbol {
    pub fn is_placeholder(&self) -> bool {
        // TODO: Prevent users from writing this symbol in userland code.
        self.name.name == "__placeholder" || self.name.name == "__reserved_word_placeholder"
    }
}

/// Represents a type name in source code. This might be a concrete
/// type, such as `List<Int>`, or may refer to generics
/// e.g. `List<T>`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeHint {
    pub sym: TypeSymbol,
    pub args: Vec<TypeHint>,
    pub position: Position,
}

impl TypeHint {
    /// The source code equivalent of this type hint.
    pub fn as_src(&self) -> String {
        if self.args.is_empty() {
            format!("{}", self.sym.name)
        } else if self.sym.name.name == "Tuple" {
            let formatted_args = self
                .args
                .iter()
                .map(|a| a.as_src())
                .collect::<Vec<_>>()
                .join(", ");

            format!("({})", formatted_args)
        } else {
            let formatted_args = self
                .args
                .iter()
                .map(|a| a.as_src())
                .collect::<Vec<_>>()
                .join(", ");
            format!("{}<{}>", self.sym.name, formatted_args)
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SymbolName {
    pub name: String,
}

impl Display for SymbolName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl SymbolName {
    pub fn is_underscore(&self) -> bool {
        self.name == "_"
    }

    pub fn is_placeholder(&self) -> bool {
        // TODO: Prevent users from writing this symbol in userland code.
        self.name == "__placeholder" || self.name == "__reserved_word_placeholder"
    }
}

impl From<&str> for SymbolName {
    fn from(s: &str) -> Self {
        SymbolName { name: s.to_owned() }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
pub struct InternedSymbolId(pub usize);

/// A symbol representing a value, such as a local variable, a
/// function name or a method name.
///
/// See also [`TypeSymbol`].
#[derive(Clone, PartialEq, Eq)]
pub struct Symbol {
    pub position: Position,
    pub name: SymbolName,
    pub id: SyntaxId,
    pub interned_id: InternedSymbolId,
}

impl Symbol {
    pub fn new<S: AsRef<str>>(position: Position, name: S, id_gen: &mut IdGenerator) -> Self {
        let name = SymbolName {
            name: name.as_ref().to_owned(),
        };

        Self {
            interned_id: id_gen.intern_symbol(&name),
            position,
            name,
            id: id_gen.next(),
        }
    }

    pub fn is_placeholder(&self) -> bool {
        self.name.is_placeholder()
    }
}

impl std::fmt::Debug for Symbol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if std::env::var("VERBOSE").is_ok() {
            f.debug_struct("Symbol")
                .field("name", &self.name)
                .field("position", &self.position)
                .field("id", &self.id)
                .field("interned_id", &self.interned_id)
                .finish()
        } else {
            write!(f, "Symbol\"{}\"", self.name.name)
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SymbolWithHint {
    pub symbol: Symbol,
    pub hint: Option<TypeHint>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOperatorKind {
    Add,
    Subtract,
    Multiply,
    Divide,
    Modulo,
    Exponent,
    Equal,
    NotEqual,
    LessThan,
    LessThanOrEqual,
    GreaterThan,
    GreaterThanOrEqual,
    And,
    Or,
    StringConcat,
}

impl Display for BinaryOperatorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            BinaryOperatorKind::Add => "+",
            BinaryOperatorKind::Subtract => "-",
            BinaryOperatorKind::Multiply => "*",
            BinaryOperatorKind::Divide => "/",
            BinaryOperatorKind::Modulo => "%",
            BinaryOperatorKind::Exponent => "^",
            BinaryOperatorKind::Equal => "==",
            BinaryOperatorKind::NotEqual => "!=",
            BinaryOperatorKind::LessThan => "<",
            BinaryOperatorKind::LessThanOrEqual => "<=",
            BinaryOperatorKind::GreaterThan => ">",
            BinaryOperatorKind::GreaterThanOrEqual => ">=",
            BinaryOperatorKind::And => "&&",
            BinaryOperatorKind::Or => "||",
            BinaryOperatorKind::StringConcat => "^",
        };
        write!(f, "{}", s)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssignUpdateKind {
    Add,
    Subtract,
}

impl AssignUpdateKind {
    pub fn as_src(&self) -> &'static str {
        match self {
            AssignUpdateKind::Add => "+=",
            AssignUpdateKind::Subtract => "-=",
        }
    }
}

/// The left hand side of a case in a `match`
/// expression. E.g. `Foo(bar)` in the following code.
///
/// ```garden
/// match x {
///   Foo(bar) => {}
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pattern {
    /// E.g. `Some` or `None`.
    pub variant_sym: Symbol,
    /// E.g. `foo` in `Some(foo) => `.
    pub payload: Option<LetDestination>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionWithComma {
    pub expr: Rc<Expression>,
    pub comma: Option<Position>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParenthesizedExpression {
    pub open_paren: Position,
    pub expr: Rc<Expression>,
    pub close_paren: Position,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParenthesizedArguments {
    pub open_paren: Position,
    pub arguments: Vec<ExpressionWithComma>,
    pub close_paren: Position,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LetDestination {
    /// ```garden
    /// let x = y
    /// ```
    Symbol(Symbol),
    /// ```garden
    /// let (x, y) = z
    /// ```
    Destructure(Vec<Symbol>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expression_ {
    /// ```garden
    /// match x {
    ///     None => { get_value() }
    ///     Some(y) => y + 1,
    ///     _ => error("yikes"),
    /// }
    /// ```
    Match(Rc<Expression>, Vec<(Pattern, Block)>),
    /// ```garden
    /// if x { y }
    /// if x { y } else { z }
    /// ```
    If(Rc<Expression>, Block, Option<Block>),
    /// ```garden
    /// while x { y }
    /// ```
    While(Rc<Expression>, Block),
    /// ```garden
    /// for x in y { z }
    /// for (foo, bar) in y { z }
    /// ```
    ForIn(LetDestination, Rc<Expression>, Block),
    /// ```garden
    /// break
    /// ```
    Break,
    /// ```garden
    /// continue
    /// ```
    Continue,
    /// ```garden
    /// x = y
    /// ```
    Assign(Symbol, Rc<Expression>),
    /// ```garden
    /// x += y
    /// x -= y
    /// ```
    AssignUpdate(Symbol, AssignUpdateKind, Rc<Expression>),
    /// ```garden
    /// let x = y
    /// let x: T = y
    /// let (x, y) = z
    /// ```
    Let(LetDestination, Option<TypeHint>, Rc<Expression>),
    /// ```garden
    /// return x
    /// return // equivalent to `return Unit`
    /// ```
    Return(Option<Rc<Expression>>),
    /// ```garden
    /// 123
    /// ```
    IntLiteral(i64),
    /// ```garden
    /// "foo"
    /// ```
    StringLiteral(String),
    /// ```garden
    /// [x, y]
    /// ```
    ListLiteral(Vec<ExpressionWithComma>),
    /// ```garden
    /// ()
    /// (x,)
    /// (x, y)
    /// ```
    TupleLiteral(Vec<Rc<Expression>>),
    /// ```garden
    /// Foo { x: 1, y: bar() }
    /// ```
    ///
    /// Field values are executed in the order they occur in source
    /// code, so we want an ordered data type here.
    StructLiteral(TypeSymbol, Vec<(Symbol, Rc<Expression>)>),
    /// ```garden
    /// x + y
    /// x < y
    /// x && y
    /// ```
    BinaryOperator(Rc<Expression>, BinaryOperatorKind, Rc<Expression>),
    /// ```garden
    /// x
    /// ```
    Variable(Symbol),
    /// ```garden
    /// x()
    /// ```
    Call(Rc<Expression>, ParenthesizedArguments),
    /// ```garden
    /// foo.bar(x, y)
    /// ```
    MethodCall(Rc<Expression>, Symbol, ParenthesizedArguments),
    /// ```garden
    /// foo.bar
    /// ```
    DotAccess(Rc<Expression>, Symbol),
    /// ```garden
    /// fun(x, y) { x + y }
    /// ```
    FunLiteral(FunInfo),
    /// ```garden
    /// assert(x)
    /// ```
    Assert(Rc<Expression>),
    /// Parentheses used for grouping, particularly in nested binary operators.
    ///
    /// ```garden
    /// (x)
    /// x * (y * z)
    /// ```
    Parentheses(Position, Rc<Expression>, Position),
    /// We had a parse error in this position, so there's no
    /// expression.
    Invalid,
}

impl Expression_ {
    pub(crate) fn is_invalid_or_placeholder(&self) -> bool {
        match self {
            Expression_::Variable(sym) => sym.is_placeholder(),
            Expression_::Invalid => true,
            _ => false,
        }
    }
}

/// A syntactic item that the IDE can interact with, such as an
/// expression or a variable name.
#[derive(Clone, PartialEq, Eq, Hash, Copy)]
pub struct SyntaxId(pub usize);

impl std::fmt::Debug for SyntaxId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Avoid deriving Debug because otherwise we get:
        //
        // SyntaxId(
        //   123
        // )
        //
        // which is too verbose.
        write!(f, "SyntaxId({})", self.0)
    }
}

/// An ID that represents a piece of syntax that we can perform
/// operations on, such as go-to-def.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AstId {
    /// Syntax ID of an expression.
    Expr(SyntaxId),
    /// Syntax ID of a symbol. This is not necessarily part of an
    /// expression: it could be a function parameter or type name.
    ///
    /// ```
    /// fun f(foo: Int) {}
    /// ```
    Sym(SyntaxId),
    /// Syntax ID of a type symbol.
    ///
    /// ```
    /// fun f(_: Foo) {}
    /// ```
    TypeSym(SyntaxId),
    /// An import item.
    ///
    /// ```
    /// import "./foo.gdn"
    /// ```
    Import(SyntaxId),
}

impl AstId {
    pub fn id(&self) -> SyntaxId {
        match self {
            AstId::Expr(syntax_id) => *syntax_id,
            AstId::Sym(syntax_id) => *syntax_id,
            AstId::TypeSym(syntax_id) => *syntax_id,
            AstId::Import(syntax_id) => *syntax_id,
        }
    }
}

#[derive(Debug, Clone)]
pub struct IdGenerator {
    pub next_id: SyntaxId,
    pub interned: FxHashMap<SymbolName, InternedSymbolId>,
    pub intern_id_to_name: FxHashMap<InternedSymbolId, SymbolName>,
}

impl Default for IdGenerator {
    fn default() -> Self {
        Self {
            next_id: SyntaxId(0),
            interned: FxHashMap::default(),
            intern_id_to_name: FxHashMap::default(),
        }
    }
}

impl IdGenerator {
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> SyntaxId {
        let next_id = self.next_id;
        self.next_id = SyntaxId(next_id.0 + 1);
        next_id
    }

    pub fn intern_symbol(&mut self, name: &SymbolName) -> InternedSymbolId {
        match self.interned.get(name) {
            Some(id) => *id,
            None => {
                let id = InternedSymbolId(self.interned.len());
                self.interned.insert(name.clone(), id);
                self.intern_id_to_name.insert(id, name.clone());
                id
            }
        }
    }
}

/// Stores the source code of all the files we've loaded.
#[derive(Debug, Clone, Default)]
pub struct Vfs {
    // TODO: support a single path having different strings, because
    // users can re-evaluate individual functions after modification.
    file_srcs: FxHashMap<PathBuf, String>,
}

impl Vfs {
    pub fn insert(&mut self, path: PathBuf, src: String) {
        self.file_srcs.insert(path, src);
    }

    pub fn file_src(&self, path: &Path) -> Option<&String> {
        self.file_srcs.get(path)
    }

    pub fn pos_src(&self, pos: &Position) -> Option<&str> {
        match self.file_srcs.get(pos.path.as_path()) {
            Some(file) => Some(&file[pos.start_offset..pos.end_offset]),
            None => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Expression {
    pub position: Position,
    pub expr_: Expression_,
    /// Is this expression in a position where its value is used?
    /// This is only false in blocks, e.g. in `{ foo() bar() }`,
    /// `foo()` is ignored.
    pub value_is_used: bool,
    pub id: SyntaxId,
}

impl Expression {
    pub(crate) fn new(position: Position, expr_: Expression_, id: SyntaxId) -> Self {
        Self {
            position,
            expr_,
            value_is_used: true,
            id,
        }
    }

    /// Helper for creating Invalid expressions.
    pub fn invalid(position: Position, id: SyntaxId) -> Self {
        Self {
            position,
            expr_: Expression_::Invalid,
            value_is_used: true,
            id,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block {
    pub open_brace: Position,
    pub exprs: Vec<Rc<Expression>>,
    pub close_brace: Position,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Visibility {
    External(Position),
    CurrentFile,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToplevelExpression(pub Expression);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunInfo {
    pub pos: Position,
    pub doc_comment: Option<String>,
    /// The name of the function. This is `None` for closures.
    pub name_sym: Option<Symbol>,
    /// If this is a toplevel function, the ID of the definition.
    pub item_id: Option<ToplevelItemId>,
    pub type_params: Vec<TypeSymbol>,
    pub params: ParenthesizedParameters,
    pub return_hint: Option<TypeHint>,
    pub body: Block,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParenthesizedParameters {
    pub open_paren: Position,
    pub params: Vec<SymbolWithHint>,
    pub close_paren: Position,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestInfo {
    pub pos: Position,
    pub doc_comment: Option<String>,
    pub name_sym: Symbol,
    pub body: Block,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VariantInfo {
    pub name_sym: Symbol,
    /// If this variant is of the form `Foo(T)`, the type hint inside
    /// the parentheses.
    pub payload_hint: Option<TypeHint>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldInfo {
    pub sym: Symbol,
    pub hint: TypeHint,
    pub doc_comment: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumInfo {
    pub pos: Position,
    pub visibility: Visibility,
    pub doc_comment: Option<String>,
    pub name_sym: TypeSymbol,
    pub type_params: Vec<TypeSymbol>,
    pub variants: Vec<VariantInfo>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructInfo {
    pub pos: Position,
    pub visibility: Visibility,
    pub doc_comment: Option<String>,
    pub name_sym: TypeSymbol,
    pub type_params: Vec<TypeSymbol>,
    /// The fields of this struct.
    ///
    /// We deliberately want an ordered data type here, so we can
    /// display field information in the same order as the user
    /// defined the fields.
    pub fields: Vec<FieldInfo>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportInfo {
    pub pos: Position,
    /// The actual path being imporrted. For example, if the user
    /// wrote `import "./foo.gdn"`, then foo.gdn is the path here.
    pub path: PathBuf,
    /// The position of the string literal within
    /// `import "./foo.gdn"`.
    pub path_pos: Position,
    pub id: SyntaxId,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltinMethodKind {
    ListAppend,
    ListContains,
    ListGet,
    ListLen,
    PathExists,
    PathRead,
    StringIndexOf,
    StringLen,
    StringLines,
    StringSubstring,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MethodKind {
    BuiltinMethod(BuiltinMethodKind, Option<FunInfo>),
    UserDefinedMethod(FunInfo),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MethodInfo {
    pub pos: Position,
    /// The type that has this method.
    pub receiver_hint: TypeHint,
    /// The name of the receiver in the method definition. This is
    /// typically `self`.
    ///
    /// TODO: this only exists for user-defined methods, so it's
    /// clunky to have it for all methods.
    pub receiver_sym: Symbol,
    /// The name of the method itself, e.g. `len` in
    /// `some_string.len()`.
    pub name_sym: Symbol,
    /// User-defined or built-in.
    pub kind: MethodKind,
}

impl MethodInfo {
    pub fn fun_info(&self) -> Option<&FunInfo> {
        match &self.kind {
            MethodKind::BuiltinMethod(_, fun_info) => fun_info.as_ref(),
            MethodKind::UserDefinedMethod(fun_info) => Some(fun_info),
        }
    }

    pub fn full_name(&self) -> String {
        format!("{}::{}", self.receiver_hint.sym, self.name_sym.name)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
pub struct ToplevelItemId(pub usize);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToplevelItem {
    /// ```garden
    /// fun foo() {}
    /// external fun bar() {}
    /// ```
    Fun(Symbol, FunInfo, Visibility),
    /// ```garden
    /// fun (this: MyType) foo() {}
    /// external fun (this: MyType) baz() {}
    /// ```
    Method(MethodInfo, Visibility),
    /// ```garden
    /// test foo { assert(1 == 2) }
    /// ```
    Test(TestInfo),
    /// ```garden
    /// enum Foo { Red, Green, Custom(String) }
    /// ```
    Enum(EnumInfo),
    /// ```garden
    /// struct Foo { x: Int, y: Int }
    /// ```
    Struct(StructInfo),
    /// ```garden
    /// import "./foo.gdn"
    /// ```
    Import(ImportInfo),
    /// ```garden
    /// println("hello world")
    /// ```
    ///
    /// Should only be used in the REPL.
    Expr(ToplevelExpression),
    /// ```garden
    /// { println("hello world") }
    /// ```
    Block(Block),
}

impl ToplevelItem {
    pub fn position(&self) -> Position {
        match self {
            ToplevelItem::Fun(_, fun_info, _) => fun_info.pos.clone(),
            ToplevelItem::Method(method_info, _) => method_info.pos.clone(),
            ToplevelItem::Test(test_info) => test_info.pos.clone(),
            ToplevelItem::Enum(enum_info) => enum_info.pos.clone(),
            ToplevelItem::Struct(struct_info) => struct_info.pos.clone(),
            ToplevelItem::Import(import_info) => import_info.pos.clone(),
            ToplevelItem::Expr(toplevel_expression) => toplevel_expression.0.position.clone(),
            ToplevelItem::Block(block) => Position::merge(&block.open_brace, &block.close_brace),
        }
    }

    pub(crate) fn is_invalid_or_placeholder(&self) -> bool {
        match self {
            ToplevelItem::Fun(symbol, _, _) => symbol.is_placeholder(),
            ToplevelItem::Method(method_info, _) => method_info.name_sym.is_placeholder(),
            ToplevelItem::Test(test_info) => test_info.name_sym.is_placeholder(),
            ToplevelItem::Enum(enum_info) => enum_info.name_sym.is_placeholder(),
            ToplevelItem::Struct(struct_info) => struct_info.name_sym.is_placeholder(),
            ToplevelItem::Expr(e) => e.0.expr_.is_invalid_or_placeholder(),
            ToplevelItem::Block(_) => false,
            ToplevelItem::Import(_) => false,
        }
    }
}