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
use std::borrow::Cow;

pub mod decl;
pub mod expr;
pub mod pat;
pub mod stmt;

use decl::Decl;
use expr::{Expr, Lit, Prop};
use pat::Pat;
use stmt::Stmt;

use self::pat::RestPat;

pub trait Node {
    fn loc(&self) -> SourceLocation;
}

#[derive(Debug, Clone, PartialEq)]
pub struct Ident<'a> {
    pub slice: Slice<'a>,
}

impl<'a> Node for Ident<'a> {
    fn loc(&self) -> SourceLocation {
        self.slice.loc
    }
}

impl<'a> From<Ident<'a>> for crate::Ident<'a> {
    fn from(other: Ident<'a>) -> Self {
        Self {
            name: other.slice.source,
        }
    }
}

impl<'a> From<Slice<'a>> for Ident<'a> {
    fn from(slice: Slice<'a>) -> Self {
        Self { slice }
    }
}

impl<'a> Ident<'a> {
    pub fn name(&self) -> Cow<'a, str> {
        self.slice.source.clone()
    }
}

/// A fully parsed javascript program.
///
/// It is essentially a collection of `ProgramPart`s
/// with a flag denoting if the representation is
/// a ES6 Mod or a Script.
#[derive(Debug, Clone, PartialEq)]
pub enum Program<'a> {
    /// An ES6 Mod
    Mod(Vec<ProgramPart<'a>>),
    /// Not an ES6 Mod
    Script(Vec<ProgramPart<'a>>),
}

impl<'a> From<Program<'a>> for crate::Program<'a> {
    fn from(other: Program<'a>) -> Self {
        match other {
            Program::Mod(inner) => Self::Mod(inner.into_iter().map(From::from).collect()),
            Program::Script(inner) => Self::Script(inner.into_iter().map(From::from).collect()),
        }
    }
}

impl<'a> Node for Program<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            Self::Mod(inner) => inner.loc(),
            Self::Script(inner) => inner.loc(),
        }
    }
}

impl<'a> Program<'a> {
    pub fn module(parts: Vec<ProgramPart<'a>>) -> Self {
        Program::Mod(parts)
    }
    pub fn script(parts: Vec<ProgramPart<'a>>) -> Self {
        Program::Script(parts)
    }
}

impl<'a> Node for Vec<ProgramPart<'a>> {
    fn loc(&self) -> SourceLocation {
        let start = self
            .first()
            .map(|p| p.loc())
            .unwrap_or_else(SourceLocation::zero);
        let end = self.last().map(|p| p.loc()).unwrap_or(start);
        SourceLocation {
            start: start.start,
            end: end.end,
        }
    }
}

/// A single part of a Javascript program.
/// This will be either a Directive, Decl or a Stmt
#[derive(Debug, Clone, PartialEq)]
pub enum ProgramPart<'a> {
    /// A Directive like `'use strict';`
    Dir(Dir<'a>),
    /// A variable, function or module declaration
    Decl(Decl<'a>),
    /// Any other kind of statement
    Stmt(Stmt<'a>),
}

impl<'a> From<ProgramPart<'a>> for crate::ProgramPart<'a> {
    fn from(other: ProgramPart<'a>) -> Self {
        match other {
            ProgramPart::Dir(inner) => Self::Dir(inner.into()),
            ProgramPart::Decl(inner) => Self::Decl(inner.into()),
            ProgramPart::Stmt(inner) => Self::Stmt(inner.into()),
        }
    }
}

impl<'a> Node for ProgramPart<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            Self::Dir(inner) => inner.loc(),
            Self::Decl(inner) => inner.loc(),
            Self::Stmt(inner) => inner.loc(),
        }
    }
}

impl<'a> ProgramPart<'a> {
    pub fn decl(inner: Decl<'a>) -> Self {
        ProgramPart::Decl(inner)
    }
    pub fn stmt(inner: Stmt<'a>) -> Self {
        ProgramPart::Stmt(inner)
    }
}

/// pretty much always `'use strict'`, this can appear at the
/// top of a file or function
#[derive(Debug, Clone, PartialEq)]
pub struct Dir<'a> {
    pub expr: Lit<'a>,
    pub dir: Cow<'a, str>,
    pub semi_colon: Option<Slice<'a>>,
}

impl<'a> From<Dir<'a>> for crate::Dir<'a> {
    fn from(other: Dir<'a>) -> Self {
        Self {
            expr: other.expr.into(),
            dir: other.dir,
        }
    }
}

