ad-astra 1.0.0

Embeddable scripting language platform Ad Astra. Main Crate.
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
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
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming       //
// language platform.                                                         //
//                                                                            //
// This work is proprietary software with source-available code.              //
//                                                                            //
// To copy, use, distribute, or contribute to this work, you must agree to    //
// the terms of the General License Agreement:                                //
//                                                                            //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
//                                                                            //
// The agreement grants a Basic Commercial License, allowing you to use       //
// this work in non-commercial and limited commercial products with a total   //
// gross revenue cap. To remove this commercial limit for one of your         //
// products, you must acquire a Full Commercial License.                      //
//                                                                            //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions.             //
// Contributions are governed by the "Contributions" section of the General   //
// License Agreement.                                                         //
//                                                                            //
// Copying the work in parts is strictly forbidden, except as permitted       //
// under the General License Agreement.                                       //
//                                                                            //
// If you do not or cannot agree to the terms of this Agreement,              //
// do not use this work.                                                      //
//                                                                            //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid.                         //
//                                                                            //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

use std::{
    cmp::Ordering,
    fmt::{Debug, Display, Formatter},
    hash::{Hash, Hasher},
    ops::{Bound, Range, RangeFrom, RangeInclusive},
};

use compact_str::CompactString;
use lady_deirdre::{
    arena::{Entry, Id, Identifiable},
    lexis::{
        Column,
        Line,
        Position,
        PositionSpan,
        SiteRef,
        SiteRefSpan,
        SiteSpan,
        SourceCode,
        ToSpan,
        TokenRef,
    },
    syntax::PolyRef,
};

use crate::runtime::{Ident, PackageMeta, ScriptIdent};

/// A representation of a Rust or Script source code range.
///
/// The primary purpose of this object is to track data flow points (both
/// Rust and Script points) during script evaluation.
///
/// Typically, you don't need to create this object manually. You receive
/// instances of Origin in runtime trait functions and pass them to the
/// corresponding runtime API functions.
///
/// This Origin object helps the Script Engine generate descriptive
/// [runtime errors](crate::runtime::RuntimeError) if an error occurs during
/// script evaluation. Additionally, its [ScriptOrigin] variant is widely used
/// in the static script code analysis API to represent script source code
/// spans.
///
/// For debugging purposes, you can instantiate the Origin object as
/// [Origin::nil], which intentionally does not point to any source code.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Origin {
    /// A range representing a portion of Rust source code.
    Rust(&'static RustOrigin),

    /// A range representing a portion of Script source code.
    Script(ScriptOrigin),
}

impl Default for Origin {
    #[inline(always)]
    fn default() -> Self {
        Self::nil()
    }
}

impl Debug for Origin {
    #[inline(always)]
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Rust(origin) => Debug::fmt(origin, formatter),
            Self::Script(origin) => Debug::fmt(origin, formatter),
        }
    }
}

impl From<&'static RustOrigin> for Origin {
    #[inline(always)]
    fn from(value: &'static RustOrigin) -> Self {
        Self::Rust(value)
    }
}

impl From<ScriptOrigin> for Origin {
    #[inline(always)]
    fn from(value: ScriptOrigin) -> Self {
        Self::Script(value)
    }
}

