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
use std::any::Any;
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::iter::{Extend, FromIterator};
use std::ops::{Index, IndexMut};
use std::slice;
use std::str;
use std::vec;
use codespan_reporting::{Diagnostic, Label};
use crate::pos::{BytePos, Span, Spanned};
#[derive(Clone, Debug, PartialEq)]
pub struct Errors<T> {
errors: Vec<T>,
}
impl<T> Default for Errors<T> {
fn default() -> Self {
Errors::new()
}
}
impl<T> Errors<T> {
pub fn new() -> Errors<T> {
Errors::from(Vec::new())
}
pub fn has_errors(&self) -> bool {
!self.is_empty()
}
pub fn len(&self) -> usize {
self.errors.len()
}
pub fn is_empty(&self) -> bool {
self.errors.is_empty()
}
pub fn push(&mut self, t: T) {
self.errors.push(t);
}
pub fn pop(&mut self) -> Option<T> {
self.errors.pop()
}
pub fn iter(&self) -> slice::Iter<T> {
self.errors.iter()
}
}
impl<T> Index<usize> for Errors<T> {
type Output = T;
fn index(&self, index: usize) -> &T {
&self.errors[index]
}
}
impl<T> IndexMut<usize> for Errors<T> {
fn index_mut(&mut self, index: usize) -> &mut T {
&mut self.errors[index]
}
}
impl<T> Extend<T> for Errors<T> {
fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
self.errors.extend(iter);
}
}
impl<T> From<Vec<T>> for Errors<T> {
fn from(errors: Vec<T>) -> Errors<T> {
Errors { errors }
}
}
impl<T> FromIterator<T> for Errors<T> {
fn from_iter<Iter: IntoIterator<Item = T>>(iter: Iter) -> Errors<T> {
Errors {
errors: iter.into_iter().collect(),
}
}
}
impl<T> Into<Vec<T>> for Errors<T> {
fn into(self) -> Vec<T> {
self.errors
}
}
impl<T> IntoIterator for Errors<T> {
type Item = T;
type IntoIter = vec::IntoIter<T>;
fn into_iter(self) -> vec::IntoIter<T> {
self.errors.into_iter()
}
}
impl<'a, T> IntoIterator for &'a Errors<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> slice::Iter<'a, T> {
self.errors.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Errors<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
fn into_iter(self) -> slice::IterMut<'a, T> {
self.errors.iter_mut()
}
}
impl<T: fmt::Display> fmt::Display for Errors<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, error) in self.errors.iter().enumerate() {
write!(f, "{}", error)?;
if i + 1 != self.errors.len() {
writeln!(f)?;
writeln!(f)?;
}
}
Ok(())
}
}
impl<T: fmt::Display + fmt::Debug + Any> StdError for Errors<T> {
fn description(&self) -> &str {
"Errors"
}
}
#[derive(Clone, Debug)]
pub struct InFile<E> {
source: ::codespan::CodeMap,
error: Errors<Spanned<E, BytePos>>,
}
impl<E> PartialEq for InFile<E>
where
E: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.error == other.error
}
}
impl<E: fmt::Display> InFile<E> {
pub fn new(source: ::codespan::CodeMap, error: Errors<Spanned<E, BytePos>>) -> InFile<E> {
let err = InFile { source, error };
debug_assert!({
err.source_name();
true
});
err
}
pub fn source_name(&self) -> &::codespan::FileName {
self.source
.find_file(self.error[0].span.start())
.unwrap_or_else(|| {
panic!(
"Source file does not exist in associated code map. Error: {}",
self.error
)
})
.name()
}
pub fn errors(self) -> Errors<Spanned<E, BytePos>> {
self.error
}
pub fn emit_string(&self, code_map: &::codespan::CodeMap) -> io::Result<String>
where
E: AsDiagnostic,
{
let mut output = Vec::new();
self.emit(
&mut ::codespan_reporting::termcolor::NoColor::new(&mut output),
code_map,
)?;
Ok(String::from_utf8(output).unwrap())
}
pub fn emit<W>(&self, writer: &mut W, code_map: &::codespan::CodeMap) -> io::Result<()>
where
W: ?Sized + ::codespan_reporting::termcolor::WriteColor,
E: AsDiagnostic,
{
let iter = self
.error
.iter()
.map(AsDiagnostic::as_diagnostic)
.enumerate();
for (i, diagnostic) in iter {
if i != 0 {
writeln!(writer)?;
}
::codespan_reporting::emit(&mut *writer, &code_map, &diagnostic)?;
}
Ok(())
}
}
impl<E: fmt::Display + AsDiagnostic> fmt::Display for InFile<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buffer = Vec::new();
{
let mut writer = ::codespan_reporting::termcolor::NoColor::new(&mut buffer);
self.emit(&mut writer, &self.source)
.map_err(|_| fmt::Error)?;
}
write!(f, "{}", str::from_utf8(&buffer).unwrap())
}
}
impl<E: fmt::Display + fmt::Debug + Any + AsDiagnostic> StdError for InFile<E> {
fn description(&self) -> &str {
"Error in file"
}
}
#[derive(Debug, PartialEq)]
pub struct Help<E, H> {
pub error: E,
pub help: Option<H>,
}
impl<E, H> fmt::Display for Help<E, H>
where
E: fmt::Display,
H: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.error)?;
if let Some(ref help) = self.help {
writeln!(f)?;
write!(f, "help: {}", help)?;
}
Ok(())
}
}
impl<E, H> From<E> for Help<E, H> {
fn from(error: E) -> Help<E, H> {
Help { error, help: None }
}
}
pub trait AsDiagnostic {
fn as_diagnostic(&self) -> Diagnostic;
}
impl<E> AsDiagnostic for Spanned<E, BytePos>
where
E: AsDiagnostic,
{
fn as_diagnostic(&self) -> Diagnostic {
let mut diagnostic = self.value.as_diagnostic();
diagnostic.labels.insert(0, Label::new_primary(self.span));
diagnostic
}
}
impl<E, H> AsDiagnostic for Help<E, H>
where
E: AsDiagnostic,
H: fmt::Display,
{
fn as_diagnostic(&self) -> Diagnostic {
let mut diagnostic = self.error.as_diagnostic();
if let Some(ref help) = self.help {
diagnostic.labels.push(
Label::new_secondary(Span::new(BytePos::none(), BytePos::none()))
.with_message(help.to_string()),
);
}
diagnostic
}
}
impl AsDiagnostic for Box<::std::error::Error + Send + Sync> {
fn as_diagnostic(&self) -> Diagnostic {
Diagnostic::new_error(self.to_string())
}
}