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
use std::fmt::{self, Display, Formatter, Debug};
use parse::Span;
#[cfg(test)]
use Result;

mod diagnostic_bag;
pub use self::diagnostic_bag::DiagnosticBag;

#[macro_export]
macro_rules! diagnostic {
    (ERROR, $span:expr, $fmt:expr) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Error;
        $crate::diagnostic::Diagnostic::new(format!($fmt), $span).with_error_level(error_level)
    }};
    (ERROR, $span:expr, $fmt:expr, $($arg:tt)*) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Error;
        $crate::diagnostic::Diagnostic::new(format!($fmt, $($arg)*), $span).with_error_level(error_level)
    }};
    (INFO, $span:expr, $fmt:expr) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Info;
        $crate::diagnostic::Diagnostic::new(format!($fmt), $span).with_error_level(error_level)
    }};
    (INFO, $span:expr, $fmt:expr, $($arg:tt)*) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Info;
        $crate::diagnostic::Diagnostic::new(format!($fmt, $($arg)*), $span).with_error_level(error_level)
    }};
    (WARN, $span:expr, $fmt:expr) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Warn;
        $crate::diagnostic::Diagnostic::new(format!($fmt), $span).with_error_level(error_level)
    }};
    (WARN, $span:expr, $fmt:expr, $($arg:tt)*) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Warn;
        $crate::diagnostic::Diagnostic::new(format!($fmt, $($arg)*), $span).with_error_level(error_level)
    }};
    (CUSTOM($custom:expr), $span:expr, $fmt:expr) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Custom($custom);
        $crate::diagnostic::Diagnostic::new(format!($fmt), $span).with_error_level(error_level)
    }};
    (CUSTOM($custom:expr), $span:expr, $fmt:expr, $($arg:tt)*) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Custom($custom);
        $crate::diagnostic::Diagnostic::new(format!($fmt, $($arg)*), $span).with_error_level(error_level)
    }};
    ($span:expr, $fmt:expr) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Error;
        $crate::diagnostic::Diagnostic::new(format!($fmt), $span).with_error_level(error_level)
    }};
    ($span:expr, $fmt:expr, $($arg:tt)*) => {{
        let error_level = $crate::diagnostic::DiagnosticLevel::Error;
        $crate::diagnostic::Diagnostic::new(format!($fmt, $($arg)*), $span).with_error_level(error_level)
    }};
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
pub enum DiagnosticLevel {
    Info,
    Warn,
    Error,
    Custom(String),
}

#[derive(Eq, PartialEq, PartialOrd, Ord, Clone)]
pub struct Diagnostic {
    pub message: String,
    pub annotations: Vec<DiagnosticAnnotation>,
    pub global_span: Span,
    pub padding: usize,
    pub error_level: DiagnosticLevel,

    // optional
    pub min_gap: Option<usize>,
}

#[derive(Eq, PartialEq, PartialOrd, Ord, Clone)]
pub struct DiagnosticAnnotation {
    pub message: String,
    pub span: Span,
}

impl DiagnosticLevel {
    fn as_str(&self) -> &str {
        match self {
            &DiagnosticLevel::Info => "info",
            &DiagnosticLevel::Warn => "warn",
            &DiagnosticLevel::Error => "error",
            &DiagnosticLevel::Custom(ref s) => s,
        }
    }
}

impl Diagnostic {
    pub fn new<T: Into<String>>(message: T, span: &Span) -> Diagnostic {
        Diagnostic {
            message: message.into(),
            annotations: vec![],
            global_span: span.clone(),
            padding: 2,

            min_gap: None,
            error_level: DiagnosticLevel::Error,
        }
    }

    pub fn with_error_level(mut self, level: DiagnosticLevel) -> Diagnostic {
        self.error_level = level;
        self
    }

    pub fn with_min_gap(mut self, gap: usize) -> Diagnostic {
        self.min_gap = Some(gap);
        self
    }

    pub fn with_garunteed_padding(mut self, padding: usize) -> Diagnostic {
        self.padding = padding;
        self
    }

    pub fn add_annotation(mut self, annotation: DiagnosticAnnotation) -> Diagnostic {
        self.annotations.push(annotation);
        self
    }
}

impl DiagnosticAnnotation {
    pub fn new(message: String, span: Span) -> DiagnosticAnnotation {
        DiagnosticAnnotation {
            message: message,
            span: span,
        }
    }
}

impl Debug for Diagnostic {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl Display for Diagnostic {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let builder = self;
        // "error" message
        writeln!(f, "{}: {}", builder.error_level.as_str(), builder.message)?;

