From f882942f3fb0adc66e1ecae41d48e5fe49141378 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 15 Mar 2023 16:41:04 -0500 Subject: [PATCH] Specific reason for failure for syntax error --- src/error_handling/syntax_error.rs | 7 ++++--- src/syntax/binding.rs | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/error_handling/syntax_error.rs b/src/error_handling/syntax_error.rs index e8353cb..a68d4b2 100644 --- a/src/error_handling/syntax_error.rs +++ b/src/error_handling/syntax_error.rs @@ -10,8 +10,9 @@ impl PrintableError for SyntaxError { let indicator = vec!['^'; length].iter().collect::(); format!( - "\n{}\n{}{}\n\n{}{}{}", - line, whitespace, indicator, "Syntax error at pos ", self.error_start, ": " + "\n{}\n{}{}\n\n{}{}{}\n{}", + line, whitespace, indicator, "Syntax error at pos ", self.error_start, ":", + self.reason ) } } @@ -121,7 +122,7 @@ mod tests { let (chars, error) = get_error_data(String::from("val")); let actual_err = error.get_error_str(&chars); // TODO: Write a better error message (something that explains why it failed) - let expected_str = format!("\n{}\n{}\n\n{}", "val", "^^^", "Syntax error at pos 0: "); + let expected_str = format!("\n{}\n{}\n\n{}\n{}", "val", "^^^", "Syntax error at pos 0:", "There should be an identifier after a `val` token"); assert_eq!(expected_str, actual_err); } diff --git a/src/syntax/binding.rs b/src/syntax/binding.rs index e237aca..9260464 100644 --- a/src/syntax/binding.rs +++ b/src/syntax/binding.rs @@ -46,10 +46,14 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option let identifier = try_token_type(tokens, pos + 1, TokenType::Identifier); if identifier.is_none() { - // TODO: return Error + // TODO: Differentiate between no token found and incorrect token found. + // TODO: // The parser didn't find an Identifier after VAL/VAR return Some(SyntaxResult::Err(SyntaxError { - reason: String::from("D:"), + reason: format!( + "There should be an identifier after a `{}` token", + if is_val {"val"} else {"var"} + ), error_start: binding_token.position, error_end: binding_token.position + binding_token.value.len(), }));