Improve code display in error msg
This commit is contained in:
parent
52bb445f90
commit
6e2b7da22f
@ -2,21 +2,22 @@ use super::{LexError, PrintableError};
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
impl PrintableError for LexError {
|
impl PrintableError for LexError {
|
||||||
// TODO: Count and show line number
|
|
||||||
fn get_error_str(&self, chars: &Vec<char>) -> String {
|
fn get_error_str(&self, chars: &Vec<char>) -> String {
|
||||||
let line_number = get_line_number(chars, self.position);
|
let line_number = get_line_number(chars, self.position);
|
||||||
let (erroneous_code, back_count) = get_line(chars, self.position);
|
let (erroneous_code, column_number_zero) = get_line(chars, self.position);
|
||||||
|
let column_number = column_number_zero + 1;
|
||||||
|
|
||||||
let whitespace = " ".repeat(back_count + line_number.to_string().len() + 1);
|
let line_number_whitespace = " ".repeat(line_number.to_string().len());
|
||||||
|
let whitespace = " ".repeat(column_number_zero);
|
||||||
|
let reason = &self.reason;
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"\n{}|{}\n{}^\n\n{}{}\n{}",
|
r#"
|
||||||
line_number,
|
{line_number_whitespace} |
|
||||||
erroneous_code,
|
{line_number } | {erroneous_code}
|
||||||
whitespace,
|
{line_number_whitespace} | {whitespace}^
|
||||||
"Invalid character at pos ",
|
|
||||||
self.position + 1,
|
{reason} at line {line_number}:{column_number}"#,
|
||||||
self.reason,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,8 +105,12 @@ mod tests {
|
|||||||
let err_str = err_data.get_error_str(&chars);
|
let err_str = err_data.get_error_str(&chars);
|
||||||
|
|
||||||
let expected_str = format!(
|
let expected_str = format!(
|
||||||
"\n1|{}\n {}^\n\nInvalid character at pos 9\n{}",
|
r#"
|
||||||
"val name' = 20", " ", "Unrecognized character `'` (escaped: `\\'`)"
|
|
|
||||||
|
1 | val name' = 20
|
||||||
|
| ^
|
||||||
|
|
||||||
|
Illegal character `'` (escaped: \') at line 1:9"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(expected_str, err_str,);
|
assert_eq!(expected_str, err_str,);
|
||||||
|
@ -120,7 +120,7 @@ fn next_token(
|
|||||||
let error = LexError {
|
let error = LexError {
|
||||||
position: current_pos,
|
position: current_pos,
|
||||||
reason: format!(
|
reason: format!(
|
||||||
"Unrecognized character `{}` (escaped: `{}`)",
|
"Illegal character `{}` (escaped: {})",
|
||||||
next_char,
|
next_char,
|
||||||
next_char.escape_default().to_string(),
|
next_char.escape_default().to_string(),
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user