impl<'a> Node for Dir<'a> {
    fn loc(&self) -> SourceLocation {
        self.expr.loc()
    }
}

/// A function, this will be part of either a function
/// declaration (ID is required) or a function expression
/// (ID is optional)
/// ```js
/// //function declaration
/// function thing() {}
/// //function expressions
/// var x = function() {}
/// let y = function q() {}
/// ```
#[derive(PartialEq, Debug, Clone)]
pub struct Func<'a> {
    pub keyword: Slice<'a>,
    pub id: Option<Ident<'a>>,
    pub open_paren: Slice<'a>,
    pub params: Vec<ListEntry<'a, FuncArg<'a>>>,
    pub close_paren: Slice<'a>,
    pub body: FuncBody<'a>,
    pub star: Option<Slice<'a>>,
    pub keyword_async: Option<Slice<'a>>,
}

impl<'a> Func<'a> {
    pub fn is_async(&self) -> bool {
        self.keyword_async.is_some()
    }
    pub fn generator(&self) -> bool {
        self.star.is_some()
    }
}

impl<'a> From<Func<'a>> for crate::Func<'a> {
    fn from(other: Func<'a>) -> Self {
        Self {
            generator: other.generator(),
            is_async: other.is_async(),
            id: other.id.map(From::from),
            params: other
                .params
                .into_iter()
                .map(|e| From::from(e.item))
                .collect(),
            body: other.body.into(),
        }
    }
}

impl<'a> Node for Func<'a> {
    fn loc(&self) -> SourceLocation {
        let start = if let Some(keyword) = &self.keyword_async {
            keyword.loc.start
        } else {
            self.keyword.loc.start
        };
        let end = self.body.close_brace.loc.end.clone();
        SourceLocation { start, end }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FuncArgEntry<'a> {
    pub value: FuncArg<'a>,
    pub comma: Option<Slice<'a>>,
}

impl<'a> From<FuncArgEntry<'a>> for crate::FuncArg<'a> {
    fn from(other: FuncArgEntry<'a>) -> Self {
        other.value.into()
    }
}

impl<'a> Node for FuncArgEntry<'a> {
    fn loc(&self) -> SourceLocation {
        if let Some(comma) = &self.comma {
            return SourceLocation {
                start: self.value.loc().start,
                end: comma.loc.end,
            };
        }
        self.value.loc()
    }
}

/// A single function argument from a function signature
#[derive(Debug, Clone, PartialEq)]
pub enum FuncArg<'a> {
    Expr(Expr<'a>),
    Pat(Pat<'a>),
    Rest(Box<RestPat<'a>>),
}

impl<'a> From<FuncArg<'a>> for crate::FuncArg<'a> {
    fn from(other: FuncArg<'a>) -> Self {
        match other {
            FuncArg::Expr(inner) => Self::Expr(inner.into()),
            FuncArg::Pat(inner) => Self::Pat(inner.into()),
            FuncArg::Rest(inner) => {
                Self::Pat(crate::pat::Pat::RestElement(Box::new(inner.pat.into())))
            }
        }
    }
}

impl<'a> Node for FuncArg<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            FuncArg::Expr(inner) => inner.loc(),
            FuncArg::Pat(inner) => inner.loc(),
            FuncArg::Rest(inner) => inner.loc(),
        }
    }
}

/// The block statement that makes up the function's body
#[derive(Debug, Clone, PartialEq)]
pub struct FuncBody<'a> {
    pub open_brace: Slice<'a>,
    pub stmts: Vec<ProgramPart<'a>>,
    pub close_brace: Slice<'a>,
}

impl<'a> From<FuncBody<'a>> for crate::FuncBody<'a> {
    fn from(other: FuncBody<'a>) -> Self {
        Self(other.stmts.into_iter().map(From::from).collect())
    }
}

impl<'a> Node for FuncBody<'a> {
    fn loc(&self) -> SourceLocation {
        SourceLocation {
            start: self.open_brace.loc.start,
            end: self.close_brace.loc.end,
        }
    }
}

/// A way to declare object templates
/// ```js
/// class Thing {
///     constructor() {
///         this._a = 0;
///     }
///     stuff() {
///         return 'stuff'
///     }
///     set a(value) {
///         if (value > 100) {
///             this._a = 0;
///         } else {
///             this._a = value;
///         }
///     }
///     get a() {
///         return this._a;
///     }
/// }
/// let y = class {
///     constructor() {
///         this.a = 100;
///     }
/// }
/// ```
#[derive(PartialEq, Debug, Clone)]
pub struct Class<'a> {
    pub keyword: Slice<'a>,
    pub id: Option<Ident<'a>>,
    pub super_class: Option<Box<Expr<'a>>>,
    pub body: ClassBody<'a>,
}

