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
use std::error::Error;
use std::fmt;
use std::io;
use std::num::{ParseFloatError, ParseIntError};
#[derive(Debug)]
pub struct ScanError {
pub at: ScanErrorAt,
pub kind: ScanErrorKind,
_priv: (),
}
impl ScanError {
pub fn new(at: usize, kind: ScanErrorKind) -> Self {
ScanError {
at: ScanErrorAt { bytes: at },
kind: kind,
_priv: (),
}
}
pub fn expected_end() -> Self {
Self::new(0, ScanErrorKind::ExpectedEnd)
}
pub fn float(err: ParseFloatError) -> Self {
Self::new(0, ScanErrorKind::Float(err))
}
pub fn int(err: ParseIntError) -> Self {
Self::new(0, ScanErrorKind::Int(err))
}
pub fn io(err: io::Error) -> Self {
Self::new(0, ScanErrorKind::Io(err))
}
pub fn literal_mismatch() -> Self {
Self::new(0, ScanErrorKind::LiteralMismatch)
}
pub fn syntax(desc: &'static str) -> Self {
Self::new(0, ScanErrorKind::Syntax(desc))
}
pub fn syntax_no_message() -> Self {
Self::new(0, ScanErrorKind::SyntaxNoMessage)
}
pub fn other<E: Into<Box<Error>>>(err: E) -> Self {
Self::new(0, ScanErrorKind::from_other(err))
}
pub fn furthest_along(self, other: Self) -> Self {
if self.at.offset() >= other.at.offset() {
self
} else {
other
}
}
pub fn add_offset(self, bytes: usize) -> Self {
ScanError::new(self.at.offset() + bytes, self.kind)
}
}
impl<'a> fmt::Display for ScanError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
try!("scan error: ".fmt(fmt));
try!(self.kind.fmt(fmt));
try!(", at offset: ".fmt(fmt));
try!(self.at.offset().fmt(fmt));
Ok(())
}
}
impl Error for ScanError {
fn cause(&self) -> Option<&Error> {
self.kind.cause()
}
fn description(&self) -> &str {
self.kind.description()
}
}
#[derive(Debug)]
pub struct ScanErrorAt {
bytes: usize,
}
impl ScanErrorAt {
pub fn offset(&self) -> usize {
self.bytes
}
}
#[derive(Debug)]
pub enum ScanErrorKind {
LiteralMismatch,
Syntax(&'static str),
SyntaxNoMessage,
ExpectedEnd,
Float(ParseFloatError),
Int(ParseIntError),
Io(io::Error),
Other(Box<Error>),
#[doc(hidden)]
__DoNotMatch,
}
impl ScanErrorKind {
pub fn from_other<E: Into<Box<Error>>>(err: E) -> Self {
ScanErrorKind::Other(err.into())
}
}
impl fmt::Display for ScanErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::ScanErrorKind::*;
match *self {
LiteralMismatch => "did not match literal".fmt(fmt),
Syntax(desc) => {
try!("syntax error: ".fmt(fmt));
try!(desc.fmt(fmt));
Ok(())
},
SyntaxNoMessage => "unknown syntax error".fmt(fmt),
ExpectedEnd => "expected end of input".fmt(fmt),
Float(ref err) => err.fmt(fmt),
Int(ref err) => err.fmt(fmt),
Io(ref err) => err.fmt(fmt),
Other(ref err) => err.fmt(fmt),
__DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
}
}
}
impl Error for ScanErrorKind {
fn cause(&self) -> Option<&Error> {
use self::ScanErrorKind::*;
match *self {
LiteralMismatch
| Syntax(_)
| SyntaxNoMessage
| ExpectedEnd
=> None,
Float(ref err) => err.cause(),
Int(ref err) => err.cause(),
Io(ref err) => err.cause(),
Other(ref err) => err.cause(),
__DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
}
}
fn description(&self) -> &str {
use self::ScanErrorKind::*;
match *self {
LiteralMismatch => "did not match literal",
Syntax(_) => "syntax error",
SyntaxNoMessage => "unknown syntax error",
ExpectedEnd => "expected end of input",
Float(ref err) => err.description(),
Int(ref err) => err.description(),
Io(ref err) => err.description(),
Other(ref err) => err.description(),
__DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
}
}
}