mago_span/
lib.rs

1//! Provides fundamental types for source code location tracking.
2//!
3//! This crate defines the core primitives [`Position`] and [`Span`] used throughout
4//! mago to identify specific locations in source files. It also provides
5//! the generic traits [`HasPosition`] and [`HasSpan`] to abstract over any syntax
6//! tree node or token that has a location.
7
8use std::ops::Range;
9
10use serde::Deserialize;
11use serde::Serialize;
12
13use mago_database::file::FileId;
14use mago_database::file::HasFileId;
15
16/// Represents a specific byte offset within a single source file.
17#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
18#[repr(transparent)]
19pub struct Position {
20    pub offset: u32,
21}
22
23/// Represents a contiguous range of source code within a single file.
24///
25/// A `Span` is defined by a `start` and `end` [`Position`], marking the beginning
26/// (inclusive) and end (exclusive) of a source code segment.
27#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
28pub struct Span {
29    /// The unique identifier of the file this span belongs to.
30    pub file_id: FileId,
31    /// The start position is inclusive, meaning it includes the byte at this position.
32    pub start: Position,
33    /// The end position is exclusive, meaning it does not include the byte at this position.
34    pub end: Position,
35}
36
37/// A trait for types that have a single, defined source position.
38pub trait HasPosition {
39    /// Returns the source position.
40    fn position(&self) -> Position;
41
42    /// A convenience method to get the byte offset of the position.
43    #[inline]
44    fn offset(&self) -> u32 {
45        self.position().offset
46    }
47}
48
49/// A trait for types that cover a span of source code.
50pub trait HasSpan {
51    /// Returns the source span.
52    fn span(&self) -> Span;
53
54    /// A convenience method to get the starting position of the span.
55    fn start_position(&self) -> Position {
56        self.span().start
57    }
58
59    /// A convenience method to get the ending position of the span.
60    fn end_position(&self) -> Position {
61        self.span().end
62    }
63}
64
65impl Position {
66    /// Creates a new `Position` from a byte offset.
67    pub const fn new(offset: u32) -> Self {
68        Self { offset }
69    }
70
71    /// Creates a new `Position` with an offset of zero.
72    pub const fn zero() -> Self {
73        Self { offset: 0 }
74    }
75
76    /// Checks if this position is at the start of a file.
77    pub const fn is_zero(&self) -> bool {
78        self.offset == 0
79    }
80
81    /// Returns a new position moved forward by the given offset.
82    ///
83    /// Uses saturating arithmetic to prevent overflow.
84    pub const fn forward(&self, offset: u32) -> Self {
85        Self { offset: self.offset.saturating_add(offset) }
86    }
87
88    /// Returns a new position moved backward by the given offset.
89    ///
90    /// Uses saturating arithmetic to prevent underflow.
91    pub const fn backward(&self, offset: u32) -> Self {
92        Self { offset: self.offset.saturating_sub(offset) }
93    }
94
95    /// Creates a `Range<u32>` starting at this position's offset with a given length.
96    pub const fn range_for(&self, length: u32) -> Range<u32> {
97        self.offset..self.offset.saturating_add(length)
98    }
99}
100
101impl Span {
102    /// Creates a new `Span` from a start and end position.
103    ///
104    /// # Panics
105    ///
106    /// In debug builds, this will panic if the start and end positions are not
107    /// from the same file (unless one is a dummy position).
108    pub fn new(file_id: FileId, start: Position, end: Position) -> Self {
109        Self { file_id, start, end }
110    }
111
112    /// Creates a "dummy" span with a null file ID.
113    pub fn dummy(start_offset: u32, end_offset: u32) -> Self {
114        Self::new(FileId::zero(), Position::new(start_offset), Position::new(end_offset))
115    }
116
117    /// Creates a new span that starts at the beginning of the first span
118    /// and ends at the conclusion of the second span.
119    pub fn between(start: Span, end: Span) -> Self {
120        start.join(end)
121    }
122
123    /// Creates a new span that encompasses both `self` and `other`.
124    /// The new span starts at `self.start` and ends at `other.end`.
125    pub fn join(self, other: Span) -> Span {
126        Span::new(self.file_id, self.start, other.end)
127    }
128
129    /// Creates a new span that starts at the beginning of this span
130    /// and ends at the specified position.
131    pub fn to_end(&self, end: Position) -> Span {
132        Span::new(self.file_id, self.start, end)
133    }
134
135    /// Creates a new span that starts at the specified position
136    /// and ends at the end of this span.
137    pub fn from_start(&self, start: Position) -> Span {
138        Span::new(self.file_id, start, self.end)
139    }
140
141    /// Creates a new span that is a subspan of this span, defined by the given byte offsets.
142    /// The `start` and `end` parameters are relative to the start of this span.
143    pub fn subspan(&self, start: u32, end: u32) -> Span {
144        Span::new(self.file_id, self.start.forward(start), self.start.forward(end))
145    }
146
147    /// Checks if a position is contained within this span's byte offsets.
148    pub fn contains(&self, position: &impl HasPosition) -> bool {
149        self.has_offset(position.offset())
150    }
151
152    /// Checks if a raw byte offset is contained within this span.
153    pub fn has_offset(&self, offset: u32) -> bool {
154        self.start.offset <= offset && offset <= self.end.offset
155    }
156
157    /// Converts the span to a `Range<u32>` of its byte offsets.
158    pub fn to_range(&self) -> Range<u32> {
159        self.start.offset..self.end.offset
160    }
161
162    /// Converts the span to a `Range<usize>` of its byte offsets.
163    pub fn to_range_usize(&self) -> Range<usize> {
164        let start = self.start.offset as usize;
165        let end = self.end.offset as usize;
166
167        start..end
168    }
169
170    /// Converts the span to a tuple of byte offsets.
171    pub fn to_offset_tuple(&self) -> (u32, u32) {
172        (self.start.offset, self.end.offset)
173    }
174
175    /// Returns the length of the span in bytes.
176    pub fn length(&self) -> u32 {
177        self.end.offset.saturating_sub(self.start.offset)
178    }
179
180    pub fn is_before(&self, other: impl HasPosition) -> bool {
181        self.end.offset <= other.position().offset
182    }
183
184    pub fn is_after(&self, other: impl HasPosition) -> bool {
185        self.start.offset >= other.position().offset
186    }
187}
188
189impl HasPosition for Position {
190    fn position(&self) -> Position {
191        *self
192    }
193}
194
195impl HasSpan for Span {
196    fn span(&self) -> Span {
197        *self
198    }
199}
200
201/// A blanket implementation that allows any `HasSpan` type to also be treated
202/// as a `HasPosition` type, using the span's start as its position.
203impl<T: HasSpan> HasPosition for T {
204    fn position(&self) -> Position {
205        self.start_position()
206    }
207}
208
209impl HasFileId for Span {
210    fn file_id(&self) -> FileId {
211        self.file_id
212    }
213}
214
215/// Ergonomic blanket impl for references.
216impl<T: HasSpan> HasSpan for &T {
217    fn span(&self) -> Span {
218        (*self).span()
219    }
220}
221
222/// Ergonomic blanket impl for boxed values.
223impl<T: HasSpan> HasSpan for Box<T> {
224    fn span(&self) -> Span {
225        self.as_ref().span()
226    }
227}
228
229impl From<Span> for Range<u32> {
230    fn from(span: Span) -> Range<u32> {
231        span.to_range()
232    }
233}
234
235impl From<&Span> for Range<u32> {
236    fn from(span: &Span) -> Range<u32> {
237        span.to_range()
238    }
239}
240
241impl From<Span> for Range<usize> {
242    fn from(span: Span) -> Range<usize> {
243        let start = span.start.offset as usize;
244        let end = span.end.offset as usize;
245
246        start..end
247    }
248}
249
250impl From<&Span> for Range<usize> {
251    fn from(span: &Span) -> Range<usize> {
252        let start = span.start.offset as usize;
253        let end = span.end.offset as usize;
254
255        start..end
256    }
257}
258
259impl From<Position> for u32 {
260    fn from(position: Position) -> u32 {
261        position.offset
262    }
263}
264
265impl From<&Position> for u32 {
266    fn from(position: &Position) -> u32 {
267        position.offset
268    }
269}
270
271impl From<u32> for Position {
272    fn from(offset: u32) -> Self {
273        Position { offset }
274    }
275}
276
277impl std::ops::Add<u32> for Position {
278    type Output = Position;
279
280    fn add(self, rhs: u32) -> Self::Output {
281        self.forward(rhs)
282    }
283}
284
285impl std::ops::Sub<u32> for Position {
286    type Output = Position;
287
288    fn sub(self, rhs: u32) -> Self::Output {
289        self.backward(rhs)
290    }
291}
292
293impl std::ops::AddAssign<u32> for Position {
294    fn add_assign(&mut self, rhs: u32) {
295        self.offset = self.offset.saturating_add(rhs);
296    }
297}
298
299impl std::ops::SubAssign<u32> for Position {
300    /// Moves the position backward in-place.
301    fn sub_assign(&mut self, rhs: u32) {
302        self.offset = self.offset.saturating_sub(rhs);
303    }
304}
305
306impl std::fmt::Display for Position {
307    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
308        write!(f, "{}", self.offset)
309    }
310}
311
312impl std::fmt::Display for Span {
313    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
314        write!(f, "{}..{}", self.start.offset, self.end.offset)
315    }
316}