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
|
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Range;
#[cfg(test)]
use std::cell::Cell;
#[cfg(test)]
thread_local! {
static CMP_SPANS: Cell<bool> = Cell::new(true);
}
/// Annotate a value with a span.
pub trait WithSpan: Sized {
/// Wraps `self` in a `Spanned` with the given span.
fn with_span(self, span: impl Into<Span>) -> Spanned<Self> {
Spanned::new(self, span)
}
}
impl<T> WithSpan for T {}
/// Span offsetting.
pub trait Offset {
/// Offset all spans contained in `Self` by the given position.
fn offset(self, by: Pos) -> Self;
}
/// A vector of spanned values of type `T`.
pub type SpanVec<T> = Vec<Spanned<T>>;
impl<T> Offset for SpanVec<T> {
fn offset(mut self, by: Pos) -> Self {
for spanned in &mut self {
spanned.span = spanned.span.offset(by);
}
self
}
}
/// A value with the span it corresponds to in the source code.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Spanned<T> {
/// The spanned value.
pub v: T,
/// The location in source code of the value.
pub span: Span,
}
impl<T> Spanned<T> {
/// Create a new instance from a value and its span.
pub fn new(v: T, span: impl Into<Span>) -> Self {
Self { v, span: span.into() }
}
/// Create a new instance from a value with the zero span.
pub fn zero(v: T) -> Self {
Self { v, span: Span::ZERO }
}
/// Convert from `&Spanned<T>` to `Spanned<&T>`
pub fn as_ref(&self) -> Spanned<&T> {
Spanned { v: &self.v, span: self.span }
}
/// Map the value using a function while keeping the span.
pub fn map<F, U>(self, f: F) -> Spanned<U>
where
F: FnOnce(T) -> U,
{
Spanned { v: f(self.v), span: self.span }
}
/// Maps the span while keeping the value.
pub fn map_span<F>(mut self, f: F) -> Self
where
F: FnOnce(Span) -> Span,
{
self.span = f(self.span);
self
}
}
impl<T> Spanned<Option<T>> {
/// Swap the spanned and the option.
pub fn transpose(self) -> Option<Spanned<T>> {
let Spanned { v, span } = self;
v.map(|v| v.with_span(span))
}
}
impl<T> Offset for Spanned<T> {
fn offset(self, by: Pos) -> Self {
self.map_span(|span| span.offset(by))
}
}
impl<T: Debug> Debug for Spanned<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.v.fmt(f)?;
if f.alternate() {
f.write_str(" ")?;
self.span.fmt(f)?;
}
Ok(())
}
}
/// Bounds of a slice of source code.
#[derive(Copy, Clone, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Span {
/// The inclusive start position.
pub start: Pos,
/// The inclusive end position.
pub end: Pos,
}
impl Span {
/// The zero span.
pub const ZERO: Self = Self { start: Pos::ZERO, end: Pos::ZERO };
/// Create a new span from start and end positions.
pub fn new(start: impl Into<Pos>, end: impl Into<Pos>) -> Self {
Self { start: start.into(), end: end.into() }
}
/// Create a span including just a single position.
pub fn at(pos: impl Into<Pos> + Copy) -> Self {
Self::new(pos, pos)
}
/// Create a new span with the earlier start and later end position.
pub fn join(self, other: Self) -> Self {
Self {
start: self.start.min(other.start),
end: self.end.max(other.end),
}
}
/// Expand a span by merging it with another span.
pub fn expand(&mut self, other: Self) {
*self = self.join(other)
}
/// Convert to a `Range<usize>` for indexing.
pub fn to_range(self) -> Range<usize> {
self.start.to_usize() .. self.end.to_usize()
}
/// When set to `false` comparisons with `PartialEq` ignore spans.
#[cfg(test)]
pub(crate) fn set_cmp(cmp: bool) {
CMP_SPANS.with(|cell| cell.set(cmp));
}
}
impl Offset for Span {
fn offset(self, by: Pos) -> Self {
Self {
start: self.start.offset(by),
end: self.end.offset(by),
}
}
}
impl Eq for Span {}
impl PartialEq for Span {
fn eq(&self, other: &Self) -> bool {
#[cfg(test)]
if !CMP_SPANS.with(Cell::get) {
return true;
}
self.start == other.start && self.end == other.end
}
}
impl Default for Span {
fn default() -> Self {
Span::ZERO
}
}
impl<T> From<T> for Span
where
T: Into<Pos> + Copy,
{
fn from(pos: T) -> Self {
Self::at(pos)
}
}
impl<T> From<Range<T>> for Span
where
T: Into<Pos>,
{
fn from(range: Range<T>) -> Self {
Self::new(range.start, range.end)
}
}
impl Debug for Span {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "<{:?}-{:?}>", self.start, self.end)
}
}
/// A byte position in source code.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Pos(pub u32);
impl Pos {
/// The zero position.
pub const ZERO: Self = Self(0);
/// Convert to a usize for indexing.
pub fn to_usize(self) -> usize {
self.0 as usize
}
}
impl Offset for Pos {
fn offset(self, by: Self) -> Self {
Pos(self.0 + by.0)
}
}
impl From<u32> for Pos {
fn from(index: u32) -> Self {
Self(index)
}
}
impl From<i32> for Pos {
fn from(index: i32) -> Self {
Self(index as u32)
}
}
impl From<usize> for Pos {
fn from(index: usize) -> Self {
Self(index as u32)
}
}
impl Debug for Pos {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
/// A one-indexed line-column position in source code.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Location {
/// The one-indexed line.
pub line: u32,
/// The one-indexed column.
pub column: u32,
}
impl Location {
/// Create a new location from line and column.
pub fn new(line: u32, column: u32) -> Self {
Self { line, column }
}
}
impl Display for Location {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
impl Debug for Location {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
|