Skip to content

Commit df04fe3

Browse files
committed
Fix error reporting caret
Whenever an error is reported along with a source location, a caret (^) is placed beneath the origin of error. It was misplaced in a lot of cases, because the lexer was only storing the source line from first non-whitespace character.
1 parent edf44d3 commit df04fe3

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,14 @@ impl QccErrorLoc {
252252
let row = self.1.borrow().row().to_string();
253253
let mut col = self.1.borrow().col();
254254

255-
let src_fmt = format!("\t{}\t{}", row, src);
255+
let src_fmt = format!(" {} | {}", row, src);
256256

257257
eprintln!("{}", self);
258258
eprint!("{src_fmt}");
259259

260-
col += 1 + row.len(); // +2 for inserted tabs, -1 for starting index
261-
// with 1, effectively +1
260+
// +11 for chars for manual inserted whitespces in src_fmt, +length of
261+
// row number, row is always > 0, in parse() it is initialized as (1,1)
262+
col += 11 + row.len().ilog10() as usize;
262263

263264
for c in src_fmt.chars() {
264265
if col > 0 {

src/lexer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl fmt::Debug for Location {
6565
/// `Pointer` is a movable reference into a buffer.
6666
#[derive(Debug)]
6767
pub(crate) struct Pointer {
68-
/// start of the line, as seen in source, points first non-whitespace char
68+
/// start of the line, as seen in source, points first char
6969
start: usize,
7070
/// index at previous recognized token
7171
prev: usize,
@@ -424,10 +424,6 @@ Please report this bug to {}",
424424

425425
return Some(());
426426
}
427-
// Move Ptr::start to first non-whitespace char.
428-
if self.buffer[self.ptr.start].is_ascii_whitespace() {
429-
self.ptr.start += 1;
430-
}
431427
self.ptr.end += 1;
432428
}
433429
self.ptr.end += 1;

0 commit comments

Comments
 (0)