From 3a06690e4eb6b488a0c6c97934f8bdf36b8e60ef Mon Sep 17 00:00:00 2001 From: Prajwal S N Date: Fri, 22 Dec 2023 19:51:02 +0530 Subject: [PATCH] fix(parser): infinite loop on missing semicolon Signed-off-by: Prajwal S N --- lost_syntax/src/parse.rs | 57 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/lost_syntax/src/parse.rs b/lost_syntax/src/parse.rs index ae9c6e7..33e1aa5 100644 --- a/lost_syntax/src/parse.rs +++ b/lost_syntax/src/parse.rs @@ -448,40 +448,39 @@ impl<'a> Parser<'a> { fn parse_func_call(&mut self) -> Result { let mut expr = self.parse_primary()?; loop { - if let Some(t) = self.stream.peek() { - match t.kind { - TokenKind::LPAREN => { - // Consume the opening parenthesis - self.advance(); - let mut args = vec![]; - if self.advance_if(|t| t.kind == TokenKind::RPAREN).is_none() { - loop { - args.push(self.parse_expr()?); - if self.advance_if(|t| t.kind == TokenKind::COMMA).is_none() { - break; - } + let Some(t) = self.stream.peek() else { break }; + match t.kind { + TokenKind::LPAREN => { + // Consume the opening parenthesis + self.advance(); + let mut args = vec![]; + if self.advance_if(|t| t.kind == TokenKind::RPAREN).is_none() { + loop { + args.push(self.parse_expr()?); + if self.advance_if(|t| t.kind == TokenKind::COMMA).is_none() { + break; } - // Consume the closing parenthesis - self.advance_or_err(TokenKind::RPAREN, ErrorMsg::MissingClosingParen)?; } - expr = Expr::Call { - func: Box::new(expr), - args, - }; + // Consume the closing parenthesis + self.advance_or_err(TokenKind::RPAREN, ErrorMsg::MissingClosingParen)?; } - TokenKind::DOT => { - // Consume the dot - self.advance(); - expr = Expr::FieldGet { - object: Box::new(expr), - field: Literal::Ident( - self.advance_or_err(TokenKind::IDENT, ErrorMsg::InvalidIdent)? - .lexeme, - ), - } + expr = Expr::Call { + func: Box::new(expr), + args, + }; + } + TokenKind::DOT => { + // Consume the dot + self.advance(); + expr = Expr::FieldGet { + object: Box::new(expr), + field: Literal::Ident( + self.advance_or_err(TokenKind::IDENT, ErrorMsg::InvalidIdent)? + .lexeme, + ), } - _ => break, } + _ => break, } }