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)]
10pub struct Span {
11    start: usize,
12    end: usize,
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, end }
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() {
38            None
39        } else {
40            Some(self.start..self.end)
41        }
42    }
43
44    /// Converts this span to a [`std::ops::Range`], without checking if it is
45    /// empty.
46    pub fn range_unchecked(self) -> Range<usize> {
47        self.start..self.end
48    }
49
50    /// Returns a new span that points to this span's start, but has a length of
51    /// 0.
52    pub fn start(&self) -> Span {
53        Span { start: self.start, end: self.start }
54    }
55
56    pub(crate) fn join(self, other: Span) -> Span {
57        match (self.is_empty(), other.is_empty()) {
58            (false, false) => Span {
59                start: usize::min(self.start, other.start),
60                end: usize::max(self.end, other.end),
61            },
62            (false, true) => self,
63            (true, false) => other,
64            (true, true) => Span::empty(),
65        }
66    }
67
68    pub(crate) fn join_unchecked(self, other: Span) -> Span {
69        Span { start: usize::min(self.start, other.start), end: usize::max(self.end, other.end) }
70    }
71}
72
73impl From<Range<usize>> for Span {
74    fn from(Range { start, end }: Range<usize>) -> Self {
75        Span { start, end }
76    }
77}
78
79impl Default for Span {
80    fn default() -> Self {
81        Span::empty()
82    }
83}
84
85impl Display for Span {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(f, "{}..{}", self.start, self.end)
88    }
89}
90
91impl Debug for Span {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        write!(f, "Span({}..{})", self.start, self.end)
94    }
95}