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
use crate::parser::span::Span;
pub struct DebugOptions {
pub location: Option<String>,
pub print_full: bool,
pub print_underscore_error: bool,
}
impl DebugOptions {
pub fn new() -> Self {
Self {
print_full: false,
print_underscore_error: true,
location: None,
}
}
}
impl Default for DebugOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct Line {
line: usize,
str: String,
span: Span,
}
#[derive(Debug)]
pub struct SyntaxError {
pub message: String,
pub span: Span,
}
pub trait ErrorWithSpan {
fn get_message(&self) -> Span;
fn get_span(&self) -> Span;
}
impl SyntaxError {
pub fn new<T: ToString>(message: T, span: Span) -> Self {
Self {
message: message.to_string(),
span,
}
}
pub fn helper_create_lines<T: ToString>(payload: &T) -> Vec<Line> {
let mut lines: Vec<Line> = vec![];
let mut current_line = Line {
line: 1,
str: String::new(),
span: Span { start: 0, end: 0 },
};
for (index, ch) in payload.to_string().chars().enumerate() {
current_line.span.end = index;
if ch == '\n' {
lines.push(current_line.clone());
current_line = Line {
line: current_line.line + 1,
str: String::new(),
span: Span {
start: index + 1,
end: index + 1,
},
};
continue;
}
current_line.str.push(ch);
}
lines.push(current_line.clone());
lines
}
pub fn debug_payload<T: ToString>(&self, payload: &T) -> String {
Self::debug_payload_configurable(&self, payload, &Default::default())
}
pub fn debug_payload_configurable<T: ToString>(
&self,
payload: &T,
options: &DebugOptions,
) -> String {
let mut buff = String::new();
let mut at_line_pos: Option<String> = None;
buff.push_str(format!("SyntaxError: {}\n", self.message).as_str());
buff.push('\n');
for line in Self::helper_create_lines(payload) {
let err_start_inline =
self.span.start >= line.span.start && self.span.start <= line.span.end;
let err_end_inline = self.span.end >= line.span.start && self.span.end <= line.span.end;
let err_cover_line: bool =
self.span.start <= line.span.start && self.span.end >= line.span.end;
if !options.print_full && !(err_start_inline || err_end_inline || err_cover_line) {
continue;
}
buff.push_str(
format!(
"{} {:>4} | {}\n",
{
if err_start_inline || err_end_inline || err_cover_line {
">"
} else {
" "
}
},
line.line.to_string().as_str(),
line.str.as_str(),
)
.as_str(),
);
let err_subline_start: usize = {
if err_start_inline {
self.span.start - line.span.start
} else {
0
}
};
let err_subline_end: usize = {
if err_end_inline {
(line.span.end - line.span.start) - (line.span.end - self.span.end)
} else {
line.span.end - line.span.start
}
};
if options.print_underscore_error
&& (err_start_inline || err_end_inline || err_cover_line)
{
buff.push_str(
format!(
"{}{}\n",
" ".repeat(9 + err_subline_start),
"▀".repeat(err_subline_end - err_subline_start)
)
.as_str(),
)
}
if at_line_pos.is_none() && (err_start_inline || err_end_inline || err_cover_line) {
at_line_pos = Some(format!(
"{}:{}",
line.line,
err_subline_start + 1
))
}
}
if let Some(pos) = at_line_pos {
buff.push_str(&format!(
" at {}:{}\n",
options.location.clone().unwrap_or("<unknown>".to_string()),
pos,
))
}
buff
}
}