about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/rlox/src/errors.rs1
-rw-r--r--users/tazjin/rlox/src/parser.rs16
2 files changed, 16 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/errors.rs b/users/tazjin/rlox/src/errors.rs
index bab3f4667a74..6bd922bc6c5e 100644
--- a/users/tazjin/rlox/src/errors.rs
+++ b/users/tazjin/rlox/src/errors.rs
@@ -2,6 +2,7 @@
 pub enum ErrorKind {
     UnexpectedChar(char),
     UnterminatedString,
+    UnmatchedParens,
 }
 
 #[derive(Debug)]
diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs
index 62a8b5fe4d47..e7266c2352b4 100644
--- a/users/tazjin/rlox/src/parser.rs
+++ b/users/tazjin/rlox/src/parser.rs
@@ -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],