Specific reason for failure for syntax error

master
Araozu 2023-03-15 16:41:04 -05:00
parent e383d300f2
commit f882942f3f
2 changed files with 10 additions and 5 deletions

View File

@ -10,8 +10,9 @@ impl PrintableError for SyntaxError {
let indicator = vec!['^'; length].iter().collect::<String>();
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);
}

View File

@ -46,10 +46,14 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult>
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(),
}));