Skip to content

Commit 0da934b

Browse files
committed
Refactor
1 parent ad93613 commit 0da934b

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/syntax/parser/mod.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ where
756756

757757
/// Parse an if-else expression after the if keyword
758758
/// Returns the if condition and the it+else blocks
759-
fn parse_condblock(&mut self) -> sync::Result<(ast::Expr, ast::Block, ast::Block), Error> {
759+
fn parse_condblock(&mut self) -> sync::Result<(Box<ast::Expr>, ast::Block, ast::Block), Error> {
760760
let condition = self.parse_expression()
761761
.synchronize(self);
762762

@@ -767,37 +767,33 @@ where
767767
let then = self.parse_block();
768768

769769
let otherwise = match self.token.take() {
770-
Some(token) => match token {
771-
Token { kind: TokenKind::Keyword(Keyword::End), .. } => {
772-
self.token = Some(token);
773-
Ok(ast::Block::default())
774-
},
775-
Token { kind: TokenKind::Keyword(Keyword::ElseIf), pos, .. } => {
776-
self.step();
770+
Some(token @ Token { kind: TokenKind::Keyword(Keyword::End), .. }) => {
771+
self.token = Some(token);
772+
ast::Block::default()
773+
},
777774

778-
let (condition, then, otherwise) = self.parse_condblock()?;
775+
Some(Token { kind: TokenKind::Keyword(Keyword::ElseIf), pos, .. }) => {
776+
self.step();
779777

780-
let stmt = ast::Statement::Expr(ast::Expr::If {
781-
condition: condition.into(),
782-
then: then,
783-
otherwise: otherwise,
784-
pos: pos,
785-
});
778+
let (condition, then, otherwise) = self.parse_condblock()?;
786779

787-
Ok(ast::Block::Block(Box::new([stmt])))
788-
},
789-
Token { kind: TokenKind::Keyword(Keyword::Else), .. } => {
790-
self.step();
791-
let block = self.parse_block();
780+
let stmt = ast::Statement::Expr(ast::Expr::If { condition, then, otherwise, pos, });
781+
782+
ast::Block::Block(Box::new([stmt]))
783+
},
784+
785+
Some(Token { kind: TokenKind::Keyword(Keyword::Else), .. }) => {
786+
self.step();
787+
self.parse_block()
788+
},
789+
790+
Some(token) => Err(Error::unexpected_msg(token, "end, else or elseif"))
791+
.with_sync(sync::Strategy::block_terminator())?,
792792

793-
Ok(block)
794-
},
795-
token => Err(Error::unexpected_msg(token.clone(), "end or else"))
796-
}.with_sync(sync::Strategy::block_terminator())?,
797793
None => Err(Error::unexpected_eof())
798794
.with_sync(sync::Strategy::eof())?
799795
};
800796

801-
Ok((condition, then, otherwise))
797+
Ok((Box::new(condition), then, otherwise))
802798
}
803799
}

0 commit comments

Comments
 (0)