        // File, line number, column number information
        if let &Some(ref file) = &builder.global_span.file {
            writeln!(f,
                     " --> {}:{}:{}",
                     file,
                     builder.global_span.lines_covered.start,
                     builder.global_span.columns.start)?;
        } else {
            writeln!(f,
                     " --> {}:{}",
                     builder.global_span.lines_covered.start,
                     builder.global_span.columns.start)?;
        }

        let padding = base_10_length(builder.global_span.lines_covered.end as usize +
                                     builder.global_span.lines().as_ref().lines().count());

        let lines = builder.global_span.lines();
        let iter =
            lines
                .as_ref()
                .lines()
                .enumerate()
                .map(|(i, line)| (i + builder.global_span.lines_covered.start as usize, line));

        let mut skipped_streak = 0;
        for (i, line) in iter {
            let get_span = &get_span;
            let spans = builder.annotations.iter().map(get_span);
            if should_skip(i,
                           skipped_streak,
                           builder.padding,
                           builder.min_gap,
                           &builder.global_span,
                           spans) {
                skipped_streak += 1;
            } else {
                if skipped_streak > 0 {
                    write!(f, "{x:pd$} | ", pd = padding, x = "~")?;
                    writeln!(f,
                             "skipped <{}> through <{}>",
                             i - 1 - skipped_streak,
                             i - 1)?;
                }
                skipped_streak = 0;
                writeln!(f, "{x:pd$} | {st}", pd = padding, x = i, st = line)?;
            }
        }

        Ok(())
    }
}

fn get_span<'a>(ann: &'a DiagnosticAnnotation) -> &'a Span {
    &ann.span
}

fn should_skip<'a, I>(line: usize,
                      already_skipped: usize,
                      padding: usize,
                      max_gap_size: Option<usize>,
                      global_span: &'a Span,
                      annot_span: I)
                      -> bool
    where I: Iterator<Item = &'a Span> + Clone
{
    let max_gap = match max_gap_size {
        Some(t) => t,
        None => return false,
    };

    let dist = line_dist_all(line,
                             ::std::iter::once(global_span).chain(annot_span.clone()))
            .unwrap();

    if dist <= padding {
        return false;
    }

    let mut skip_count = already_skipped + 1;
    let mut i = 1;
    while should_skip(line + i,
                      skip_count,
                      padding,
                      max_gap_size,
                      global_span,
                      annot_span.clone()) {
        skip_count += 1;
        i += 1;
    }

    if skip_count < max_gap {
        return false;
    }

    return true;
}

pub fn base_10_length(mut x: usize) -> usize {
    let mut r = 1;
    while x >= 10 {
        r = r + 1;
        x /= 10;
    }
    r
}

fn line_dist_all<'a, I>(line: usize, i: I) -> Option<usize>
    where I: Iterator<Item = &'a Span>
{
    i.map(|s| line_distance(line, s)).min()
}

// Return the distance to
fn line_distance(line: usize, span: &Span) -> usize {

    let dist_start = (line as isize - span.lines_covered.start as isize).abs() as usize;
    let dist_end = (line as isize - span.lines_covered.end as isize).abs() as usize;

    let shortest_dist = ::std::cmp::min(dist_start, dist_end);

    shortest_dist
}

#[test]
fn test_base_10_length() {
    assert_eq!(base_10_length(0), 1);
    assert_eq!(base_10_length(5), 1);
    assert_eq!(base_10_length(10), 2);
    assert_eq!(base_10_length(100), 3);
}

#[test]
fn diagnostic_macros() {
    let source = r#"(define map (lambda (xs f)
  (if (nil xs) xs
      (cons (f (car xs))
            (map (cdr xs) f)))))
"#;

    let Result { roots, diagnostics } = ::simple_parse(source, &[], Some("<anon>"));
    let span = &roots[0].span();
    assert!(diagnostics.is_empty());

    let error = Diagnostic::new("this is the message 5", span)
        .with_error_level(DiagnosticLevel::Error);

    let macro_error = diagnostic!(ERROR, span, "this is the message {}", 5);

    assert_eq!(error, macro_error);
}


#[test]
fn test_basic_error() {
    let source = r#"(define map (lambda (xs f)
  (if (nil xs) xs
      (cons (f (car xs))
            (map (cdr xs) f)))))
"#;

    let Result { roots, diagnostics } = ::simple_parse(source, &[], Some("<anon>"));
    assert!(diagnostics.is_empty());

    let error = Diagnostic::new("this is the message", roots[0].span())
        .with_error_level(DiagnosticLevel::Info);

    println!("{}", error);
    assert_eq!(error.to_string().trim(),
               r#"info: this is the message
 --> <anon>:1:1
1 | (define map (lambda (xs f)
2 |   (if (nil xs) xs
3 |       (cons (f (car xs))
4 |             (map (cdr xs) f)))))"#);
}