Skip to content

Commit

Permalink
parser: Final touch ups on the errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaredecimal committed Apr 6, 2024
1 parent 75512b2 commit d2ceb6d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,20 @@ 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)
}
}
}

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}`"),
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/parser/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}(...) {{ ... }}"
));
Expand Down

0 comments on commit d2ceb6d

Please sign in to comment.