1
Fork 0

Convert rustc::syntax::codemap to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-27 14:57:47 -07:00
parent 9fb085560d
commit 3dc2419443
4 changed files with 50 additions and 45 deletions

View file

@ -10,6 +10,7 @@ import std::option;
import std::option::some; import std::option::some;
import std::option::none; import std::option::none;
import std::str; import std::str;
import std::istr;
import syntax::parse::parser::parse_sess; import syntax::parse::parser::parse_sess;
tag os { os_win32; os_macos; os_linux; } tag os { os_win32; os_macos; os_linux; }
@ -60,19 +61,19 @@ obj session(targ_cfg: @config,
fn get_cstore() -> metadata::cstore::cstore { cstore } fn get_cstore() -> metadata::cstore::cstore { cstore }
fn span_fatal(sp: span, msg: str) -> ! { fn span_fatal(sp: span, msg: str) -> ! {
// FIXME: Use constants, but rustboot doesn't know how to export them. // FIXME: Use constants, but rustboot doesn't know how to export them.
codemap::emit_error(some(sp), msg, parse_sess.cm); codemap::emit_error(some(sp), istr::from_estr(msg), parse_sess.cm);
fail; fail;
} }
fn fatal(msg: str) -> ! { fn fatal(msg: str) -> ! {
codemap::emit_error(none, msg, parse_sess.cm); codemap::emit_error(none, istr::from_estr(msg), parse_sess.cm);
fail; fail;
} }
fn span_err(sp: span, msg: str) { fn span_err(sp: span, msg: str) {
codemap::emit_error(some(sp), msg, parse_sess.cm); codemap::emit_error(some(sp), istr::from_estr(msg), parse_sess.cm);
err_count += 1u; err_count += 1u;
} }
fn err(msg: str) { fn err(msg: str) {
codemap::emit_error(none, msg, parse_sess.cm); codemap::emit_error(none, istr::from_estr(msg), parse_sess.cm);
err_count += 1u; err_count += 1u;
} }
fn abort_if_errors() { fn abort_if_errors() {
@ -80,14 +81,18 @@ obj session(targ_cfg: @config,
} }
fn span_warn(sp: span, msg: str) { fn span_warn(sp: span, msg: str) {
// FIXME: Use constants, but rustboot doesn't know how to export them. // FIXME: Use constants, but rustboot doesn't know how to export them.
codemap::emit_warning(some(sp), msg, parse_sess.cm); codemap::emit_warning(some(sp), istr::from_estr(msg), parse_sess.cm);
}
fn warn(msg: str) {
codemap::emit_warning(none, istr::from_estr(msg), parse_sess.cm);
} }
fn warn(msg: str) { codemap::emit_warning(none, msg, parse_sess.cm); }
fn span_note(sp: span, msg: str) { fn span_note(sp: span, msg: str) {
// FIXME: Use constants, but rustboot doesn't know how to export them. // FIXME: Use constants, but rustboot doesn't know how to export them.
codemap::emit_note(some(sp), msg, parse_sess.cm); codemap::emit_note(some(sp), istr::from_estr(msg), parse_sess.cm);
}
fn note(msg: str) {
codemap::emit_note(none, istr::from_estr(msg), parse_sess.cm);
} }
fn note(msg: str) { codemap::emit_note(none, msg, parse_sess.cm); }
fn span_bug(sp: span, msg: str) -> ! { fn span_bug(sp: span, msg: str) -> ! {
self.span_fatal(sp, #fmt["internal compiler error %s", msg]); self.span_fatal(sp, #fmt["internal compiler error %s", msg]);
} }
@ -107,7 +112,7 @@ obj session(targ_cfg: @config,
ret syntax::parse::parser::next_node_id(parse_sess); ret syntax::parse::parser::next_node_id(parse_sess);
} }
fn span_str(sp: span) -> str { fn span_str(sp: span) -> str {
ret codemap::span_to_str(sp, self.get_codemap()); ret istr::to_estr(codemap::span_to_str(sp, self.get_codemap()));
} }
fn set_main_id(d: node_id) { main_fn = some(d); } fn set_main_id(d: node_id) { main_fn = some(d); }
fn get_main_id() -> option::t<node_id> { main_fn } fn get_main_id() -> option::t<node_id> { main_fn }

View file

@ -73,26 +73,26 @@ tag opt_span {
} }
type span = {lo: uint, hi: uint, expanded_from: opt_span}; type span = {lo: uint, hi: uint, expanded_from: opt_span};
fn span_to_str(sp: &span, cm: &codemap) -> str { fn span_to_str(sp: &span, cm: &codemap) -> istr {
let cur = sp; let cur = sp;
let res = ""; let res = ~"";
let prev_file = none; let prev_file = none;
while true { while true {
let lo = lookup_char_pos(cm, cur.lo); let lo = lookup_char_pos(cm, cur.lo);
let hi = lookup_char_pos(cm, cur.hi); let hi = lookup_char_pos(cm, cur.hi);
res += res += istr::from_estr(
#fmt["%s:%u:%u: %u:%u", #fmt["%s:%u:%u: %u:%u",
if some(lo.filename) == prev_file { if some(lo.filename) == prev_file {
"-" "-"
} else { } else {
istr::to_estr(lo.filename) istr::to_estr(lo.filename)
}, lo.line, lo.col, hi.line, hi.col]; }, lo.line, lo.col, hi.line, hi.col]);
alt cur.expanded_from { alt cur.expanded_from {
os_none. { break; } os_none. { break; }
os_some(new_sp) { os_some(new_sp) {
cur = *new_sp; cur = *new_sp;
prev_file = some(lo.filename); prev_file = some(lo.filename);
res += "<<"; res += ~"<<";
} }
} }
} }
@ -100,24 +100,25 @@ fn span_to_str(sp: &span, cm: &codemap) -> str {
ret res; ret res;
} }
fn emit_diagnostic(sp: &option::t<span>, msg: &str, kind: &str, color: u8, fn emit_diagnostic(sp: &option::t<span>, msg: &istr, kind: &istr, color: u8,
cm: &codemap) { cm: &codemap) {
let ss = ""; let ss = ~"";
let maybe_lines: option::t<@file_lines> = none; let maybe_lines: option::t<@file_lines> = none;
alt sp { alt sp {
some(ssp) { some(ssp) {
ss = span_to_str(ssp, cm) + " "; ss = span_to_str(ssp, cm) + ~" ";
maybe_lines = some(span_to_lines(ssp, cm)); maybe_lines = some(span_to_lines(ssp, cm));
} }
none. { } none. { }
} }
io::stdout().write_str(istr::from_estr(ss)); io::stdout().write_str(ss);
if term::color_supported() { if term::color_supported() {
term::fg(io::stdout().get_buf_writer(), color); term::fg(io::stdout().get_buf_writer(), color);
} }
io::stdout().write_str(istr::from_estr(#fmt["%s:", kind])); io::stdout().write_str(istr::from_estr(#fmt["%s:", istr::to_estr(kind)]));
if term::color_supported() { term::reset(io::stdout().get_buf_writer()); } if term::color_supported() { term::reset(io::stdout().get_buf_writer()); }
io::stdout().write_str(istr::from_estr(#fmt[" %s\n", msg])); io::stdout().write_str(istr::from_estr(#fmt[" %s\n",
istr::to_estr(msg)]));
maybe_highlight_lines(sp, cm, maybe_lines); maybe_highlight_lines(sp, cm, maybe_lines);
} }
@ -129,12 +130,11 @@ fn maybe_highlight_lines(sp: &option::t<span>, cm: &codemap,
some(lines) { some(lines) {
// If we're not looking at a real file then we can't re-open it to // If we're not looking at a real file then we can't re-open it to
// pull out the lines // pull out the lines
if lines.name == "-" { ret; } if lines.name == ~"-" { ret; }
// FIXME: reading in the entire file is the worst possible way to // FIXME: reading in the entire file is the worst possible way to
// get access to the necessary lines. // get access to the necessary lines.
let file = istr::to_estr( let file = io::read_whole_file_str(lines.name);
io::read_whole_file_str(istr::from_estr(lines.name)));
let fm = get_filemap(cm, lines.name); let fm = get_filemap(cm, lines.name);
// arbitrarily only print up to six lines of the error // arbitrarily only print up to six lines of the error
@ -151,8 +151,8 @@ fn maybe_highlight_lines(sp: &option::t<span>, cm: &codemap,
istr::from_estr(#fmt["%s:%u ", istr::from_estr(#fmt["%s:%u ",
istr::to_estr(fm.name), line + 1u])); istr::to_estr(fm.name), line + 1u]));
let s = get_line(fm, line as int, file); let s = get_line(fm, line as int, file);
if !str::ends_with(s, "\n") { s += "\n"; } if !istr::ends_with(s, ~"\n") { s += ~"\n"; }
io::stdout().write_str(istr::from_estr(s)); io::stdout().write_str(s);
} }
if elided { if elided {
let last_line = display_lines[vec::len(display_lines) - 1u]; let last_line = display_lines[vec::len(display_lines) - 1u];
@ -194,17 +194,17 @@ fn maybe_highlight_lines(sp: &option::t<span>, cm: &codemap,
} }
} }
fn emit_warning(sp: &option::t<span>, msg: &str, cm: &codemap) { fn emit_warning(sp: &option::t<span>, msg: &istr, cm: &codemap) {
emit_diagnostic(sp, msg, "warning", 11u8, cm); emit_diagnostic(sp, msg, ~"warning", 11u8, cm);
} }
fn emit_error(sp: &option::t<span>, msg: &str, cm: &codemap) { fn emit_error(sp: &option::t<span>, msg: &istr, cm: &codemap) {
emit_diagnostic(sp, msg, "error", 9u8, cm); emit_diagnostic(sp, msg, ~"error", 9u8, cm);
} }
fn emit_note(sp: &option::t<span>, msg: &str, cm: &codemap) { fn emit_note(sp: &option::t<span>, msg: &istr, cm: &codemap) {
emit_diagnostic(sp, msg, "note", 10u8, cm); emit_diagnostic(sp, msg, ~"note", 10u8, cm);
} }
type file_lines = {name: str, lines: [uint]}; type file_lines = {name: istr, lines: [uint]};
fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines { fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
let lo = lookup_char_pos(cm, sp.lo); let lo = lookup_char_pos(cm, sp.lo);
@ -213,10 +213,10 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
for each i: uint in uint::range(lo.line - 1u, hi.line as uint) { for each i: uint in uint::range(lo.line - 1u, hi.line as uint) {
lines += [i]; lines += [i];
} }
ret @{name: istr::to_estr(lo.filename), lines: lines}; ret @{name: lo.filename, lines: lines};
} }
fn get_line(fm: filemap, line: int, file: &str) -> str { fn get_line(fm: filemap, line: int, file: &istr) -> istr {
let begin: uint = fm.lines[line].byte - fm.start_pos.byte; let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
let end: uint; let end: uint;
if line as uint < vec::len(fm.lines) - 1u { if line as uint < vec::len(fm.lines) - 1u {
@ -225,17 +225,17 @@ fn get_line(fm: filemap, line: int, file: &str) -> str {
// If we're not done parsing the file, we're at the limit of what's // 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 // parsed. If we just slice the rest of the string, we'll print out
// the remainder of the file, which is undesirable. // the remainder of the file, which is undesirable.
end = str::byte_len(file); end = istr::byte_len(file);
let rest = str::slice(file, begin, end); let rest = istr::slice(file, begin, end);
let newline = str::index(rest, '\n' as u8); let newline = istr::index(rest, '\n' as u8);
if newline != -1 { end = begin + (newline as uint); } if newline != -1 { end = begin + (newline as uint); }
} }
ret str::slice(file, begin, end); ret istr::slice(file, begin, end);
} }
fn get_filemap(cm: codemap, filename: str) -> filemap { fn get_filemap(cm: codemap, filename: istr) -> filemap {
for fm: filemap in cm.files { for fm: filemap in cm.files {
if fm.name == istr::from_estr(filename) { ret fm; } if fm.name == filename { ret fm; }
} }
//XXjdm the following triggers a mismatched type bug //XXjdm the following triggers a mismatched type bug
// (or expected function, found _|_) // (or expected function, found _|_)

View file

@ -81,7 +81,7 @@ fn new_reader(cm: &codemap::codemap, src: &istr, filemap: codemap::filemap,
fn err(m: &istr) { fn err(m: &istr) {
codemap::emit_error( codemap::emit_error(
some(ast_util::mk_sp(chpos, chpos)), some(ast_util::mk_sp(chpos, chpos)),
istr::to_estr(m), cm); m, cm);
} }
} }
let strs: [istr] = []; let strs: [istr] = [];

View file

@ -113,12 +113,12 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader,
} }
fn fatal(m: &istr) -> ! { fn fatal(m: &istr) -> ! {
codemap::emit_error(some(self.get_span()), codemap::emit_error(some(self.get_span()),
istr::to_estr(m), sess.cm); m, sess.cm);
fail; fail;
} }
fn warn(m: &istr) { fn warn(m: &istr) {
codemap::emit_warning(some(self.get_span()), codemap::emit_warning(some(self.get_span()),
istr::to_estr(m), sess.cm); m, sess.cm);
} }
fn restrict(r: restriction) { restr = r; } fn restrict(r: restriction) { restr = r; }
fn get_restriction() -> restriction { ret restr; } fn get_restriction() -> restriction { ret restr; }
@ -2581,8 +2581,8 @@ fn parse_crate_from_file(input: &istr, cfg: &ast::crate_cfg,
} else if istr::ends_with(input, ~".rs") { } else if istr::ends_with(input, ~".rs") {
parse_crate_from_source_file(input, cfg, sess) parse_crate_from_source_file(input, cfg, sess)
} else { } else {
codemap::emit_error(none, "unknown input file type: " codemap::emit_error(none, ~"unknown input file type: "
+ istr::to_estr(input), + input,
sess.cm); sess.cm);
fail fail
} }