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
use std::fmt;
use crate::common::span::Span;
#[derive(Debug, PartialEq, Eq)]
pub struct Trace {
kind: String,
message: String,
spans: Vec<Span>,
}
impl Trace {
pub fn error(kind: &str, message: &str, spans: Vec<Span>) -> Trace {
Trace {
kind: kind.to_string(),
message: message.to_string(),
spans,
}
}
pub fn add_context(&mut self, span: Span) {
self.spans.push(span);
}
}
impl fmt::Display for Trace {
fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Traceback, most recent call last:")?;
for span in self.spans.iter().rev() {
fmt::Display::fmt(span, f)?;
}
write!(f, "Runtime {} Error: {}", self.kind, self.message)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::common::source::Source;
use std::rc::Rc;
#[test]
fn traceback() {
let source = Rc::new(Source::source("incr = x -> x + 1
dub_incr = z -> (incr x) + (incr x)
forever = a -> a = a + (dub_incr a)
forever RandomLabel
"));
let target = "\
Traceback, most recent call last:\n\
In ./source:4:1\n \
|\n \
4 | forever RandomLabel\n \
| ^^^^^^^^^^^^^^^^^^^\n \
|\n\
In ./source:3:24\n \
|\n \
3 | forever = a -> a = a + (dub_incr a)\n \
| ^^^^^^^^^^^^\n \
|\n\
In ./source:2:17\n \
|\n \
2 | dub_incr = z -> (incr x) + (incr x)\n \
| ^^^^^^^^\n \
|\n\
In ./source:1:13\n \
|\n \
1 | incr = x -> x + 1\n \
| ^^^^^\n \
|\n\
Runtime Type Error: Can\'t add Label to Label\
";
let traceback = Trace::error(
"Type",
"Can't add Label to Label",
vec![
(Span::new(&source, 12, 5)),
(Span::new(&source, 34, 8)),
(Span::new(&source, 77, 12)),
(Span::new(&source, 90, 19)),
]
);
let result = format!("{}", traceback);
assert_eq!(result, target);
}
}