From d2ceb6de999d44bcd01d4722566ec0bdeb8dfce8 Mon Sep 17 00:00:00 2001 From: hexaredecimal Date: Sat, 6 Apr 2024 13:16:36 +0200 Subject: [PATCH] parser: Final touch ups on the errors --- src/parser/parser.rs | 9 +++++---- src/parser/rules.rs | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 40109c23..9d07da53 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -118,9 +118,9 @@ impl Parser { let token = self.next()?; match &token.kind { TokenKind::Identifier(n) => Ok(n.to_string()), - _ => { - let mut error = self.make_error_msg(token.pos, format!("Expected Identifier, found {}", token.raw)); - let hint = self.make_hint_msg(format!("replace the symbol `{}` with an identifier. Example `Foo`", token.raw)); + other => { + let mut error = self.make_error_msg(token.pos, format!("Expected Identifier, found `{other}`",)); + let hint = self.make_hint_msg(format!("replace the symbol `{other}` with an identifier. Example `Foo`")); error.push_str(&hint); Err(error) } @@ -128,9 +128,10 @@ impl Parser { } pub(super) fn make_error(&mut self, token_kind: TokenKind, other: Token) -> String { + let other_kind = &other.kind; self.make_error_msg( other.pos, - format!("Token `{token_kind}` not found, found `{}`", other.raw), + format!("Token `{token_kind}` not found, found `{other_kind}`"), ) } diff --git a/src/parser/rules.rs b/src/parser/rules.rs index 1f945341..9b0bb799 100644 --- a/src/parser/rules.rs +++ b/src/parser/rules.rs @@ -166,10 +166,12 @@ impl Parser { TokenKind::Assign => self.parse_inline_function()?, _ => { let token = self.peek()?; + let token_kind = token.kind; let mut error = self.make_error_msg( token.pos, - format!("Expected `{{` or `=`, got {}", token.raw), + format!("Expected `{{` or `=`, got `{token_kind}`",), ); + let hint = self.make_hint_msg(format!( "Try the following:\nfn {name}(...) = expression\nOr\nfn {name}(...) {{ ... }}" ));