Skip to content

Commit

Permalink
Fix assembler error messsage panic
Browse files Browse the repository at this point in the history
Under specific circumstances, an error message may be generated for
the final characters of the source code line, in which case the
`nom_locate` column may not represent the position of the error in
the string, since `nom` counts `\n`.

With this fix, the position of the error is the minimum between the
`nom_locate` position and the length of the source string, this
eliminating the panic in all circumstances.
  • Loading branch information
tomaz-suller committed Mar 6, 2023
1 parent 3b3b3b0 commit 6af2954
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mvn-assembler/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub fn print(
}

fn print_error(program: &str, error: MvnReportError) {
let line: usize = (error.position.line - 1).try_into().unwrap();
let source = program.lines().nth(line).unwrap();
let column = error.position.column;
// let span_length = error.span().len();
let line: usize = error.position.line.try_into().unwrap();
let source = program.lines().nth(line - 1).unwrap();
let column = error.position.column.min(source.len() - 1);

let message = error.message.unwrap_or_default();

let snippet = Snippet {
Expand Down

0 comments on commit 6af2954

Please sign in to comment.