From 3ce43f37d2ba033ff7f4bfddff6edc59ff8aabc6 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 13 Jul 2011 15:19:59 -0400 Subject: [PATCH] Fix pre-existing problem with filemap line positions always starting at 0. Fix error line output to only retrieve up to the nearest newline. --- src/comp/syntax/codemap.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/comp/syntax/codemap.rs b/src/comp/syntax/codemap.rs index 1a627619bd3..4b6aa40327a 100644 --- a/src/comp/syntax/codemap.rs +++ b/src/comp/syntax/codemap.rs @@ -26,7 +26,7 @@ fn new_codemap() -> codemap { } fn new_filemap(filename filename, uint start_pos) -> filemap { - ret @rec(name=filename, start_pos=start_pos, mutable lines=~[0u]); + ret @rec(name=filename, start_pos=start_pos, mutable lines=[start_pos]); } fn next_line(filemap file, uint pos) { file.lines += ~[pos]; } @@ -170,10 +170,18 @@ fn span_to_lines(span sp, codemap::codemap cm) -> @file_lines { fn get_line(filemap fm, int line, &str file) -> str { let uint begin = fm.lines.(line) - fm.start_pos; let uint end; - if ((line as uint) + 1u >= ivec::len(fm.lines)) { - end = str::byte_len(file); - } else { + if (line as uint < ivec::len(fm.lines) - 1u) { end = fm.lines.(line + 1) - fm.start_pos; + } 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. + end = str::byte_len(file); + auto rest = str::slice(file, begin, end); + auto newline = str::index(rest, '\n' as u8); + if (newline != -1) { + end = begin + (newline as uint); + } } ret str::slice(file, begin, end); }