2011-08-15 18:38:23 -05:00
|
|
|
import std::vec;
|
2011-07-11 01:27:03 -05:00
|
|
|
import std::uint;
|
|
|
|
import std::str;
|
2011-08-24 23:26:19 -05:00
|
|
|
import std::istr;
|
2011-08-12 00:55:08 -05:00
|
|
|
import std::term;
|
2011-08-11 21:14:38 -05:00
|
|
|
import std::io;
|
2011-07-05 04:48:19 -05:00
|
|
|
import std::option;
|
|
|
|
import std::option::some;
|
|
|
|
import std::option::none;
|
|
|
|
|
2011-08-27 02:43:22 -05:00
|
|
|
type filename = istr;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type file_pos = {ch: uint, byte: uint};
|
2011-07-16 01:01:10 -05:00
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
/* A codemap is a thing that maps uints to file/line/column positions
|
|
|
|
* in a crate. This to make it possible to represent the positions
|
|
|
|
* with single-word things, rather than passing records all over the
|
|
|
|
* compiler.
|
|
|
|
*/
|
2011-07-27 07:19:39 -05:00
|
|
|
type filemap =
|
2011-08-04 18:20:09 -05:00
|
|
|
@{name: filename, start_pos: file_pos, mutable lines: [file_pos]};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
type codemap = @{mutable files: [filemap]};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type loc = {filename: filename, line: uint, col: uint};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
fn new_codemap() -> codemap { ret @{mutable files: []}; }
|
2011-07-16 01:01:10 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn new_filemap(filename: filename, start_pos_ch: uint, start_pos_byte: uint)
|
|
|
|
-> filemap {
|
|
|
|
ret @{name: filename,
|
|
|
|
start_pos: {ch: start_pos_ch, byte: start_pos_byte},
|
2011-08-19 17:16:48 -05:00
|
|
|
mutable lines: [{ch: start_pos_ch, byte: start_pos_byte}]};
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn next_line(file: filemap, chpos: uint, byte_pos: uint) {
|
2011-08-19 17:16:48 -05:00
|
|
|
file.lines += [{ch: chpos, byte: byte_pos}];
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
type lookup_fn = fn(file_pos) -> uint;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn lookup_pos(map: codemap, pos: uint, lookup: lookup_fn) -> loc {
|
|
|
|
let a = 0u;
|
2011-08-15 18:38:23 -05:00
|
|
|
let b = vec::len(map.files);
|
2011-07-27 07:19:39 -05:00
|
|
|
while b - a > 1u {
|
|
|
|
let m = (a + b) / 2u;
|
2011-08-19 17:16:48 -05:00
|
|
|
if lookup(map.files[m].start_pos) > pos { b = m; } else { a = m; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
let f = map.files[a];
|
2011-07-05 04:48:19 -05:00
|
|
|
a = 0u;
|
2011-08-15 18:38:23 -05:00
|
|
|
b = vec::len(f.lines);
|
2011-07-27 07:19:39 -05:00
|
|
|
while b - a > 1u {
|
|
|
|
let m = (a + b) / 2u;
|
2011-08-19 17:16:48 -05:00
|
|
|
if lookup(f.lines[m]) > pos { b = m; } else { a = m; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
ret {filename: f.name, line: a + 1u, col: pos - lookup(f.lines[a])};
|
2011-07-16 01:01:10 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn lookup_char_pos(map: codemap, pos: uint) -> loc {
|
|
|
|
fn lookup(pos: file_pos) -> uint { ret pos.ch; }
|
2011-07-16 01:01:10 -05:00
|
|
|
ret lookup_pos(map, pos, lookup);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn lookup_byte_pos(map: codemap, pos: uint) -> loc {
|
|
|
|
fn lookup(pos: file_pos) -> uint { ret pos.byte; }
|
2011-07-16 01:01:10 -05:00
|
|
|
ret lookup_pos(map, pos, lookup);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
tag opt_span {
|
|
|
|
//hack (as opposed to option::t), to make `span` compile
|
2011-08-12 10:57:21 -05:00
|
|
|
os_none;
|
|
|
|
os_some(@span);
|
|
|
|
}
|
|
|
|
type span = {lo: uint, hi: uint, expanded_from: opt_span};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
fn span_to_str(sp: &span, cm: &codemap) -> istr {
|
2011-08-12 10:57:21 -05:00
|
|
|
let cur = sp;
|
2011-08-27 16:57:47 -05:00
|
|
|
let res = ~"";
|
2011-08-12 10:57:21 -05:00
|
|
|
let prev_file = none;
|
2011-08-19 17:16:48 -05:00
|
|
|
while true {
|
2011-08-12 10:57:21 -05:00
|
|
|
let lo = lookup_char_pos(cm, cur.lo);
|
|
|
|
let hi = lookup_char_pos(cm, cur.hi);
|
2011-08-27 16:57:47 -05:00
|
|
|
res += istr::from_estr(
|
2011-08-27 17:39:53 -05:00
|
|
|
#fmt["%s:%u:%u: %u:%u",
|
2011-08-19 17:16:48 -05:00
|
|
|
if some(lo.filename) == prev_file {
|
|
|
|
"-"
|
2011-08-27 02:43:22 -05:00
|
|
|
} else {
|
|
|
|
istr::to_estr(lo.filename)
|
2011-08-27 16:57:47 -05:00
|
|
|
}, lo.line, lo.col, hi.line, hi.col]);
|
2011-08-12 10:57:21 -05:00
|
|
|
alt cur.expanded_from {
|
|
|
|
os_none. { break; }
|
|
|
|
os_some(new_sp) {
|
|
|
|
cur = *new_sp;
|
|
|
|
prev_file = some(lo.filename);
|
2011-08-27 16:57:47 -05:00
|
|
|
res += ~"<<";
|
2011-08-12 10:57:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret res;
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
fn emit_diagnostic(sp: &option::t<span>, msg: &istr, kind: &istr, color: u8,
|
2011-07-27 07:19:39 -05:00
|
|
|
cm: &codemap) {
|
2011-08-27 16:57:47 -05:00
|
|
|
let ss = ~"";
|
2011-08-12 09:15:18 -05:00
|
|
|
let maybe_lines: option::t<@file_lines> = none;
|
2011-07-27 07:19:39 -05:00
|
|
|
alt sp {
|
|
|
|
some(ssp) {
|
2011-08-27 16:57:47 -05:00
|
|
|
ss = span_to_str(ssp, cm) + ~" ";
|
2011-07-27 07:19:39 -05:00
|
|
|
maybe_lines = some(span_to_lines(ssp, cm));
|
|
|
|
}
|
|
|
|
none. { }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
io::stdout().write_str(ss);
|
2011-08-12 00:55:08 -05:00
|
|
|
if term::color_supported() {
|
|
|
|
term::fg(io::stdout().get_buf_writer(), color);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
io::stdout().write_str(istr::from_estr(#fmt["%s:", istr::to_estr(kind)]));
|
2011-08-19 17:16:48 -05:00
|
|
|
if term::color_supported() { term::reset(io::stdout().get_buf_writer()); }
|
2011-08-27 16:57:47 -05:00
|
|
|
io::stdout().write_str(istr::from_estr(#fmt[" %s\n",
|
|
|
|
istr::to_estr(msg)]));
|
2011-07-30 17:50:16 -05:00
|
|
|
|
|
|
|
maybe_highlight_lines(sp, cm, maybe_lines);
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:15:18 -05:00
|
|
|
fn maybe_highlight_lines(sp: &option::t<span>, cm: &codemap,
|
|
|
|
maybe_lines: option::t<@file_lines>) {
|
2011-07-30 17:50:16 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
alt maybe_lines {
|
|
|
|
some(lines) {
|
2011-07-30 17:50:16 -05:00
|
|
|
// If we're not looking at a real file then we can't re-open it to
|
|
|
|
// pull out the lines
|
2011-08-27 16:57:47 -05:00
|
|
|
if lines.name == ~"-" { ret; }
|
2011-07-30 17:50:16 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
// FIXME: reading in the entire file is the worst possible way to
|
|
|
|
// get access to the necessary lines.
|
2011-08-27 16:57:47 -05:00
|
|
|
let file = io::read_whole_file_str(lines.name);
|
2011-07-27 07:19:39 -05:00
|
|
|
let fm = get_filemap(cm, lines.name);
|
|
|
|
|
|
|
|
// arbitrarily only print up to six lines of the error
|
|
|
|
let max_lines = 6u;
|
|
|
|
let elided = false;
|
|
|
|
let display_lines = lines.lines;
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(display_lines) > max_lines {
|
|
|
|
display_lines = vec::slice(display_lines, 0u, max_lines);
|
2011-07-27 07:19:39 -05:00
|
|
|
elided = true;
|
|
|
|
}
|
|
|
|
// Print the offending lines
|
2011-08-15 23:54:52 -05:00
|
|
|
for line: uint in display_lines {
|
2011-08-24 23:26:19 -05:00
|
|
|
io::stdout().write_str(
|
2011-08-27 02:43:22 -05:00
|
|
|
istr::from_estr(#fmt["%s:%u ",
|
|
|
|
istr::to_estr(fm.name), line + 1u]));
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = get_line(fm, line as int, file);
|
2011-08-27 16:57:47 -05:00
|
|
|
if !istr::ends_with(s, ~"\n") { s += ~"\n"; }
|
|
|
|
io::stdout().write_str(s);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
if elided {
|
2011-08-19 17:16:48 -05:00
|
|
|
let last_line = display_lines[vec::len(display_lines) - 1u];
|
2011-08-27 02:43:22 -05:00
|
|
|
let s = #fmt["%s:%u ",
|
|
|
|
istr::to_estr(fm.name), last_line + 1u];
|
2011-07-27 07:19:39 -05:00
|
|
|
let indent = str::char_len(s);
|
2011-08-24 23:26:19 -05:00
|
|
|
let out = ~"";
|
|
|
|
while indent > 0u { out += ~" "; indent -= 1u; }
|
|
|
|
out += ~"...\n";
|
2011-08-11 21:14:38 -05:00
|
|
|
io::stdout().write_str(out);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-11 13:31:57 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// If there's one line at fault we can easily point to the problem
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(lines.lines) == 1u {
|
2011-07-27 07:19:39 -05:00
|
|
|
let lo = lookup_char_pos(cm, option::get(sp).lo);
|
|
|
|
let digits = 0u;
|
2011-08-19 17:16:48 -05:00
|
|
|
let num = lines.lines[0] / 10u;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
// how many digits must be indent past?
|
|
|
|
while num > 0u { num /= 10u; digits += 1u; }
|
|
|
|
|
|
|
|
// indent past |name:## | and the 0-offset column location
|
2011-08-27 02:43:22 -05:00
|
|
|
let left = istr::char_len(fm.name) + digits + lo.col + 3u;
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = "";
|
|
|
|
while left > 0u { str::push_char(s, ' '); left -= 1u; }
|
|
|
|
|
|
|
|
s += "^";
|
|
|
|
let hi = lookup_char_pos(cm, option::get(sp).hi);
|
|
|
|
if hi.col != lo.col {
|
|
|
|
// the ^ already takes up one space
|
|
|
|
let width = hi.col - lo.col - 1u;
|
|
|
|
while width > 0u { str::push_char(s, '~'); width -= 1u; }
|
2011-07-11 13:31:57 -05:00
|
|
|
}
|
2011-08-24 23:26:19 -05:00
|
|
|
io::stdout().write_str(istr::from_estr(s + "\n"));
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
fn emit_warning(sp: &option::t<span>, msg: &istr, cm: &codemap) {
|
|
|
|
emit_diagnostic(sp, msg, ~"warning", 11u8, cm);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
fn emit_error(sp: &option::t<span>, msg: &istr, cm: &codemap) {
|
|
|
|
emit_diagnostic(sp, msg, ~"error", 9u8, cm);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
fn emit_note(sp: &option::t<span>, msg: &istr, cm: &codemap) {
|
|
|
|
emit_diagnostic(sp, msg, ~"note", 10u8, cm);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
type file_lines = {name: istr, lines: [uint]};
|
2011-07-11 01:27:03 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
|
|
|
|
let lo = lookup_char_pos(cm, sp.lo);
|
|
|
|
let hi = lookup_char_pos(cm, sp.hi);
|
2011-08-19 17:16:48 -05:00
|
|
|
let lines = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for each i: uint in uint::range(lo.line - 1u, hi.line as uint) {
|
2011-08-19 17:16:48 -05:00
|
|
|
lines += [i];
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
ret @{name: lo.filename, lines: lines};
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
fn get_line(fm: filemap, line: int, file: &istr) -> istr {
|
2011-08-19 17:16:48 -05:00
|
|
|
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
|
2011-07-27 07:19:39 -05:00
|
|
|
let end: uint;
|
2011-08-15 18:38:23 -05:00
|
|
|
if line as uint < vec::len(fm.lines) - 1u {
|
2011-08-19 17:16:48 -05:00
|
|
|
end = fm.lines[line + 1].byte - fm.start_pos.byte;
|
2011-07-13 14:19:59 -05:00
|
|
|
} else {
|
|
|
|
// If we're not done parsing the file, we're at the limit of what's
|
|
|
|
// parsed. If we just slice the rest of the string, we'll print out
|
|
|
|
// the remainder of the file, which is undesirable.
|
2011-08-27 16:57:47 -05:00
|
|
|
end = istr::byte_len(file);
|
|
|
|
let rest = istr::slice(file, begin, end);
|
|
|
|
let newline = istr::index(rest, '\n' as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
if newline != -1 { end = begin + (newline as uint); }
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2011-08-27 16:57:47 -05:00
|
|
|
ret istr::slice(file, begin, end);
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 16:57:47 -05:00
|
|
|
fn get_filemap(cm: codemap, filename: istr) -> filemap {
|
2011-08-27 02:43:22 -05:00
|
|
|
for fm: filemap in cm.files {
|
2011-08-27 16:57:47 -05:00
|
|
|
if fm.name == filename { ret fm; }
|
2011-08-27 02:43:22 -05:00
|
|
|
}
|
2011-07-11 01:27:03 -05:00
|
|
|
//XXjdm the following triggers a mismatched type bug
|
|
|
|
// (or expected function, found _|_)
|
2011-07-27 07:19:39 -05:00
|
|
|
fail; // ("asking for " + filename + " which we don't know about");
|
2011-07-11 01:27:03 -05:00
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|