impl From<SiteRefSpan> for Origin {
    #[inline(always)]
    fn from(value: SiteRefSpan) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<&'a SiteRefSpan> for Origin {
    #[inline(always)]
    fn from(value: &'a SiteRefSpan) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<SiteRef> for Origin {
    #[inline(always)]
    fn from(value: SiteRef) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<&'a SiteRef> for Origin {
    #[inline(always)]
    fn from(value: &'a SiteRef) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<TokenRef> for Origin {
    #[inline(always)]
    fn from(value: TokenRef) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<&'a TokenRef> for Origin {
    fn from(value: &'a TokenRef) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<Range<TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: Range<TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<Range<&'a TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: Range<&'a TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<RangeInclusive<TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeInclusive<TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<RangeInclusive<&'a TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeInclusive<&'a TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<RangeFrom<TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeFrom<TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<RangeFrom<&'a TokenRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeFrom<&'a TokenRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl From<RangeFrom<SiteRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeFrom<SiteRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl<'a> From<RangeFrom<&'a SiteRef>> for Origin {
    #[inline(always)]
    fn from(value: RangeFrom<&'a SiteRef>) -> Self {
        Self::Script(ScriptOrigin::from(value))
    }
}

impl Origin {
    /// Creates an instance of Origin that intentionally does not point
    /// to any source code. This serves as the Default constructor for this
    /// object.
    ///
    /// Use this function for API debugging purposes, or when the corresponding
    /// Script runtime operation cannot be associated with any Rust or Script
    /// source code.
    #[inline(always)]
    pub fn nil() -> Self {
        Self::Rust(&RustOrigin::nil())
    }

    /// Returns true if this instance is the [Nil Origin](Origin::nil).
    #[inline(always)]
    pub fn is_nil(&self) -> bool {
        match self {
            Self::Rust(origin) => origin.is_nil(),
            Self::Script(origin) => origin.is_nil(),
        }
    }

    /// Attempts to return the [script package](PackageMeta) to which this
    /// source code belongs.
    ///
    /// This function returns `None` if the package cannot be recognized (e.g.,
    /// in the case of the [Nil Origin](Origin::nil)).
    #[inline(always)]
    pub fn package(&self) -> Option<&'static PackageMeta> {
        match self {
            Self::Rust(origin) => origin.package(),
            Self::Script(origin) => origin.package(),
        }
    }

    #[inline(always)]
    pub(crate) fn into_ident(self, string: impl Into<CompactString>) -> Ident {
        match self {
            Self::Rust(..) => Ident::Script(ScriptIdent::from_string(TokenRef::nil(), string)),

            Self::Script(origin) => {
                Ident::Script(ScriptIdent::from_string(origin.into_token_ref(), string))
            }
        }
    }
}

static NIL_RUST_ORIGIN: RustOrigin = RustOrigin {
    package: None,
    code: None,
};

/// A pointer to a specific location in the Rust source code.
///
/// This object points to a particular place in a Rust file and represents the
/// origin of an exported Rust construct or a part of it.
///
/// Typically, you don't need to create this object manually. The
/// [export](crate::export) macro generates static instances of RustOrigin
/// during the introspection of Rust items. These instances are created in
/// static memory by the export system. Therefore, you would generally use
/// `&'static RustOrigin` references wrapped in [Origin].
#[derive(Clone, Copy, PartialOrd, Ord, Hash)]
pub struct RustOrigin {
    /// The name and version of the crate to which the Rust file belongs.
    pub package: Option<(&'static str, &'static str)>,

    /// The actual reference to the Rust file within the crate.
    pub code: Option<RustCode>,
}

impl Default for RustOrigin {
    #[inline(always)]
    fn default() -> Self {
        NIL_RUST_ORIGIN
    }
}

impl PartialEq for RustOrigin {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        match (&self.package, &other.package) {
            (Some(this), Some(other)) => {
                if this.ne(other) {
                    return false;
                }
            }

            _ => return false,
        }

        if let (Some(this), Some(other)) = (&self.code, &other.code) {
            if this.ne(other) {
                return false;
            }
        }

        true
    }
}

impl Eq for RustOrigin {}

impl Debug for RustOrigin {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        if self.package.is_none() && self.code.is_none() {
            return formatter.write_str("RustOrigin(invalid)");
        }

        let mut debug_struct = formatter.debug_struct("RustOrigin");

        if let Some((name, version)) = &self.package {
            debug_struct.field("package", &format_args!("{name}@{version}"));
        }

        if let Some(code) = &self.code {
            debug_struct.field("code", &code);
        }

        debug_struct.finish()
    }
}

impl Display for RustOrigin {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(code) = &self.code {
            return Display::fmt(code, formatter);
        }

        if let Some((name, _)) = self.package {
            return formatter.write_str(name);
        }

        formatter.write_str("[?]")
    }
}

