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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use nom::Slice;
use crate::{
alloc::{format, String, ToOwned},
Error,
};
pub type InputSpan<'a> = nom_locate::LocatedSpan<&'a str, ()>;
pub type NomResult<'a, T> = nom::IResult<InputSpan<'a>, T, Error<'a>>;
#[derive(Debug, Clone, Copy)]
pub struct LocatedSpan<Span, T = ()> {
offset: usize,
line: u32,
column: usize,
fragment: Span,
pub extra: T,
}
impl<Span: PartialEq, T> PartialEq for LocatedSpan<Span, T> {
fn eq(&self, other: &Self) -> bool {
self.line == other.line && self.offset == other.offset && self.fragment == other.fragment
}
}
impl<Span, T> LocatedSpan<Span, T> {
pub fn location_offset(&self) -> usize {
self.offset
}
pub fn location_line(&self) -> u32 {
self.line
}
pub fn get_column(&self) -> usize {
self.column
}
pub fn fragment(&self) -> &Span {
&self.fragment
}
pub fn map_extra<U>(self, map_fn: impl FnOnce(T) -> U) -> LocatedSpan<Span, U> {
LocatedSpan {
offset: self.offset,
line: self.line,
column: self.column,
fragment: self.fragment,
extra: map_fn(self.extra),
}
}
pub fn map_fragment<U>(self, map_fn: impl FnOnce(Span) -> U) -> LocatedSpan<U, T> {
LocatedSpan {
offset: self.offset,
line: self.line,
column: self.column,
fragment: map_fn(self.fragment),
extra: self.extra,
}
}
}
impl<Span: Copy, T> LocatedSpan<Span, T> {
pub fn as_ref(&self) -> LocatedSpan<Span, &T> {
LocatedSpan {
offset: self.offset,
line: self.line,
column: self.column,
fragment: self.fragment,
extra: &self.extra,
}
}
pub fn copy_with_extra<U>(&self, value: U) -> LocatedSpan<Span, U> {
LocatedSpan {
offset: self.offset,
line: self.line,
column: self.column,
fragment: self.fragment,
extra: value,
}
}
pub fn with_no_extra(&self) -> LocatedSpan<Span> {
self.copy_with_extra(())
}
}
impl<'a, T> From<nom_locate::LocatedSpan<&'a str, T>> for LocatedSpan<&'a str, T> {
fn from(value: nom_locate::LocatedSpan<&'a str, T>) -> Self {
Self {
offset: value.location_offset(),
line: value.location_line(),
column: value.get_column(),
fragment: *value.fragment(),
extra: value.extra,
}
}
}
pub type Spanned<'a, T = ()> = LocatedSpan<&'a str, T>;
impl<'a, T> Spanned<'a, T> {
pub(crate) fn new(span: InputSpan<'a>, extra: T) -> Self {
Self {
offset: span.location_offset(),
line: span.location_line(),
column: span.get_column(),
fragment: *span.fragment(),
extra,
}
}
}
impl<'a> Spanned<'a> {
pub fn from_str<R>(code: &'a str, range: R) -> Self
where
InputSpan<'a>: Slice<R>,
{
let input = InputSpan::new(code);
Self::new(input.slice(range), ())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CodeFragment<'a> {
Str(&'a str),
Stripped(usize),
}
impl PartialEq<&str> for CodeFragment<'_> {
fn eq(&self, &other: &&str) -> bool {
match self {
Self::Str(string) => *string == other,
Self::Stripped(_) => false,
}
}
}
impl CodeFragment<'_> {
pub fn strip(self) -> CodeFragment<'static> {
match self {
Self::Str(string) => CodeFragment::Stripped(string.len()),
Self::Stripped(len) => CodeFragment::Stripped(len),
}
}
pub fn len(self) -> usize {
match self {
Self::Str(string) => string.len(),
Self::Stripped(len) => len,
}
}
pub fn is_empty(self) -> bool {
self.len() == 0
}
}
impl<'a> From<&'a str> for CodeFragment<'a> {
fn from(value: &'a str) -> Self {
CodeFragment::Str(value)
}
}
pub type MaybeSpanned<'a, T = ()> = LocatedSpan<CodeFragment<'a>, T>;
impl<'a> MaybeSpanned<'a> {
pub fn from_str<R>(code: &'a str, range: R) -> Self
where
InputSpan<'a>: Slice<R>,
{
Spanned::from_str(code, range).into()
}
}
impl<T> MaybeSpanned<'_, T> {
pub fn code_or_location(&self, default_name: &str) -> String {
match self.fragment {
CodeFragment::Str(code) => code.to_owned(),
CodeFragment::Stripped(_) => {
format!("{} at {}:{}", default_name, self.line, self.column)
}
}
}
}
impl<'a, T> From<Spanned<'a, T>> for MaybeSpanned<'a, T> {
fn from(value: Spanned<'a, T>) -> Self {
value.map_fragment(CodeFragment::from)
}
}
pub trait StripCode {
type Stripped: 'static;
fn strip_code(self) -> Self::Stripped;
}
impl<T: Clone + 'static> StripCode for MaybeSpanned<'_, T> {
type Stripped = MaybeSpanned<'static, T>;
fn strip_code(self) -> Self::Stripped {
self.map_fragment(CodeFragment::strip)
}
}
pub fn with_span<'a, O>(
mut parser: impl FnMut(InputSpan<'a>) -> NomResult<'a, O>,
) -> impl FnMut(InputSpan<'a>) -> NomResult<'a, Spanned<'_, O>> {
move |input: InputSpan<'_>| {
parser(input).map(|(rest, output)| {
let len = rest.location_offset() - input.location_offset();
let spanned = Spanned {
offset: input.location_offset(),
line: input.location_line(),
column: input.get_column(),
fragment: &input.fragment()[..len],
extra: output,
};
(rest, spanned)
})
}
}
pub(crate) fn unite_spans<'a, T, U>(
input: InputSpan<'a>,
start: &Spanned<'_, T>,
end: &Spanned<'_, U>,
) -> Spanned<'a> {
debug_assert!(input.location_offset() <= start.location_offset());
debug_assert!(start.location_offset() <= end.location_offset());
debug_assert!(
input.location_offset() + input.fragment().len()
>= end.location_offset() + end.fragment().len()
);
let start_idx = start.location_offset() - input.location_offset();
let end_idx = end.location_offset() + end.fragment().len() - input.location_offset();
Spanned {
offset: start.offset,
line: start.line,
column: start.column,
fragment: &input.fragment()[start_idx..end_idx],
extra: (),
}
}
pub trait StripResultExt {
type Ok;
type StrippedErr: 'static;
fn strip_err(self) -> Result<Self::Ok, Self::StrippedErr>;
}
impl<T, E: StripCode> StripResultExt for Result<T, E> {
type Ok = T;
type StrippedErr = E::Stripped;
fn strip_err(self) -> Result<T, Self::StrippedErr> {
self.map_err(StripCode::strip_code)
}
}