From a6fc1b58f1ab6b4be02f2839c1e1e6c617c58a54 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 28 Jul 2022 15:52:45 +0100 Subject: [PATCH] parser: reorganise poor expression handling --- phpast/samples/array-assign.php | 3 +-- trunk_parser/src/parser/mod.rs | 44 +++++++++++++++++++++++++-------- trunk_parser/src/parser/punc.rs | 10 ++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/phpast/samples/array-assign.php b/phpast/samples/array-assign.php index 63b5ef3..a066b15 100644 --- a/phpast/samples/array-assign.php +++ b/phpast/samples/array-assign.php @@ -1,4 +1,3 @@ Expression::Variable(v.to_string()), - TokenKind::Int(i) => Expression::Int(*i), - TokenKind::Identifier(i) => Expression::Identifier(i.to_string()), - TokenKind::ConstantString(s) => Expression::ConstantString(s.to_string()), - TokenKind::True => Expression::Bool(true), - TokenKind::False => Expression::Bool(false), + TokenKind::Variable(v) => { + let e = Expression::Variable(v.to_string()); + self.next(); + e + }, + TokenKind::Int(i) => { + let e = Expression::Int(*i); + self.next(); + e + }, + TokenKind::Identifier(i) => { + let e = Expression::Identifier(i.to_string()); + self.next(); + e + }, + TokenKind::ConstantString(s) => { + let e = Expression::ConstantString(s.to_string()); + self.next(); + e + }, + TokenKind::True => { + let e = Expression::Bool(true); + self.next(); + e + }, + TokenKind::False => { + let e = Expression::Bool(false); + self.next(); + e + }, TokenKind::LeftParen => { self.next(); let e = self.expression(0)?; - if self.current.kind != TokenKind::RightParen { - return Err(ParseError::ExpectedToken("expected right paren".into(), self.current.span)); - } + self.rparen()?; e }, @@ -747,6 +769,8 @@ impl Parser { self.next(); } } + + self.rbracket()?; Expression::Array(items) }, @@ -901,7 +925,7 @@ impl Parser { return Ok(lhs); } - self.next(); + // self.next(); loop { let kind = match &self.current { diff --git a/trunk_parser/src/parser/punc.rs b/trunk_parser/src/parser/punc.rs index 4a3a4d4..a627326 100644 --- a/trunk_parser/src/parser/punc.rs +++ b/trunk_parser/src/parser/punc.rs @@ -29,4 +29,14 @@ impl Parser { expect!(self, TokenKind::RightParen, (), "expected )"); Ok(()) } + + pub(crate) fn lbracket(&mut self) -> ParseResult<()> { + expect!(self, TokenKind::LeftBracket, (), "expected ["); + Ok(()) + } + + pub(crate) fn rbracket(&mut self) -> ParseResult<()> { + expect!(self, TokenKind::RightBracket, (), "expected ]"); + Ok(()) + } } \ No newline at end of file