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
use std::num::ParseIntError;
use std::ops::Range;
use codespan_reporting::diagnostic::{Diagnostic, Label};
use nom::error::{ErrorKind as NomErrorKind, ParseError};
use super::types::Input;
#[derive(Clone, Debug)]
pub struct Error {
span: Range<usize>,
kind: ErrorKind,
}
impl Error {
#[must_use]
pub(crate) fn new(span: Range<usize>, kind: ErrorKind) -> Self {
Self { span, kind }
}
#[must_use]
pub fn to_diagnostic(&self) -> Diagnostic<()> {
match &self.kind {
ErrorKind::Base64AlphabetInvalidLength { length } => Diagnostic::error()
.with_message("base64 modifier alphabet must contain exactly 64 characters")
.with_labels(vec![Label::primary((), self.span.clone())
.with_message(format!("this contains {} characters", length))]),
ErrorKind::Base64AlphabetIncompatible => Diagnostic::error()
.with_message("alphabets used for base64 and base64wide must be identical")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::RegexClassRangeInvalid => Diagnostic::error()
.with_message("invalid regex class range, start must be <= to end")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::RegexNonAsciiByte => Diagnostic::error()
.with_message("regex should only contain ascii bytes")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::RegexRangeInvalid => Diagnostic::error()
.with_message("invalid regex range, start must be <= to end")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::JumpAtBound => Diagnostic::error()
.with_message("a list of tokens cannot start or end with a jump")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::JumpEmpty => Diagnostic::error()
.with_message("jump cannot have a length of 0")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::JumpRangeInvalid { from, to } => Diagnostic::error()
.with_message(format!("invalid range for the jump: {} > {}", from, to))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::JumpTooBigInAlternation { limit } => Diagnostic::error()
.with_message(format!(
"jumps over {} not allowed inside alternations (|)",
limit
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::JumpUnboundedInAlternation => Diagnostic::error()
.with_message("unbounded jumps not allowed inside alternations (|)")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::ModifiersDuplicated { modifier_name } => Diagnostic::error()
.with_message(format!(
"string modifier {} appears multiple times",
modifier_name
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::ModifiersIncompatible {
first_modifier_name,
second_modifier_name,
} => Diagnostic::error()
.with_message(format!(
"string modifiers {} and {} are incompatible",
first_modifier_name, second_modifier_name,
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::MulOverflow { left, right } => Diagnostic::error()
.with_message(format!("multiplication {} * {} overflows", left, right))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::NomError(_) => Diagnostic::error()
.with_message("syntax error")
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::StrToIntError(err) => Diagnostic::error()
.with_message(format!("error converting to integer: {}", err))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::StrToHexIntError(err) => Diagnostic::error()
.with_message(format!(
"error converting hexadecimal notation to integer: {}",
err
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::StrToOctIntError(err) => Diagnostic::error()
.with_message(format!(
"error converting octal notation to integer: {}",
err
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::XorRangeInvalidValue { value } => Diagnostic::error()
.with_message(format!(
"xor range value {} invalid, must be in [0-255]",
value
))
.with_labels(vec![Label::primary((), self.span.clone())]),
ErrorKind::XorRangeInvalid { from, to } => Diagnostic::error()
.with_message(format!("xor range invalid: {} > {}", from, to))
.with_labels(vec![Label::primary((), self.span.clone())]),
}
}
fn from_nom_error_kind(position: usize, kind: NomErrorKind) -> Self {
Self {
span: position..(position + 1),
kind: ErrorKind::NomError(kind),
}
}
}
impl ParseError<Input<'_>> for Error {
fn from_error_kind(input: Input, kind: NomErrorKind) -> Self {
Self::from_nom_error_kind(input.get_position(), kind)
}
fn append(_: Input, _: NomErrorKind, other: Self) -> Self {
other
}
}
#[derive(Clone, Debug)]
pub enum ErrorKind {
Base64AlphabetInvalidLength { length: usize },
Base64AlphabetIncompatible,
JumpAtBound,
JumpEmpty,
JumpRangeInvalid { from: u32, to: u32 },
JumpTooBigInAlternation {
limit: u32,
},
JumpUnboundedInAlternation,
ModifiersDuplicated {
modifier_name: String,
},
ModifiersIncompatible {
first_modifier_name: String,
second_modifier_name: String,
},
MulOverflow { left: i64, right: i64 },
NomError(NomErrorKind),
RegexClassRangeInvalid,
RegexNonAsciiByte,
RegexRangeInvalid,
StrToIntError(ParseIntError),
StrToHexIntError(ParseIntError),
StrToOctIntError(ParseIntError),
XorRangeInvalidValue { value: i64 },
XorRangeInvalid { from: u8, to: u8 },
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::test_public_type;
#[test]
fn test_public_types() {
test_public_type(Error::new(0..3, ErrorKind::JumpEmpty));
}
}