rune/ast/
span.rs

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
use core::cmp;
use core::fmt;
use core::ops;

use serde::{Deserialize, Serialize};

use crate::ast::prelude::*;

/// A span corresponding to a range in the source file being parsed.
#[derive(Default, TryClone, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[try_clone(copy)]
pub struct Span {
    /// The start of the span in bytes.
    pub start: ByteIndex,
    /// The end of the span in bytes.
    pub end: ByteIndex,
}

impl Span {
    /// Construct a new span.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast::Span;
    ///
    /// let span = Span::new(42, 50);
    /// assert!(span < Span::new(100, 101));
    /// ```
    pub fn new<S, E>(start: S, end: E) -> Self
    where
        S: TryInto<ByteIndex>,
        S::Error: fmt::Debug,
        E: TryInto<ByteIndex>,
        E::Error: fmt::Debug,
    {
        Self {
            start: start.try_into().expect("start out of bounds"),
            end: end.try_into().expect("end out of bounds"),
        }
    }

    /// Get a span corresponding to a single point where both start and end are
    /// the same byte offset.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast::Span;
    ///
    /// assert_eq!(Span::point(42), Span::new(42, 42));
    /// ```
    pub fn point<P>(pos: P) -> Self
    where
        P: TryInto<ByteIndex>,
        P::Error: fmt::Debug,
    {
        let pos = pos.try_into().expect("point out of bounds");

        Self {
            start: pos,
            end: pos,
        }
    }

    /// Constant function to build an empty span.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast::Span;
    ///
    /// assert_eq!(Span::empty(), Span::new(0, 0));
    /// ```
    pub const fn empty() -> Self {
        Self {
            start: ByteIndex(0),
            end: ByteIndex(0),
        }
    }

    /// Get the head of the span.
    pub fn head(self) -> Self {
        Self {
            start: self.start,
            end: self.start,
        }
    }

    /// Get the tail of the span.
    pub fn tail(self) -> Self {
        Self {
            start: self.end,
            end: self.end,
        }
    }

    /// Join two spans creating the larger of the two spans.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast::Span;
    ///
    /// let a = Span::new(10, 12);
    /// let b = Span::new(20, 22);
    ///
    /// assert_eq!(a.join(b), Span::new(10, 22));
    /// ```
    pub fn join(self, other: Self) -> Self {
        Self {
            start: ByteIndex::min(self.start, other.start),
            end: ByteIndex::max(self.end, other.end),
        }
    }

    /// Narrow the span with the given amount.
    ///
    /// If the narrowing causes the span to become empty, the resulting span
    /// will reflect the starting point of the narrowed span.
    ///
    /// # Examples
    ///
    /// ```
    /// use rune::ast::Span;
    ///
    /// assert_eq!(Span::new(10, 12).narrow(4), Span::new(10, 10));
    /// assert_eq!(Span::new(5, 15).narrow(2), Span::new(7, 13));
    /// ```
    pub fn narrow(self, amount: impl Into<ByteIndex>) -> Self {
        let amount = amount.into();
        let end = ByteIndex::max(self.start, self.end.saturating_sub(amount));
        let start = ByteIndex::min(self.start.saturating_add(amount), end);
        Self { start, end }
    }

    /// Get the span as a range of usize.
    ///
    /// # Panics
    ///
    /// Panics if the span contains ranges which cannot be faithfully
    /// represented in an [usize].
    pub fn range(self) -> ops::Range<usize> {
        ops::Range {
            start: usize::try_from(self.start.0).expect("start index out of bounds"),
            end: usize::try_from(self.end.0).expect("end index out of bounds"),
        }
    }

    /// Trim the start of the span by the given amount.
    pub(crate) fn trim_start(self, amount: impl Into<ByteIndex>) -> Self {
        let amount = amount.into();

        Self {
            start: ByteIndex::min(self.start.saturating_add(amount), self.end),
            end: self.end,
        }
    }

    /// Trim the end of the span by the given amount.
    pub(crate) fn trim_end(self, amount: impl Into<ByteIndex>) -> Self {
        let amount = amount.into();

        Self {
            start: self.start,
            end: ByteIndex::max(self.end.saturating_sub(amount), self.start),
        }
    }
}

impl Serialize for Span {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        (self.start, self.end).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Span {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let (start, end) = <(ByteIndex, ByteIndex)>::deserialize(deserializer)?;
        Ok(Self { start, end })
    }
}

impl fmt::Display for Span {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{}:{}", self.start, self.end)
    }
}

impl fmt::Debug for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.start, self.end)
    }
}

/// A single index in a [Span], like the start or ending index.
#[derive(
    Default, TryClone, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[repr(transparent)]
#[serde(transparent)]
#[try_clone(copy)]
pub struct ByteIndex(#[doc(hidden)] pub u32);

impl ByteIndex {
    /// Convert a byte index into a usize.
    ///
    /// # Panics
    ///
    /// Panics if the byte index contains values which cannot be faithfully
    /// represented in an [usize].
    pub fn into_usize(self) -> usize {
        usize::try_from(self.0).expect("byte index out of range")
    }

    fn min(a: Self, b: Self) -> Self {
        Self(u32::min(a.0, b.0))
    }

    fn max(a: Self, b: Self) -> Self {
        Self(u32::max(a.0, b.0))
    }

    pub(crate) fn saturating_sub(self, other: Self) -> Self {
        Self(self.0.saturating_sub(other.0))
    }

    fn saturating_add(self, other: Self) -> Self {
        Self(self.0.saturating_add(other.0))
    }
}

impl From<u32> for ByteIndex {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl TryFrom<usize> for ByteIndex {
    type Error = <usize as TryFrom<u32>>::Error;

    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Ok(Self(u32::try_from(value)?))
    }
}

impl TryFrom<i32> for ByteIndex {
    type Error = <i32 as TryFrom<u32>>::Error;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        Ok(Self(u32::try_from(value)?))
    }
}

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

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

impl cmp::PartialEq<usize> for ByteIndex {
    fn eq(&self, other: &usize) -> bool {
        match u32::try_from(*other) {
            Ok(other) => self.0 == other,
            Err(..) => false,
        }
    }
}

impl cmp::PartialEq<u32> for ByteIndex {
    fn eq(&self, other: &u32) -> bool {
        self.0 == *other
    }
}