@@ -756,7 +756,7 @@ where
756
756
757
757
/// Parse an if-else expression after the if keyword
758
758
/// 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 > {
760
760
let condition = self . parse_expression ( )
761
761
. synchronize ( self ) ;
762
762
@@ -767,37 +767,33 @@ where
767
767
let then = self . parse_block ( ) ;
768
768
769
769
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
+ } ,
777
774
778
- let ( condition, then, otherwise) = self . parse_condblock ( ) ?;
775
+ Some ( Token { kind : TokenKind :: Keyword ( Keyword :: ElseIf ) , pos, .. } ) => {
776
+ self . step ( ) ;
779
777
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 ( ) ?;
786
779
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 ( ) ) ?,
792
792
793
- Ok ( block)
794
- } ,
795
- token => Err ( Error :: unexpected_msg ( token. clone ( ) , "end or else" ) )
796
- } . with_sync ( sync:: Strategy :: block_terminator ( ) ) ?,
797
793
None => Err ( Error :: unexpected_eof ( ) )
798
794
. with_sync ( sync:: Strategy :: eof ( ) ) ?
799
795
} ;
800
796
801
- Ok ( ( condition, then, otherwise) )
797
+ Ok ( ( Box :: new ( condition) , then, otherwise) )
802
798
}
803
799
}
0 commit comments