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
#![warn(rust_2018_idioms)]
#![warn(missing_docs)]
#![allow(deprecated)]
pub mod nom {
pub use nom::{error::ErrorKind, Err, IResult, Needed};
}
pub mod expr;
pub mod literal;
pub mod token;
#[derive(Debug)]
pub enum ErrorKind {
ExactToken(token::Kind, &'static [u8]),
ExactTokens(token::Kind, &'static [&'static str]),
TypedToken(token::Kind),
UnknownIdentifier,
InvalidLiteral,
Partial,
Parser(nom::ErrorKind),
}
impl From<nom::ErrorKind> for ErrorKind {
fn from(k: nom::ErrorKind) -> Self {
ErrorKind::Parser(k)
}
}
impl From<u32> for ErrorKind {
fn from(_: u32) -> Self {
ErrorKind::InvalidLiteral
}
}
#[derive(Debug)]
pub struct Error<I> {
pub input: I,
pub error: ErrorKind,
}
impl<I> From<(I, nom::ErrorKind)> for Error<I> {
fn from(e: (I, nom::ErrorKind)) -> Self {
Self::from((e.0, ErrorKind::from(e.1)))
}
}
impl<I> From<(I, ErrorKind)> for Error<I> {
fn from(e: (I, ErrorKind)) -> Self {
Self {
input: e.0,
error: e.1,
}
}
}
impl<I> ::nom::error::ParseError<I> for Error<I> {
fn from_error_kind(input: I, kind: nom::ErrorKind) -> Self {
Self {
input,
error: kind.into(),
}
}
fn append(_: I, _: nom::ErrorKind, other: Self) -> Self {
other
}
}
trait ToCexprResult<I, O> {
fn to_cexpr_result(self) -> nom::IResult<I, O, Error<I>>;
}
impl<I, O, E> ToCexprResult<I, O> for nom::IResult<I, O, E>
where
Error<I>: From<E>,
{
fn to_cexpr_result(self) -> nom::IResult<I, O, Error<I>> {
match self {
Ok(v) => Ok(v),
Err(nom::Err::Incomplete(n)) => Err(nom::Err::Incomplete(n)),
Err(nom::Err::Error(e)) => Err(nom::Err::Error(e.into())),
Err(nom::Err::Failure(e)) => Err(nom::Err::Failure(e.into())),
}
}
}
pub fn assert_full_parse<'i, I: 'i, O, E>(
result: nom::IResult<&'i [I], O, E>,
) -> nom::IResult<&'i [I], O, Error<&'i [I]>>
where
Error<&'i [I]>: From<E>,
{
match result.to_cexpr_result() {
Ok((rem, output)) => {
if rem.is_empty() {
Ok((rem, output))
} else {
Err(nom::Err::Error((rem, ErrorKind::Partial).into()))
}
}
Err(nom::Err::Incomplete(n)) => Err(nom::Err::Incomplete(n)),
Err(nom::Err::Failure(e)) => Err(nom::Err::Failure(e)),
Err(nom::Err::Error(e)) => Err(nom::Err::Error(e)),
}
}