Fix bad line printing for parse errors

The code that extracted lines from partially-parsed files
was broken.

Closes #1848
This commit is contained in:
Marijn Haverbeke 2012-02-15 10:19:51 +01:00
parent 6627890f6b
commit 9ff5ba085d
3 changed files with 20 additions and 12 deletions

View File

@ -204,8 +204,7 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
// Print the offending lines
for line: uint in display_lines {
io::stderr().write_str(#fmt["%s:%u ", fm.name, line + 1u]);
let s = codemap::get_line(fm, line as int);
if !str::ends_with(s, "\n") { s += "\n"; }
let s = codemap::get_line(fm, line as int) + "\n";
io::stderr().write_str(s);
}
if elided {

View File

@ -157,16 +157,11 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
fn get_line(fm: filemap, line: int) -> str unsafe {
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
let end: uint;
if line as uint < vec::len(fm.lines) - 1u {
end = fm.lines[line + 1].byte - fm.start_pos.byte;
ret str::unsafe::slice_bytes(*fm.src, begin, end);
} 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.
ret str::splitn_char(*fm.src, '\n', 1u)[0];
}
let end = alt str::byte_index(*fm.src, '\n' as u8, begin) {
some(e) { e }
none { str::len(*fm.src) }
};
str::unsafe::slice_bytes(*fm.src, begin, end)
}
fn lookup_byte_offset(cm: codemap::codemap, chpos: uint)

View File

@ -68,6 +68,7 @@ export
// Searching
index,
byte_index,
rindex,
find,
find_bytes,
@ -852,6 +853,19 @@ fn index(ss: str, cc: char) -> option<uint> {
ret option::none;
}
// Function: byte_index
//
// Returns the index of the first matching byte
// (as option some/none)
fn byte_index(s: str, b: u8, start: uint) -> option<uint> {
let i = start, l = len_bytes(s);
while i < l {
if s[i] == b { ret some(i); }
i += 1u;
}
ret none;
}
// Function: rindex
//
// Returns the index of the first matching char