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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use core::ops::{Add, AddAssign};
use crate::utils::{CharLen, Lexeme, PositionedChar, SimpleSpan, human_display::DisplayHuman};
/// An error indicating that an unexpected prefix was found after a valid token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnexpectedPrefix<Char, Knowledge, O = usize> {
token: SimpleSpan<O>,
prefix: Lexeme<Char, O>,
knowledge: Option<Knowledge>,
}
impl<Char, Knowledge, O> UnexpectedPrefix<Char, Knowledge, O> {
/// Create a new `UnexpectedPrefix` error indicating a leading zero was found.
///
/// ## Panics
/// - If the positioned character's position is before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, knowledge::IntLiteral}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, IntLiteral> = UnexpectedPrefix::leading_zero(
/// SimpleSpan::new(1, 5),
/// 0,
/// '0'
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn leading_zero(token: SimpleSpan<O>, pos: O, ch: Char) -> Self
where
Knowledge: DenyLeadingZero,
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
Self::from_char(token, pos, ch).with_knowledge(Knowledge::INIT)
}
/// Create a new `UnexpectedPrefix` error from the given token span and the prefix span
///
/// ## Panics
/// - If the prefix span starts before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, knowledge::IntLiteral}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, IntLiteral> = UnexpectedPrefix::leading_zeros(
/// SimpleSpan::new(6, 10),
/// SimpleSpan::new(0, 6)
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn leading_zeros(token: SimpleSpan<O>, span: SimpleSpan<O>) -> Self
where
Knowledge: DenyLeadingZero,
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
Self::from_prefix(token, span).with_knowledge(Knowledge::INIT)
}
}
impl<Char, Knowledge, O> UnexpectedPrefix<Char, Knowledge, O> {
/// Creates a new `UnexpectedPrefix` error with the span of the valid token and the unexpected prefix.
///
/// ## Panics
/// - If the prefix span ends after the token span starts.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn new(token: SimpleSpan<O>, prefix: Lexeme<Char, O>) -> Self
where
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
assert!(
prefix.end().le(token.start_ref()),
"prefix ends after token starts"
);
Self {
token,
prefix,
knowledge: None,
}
}
/// Create a new `UnexpectedPrefix` 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::{PositionedChar, SimpleSpan}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::from_char(
/// SimpleSpan::new(1, 5),
/// 0,
/// 'x'
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_char(token: SimpleSpan<O>, pos: O, ch: Char) -> Self
where
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
Self::from_positioned_char(token, PositionedChar::with_position(ch, pos))
}
/// Create a new `UnexpectedPrefix` 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::{PositionedChar, SimpleSpan}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::from_positioned_char(
/// SimpleSpan::new(1, 5),
/// PositionedChar::with_position('x', 0)
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_positioned_char(token: SimpleSpan<O>, ch: PositionedChar<Char, O>) -> Self
where
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
Self::new(token, Lexeme::Char(ch))
}
/// Adds knowledge to the `UnexpectedPrefix` 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 `UnexpectedPrefix` error.
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_knowledge(mut self, knowledge: Knowledge) -> Self {
self.knowledge = Some(knowledge);
self
}
/// Create a new `UnexpectedPrefix` error from the given token span and the prefix span
///
/// ## Panics
/// - If the prefix span starts before the token span ends.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::from_prefix(
/// SimpleSpan::new(6, 10),
/// SimpleSpan::new(0, 6)
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_prefix(token: SimpleSpan<O>, span: SimpleSpan<O>) -> Self
where
Char: CharLen,
O: Clone + Ord,
for<'a> &'a O: Add<usize, Output = O>,
{
Self::new(token, Lexeme::Range(span))
}
/// Returns the full span since the start of the unexpected prefix to the end of the valid token.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// assert_eq!(error.span(), SimpleSpan::new(0, 5));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn span(&self) -> SimpleSpan<O>
where
Char: CharLen,
O: Clone + Ord,
{
let end = self.token.end_ref();
let start = match &self.prefix {
Lexeme::Char(positioned_char) => positioned_char.position_ref().clone(),
Lexeme::Range(span) => span.start_ref().clone(),
};
SimpleSpan::new(start, end.clone())
}
/// Returns the span of the valid token before the unexpected prefix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// assert_eq!(error.token(), SimpleSpan::new(1, 5));
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn token(&self) -> SimpleSpan<O>
where
O: Copy,
{
self.token
}
/// Returns the unexpected prefix found before the valid token.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
///
/// assert_eq!(
/// error.prefix(),
/// &Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn prefix(&self) -> &Lexeme<Char, O> {
&self.prefix
}
/// Consumes the error and returns the unexpected prefix.
///
/// ## Examples
///
/// ```rust
/// use tokit::{utils::{SimpleSpan, Lexeme, PositionedChar}, error::UnexpectedPrefix};
///
/// let error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// let (token, prefix) = error.into_components();
/// assert_eq!(token, SimpleSpan::new(1, 5));
/// assert_eq!(
/// prefix,
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// ```
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn into_components(self) -> (SimpleSpan<O>, Lexeme<Char, O>) {
(self.token, self.prefix)
}
/// 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::UnexpectedPrefix};
///
/// let mut error: UnexpectedPrefix<char, ()> = UnexpectedPrefix::new(
/// SimpleSpan::new(1, 5),
/// Lexeme::Char(PositionedChar::with_position('x', 0))
/// );
/// error.bump(10);
/// assert_eq!(error.token(), SimpleSpan::new(11, 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 UnexpectedPrefix<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.prefix {
Lexeme::Char(positioned_char) => match &self.knowledge {
None => write!(
f,
"unexpected prefix '{}' at position {} found before {}",
positioned_char.char_ref().display(),
positioned_char.position(),
self.token
),
Some(knowledge) => {
write!(
f,
"unexpected prefix '{}' at position {} found before '{}'@({})",
positioned_char.char_ref().display(),
positioned_char.position(),
knowledge.display(),
self.token
)
}
},
Lexeme::Range(span) => match &self.knowledge {
Some(knowledge) => write!(
f,
"unexpected prefix at {} found before '{}'@({})",
span,
knowledge.display(),
self.token
),
None => write!(
f,
"unexpected prefix at {} found before {}",
span, self.token
),
},
}
}
}
impl<Char, Knowledge> core::error::Error for UnexpectedPrefix<Char, Knowledge>
where
Char: DisplayHuman + core::fmt::Debug,
Knowledge: DisplayHuman + core::fmt::Debug,
{
}
/// A marker trait indicating that leading zeros are not allowed for the implementing knowledge type.
pub trait DenyLeadingZero: sealed::Sealed {}
impl<T> DenyLeadingZero for T where T: sealed::Sealed {}
mod sealed {
use crate::utils::knowledge::{
BinaryLiteral, FloatLiteral, HexFloatLiteral, HexLiteral, IntLiteral, OctalLiteral,
};
pub trait Sealed {
const INIT: Self;
}
impl Sealed for FloatLiteral {
const INIT: Self = FloatLiteral(());
}
impl Sealed for HexFloatLiteral {
const INIT: Self = HexFloatLiteral(());
}
impl Sealed for IntLiteral {
const INIT: Self = IntLiteral(());
}
impl Sealed for HexLiteral {
const INIT: Self = HexLiteral(());
}
impl Sealed for BinaryLiteral {
const INIT: Self = BinaryLiteral(());
}
impl Sealed for OctalLiteral {
const INIT: Self = OctalLiteral(());
}
}