pomsky_syntax/
span.rs

1use std::{
2    fmt::{Debug, Display},
3    ops::Range,
4};
5
6/// A source code location, marked by the start and end byte offset. If both are
7/// zero, this is considered as "empty" or "missing", and [`Span::range`]
8/// returns `None`.
9#[derive(Copy, Clone, PartialEq, Eq, Hash)]
10pub struct Span {
11    start: u32,
12    end: u32,
13}
14
15impl Span {
16    /// Constructs a new [`Span`]. If both `start` and `end` is 0, this is
17    /// considered as "empty" or "missing"
18    #[must_use]
19    pub fn new(start: usize, end: usize) -> Self {
20        Span { start: start as u32, end: end as u32 }
21    }
22
23    /// Constructs an empty [`Span`].
24    #[must_use]
25    pub fn empty() -> Self {
26        Span { start: 0, end: 0 }
27    }
28
29    /// Returns whether this span is "empty" or "missing".
30    pub fn is_empty(&self) -> bool {
31        self.end == 0
32    }
33
34    /// Converts this span to a [`std::ops::Range`]. If it is empty, `None` is
35    /// returned.
36    pub fn range(self) -> Option<Range<usize>> {
37        if self.is_empty() { None } else { Some(self.start as usize..self.end as usize) }
38    }
39
40    /// Converts this span to a [`std::ops::Range`], without checking if it is
41    /// empty.
42    pub fn range_unchecked(self) -> Range<usize> {
43        self.start as usize..self.end as usize
44    }
45
46    /// Returns a new span that points to this span's start, but has a length of
47    /// 0.
48    pub fn start(&self) -> Span {
49        Span { start: self.start, end: self.start }
50    }
51
52    pub fn join(self, other: Span) -> Span {
53        match (self.is_empty(), other.is_empty()) {
54            (false, false) => Span {
55                start: u32::min(self.start, other.start),
56                end: u32::max(self.end, other.end),
57            },
58            (false, true) => self,
59            (true, false) => other,
60            (true, true) => Span::empty(),
61        }
62    }
63
64    pub(crate) fn join_unchecked(self, other: Span) -> Span {
65        Span { start: u32::min(self.start, other.start), end: u32::max(self.end, other.end) }
66    }
67}
68
69impl From<Range<usize>> for Span {
70    fn from(Range { start, end }: Range<usize>) -> Self {
71        Span { start: start as u32, end: end as u32 }
72    }
73}
74
75impl Default for Span {
76    fn default() -> Self {
77        Span::empty()
78    }
79}
80
81impl Display for Span {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}..{}", self.start, self.end)
84    }
85}
86
87impl Debug for Span {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        write!(f, "Span({}..{})", self.start, self.end)
90    }
91}
92
93#[cfg(feature = "arbitrary")]
94impl arbitrary::Arbitrary<'_> for Span {
95    fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
96        Ok(Span { start: 0, end: 0 })
97    }
98
99    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
100        (0, Some(0))
101    }
102}