Skip to content

Commit 8da8a4a

Browse files
committed
rustc: Extract error reporting from codemap
1 parent f2a3270 commit 8da8a4a

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

src/comp/syntax/codemap.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,54 @@ fn span_to_str(sp: span, cm: codemap) -> str {
9292
ret res;
9393
}
9494

95-
fn emit_diagnostic(sp: option::t<span>, msg: str, kind: str, color: u8,
95+
tag diagnostictype {
96+
warning;
97+
error;
98+
note;
99+
}
100+
101+
fn diagnosticstr(t: diagnostictype) -> str {
102+
alt t {
103+
warning. { "warning" }
104+
error. { "error" }
105+
note. { "note" }
106+
}
107+
}
108+
109+
fn diagnosticcolor(t: diagnostictype) -> u8 {
110+
alt t {
111+
warning. { term::color_bright_yellow }
112+
error. { term::color_bright_red }
113+
note. { term::color_bright_green }
114+
}
115+
}
116+
117+
fn print_diagnostic(topic: str, t: diagnostictype, msg: str) {
118+
if str::is_not_empty(topic) {
119+
io::stdout().write_str(#fmt["%s ", topic]);
120+
}
121+
if term::color_supported() {
122+
term::fg(io::stdout().get_buf_writer(), diagnosticcolor(t));
123+
}
124+
io::stdout().write_str(#fmt["%s:", diagnosticstr(t)]);
125+
if term::color_supported() {
126+
term::reset(io::stdout().get_buf_writer());
127+
}
128+
io::stdout().write_str(#fmt[" %s\n", msg]);
129+
}
130+
131+
fn emit_diagnostic(sp: option::t<span>, msg: str, t: diagnostictype,
96132
cm: codemap) {
97133
let ss = "";
98134
let maybe_lines: option::t<@file_lines> = none;
99135
alt sp {
100136
some(ssp) {
101-
ss = span_to_str(ssp, cm) + " ";
137+
ss = span_to_str(ssp, cm);
102138
maybe_lines = some(span_to_lines(ssp, cm));
103139
}
104140
none. { }
105141
}
106-
io::stdout().write_str(ss);
107-
if term::color_supported() {
108-
term::fg(io::stdout().get_buf_writer(), color);
109-
}
110-
io::stdout().write_str(#fmt["%s:", kind]);
111-
if term::color_supported() { term::reset(io::stdout().get_buf_writer()); }
112-
io::stdout().write_str(#fmt[" %s\n", msg]);
113-
142+
print_diagnostic(ss, t, msg);
114143
maybe_highlight_lines(sp, cm, maybe_lines);
115144
}
116145

@@ -183,13 +212,13 @@ fn maybe_highlight_lines(sp: option::t<span>, cm: codemap,
183212
}
184213

185214
fn emit_warning(sp: option::t<span>, msg: str, cm: codemap) {
186-
emit_diagnostic(sp, msg, "warning", term::color_bright_yellow, cm);
215+
emit_diagnostic(sp, msg, warning, cm);
187216
}
188217
fn emit_error(sp: option::t<span>, msg: str, cm: codemap) {
189-
emit_diagnostic(sp, msg, "error", term::color_bright_red, cm);
218+
emit_diagnostic(sp, msg, error, cm);
190219
}
191220
fn emit_note(sp: option::t<span>, msg: str, cm: codemap) {
192-
emit_diagnostic(sp, msg, "note", term::color_bright_green, cm);
221+
emit_diagnostic(sp, msg, note, cm);
193222
}
194223

195224
type file_lines = {name: str, lines: [uint]};

0 commit comments

Comments
 (0)