From 9f903556f9e3c1eae571a0baee23136c2e328b4c Mon Sep 17 00:00:00 2001 From: Seymur Bagirov Date: Sun, 5 Jan 2025 06:26:31 +0400 Subject: [PATCH] feat: implement #3 challenge for ch6 i kinda cheated, i was checking the answers and then stumbled upon the grammar. Sorry :( --- src/parser.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index b65ba4a..853e5f1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -90,6 +90,11 @@ impl Parser<'_> { self.primary() } + /* error boundaries: + ("!=" | "==") equality + | (">" | ">=" | "<" | "<=") comparison + | ("+") term + | ("/" | "*") factor ; */ fn primary(&mut self) -> Result { use LiteralType::*; use TokenType::*; @@ -123,6 +128,38 @@ impl Parser<'_> { }); } + if self.match_token(&[Equal, BangEqual]) { + let _ = self.equality(); + return Err(ParseError { + token: self.previous().clone(), + msg: "Missing left-hand operand.".to_string(), + }); + } + + if self.match_token(&[Greater, GreaterEqual, Less, LessEqual]) { + let _ = self.comparison(); + return Err(ParseError { + token: self.previous().clone(), + msg: "Missing left-hand operand.".to_string(), + }); + } + + if self.match_token(&[Plus]) { + let _ = self.term(); + return Err(ParseError { + token: self.previous().clone(), + msg: "Missing left-hand operand.".to_string(), + }); + } + + if self.match_token(&[Star, Slash]) { + let _ = self.factor(); + return Err(ParseError { + token: self.previous().clone(), + msg: "Missing left-hand operand.".to_string(), + }); + } + Err(ParseError { token: self.peek().clone(), msg: "Expect expression.".to_string(),