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
use crate::*;
#[allow(type_alias_bounds)]
pub(crate) type Res<T: Scanner> = Result<T::Output, T::Input>;
#[derive(Debug, PartialEq, Clone)]
pub enum Expected<E> {
Any,
End,
Token(E),
Unknown,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Error<E> {
pub(crate) pos: usize,
pub(crate) unexpected: Option<E>,
pub(crate) expected: Expected<E>,
}
impl<E> Error<E> {
pub(crate) fn new(pos: usize, unexpected: Option<E>, expected: Expected<E>) -> Self {
Error {
pos,
unexpected,
expected,
}
}
}
impl<E: std::fmt::Debug> std::fmt::Display for Error<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"unexpected {:?}. expected {:?} at {}",
self.unexpected, self.expected, self.pos
)
}
}
impl<E: std::fmt::Debug> std::error::Error for Error<E> {}