Skip to content

Commit d58c8d6

Browse files
jdmbrson
authored andcommitted
---
yaml --- r: 3822 b: refs/heads/master c: b62fcdc h: refs/heads/master v: v3
1 parent 98d5e6c commit d58c8d6

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 1870e975599e3687672db4656c60b494c2ce7232
2+
refs/heads/master: b62fcdcc36a451a23a0b4639f78e4434949190ba

trunk/src/comp/syntax/codemap.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
1+
import std::uint;
2+
import std::str;
23
import std::vec;
34
import std::term;
45
import std::io;
@@ -58,8 +59,12 @@ fn span_to_str(&span sp, &codemap cm) -> str {
5859
fn emit_diagnostic(&option::t[span] sp, &str msg, &str kind, u8 color,
5960
&codemap cm) {
6061
auto ss = "<input>:0:0:0:0";
62+
let option::t[@file_lines] maybe_lines = none;
6163
alt (sp) {
62-
case (some(?ssp)) { ss = span_to_str(ssp, cm); }
64+
case (some(?ssp)) {
65+
ss = span_to_str(ssp, cm);
66+
maybe_lines = some(span_to_lines(ssp, cm));
67+
}
6368
case (none) { }
6469
}
6570
io::stdout().write_str(ss + ": ");
@@ -71,6 +76,22 @@ fn emit_diagnostic(&option::t[span] sp, &str msg, &str kind, u8 color,
7176
term::reset(io::stdout().get_buf_writer());
7277
}
7378
io::stdout().write_str(#fmt(" %s\n", msg));
79+
alt (maybe_lines) {
80+
case (some(?lines)) {
81+
auto rdr = io::file_reader(lines.name);
82+
auto file = str::unsafe_from_bytes(rdr.read_whole_stream());
83+
auto fm = codemap::get_filemap(cm, lines.name);
84+
for (uint line in lines.lines) {
85+
io::stdout().write_str(#fmt("%s:%u ", fm.name, line + 1u));
86+
auto s = codemap::get_line(fm, line as int, file);
87+
if (!str::ends_with(s, "\n")) {
88+
s += "\n";
89+
}
90+
io::stdout().write_str(s);
91+
}
92+
}
93+
case (_) {}
94+
}
7495
}
7596

7697
fn emit_warning(&option::t[span] sp, &str msg, &codemap cm) {
@@ -83,6 +104,38 @@ fn emit_note(&option::t[span] sp, &str msg, &codemap cm) {
83104
emit_diagnostic(sp, msg, "note", 10u8, cm);
84105
}
85106

107+
type file_lines = rec(str name, vec[uint] lines);
108+
109+
fn span_to_lines(span sp, codemap::codemap cm) -> @file_lines {
110+
auto lo = codemap::lookup_pos(cm, sp.lo);
111+
auto hi = codemap::lookup_pos(cm, sp.hi);
112+
auto lines = [];
113+
for each (uint i in uint::range(lo.line - 1u, hi.line as uint)) {
114+
lines += [i];
115+
}
116+
ret @rec(name=lo.filename, lines=lines);
117+
}
118+
119+
fn get_line(filemap fm, int line, &str file) -> str {
120+
let uint end;
121+
if ((line as uint) + 1u >= vec::len(fm.lines)) {
122+
end = str::byte_len(file);
123+
} else {
124+
end = fm.lines.(line + 1);
125+
}
126+
ret str::slice(file, fm.lines.(line), end);
127+
}
128+
129+
fn get_filemap(codemap cm, str filename) -> filemap {
130+
for (filemap fm in cm.files) {
131+
if (fm.name == filename) {
132+
ret fm;
133+
}
134+
}
135+
//XXjdm the following triggers a mismatched type bug
136+
// (or expected function, found _|_)
137+
fail;// ("asking for " + filename + " which we don't know about");
138+
}
86139

87140
//
88141
// Local Variables:

0 commit comments

Comments
 (0)