From 603a244b781c7c5c9afd48c520f77ace4f1c9acd Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 29 Jul 2021 19:17:10 +0200 Subject: [PATCH] Nest the loop inside a match arm --- src/parser.rs | 62 ++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 978ca6a..e271488 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -237,39 +237,41 @@ impl<'source> Parser<'source> { let mut buf = None; match self.checked_next()? { - Token::RightBracket => return Ok(ExprKind::Cart(cart)), - t => buf = Some(self.parse_expr(t, &mut buf)?), - } - - 'cart: loop { - let value = loop { - match self.checked_next()? { - Token::Arrow => break buf.take(), - t => buf = Some(self.parse_expr(t, &mut buf)?), - } - } - .ok_or_else(|| { - Error::new(ErrorKind::UnexpectedToken(Token::Arrow), self.lexer.span()) - })?; - - let key = loop { - match self.checked_next()? { - Token::RightBracket => { - cart.push(( - value, - buf.take() - .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?, - )); - - break 'cart; + Token::RightBracket => (), + t => { + buf = Some(self.parse_expr(t, &mut buf)?); + 'cart: loop { + let value = loop { + match self.checked_next()? { + Token::Arrow => break buf.take(), + t => buf = Some(self.parse_expr(t, &mut buf)?), + } } - Token::Comma => break buf.take(), - t => buf = Some(self.parse_expr(t, &mut buf)?), + .ok_or_else(|| { + Error::new(ErrorKind::UnexpectedToken(Token::Arrow), self.lexer.span()) + })?; + + let key = loop { + match self.checked_next()? { + Token::RightBracket => { + cart.push(( + value, + buf.take().ok_or_else(|| { + Error::unexpected_eof(self.lexer.span().start) + })?, + )); + + break 'cart; + } + Token::Comma => break buf.take(), + t => buf = Some(self.parse_expr(t, &mut buf)?), + } + } + .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?; + + cart.push((value, key)); } } - .ok_or_else(|| Error::unexpected_eof(self.lexer.span().start))?; - - cart.push((value, key)); } Ok(ExprKind::Cart(cart))