Skip to content

Commit

Permalink
fix(parser): improve expression parsing logic to handle end of tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
garritfra committed Dec 10, 2024
1 parent 4d01b3d commit 4219728
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
32 changes: 22 additions & 10 deletions src/parser/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ impl Parser {
// self
TokenKind::Keyword(Keyword::Selff) => Expression::Selff,
TokenKind::Identifier(val) => {
if !self.has_more() {
return Ok(Expression::Variable(val));
}
let next = self.peek()?;
match &next.kind {
// foo()
Expand All @@ -403,18 +406,27 @@ impl Parser {
other => return Err(format!("Expected Expression, found {:?}", other)),
};

// Check if the parsed expression continues
if let Ok(next) = self.peek() {
if next.kind == TokenKind::Dot {
// foo.bar
return self.parse_field_access(expr);
} else if BinOp::try_from(next.kind).is_ok() {
// 1 + 2
return self.parse_bin_op(Some(expr));
// Only try to peek if we have more tokens
if !self.has_more() {
return Ok(expr);
}

// Now it's safe to peek since we know we have more tokens
let next = self.peek()?;
match next.kind {
TokenKind::Dot => self.parse_field_access(expr),
kind if BinOp::try_from(kind.clone()).is_ok() => {
self.next()?; // consume the operator
let op = BinOp::try_from(kind).unwrap();
let rhs = self.parse_expression()?;
Ok(Expression::BinOp {
lhs: Box::from(expr),
op,
rhs: Box::from(rhs),
})
}
_ => Ok(expr),
}
// Nope, the expression was fully parsed
Ok(expr)
}

fn parse_field_access(&mut self, lhs: Expression) -> Result<Expression, String> {
Expand Down
1 change: 0 additions & 1 deletion src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ fn test_parse_inline_function() {
}

#[test]
#[ignore]
// I don't know how this fails yet. It seems to have something to do with how
// `parse_expression` peeks tokens. It tries to peek a token after the
// expression body but it's empty, so it errors out.
Expand Down

0 comments on commit 4219728

Please sign in to comment.