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
use std::{
fmt,
num::{IntErrorKind, ParseIntError},
};
use crate::Span;
pub use crate::lexer::{LexErrorMsg, Token};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
pub kind: ParseErrorKind,
pub span: Span,
}
impl core::fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(std::ops::Range { start, end }) = self.span.range() {
write!(f, "{}\n at {start}..{end}", self.kind)
} else {
self.kind.fmt(f)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseErrorKind {
Multiple(Box<[ParseError]>),
UnknownToken,
LexErrorWithMessage(LexErrorMsg),
KeywordAfterLet(String),
KeywordAfterColon(String),
NonAsciiIdentAfterColon(char),
GroupNameTooLong(usize),
UnexpectedKeyword(String),
Deprecated(DeprecationError),
Expected(&'static str),
LeftoverTokens,
ExpectedToken(Token),
ExpectedCodePointOrChar,
RangeIsNotIncreasing,
UnallowedNot,
UnallowedMultiNot(usize),
LonePipe,
LetBindingExists,
InvalidEscapeInStringAt(usize),
CharString(CharStringError),
CharClass(CharClassError),
CodePoint(CodePointError),
Number(NumberError),
Repetition(RepetitionError),
RecursionLimit,
}
impl ParseErrorKind {
pub fn at(self, span: Span) -> ParseError {
ParseError { kind: self, span }
}
}
impl From<RepetitionError> for ParseErrorKind {
fn from(e: RepetitionError) -> Self {
ParseErrorKind::Repetition(e)
}
}
impl From<CharClassError> for ParseErrorKind {
fn from(e: CharClassError) -> Self {
ParseErrorKind::CharClass(e)
}
}
impl From<DeprecationError> for ParseErrorKind {
fn from(e: DeprecationError) -> Self {
ParseErrorKind::Deprecated(e)
}
}
impl From<NumberError> for ParseErrorKind {
fn from(e: NumberError) -> Self {
ParseErrorKind::Number(e)
}
}
impl std::error::Error for ParseErrorKind {}
impl core::fmt::Display for ParseErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ParseErrorKind::Multiple(_) => writeln!(f, "Multiple parsing errors encountered"),
ParseErrorKind::UnknownToken => write!(f, "Unknown token"),
ParseErrorKind::LexErrorWithMessage(msg) => msg.fmt(f),
ParseErrorKind::KeywordAfterLet(keyword)
| ParseErrorKind::UnexpectedKeyword(keyword)
| ParseErrorKind::KeywordAfterColon(keyword) => {
write!(f, "Unexpected keyword `{keyword}`")
}
&ParseErrorKind::NonAsciiIdentAfterColon(char) => {
let num = char as u32;
write!(f, "Group name contains illegal code point `{char}` (U+{num:04X}). Group names must be ASCII only.")
}
&ParseErrorKind::GroupNameTooLong(len) => {
write!(f, "Group name is too long. It is {len} code points long, but must be at most 32 code points.")
}
ParseErrorKind::Deprecated(deprecation) => deprecation.fmt(f),
ParseErrorKind::Expected(expected) => write!(f, "Expected {expected}"),
ParseErrorKind::LeftoverTokens => {
write!(f, "There are leftover tokens that couldn't be parsed")
}
ParseErrorKind::ExpectedToken(token) => write!(f, "Expected {token}"),
ParseErrorKind::ExpectedCodePointOrChar => {
write!(f, "Expected code point or character")
}
ParseErrorKind::RangeIsNotIncreasing => {
write!(f, "The first number in a range must be smaller than the second")
}
ParseErrorKind::UnallowedNot => write!(f, "This expression can't be negated"),
ParseErrorKind::UnallowedMultiNot(_) => {
write!(f, "An expression can't be negated more than once")
}
ParseErrorKind::LonePipe => write!(f, "A pipe must be followed by an expression"),
ParseErrorKind::LetBindingExists => {
write!(f, "A variable with the same name already exists in this scope")
}
ParseErrorKind::InvalidEscapeInStringAt(_) => {
write!(f, "Unsupported escape sequence in string")
}
ParseErrorKind::CharString(error) => error.fmt(f),
ParseErrorKind::CharClass(error) => error.fmt(f),
ParseErrorKind::CodePoint(error) => error.fmt(f),
ParseErrorKind::Number(error) => error.fmt(f),
ParseErrorKind::Repetition(error) => error.fmt(f),
ParseErrorKind::RecursionLimit => write!(f, "Recursion limit reached"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeprecationError {
CodepointInSet,
CpInSet,
}
impl std::error::Error for DeprecationError {}
impl core::fmt::Display for DeprecationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error = match self {
DeprecationError::CodepointInSet => "`[codepoint]` is deprecated",
DeprecationError::CpInSet => "`[cp]` is deprecated",
};
f.write_str(error)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CharStringError {
Empty,
TooManyCodePoints,
}
impl std::error::Error for CharStringError {}
impl core::fmt::Display for CharStringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error = match self {
CharStringError::Empty => "Strings used in ranges can't be empty",
CharStringError::TooManyCodePoints => {
"Strings used in ranges can only contain 1 code point"
}
};
f.write_str(error)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CharClassError {
Empty,
CaretInGroup,
DescendingRange(char, char),
Invalid,
Unallowed,
UnknownNamedClass {
found: Box<str>,
#[cfg(feature = "suggestions")]
similar: Option<Box<str>>,
},
Negative,
Keyword(String),
}
impl std::error::Error for CharClassError {}
impl core::fmt::Display for CharClassError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CharClassError::Empty => write!(f, "This character class is empty"),
CharClassError::CaretInGroup => write!(f, "`^` is not a valid token"),
&CharClassError::DescendingRange(a, b) => write!(
f,
"Character range must be in increasing order, but it is U+{:04X?} - U+{:04X?}",
a as u32, b as u32
),
CharClassError::Invalid => {
write!(f, "Expected string, range, code point or named character class")
}
CharClassError::Unallowed => {
write!(f, "This combination of character classes is not allowed")
}
CharClassError::UnknownNamedClass { found, .. } => {
write!(f, "Unknown character class `{found}`")
}
CharClassError::Negative => write!(f, "This character class can't be negated"),
CharClassError::Keyword(keyword) => write!(f, "Unexpected keyword `{keyword}`"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodePointError {
Invalid,
}
impl std::error::Error for CodePointError {}
impl core::fmt::Display for CodePointError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error = match self {
CodePointError::Invalid => "This code point is outside the allowed range",
};
f.write_str(error)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum NumberError {
Empty,
InvalidDigit,
TooLarge,
TooSmall,
Zero,
}
impl From<ParseIntError> for NumberError {
fn from(e: ParseIntError) -> Self {
match e.kind() {
IntErrorKind::Empty => NumberError::Empty,
IntErrorKind::InvalidDigit => NumberError::InvalidDigit,
IntErrorKind::PosOverflow => NumberError::TooLarge,
IntErrorKind::NegOverflow => NumberError::TooSmall,
IntErrorKind::Zero => NumberError::Zero,
_ => unimplemented!(),
}
}
}
impl std::error::Error for NumberError {}
impl core::fmt::Display for NumberError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error = match self {
NumberError::Empty => "cannot parse integer from empty string",
NumberError::InvalidDigit => "invalid digit found in string",
NumberError::TooLarge => "number too large",
NumberError::TooSmall => "number too small",
NumberError::Zero => "number would be zero for non-zero type",
};
f.write_str(error)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RepetitionError {
NotAscending,
QmSuffix,
Multi,
}
impl std::error::Error for RepetitionError {}
impl core::fmt::Display for RepetitionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error = match self {
RepetitionError::NotAscending => "Lower bound can't be greater than the upper bound",
RepetitionError::QmSuffix => "Unexpected `?` following a repetition",
RepetitionError::Multi => "Only one repetition allowed",
};
f.write_str(error)
}
}