Skip to content

Commit

Permalink
fix diagnostics for simple error, with correct line number
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Aug 27, 2024
1 parent a2b2ed9 commit 2906271
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
26 changes: 12 additions & 14 deletions crates/filerunner/tests/error_message_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@ pub fn setup_logger() {
}

#[test]
#[ignore = "this still needs work"]
fn test_error_message() {
setup_logger();

let mut test_file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_file_path.push("tests/test_error.las");
let file_name = test_file_path.to_str().unwrap();

let file_content = fs::read_to_string(file_name).expect("Should be able to read string");

let mut parser = Parser::new(&file_content);
let program = parser.parse_program();

let first_expected_parse_error = "Error on line 2: expected binding to let statement\n";
let second_expected_parse_error = "Error on line 4: expected identifier in let statement\n";
let expected_error_messages = [
"Error on line 2: expected binding to let statement\n",
"Error on line 4: expected identifier in let statement\n",
];

match program {
ParsedProgram::InvalidProgram(parse_errors) => {
if parse_errors.len() != 2 {
for ele in parse_errors {
println!("{ele}");
}
panic!("Expected 2 parse errors, but got the above ones instaed");
panic!("Expected 2 parse errors, but got the above ones instead");
}
for (idx, expected_message) in expected_error_messages.iter().enumerate() {
assert_eq!(
expected_message,
&parse_errors.get(idx).unwrap().to_string()
);
}

assert_eq!(
first_expected_parse_error,
parse_errors.first().unwrap().to_string()
);
assert_eq!(
second_expected_parse_error,
parse_errors.get(1).unwrap().to_string()
);
}
_ => panic!("Should return error"),
}
Expand Down
21 changes: 18 additions & 3 deletions crates/interpreter/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@ impl<'a> Parser<'a> {
statements.push(parsed_statement)
}
Err(parse_error) => {
// TODO: maybe it is possible to defer creating the error object
// with the correct document location to here
event!(
Level::DEBUG,
"Found an error on line {}",
&parse_error.parse_error.token.location.line_number
);
event!(Level::DEBUG, "Lexer is at: {:?}", self.lexer.peek());

match self.lexer.peek() {
Some(current_token) => {
if current_token.location > parse_error.parse_error.token.location {
parse_errors.push(parse_error);
continue;
}
}
None => break,
}

parse_errors.push(parse_error);
self.lexer.iterate_to_next_statement();
parse_errors.push(parse_error)
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/parser/expressions/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ mod tests {
parser::{
ast::{Identifier, Operator, PrefixOperator, StatementType},
expressions::expression::Expression,
lexer::token::{Token, TokenKind},
lexer::token::TokenKind,
},
test_util,
};
Expand Down
19 changes: 2 additions & 17 deletions crates/interpreter/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,9 @@ impl<'a> Lexer<'a> {
/// However, we need to figure out a good strategy to parse, since we dont use
/// statement seperators, and newlines dont matter
pub fn iterate_to_next_statement(&mut self) {
// FIXME: This does not work if the parser fails on a token that
// is a valid statement-starter, as it will loop forever
// if self
// .current_token
// .as_ref()
// .map_or(false, |token| token.token_kind.is_beginning_of_statement())
// {
// event!(
// Level::TRACE,
// "Current token {:?} is already valid beginning",
// self.current_token
// );
// return;
// }

event!(Level::TRACE, "Iterating to next statement");
while let Some(token) = self.consume() {
event!(Level::TRACE, "Iterating to next statement: {:?}", token);
if token.token_kind.is_beginning_of_statement() {
if let TokenKind::NewLine = token.token_kind {
event!(Level::TRACE, "Found beginning, stop advancing");
break;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/parser/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Debug for Token {
}
}

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Debug, Clone, Eq, PartialOrd, Ord)]
pub struct Location {
pub line_number: u16,
}
Expand Down

0 comments on commit 2906271

Please sign in to comment.