@@ -771,9 +771,9 @@ class parser {
771
771
} else if self . eat_keyword ( "if" ) {
772
772
ret pexpr ( self . parse_if_expr ( ) ) ;
773
773
} else if self . eat_keyword ( "for" ) {
774
- ret pexpr ( self . parse_for_expr ( ) ) ;
774
+ ret pexpr ( self . parse_sugary_call_expr ( "for" , expr_loop_body ) ) ;
775
775
} else if self . eat_keyword ( "do" ) {
776
- ret pexpr ( self . parse_do_expr ( ) ) ;
776
+ ret pexpr ( self . parse_sugary_call_expr ( "do" , expr_do_body ) ) ;
777
777
} else if self . eat_keyword ( "while" ) {
778
778
ret pexpr ( self . parse_while_expr ( ) ) ;
779
779
} else if self . eat_keyword ( "loop" ) {
@@ -1283,36 +1283,20 @@ class parser {
1283
1283
}
1284
1284
}
1285
1285
1286
- fn parse_for_expr ( ) -> @expr {
1286
+ fn parse_sugary_call_expr ( keyword : str , ctor : fn ( +@expr ) -> expr_ ) -> @expr {
1287
1287
let lo = self . last_span;
1288
1288
let call = self . parse_expr_res( RESTRICT_STMT_EXPR ) ;
1289
1289
alt call. node {
1290
1290
expr_call( f, args, true ) {
1291
1291
let b_arg = vec:: last( args) ;
1292
1292
let last = self . mk_expr( b_arg. span. lo, b_arg. span. hi,
1293
- expr_loop_body ( b_arg) ) ;
1293
+ ctor ( b_arg) ) ;
1294
1294
@{ node: expr_call( f, vec:: init( args) + [ last] , true )
1295
1295
with * call}
1296
1296
}
1297
1297
_ {
1298
- self . span_fatal( lo, "`for` must be followed by a block call" ) ;
1299
- }
1300
- }
1301
- }
1302
-
1303
- fn parse_do_expr( ) -> @expr {
1304
- let lo = self . last_span;
1305
- let call = self . parse_expr_res( RESTRICT_STMT_EXPR ) ;
1306
- alt call. node {
1307
- expr_call( f, args, true ) {
1308
- let b_arg = vec:: last( args) ;
1309
- let last = self . mk_expr( b_arg. span. lo, b_arg. span. hi,
1310
- expr_do_body( b_arg) ) ;
1311
- @{ node: expr_call( f, vec:: init( args) + [ last] , true )
1312
- with * call}
1313
- }
1314
- _ {
1315
- self . span_fatal( lo, "`do` must be followed by a block call" ) ;
1298
+ self . span_fatal(
1299
+ lo, #fmt( "`%s` must be followed by a block call" , keyword) ) ;
1316
1300
}
1317
1301
}
1318
1302
}
0 commit comments