Skip to content

Commit

Permalink
parser: Adds more hints and improve the error output
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaredecimal committed Apr 6, 2024
1 parent 47efb52 commit 75512b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,19 @@ impl Parser {
let token = self.next()?;
match &token.kind {
TokenKind::Keyword(ref k) if k == &keyword => Ok(()),
_ => Err(self.make_error(TokenKind::SemiColon, token)),
_ => {
let mut error = self.make_error_msg(token.pos, format!("Expected keyword, found {}", token.raw));
let hint = self.make_hint_msg(format!("replace the symbol `{}` with the appropriate keyword. ", token.raw));
error.push_str(&hint);
Err(error)
}
}
}

pub(super) fn match_operator(&mut self) -> Result<BinOp, String> {
BinOp::try_from(self.next()?.kind)
}

pub(super) fn match_identifier(&mut self) -> Result<String, String> {
let token = self.next()?;
match &token.kind {
Expand All @@ -124,7 +130,7 @@ impl Parser {
pub(super) fn make_error(&mut self, token_kind: TokenKind, other: Token) -> String {
self.make_error_msg(
other.pos,
format!("Token {:?} not found, found {:?}", token_kind, other),
format!("Token `{token_kind}` not found, found `{}`", other.raw),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl Parser {
TokenKind::SquareBraceOpen => self.parse_array()?,
// new Foo {}
TokenKind::Keyword(Keyword::New) => self.parse_struct_initialization()?,
other => return Err(format!("Expected Expression, found {:?}", other)),
other => return Err(format!("Expected Expression, found `{other}`")),
};

// Check if the parsed expression continues
Expand Down

0 comments on commit 75512b2

Please sign in to comment.