impl RustOrigin {
    /// Returns a RustOrigin that intentionally does not point to any Rust code.
    /// This is the [Default] value of this object.
    #[inline(always)]
    pub fn nil() -> &'static Self {
        &NIL_RUST_ORIGIN
    }

    /// Returns true if this instance is the [Nil RustOrigin](Self::nil).
    pub fn is_nil(&self) -> bool {
        self == &NIL_RUST_ORIGIN
    }

    /// Returns the [script package metadata](PackageMeta) of the crate that
    /// this RustOrigin points to.
    ///
    /// Returns None if the package is not specified or if the crate is not
    /// a script package.
    #[inline(always)]
    pub fn package(&self) -> Option<&'static PackageMeta> {
        if let Some((name, version)) = self.package {
            return PackageMeta::of(name, &format!("={}", version));
        }

        None
    }

    /// This function is guaranteed to panic with the provided `message`.
    ///
    /// Unlike a normal `panic!`, the stack trace for this panic will typically
    /// start from the Rust code that this RustOrigin points to.
    #[inline(never)]
    pub fn blame<T>(&self, message: &str) -> T {
        if let Some(code) = self.code {
            (code.blame_fn)(message);
        }

        match self.package {
            Some((name, _)) => panic!("{}: {}", name, message),
            None => panic!("{}", message),
        }
    }
}

/// A representation of a range within Script source code.
///
/// This object points to specific span within the
/// [ScriptModule](crate::analysis::ScriptModule)'s source code text, often
/// highlighting identifiers and similar syntactic constructs. Generally,
/// it can span arbitrary range of text tokens.
///
/// Unlike absolute ranges (such as `10..20` or
/// `Position::new(1, 4)..Position::new(3, 8)`), the ScriptOrigin range is
/// relative. If you edit the script's text before, after, or inside the
/// referenced range, this range will realign accordingly. However, if the
/// text edits affect the ScriptOrigin bounds directly, the object will
/// become obsolete.
///
/// In general, ScriptOrigin may represent an invalid range or become
/// invalid over time if the source code is edited. You can check the range's
/// validity using the [is_valid_span](ToSpan::is_valid_span) method, passing
/// an instance of [ModuleText](crate::analysis::ModuleText):
/// `script_origin.is_valid_span(&module_text)`.
///
/// Note that ScriptOrigin belongs to a specific ScriptModule instance
/// and is valid only for that module. The [id](Id::id) function of ScriptOrigin
/// returns a globally unique identifier for the script module to which this
/// ScriptOrigin object belongs.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ScriptOrigin {
    id: Id,
    start: Option<Entry>,
    end: Bound<Entry>,
}

impl Default for ScriptOrigin {
    #[inline(always)]
    fn default() -> Self {
        Self::nil()
    }
}

impl PartialOrd for ScriptOrigin {
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ScriptOrigin {
    #[inline(always)]
    fn cmp(&self, other: &Self) -> Ordering {
        match self.id.cmp(&other.id) {
            Ordering::Equal => match self.start.cmp(&other.start) {
                Ordering::Equal => match (&self.end, &other.end) {
                    (Bound::Included(this), Bound::Included(other)) => this.cmp(other),
                    (Bound::Included(..), Bound::Excluded(..)) => Ordering::Less,
                    (Bound::Included(..), Bound::Unbounded) => Ordering::Less,

                    (Bound::Excluded(..), Bound::Included(..)) => Ordering::Greater,
                    (Bound::Excluded(this), Bound::Excluded(other)) => this.cmp(other),
                    (Bound::Excluded(..), Bound::Unbounded) => Ordering::Less,

                    (Bound::Unbounded, Bound::Unbounded) => Ordering::Equal,
                    (Bound::Unbounded, _) => Ordering::Greater,
                },

                other => other,
            },

            other => other,
        }
    }
}

impl Identifiable for ScriptOrigin {
    #[inline(always)]
    fn id(&self) -> Id {
        self.id
    }
}

impl From<SiteRefSpan> for ScriptOrigin {
    #[inline(always)]
    fn from(value: SiteRefSpan) -> Self {
        Self::from(&value)
    }
}

impl<'a> From<&'a SiteRefSpan> for ScriptOrigin {
    #[inline(always)]
    fn from(value: &'a SiteRefSpan) -> Self {
        let id = value.start.id();

        if id.is_nil() || id != value.end.id() {
            return Self::default();
        }

        Self {
            id,

            start: {
                let bound = value.start.token_ref();

                match bound.is_nil() {
                    true => None,
                    false => Some(bound.entry),
                }
            },

            end: {
                let bound = value.end.token_ref();

                match bound.is_nil() {
                    true => Bound::Unbounded,
                    false => Bound::Excluded(bound.entry),
                }
            },
        }
    }
}

impl From<SiteRef> for ScriptOrigin {
    #[inline(always)]
    fn from(value: SiteRef) -> Self {
        Self::from(&value)
    }
}

impl<'a> From<&'a SiteRef> for ScriptOrigin {
    #[inline(always)]
    fn from(value: &'a SiteRef) -> Self {
        let id = value.id();

        if id.is_nil() {
            return Self::default();
        }

        let bound = value.token_ref();

        match bound.id.is_nil() {
            true => Self {
                id,
                start: None,
                end: Bound::Unbounded,
            },

            false => Self {
                id,
                start: Some(bound.entry),
                end: Bound::Excluded(bound.entry),
            },
        }
    }
}

impl From<TokenRef> for ScriptOrigin {
    #[inline(always)]
    fn from(value: TokenRef) -> Self {
        Self::from(&value)
    }
}

impl<'a> From<&'a TokenRef> for ScriptOrigin {
    #[inline(always)]
    fn from(value: &'a TokenRef) -> Self {
        Self {
            id: value.id,
            start: Some(value.entry),
            end: Bound::Included(value.entry),
        }
    }
}

impl From<Range<TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: Range<TokenRef>) -> Self {
        Self::from(&value.start..&value.end)
    }
}