impl<'a> From<Class<'a>> for crate::Class<'a> {
    fn from(other: Class<'a>) -> Self {
        Self {
            id: other.id.map(From::from),
            super_class: other.super_class.map(|e| Box::new(From::from(*e))),
            body: other.body.into(),
        }
    }
}

impl<'a> Node for Class<'a> {
    fn loc(&self) -> SourceLocation {
        SourceLocation {
            start: self.keyword.loc.start,
            end: self.body.close_brace.loc.end,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClassBody<'a> {
    pub open_brace: Slice<'a>,
    pub props: Vec<Prop<'a>>,
    pub close_brace: Slice<'a>,
}

impl<'a> From<ClassBody<'a>> for crate::ClassBody<'a> {
    fn from(other: ClassBody<'a>) -> Self {
        Self(other.props.into_iter().map(From::from).collect())
    }
}

impl<'a> Node for ClassBody<'a> {
    fn loc(&self) -> SourceLocation {
        let start = self.open_brace.loc.start;
        let end = self.close_brace.loc.end;
        SourceLocation { start, end }
    }
}

/// The kind of variable being defined (`var`/`let`/`const`)
#[derive(Debug, Clone, PartialEq)]
pub enum VarKind<'a> {
    Var(Option<Slice<'a>>),
    Let(Slice<'a>),
    Const(Slice<'a>),
}

impl<'a> From<VarKind<'a>> for crate::VarKind {
    fn from(other: VarKind<'a>) -> Self {
        match other {
            VarKind::Var(_) => Self::Var,
            VarKind::Let(_) => Self::Let,
            VarKind::Const(_) => Self::Const,
        }
    }
}

impl<'a> Node for VarKind<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            VarKind::Var(Some(slice)) => slice.loc,
            VarKind::Let(slice) => slice.loc,
            VarKind::Const(slice) => slice.loc,
            _ => SourceLocation::zero(),
        }
    }
}

impl<'a> VarKind<'a> {
    pub fn is_var(&self) -> bool {
        matches!(self, VarKind::Var(_))
    }
}

