mago_span/
lib.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
use std::ops::Range;

use mago_source::HasSource;
use serde::Deserialize;
use serde::Serialize;

use mago_source::SourceIdentifier;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct Position {
    pub source: SourceIdentifier,
    pub offset: usize,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct Span {
    pub start: Position,
    pub end: Position,
}

pub trait HasPosition {
    fn position(&self) -> Position;

    fn offset(&self) -> usize {
        self.position().offset
    }
}

pub trait HasSpan {
    fn span(&self) -> Span;

    fn start_position(&self) -> Position {
        self.span().start
    }

    fn end_position(&self) -> Position {
        self.span().end
    }
}

impl Position {
    pub fn new(source: SourceIdentifier, offset: usize) -> Self {
        Self { source, offset }
    }

    pub fn dummy(offset: usize) -> Self {
        Self::new(SourceIdentifier::dummy(), offset)
    }

    pub fn start_of(source: SourceIdentifier) -> Self {
        Self::new(source, 0)
    }

    /// Return the position moved by the given offset.
    ///
    /// # Parameters
    ///
    /// - `offset`: The offset to move the position by.
    ///
    /// # Returns
    ///
    /// The position moved by the given offset.
    pub fn forward(&self, offset: usize) -> Self {
        Self { source: self.source, offset: self.offset + offset }
    }

    /// Return the position moved back by the given offset.
    ///
    /// # Parameters
    ///
    /// - `offset`: The offset to move the position back by.
    ///
    /// # Returns
    ///
    /// The position moved back by the given offset.
    pub fn backward(&self, offset: usize) -> Self {
        Self { source: self.source, offset: self.offset - offset }
    }

    pub fn range_for(&self, length: usize) -> Range<usize> {
        self.offset..self.offset + length
    }
}

impl Span {
    pub fn new(start: Position, end: Position) -> Self {
        debug_assert!(start.source == end.source, "span start and end must be in the same file");

        Self { start, end }
    }

    pub fn between(start: Span, end: Span) -> Self {
        start.join(end)
    }

    pub fn join(self, other: Span) -> Span {
        Span::new(self.start, other.end)
    }

    pub fn contains(&self, position: &impl HasPosition) -> bool {
        self.has_offset(position.offset())
    }

    pub fn has_offset(&self, offset: usize) -> bool {
        self.start.offset <= offset && offset <= self.end.offset
    }

    pub fn to_range(&self) -> Range<usize> {
        self.start.offset..self.end.offset
    }

    pub fn length(&self) -> usize {
        self.end.offset - self.start.offset
    }

    pub fn subspan(&self, start: usize, end: usize) -> Span {
        Span::new(self.start.forward(start), self.start.forward(end))
    }
}

impl HasPosition for Position {
    fn position(&self) -> Position {
        *self
    }
}

impl HasSource for Position {
    fn source(&self) -> SourceIdentifier {
        self.source
    }
}

impl HasSpan for Span {
    fn span(&self) -> Span {
        *self
    }
}

impl<T> HasSpan for &T
where
    T: HasSpan,
{
    fn span(&self) -> Span {
        (*self).span()
    }
}

impl<T: HasSpan> HasPosition for T {
    fn position(&self) -> Position {
        self.start_position()
    }
}

impl HasSource for Span {
    fn source(&self) -> SourceIdentifier {
        self.start.source
    }
}

impl HasSource for dyn HasPosition {
    fn source(&self) -> SourceIdentifier {
        self.position().source()
    }
}

impl HasSource for dyn HasSpan {
    fn source(&self) -> SourceIdentifier {
        self.span().source()
    }
}

impl<T: HasSpan> HasSpan for Box<T> {
    fn span(&self) -> Span {
        self.as_ref().span()
    }
}

impl From<Position> for usize {
    fn from(position: Position) -> usize {
        position.offset
    }
}

impl From<&Position> for usize {
    fn from(position: &Position) -> usize {
        position.offset
    }
}

impl From<Span> for Range<usize> {
    fn from(span: Span) -> Range<usize> {
        Range { start: span.start.into(), end: span.end.into() }
    }
}

impl From<&Span> for Range<usize> {
    fn from(span: &Span) -> Range<usize> {
        Range { start: span.start.into(), end: span.end.into() }
    }
}

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

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

impl From<Position> for SourceIdentifier {
    fn from(position: Position) -> SourceIdentifier {
        position.source
    }
}

impl From<&Position> for SourceIdentifier {
    fn from(position: &Position) -> SourceIdentifier {
        position.source
    }
}

impl From<Span> for SourceIdentifier {
    fn from(span: Span) -> SourceIdentifier {
        span.start.source
    }
}

impl From<&Span> for SourceIdentifier {
    fn from(span: &Span) -> SourceIdentifier {
        span.start.source
    }
}