impl<'a> From<Range<&'a TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: Range<&'a TokenRef>) -> Self {
        let id = value.start.id;

        if id.is_nil() || id != value.end.id {
            return Self::default();
        }

        Self {
            id,
            start: Some(value.start.entry),
            end: Bound::Excluded(value.end.entry),
        }
    }
}

impl From<RangeInclusive<TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeInclusive<TokenRef>) -> Self {
        Self::from(value.start()..=value.end())
    }
}

impl<'a> From<RangeInclusive<&'a TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeInclusive<&'a TokenRef>) -> Self {
        let id = value.start().id;

        if id.is_nil() || id != value.end().id {
            return Self::default();
        }

        Self {
            id,
            start: Some(value.start().entry),
            end: Bound::Included(value.end().entry),
        }
    }
}

impl From<RangeFrom<TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeFrom<TokenRef>) -> Self {
        Self::from(&value.start..)
    }
}

impl<'a> From<RangeFrom<&'a TokenRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeFrom<&'a TokenRef>) -> Self {
        let id = value.start.id();

        if id.is_nil() {
            return Self::default();
        }

        Self {
            id: value.start.id,
            start: Some(value.start.entry),
            end: Bound::Unbounded,
        }
    }
}

impl From<RangeFrom<SiteRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeFrom<SiteRef>) -> Self {
        Self::from(&value.start..)
    }
}

impl<'a> From<RangeFrom<&'a SiteRef>> for ScriptOrigin {
    #[inline(always)]
    fn from(value: RangeFrom<&'a SiteRef>) -> Self {
        let id = value.start.id();

        if id.is_nil() {
            return Self::default();
        }

        let bound = value.start.token_ref();

        match bound.id.is_nil() {
            true => Self {
                id,
                start: None,
                end: Bound::Unbounded,
            },

            false => Self {
                id,
                start: Some(bound.entry),
                end: Bound::Unbounded,
            },
        }
    }
}

// Safety: `is_valid_span` falls back to `to_site_span`.
unsafe impl ToSpan for ScriptOrigin {
    #[inline(always)]
    fn to_site_span(&self, code: &impl SourceCode) -> Option<SiteSpan> {
        if self.id != code.id() {
            return None;
        }

        let length = code.length();

        let start = match &self.start {
            Some(entry) => code.get_site(entry)?.min(length),
            None => length,
        };

        let end = match &self.end {
            Bound::Included(entry) => {
                let chunk_length = code.get_length(entry)?;
                let bound = (code.get_site(entry)? + chunk_length).min(length);

                if bound < start {
                    return None;
                }

                bound
            }

            Bound::Excluded(entry) => {
                let bound = code.get_site(entry)?;

                if bound < start {
                    return None;
                }

                bound
            }

            Bound::Unbounded => length,
        };

        Some(start..end)
    }

    #[inline(always)]
    fn is_valid_span(&self, code: &impl SourceCode) -> bool {
        self.to_site_span(code).is_some()
    }
}