/// The available operators for assignment Exprs
#[derive(Debug, Clone, PartialEq)]
pub enum AssignOp<'a> {
    Equal(Slice<'a>),
    PlusEqual(Slice<'a>),
    MinusEqual(Slice<'a>),
    TimesEqual(Slice<'a>),
    DivEqual(Slice<'a>),
    ModEqual(Slice<'a>),
    LeftShiftEqual(Slice<'a>),
    RightShiftEqual(Slice<'a>),
    UnsignedRightShiftEqual(Slice<'a>),
    OrEqual(Slice<'a>),
    XOrEqual(Slice<'a>),
    AndEqual(Slice<'a>),
    PowerOfEqual(Slice<'a>),
}

impl<'a> From<AssignOp<'a>> for crate::AssignOp {
    fn from(other: AssignOp<'a>) -> Self {
        match other {
            AssignOp::Equal(_) => Self::Equal,
            AssignOp::PlusEqual(_) => Self::PlusEqual,
            AssignOp::MinusEqual(_) => Self::MinusEqual,
            AssignOp::TimesEqual(_) => Self::TimesEqual,
            AssignOp::DivEqual(_) => Self::DivEqual,
            AssignOp::ModEqual(_) => Self::ModEqual,
            AssignOp::LeftShiftEqual(_) => Self::LeftShiftEqual,
            AssignOp::RightShiftEqual(_) => Self::RightShiftEqual,
            AssignOp::UnsignedRightShiftEqual(_) => Self::UnsignedRightShiftEqual,
            AssignOp::OrEqual(_) => Self::OrEqual,
            AssignOp::XOrEqual(_) => Self::XOrEqual,
            AssignOp::AndEqual(_) => Self::AndEqual,
            AssignOp::PowerOfEqual(_) => Self::PowerOfEqual,
        }
    }
}

impl<'a> Node for AssignOp<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            AssignOp::Equal(slice) => slice.loc,
            AssignOp::PlusEqual(slice) => slice.loc,
            AssignOp::MinusEqual(slice) => slice.loc,
            AssignOp::TimesEqual(slice) => slice.loc,
            AssignOp::DivEqual(slice) => slice.loc,
            AssignOp::ModEqual(slice) => slice.loc,
            AssignOp::LeftShiftEqual(slice) => slice.loc,
            AssignOp::RightShiftEqual(slice) => slice.loc,
            AssignOp::UnsignedRightShiftEqual(slice) => slice.loc,
            AssignOp::OrEqual(slice) => slice.loc,
            AssignOp::XOrEqual(slice) => slice.loc,
            AssignOp::AndEqual(slice) => slice.loc,
            AssignOp::PowerOfEqual(slice) => slice.loc,
        }
    }
}

/// The available logical operators
#[derive(Debug, Clone, PartialEq)]
pub enum LogicalOp<'a> {
    Or(Slice<'a>),
    And(Slice<'a>),
}

impl<'a> From<LogicalOp<'a>> for crate::LogicalOp {
    fn from(other: LogicalOp<'a>) -> Self {
        match other {
            LogicalOp::Or(_) => Self::Or,
            LogicalOp::And(_) => Self::And,
        }
    }
}

impl<'a> Node for LogicalOp<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            LogicalOp::Or(slice) => slice.loc,
            LogicalOp::And(slice) => slice.loc,
        }
    }
}

/// The available operations for `Binary` Exprs
#[derive(Debug, Clone, PartialEq)]
pub enum BinaryOp<'a> {
    Equal(Slice<'a>),
    NotEqual(Slice<'a>),
    StrictEqual(Slice<'a>),
    StrictNotEqual(Slice<'a>),
    LessThan(Slice<'a>),
    GreaterThan(Slice<'a>),
    LessThanEqual(Slice<'a>),
    GreaterThanEqual(Slice<'a>),
    LeftShift(Slice<'a>),
    RightShift(Slice<'a>),
    UnsignedRightShift(Slice<'a>),
    Plus(Slice<'a>),
    Minus(Slice<'a>),
    Times(Slice<'a>),
    Over(Slice<'a>),
    Mod(Slice<'a>),
    Or(Slice<'a>),
    XOr(Slice<'a>),
    And(Slice<'a>),
    In(Slice<'a>),
    InstanceOf(Slice<'a>),
    PowerOf(Slice<'a>),
}

impl<'a> From<BinaryOp<'a>> for crate::BinaryOp {
    fn from(other: BinaryOp<'a>) -> Self {
        match other {
            BinaryOp::Equal(_) => Self::Equal,
            BinaryOp::NotEqual(_) => Self::NotEqual,
            BinaryOp::StrictEqual(_) => Self::StrictEqual,
            BinaryOp::StrictNotEqual(_) => Self::StrictNotEqual,
            BinaryOp::LessThan(_) => Self::LessThan,
            BinaryOp::GreaterThan(_) => Self::GreaterThan,
            BinaryOp::LessThanEqual(_) => Self::LessThanEqual,
            BinaryOp::GreaterThanEqual(_) => Self::GreaterThanEqual,
            BinaryOp::LeftShift(_) => Self::LeftShift,
            BinaryOp::RightShift(_) => Self::RightShift,
            BinaryOp::UnsignedRightShift(_) => Self::UnsignedRightShift,
            BinaryOp::Plus(_) => Self::Plus,
            BinaryOp::Minus(_) => Self::Minus,
            BinaryOp::Times(_) => Self::Times,
            BinaryOp::Over(_) => Self::Over,
            BinaryOp::Mod(_) => Self::Mod,
            BinaryOp::Or(_) => Self::Or,
            BinaryOp::XOr(_) => Self::XOr,
            BinaryOp::And(_) => Self::And,
            BinaryOp::In(_) => Self::In,
            BinaryOp::InstanceOf(_) => Self::InstanceOf,
            BinaryOp::PowerOf(_) => Self::PowerOf,
        }
    }
}

impl<'a> Node for BinaryOp<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            BinaryOp::Equal(slice) => slice.loc,
            BinaryOp::NotEqual(slice) => slice.loc,
            BinaryOp::StrictEqual(slice) => slice.loc,
            BinaryOp::StrictNotEqual(slice) => slice.loc,
            BinaryOp::LessThan(slice) => slice.loc,
            BinaryOp::GreaterThan(slice) => slice.loc,
            BinaryOp::LessThanEqual(slice) => slice.loc,
            BinaryOp::GreaterThanEqual(slice) => slice.loc,
            BinaryOp::LeftShift(slice) => slice.loc,
            BinaryOp::RightShift(slice) => slice.loc,
            BinaryOp::UnsignedRightShift(slice) => slice.loc,
            BinaryOp::Plus(slice) => slice.loc,
            BinaryOp::Minus(slice) => slice.loc,
            BinaryOp::Times(slice) => slice.loc,
            BinaryOp::Over(slice) => slice.loc,
            BinaryOp::Mod(slice) => slice.loc,
            BinaryOp::Or(slice) => slice.loc,
            BinaryOp::XOr(slice) => slice.loc,
            BinaryOp::And(slice) => slice.loc,
            BinaryOp::In(slice) => slice.loc,
            BinaryOp::InstanceOf(slice) => slice.loc,
            BinaryOp::PowerOf(slice) => slice.loc,
        }
    }
}

/// `++` or `--`
#[derive(Debug, Clone, PartialEq)]
pub enum UpdateOp<'a> {
    Increment(Slice<'a>),
    Decrement(Slice<'a>),
}

impl<'a> From<UpdateOp<'a>> for crate::UpdateOp {
    fn from(other: UpdateOp<'a>) -> Self {
        match other {
            UpdateOp::Increment(_) => Self::Increment,
            UpdateOp::Decrement(_) => Self::Decrement,
        }
    }
}

impl<'a> Node for UpdateOp<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            UpdateOp::Increment(slice) => slice.loc,
            UpdateOp::Decrement(slice) => slice.loc,
        }
    }
}

/// The allowed operators for an Expr
/// to be `Unary`
#[derive(Debug, Clone, PartialEq)]
pub enum UnaryOp<'a> {
    Minus(Slice<'a>),
    Plus(Slice<'a>),
    Not(Slice<'a>),
    Tilde(Slice<'a>),
    TypeOf(Slice<'a>),
    Void(Slice<'a>),
    Delete(Slice<'a>),
}

impl<'a> From<UnaryOp<'a>> for crate::UnaryOp {
    fn from(other: UnaryOp<'a>) -> Self {
        match other {
            UnaryOp::Minus(_) => Self::Minus,
            UnaryOp::Plus(_) => Self::Plus,
            UnaryOp::Not(_) => Self::Not,
            UnaryOp::Tilde(_) => Self::Tilde,
            UnaryOp::TypeOf(_) => Self::TypeOf,
            UnaryOp::Void(_) => Self::Void,
            UnaryOp::Delete(_) => Self::Delete,
        }
    }
}

impl<'a> Node for UnaryOp<'a> {
    fn loc(&self) -> SourceLocation {
        match self {
            UnaryOp::Minus(slice) => slice.loc,
            UnaryOp::Plus(slice) => slice.loc,
            UnaryOp::Not(slice) => slice.loc,
            UnaryOp::Tilde(slice) => slice.loc,
            UnaryOp::TypeOf(slice) => slice.loc,
            UnaryOp::Void(slice) => slice.loc,
            UnaryOp::Delete(slice) => slice.loc,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Slice<'a> {
    pub source: Cow<'a, str>,
    pub loc: SourceLocation,
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub struct SourceLocation {
    pub start: Position,
    pub end: Position,
}

impl SourceLocation {
    fn zero() -> Self {
        Self {
            start: Position { line: 0, column: 0 },
            end: Position { line: 0, column: 0 },
        }
    }
}

impl core::cmp::PartialOrd for SourceLocation {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match self.start.partial_cmp(&other.start) {
            Some(core::cmp::Ordering::Equal) => {}
            ord => return ord,
        }
        self.end.partial_cmp(&other.end)
    }
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub struct Position {
    pub line: usize,
    pub column: usize,
}

impl std::cmp::PartialOrd for Position {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let line = self.line.partial_cmp(&other.line)?;
        if matches!(line, core::cmp::Ordering::Equal) {
            return self.column.partial_cmp(&other.column);
        }
        Some(line)
    }
}

impl std::ops::Add for Position {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            line: self.line + rhs.line,
            column: self.column + rhs.column,
        }
    }
}

impl std::ops::Sub for Position {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            line: self.line - rhs.line,
            column: self.column - rhs.column,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ListEntry<'a, T> {
    pub item: T,
    pub comma: Option<Slice<'a>>,
}

impl<'a, T> ListEntry<'a, T> {
    pub fn no_comma(item: T) -> Self {
        Self { item, comma: None }
    }
}

impl<'a, T> Node for ListEntry<'a, T>
where
    T: Node,
{
    fn loc(&self) -> SourceLocation {
        if let Some(comma) = &self.comma {
            return SourceLocation {
                start: self.item.loc().start,
                end: comma.loc.end,
            };
        }
        self.item.loc()
    }
}