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
317
318
319
320
321
322
323
324
use core::ops::{Add, AddAssign};
use crate::utils::{CharLen, Lexeme, PositionedChar, SimpleSpan, human_display::DisplayHuman};
/// An error indicating that an unexpected suffix was found after a valid token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnexpectedSuffix<Char, Knowledge, O = usize> {
token: SimpleSpan<O>,
suffix: Lexeme<Char, O>,
knowledge: Option<Knowledge>,
}
impl<Char, Knowledge, O> UnexpectedSuffix<Char, Knowledge, O> {
/// Creates a new `UnexpectedSuffix` error with the span of the valid token and the unexpected suffix.
///
/// ## Panics
/// - If the suffix span starts before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn new(token: SimpleSpan<O>, suffix: Lexeme<Char, O>) -> Self
where
O: Ord,
{
assert!(
suffix.start_ref() >= token.end_ref(),
"suffix starts before token ends"
);
Self {
token,
suffix,
knowledge: None,
}
}
/// Create a new `UnexpectedSuffix` error from the given token span and character with position.
///
/// ## Panics
/// - If the positioned character's position is before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::from_char(
/// SimpleSpan::new(0, 5),
/// 5,
/// 'x'
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_char(token: SimpleSpan<O>, pos: O, ch: Char) -> Self
where
O: Ord,
{
Self::from_positioned_char(token, PositionedChar::with_position(ch, pos))
}
/// Create a new `UnexpectedSuffix` error from the given token span and character with position.
///
/// ## Panics
/// - If the positioned character's position is before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::from_positioned_char(
/// SimpleSpan::new(0, 5),
/// PositionedChar::with_position('x', 5)
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_positioned_char(token: SimpleSpan<O>, ch: PositionedChar<Char, O>) -> Self
where
O: Ord,
{
Self::new(token, Lexeme::Char(ch))
}
/// Adds knowledge to the `UnexpectedSuffix` error.
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn with_knowledge_const(mut self, knowledge: Knowledge) -> Self
where
Knowledge: Copy,
{
self.knowledge = Some(knowledge);
self
}
/// Adds knowledge to the `UnexpectedSuffix` error.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_knowledge(mut self, knowledge: Knowledge) -> Self {
self.knowledge = Some(knowledge);
self
}
/// Create a new `UnexpectedSuffix` error from the given token span and the suffix span
///
/// ## Panics
/// - If the suffix span starts before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::from_suffix(
/// SimpleSpan::new(0, 5),
/// SimpleSpan::new(5, 10)
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_suffix(token: SimpleSpan<O>, span: SimpleSpan<O>) -> Self
where
O: Ord,
{
Self::new(token, Lexeme::Range(span))
}
/// Returns the full span since the start of the valid token to the end of the unexpected suffix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// assert_eq!(error.span(), SimpleSpan::new(0, 6));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn span(&self) -> SimpleSpan<O>
where
Char: CharLen,
for<'a> &'a O: Add<usize, Output = O>,
O: Clone + Ord,
{
let start = self.token.start_ref();
let end = match &self.suffix {
Lexeme::Char(positioned_char) => {
positioned_char.position_ref() + positioned_char.char_ref().char_len()
}
Lexeme::Range(span) => span.end_ref().clone(),
};
SimpleSpan::new(start.clone(), end)
}
/// Returns the span of the valid token before the unexpected suffix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// assert_eq!(error.token(), SimpleSpan::new(0, 5));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn token(&self) -> SimpleSpan<O>
where
O: Copy,
{
self.token
}
/// Returns the span of the valid token before the unexpected suffix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// assert_eq!(error.token_ref(), SimpleSpan::new(&0, &5));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn token_ref(&self) -> SimpleSpan<&O> {
self.token.as_ref()
}
/// Returns the unexpected suffix found after the valid token.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
///
/// assert_eq!(
/// error.suffix(),
/// &Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn suffix(&self) -> &Lexeme<Char, O> {
&self.suffix
}
/// Consumes the error and returns the unexpected suffix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// let (token, suffix) = error.into_components();
/// assert_eq!(token, SimpleSpan::new(0, 5));
/// assert_eq!(
/// suffix,
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn into_components(self) -> (SimpleSpan<O>, Lexeme<Char, O>) {
(self.token, self.suffix)
}
/// Bumps both the start and end positions of the token span by the given offset.
///
/// This is useful when adjusting error positions after processing or
/// when combining spans from different contexts.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedSuffix};
///
/// let mut error: UnexpectedSuffix<char, ()> = UnexpectedSuffix::new(
/// SimpleSpan::new(0, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 5))
/// );
/// error.bump(10);
/// assert_eq!(error.token(), SimpleSpan::new(10, 15));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn bump(&mut self, offset: &O) -> &mut Self
where
O: for<'a> AddAssign<&'a O> + Clone,
{
self.token.bump(offset);
self
}
}
impl<Char, Knowledge> core::fmt::Display for UnexpectedSuffix<Char, Knowledge>
where
Char: DisplayHuman,
Knowledge: DisplayHuman,
{
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match &self.suffix {
Lexeme::Char(positioned_char) => match &self.knowledge {
None => write!(
f,
"unexpected suffix '{}' at position {} found after {}",
positioned_char.char_ref().display(),
positioned_char.position(),
self.token
),
Some(knowledge) => {
write!(
f,
"unexpected suffix '{}' at position {} found after '{}'@({})",
positioned_char.char_ref().display(),
positioned_char.position(),
knowledge.display(),
self.token
)
}
},
Lexeme::Range(span) => match &self.knowledge {
Some(knowledge) => write!(
f,
"unexpected suffix at {} found after '{}'@({})",
span,
knowledge.display(),
self.token
),
None => write!(
f,
"unexpected suffix at {} found after {}",
span, self.token
),
},
}
}
}
impl<Char, Knowledge> core::error::Error for UnexpectedSuffix<Char, Knowledge>
where
Char: DisplayHuman + core::fmt::Debug,
Knowledge: DisplayHuman + core::fmt::Debug,
{
}