impl ScriptOrigin {
    /// Creates an instance of ScriptOrigin that intentionally does not point
    /// to any Script code. This serves as the [Default] constructor for this
    /// object.
    #[inline(always)]
    pub const fn nil() -> Self {
        Self {
            id: Id::nil(),
            start: None,
            end: Bound::Unbounded,
        }
    }

    #[inline(always)]
    pub(crate) fn invalid(id: Id) -> Self {
        Self {
            id,
            start: Some(Entry::nil()),
            end: Bound::Unbounded,
        }
    }

    #[inline(always)]
    pub(crate) fn eoi(id: Id) -> Self {
        Self {
            id,
            start: None,
            end: Bound::Unbounded,
        }
    }

    /// Returns the [script package metadata](PackageMeta) of the
    /// [ScriptModule](crate::analysis::ScriptModule) that this ScriptOrigin
    /// points to.
    ///
    /// Returns None if the package cannot be found, for instance, if the
    /// corresponding ScriptModule has been dropped.
    #[inline(always)]
    pub fn package(&self) -> Option<&'static PackageMeta> {
        PackageMeta::by_id(self.id)
    }

    /// Returns true if this instance is the [Nil ScriptOrigin](Self::nil).
    #[inline(always)]
    pub const fn is_nil(&self) -> bool {
        if self.id.is_nil() {
            return true;
        }

        if let Some(entry) = &self.start {
            if entry.is_nil() {
                return true;
            }
        }

        match &self.end {
            Bound::Included(entry) | Bound::Excluded(entry) => {
                if entry.is_nil() {
                    return true;
                }
            }
            _ => (),
        }

        false
    }

    #[inline(always)]
    pub(crate) fn union(&mut self, other: &Self) {
        self.end = other.end;
    }

    #[inline(always)]
    pub(crate) fn unbound(&mut self) {
        self.end = Bound::Unbounded;
    }

    fn into_token_ref(self) -> TokenRef {
        let Some(entry) = self.start else {
            return TokenRef::nil();
        };

        TokenRef { id: self.id, entry }
    }
}

/// A component of [RustOrigin] that indicates a specific location in the Rust
/// source code.
#[derive(Clone, Copy, PartialOrd, Ord)]
pub struct RustCode {
    /// The name of the Rust module.
    pub module: &'static str,

    /// A one-based line number within the module file.
    pub line: u32,

    /// A one-based column number within a line of the module file.
    pub column: u32,

    /// A function that panics at the location of the pointed Rust construct
    /// with the specified error message.
    pub blame_fn: fn(&str),
}

impl Hash for RustCode {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.module.hash(state);
        self.line.hash(state);
        self.column.hash(state);
    }
}

impl PartialEq for RustCode {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        if self.module.ne(other.module) {
            return false;
        }

        if self.line.ne(&other.line) {
            return false;
        }

        if self.column.ne(&other.column) {
            return false;
        }

        true
    }
}

impl Eq for RustCode {}

impl Debug for RustCode {
    #[inline(always)]
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("RustCode")
            .field("module", &self.module)
            .field("position", &format_args!("{}", self.position()))
            .finish()
    }
}

impl Display for RustCode {
    #[inline(always)]
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter.write_fmt(format_args!("{} [{}]", self.module, self.span_string()))
    }
}

impl RustCode {
    /// Similar to [RustOrigin::blame]. Refer to its documentation for details.
    #[inline(never)]
    pub fn blame(&self, message: &str) {
        (self.blame_fn)(message);
        panic!("{}: {}", self, message);
    }

    /// A helper function that returns a [Position] representation of the line
    /// and column of this RustCode.
    #[inline(always)]
    pub fn position(&self) -> Position {
        Position::new(self.line as Line, self.column as Column)
    }

    /// A helper function that returns a zero-span [Range] of
    /// [positions](Self::position): `code.position()..code.position()`.
    #[inline(always)]
    pub fn span(&self) -> PositionSpan {
        let bound = self.position();

        bound..bound
    }

    /// A helper function that formats the line and column in the canonical
    /// end-user-facing form of `[<line>]:[<column>]`.
    pub fn span_string(&self) -> String {
        format!("{}:{}", self.line, self.column)
    }
}