feat(tazjin/rlox): Implement parsing of parenthesised expressions

Change-Id: I0e6bd71fd787b719104ef93fc52df4090dc415b9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2234
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2020-12-06 15:28:16 +01:00 committed by tazjin
parent 28002fcea5
commit 4812fc2ee6
2 changed files with 16 additions and 1 deletions

View file

@ -2,6 +2,7 @@
pub enum ErrorKind {
UnexpectedChar(char),
UnterminatedString,
UnmatchedParens,
}
#[derive(Debug)]

View file

@ -118,7 +118,9 @@ impl<'a> Parser<'a> {
TokenKind::String(string) => Literal::String(string),
TokenKind::LeftParen => {
unimplemented!("need error handling to deal with unbalanced parens");
let expr = self.expression()?;
self.consume(&TokenKind::RightParen, ErrorKind::UnmatchedParens)?;
return Ok(Expr::Grouping(Grouping(Box::new(expr))));
}
// This branch indicates a parser bug, not invalid input.
@ -168,6 +170,18 @@ impl<'a> Parser<'a> {
self.tokens[self.current - 1].clone()
}
fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result<(), Error> {
if self.check_token(kind) {
self.advance();
return Ok(());
}
Err(Error {
line: self.peek().line,
kind: err,
})
}
fn binary_operator(
&mut self,
oneof: &[TokenKind],