@@ -88,14 +88,13 @@ use std::mem;
88
88
use std:: rc:: Rc ;
89
89
use std:: iter;
90
90
91
- #[ allow( non_camel_case_types) ]
92
- #[ deriving( PartialEq ) ]
93
- pub enum restriction {
94
- UNRESTRICTED ,
95
- RESTRICT_STMT_EXPR ,
96
- RESTRICT_NO_BAR_OP ,
97
- RESTRICT_NO_BAR_OR_DOUBLEBAR_OP ,
98
- RESTRICT_NO_STRUCT_LITERAL ,
91
+ bitflags ! {
92
+ flags Restrictions : u8 {
93
+ static Unrestricted = 0b0000 ,
94
+ static RestrictionStmtExpr = 0b0001 ,
95
+ static RestrictionNoBarOp = 0b0010 ,
96
+ static RestrictionNoStructLiteral = 0b0100
97
+ }
99
98
}
100
99
101
100
type ItemInfo = ( Ident , Item_ , Option < Vec < Attribute > > ) ;
@@ -314,7 +313,7 @@ pub struct Parser<'a> {
314
313
pub buffer_start : int ,
315
314
pub buffer_end : int ,
316
315
pub tokens_consumed : uint ,
317
- pub restriction : restriction ,
316
+ pub restrictions : Restrictions ,
318
317
pub quote_depth : uint , // not (yet) related to the quasiquoter
319
318
pub reader : Box < Reader +' a > ,
320
319
pub interner : Rc < token:: IdentInterner > ,
@@ -383,7 +382,7 @@ impl<'a> Parser<'a> {
383
382
buffer_start : 0 ,
384
383
buffer_end : 0 ,
385
384
tokens_consumed : 0 ,
386
- restriction : UNRESTRICTED ,
385
+ restrictions : Unrestricted ,
387
386
quote_depth : 0 ,
388
387
obsolete_set : HashSet :: new ( ) ,
389
388
mod_path_stack : Vec :: new ( ) ,
@@ -2189,7 +2188,7 @@ impl<'a> Parser<'a> {
2189
2188
if self . token == token:: LBRACE {
2190
2189
// This is a struct literal, unless we're prohibited
2191
2190
// from parsing struct literals here.
2192
- if self . restriction != RESTRICT_NO_STRUCT_LITERAL {
2191
+ if ! self . restrictions . contains ( RestrictionNoStructLiteral ) {
2193
2192
// It's a struct literal.
2194
2193
self . bump ( ) ;
2195
2194
let mut fields = Vec :: new ( ) ;
@@ -2651,12 +2650,9 @@ impl<'a> Parser<'a> {
2651
2650
2652
2651
// Prevent dynamic borrow errors later on by limiting the
2653
2652
// scope of the borrows.
2654
- match ( & self . token , & self . restriction ) {
2655
- ( & token:: BINOP ( token:: OR ) , & RESTRICT_NO_BAR_OP ) => return lhs,
2656
- ( & token:: BINOP ( token:: OR ) ,
2657
- & RESTRICT_NO_BAR_OR_DOUBLEBAR_OP ) => return lhs,
2658
- ( & token:: OROR , & RESTRICT_NO_BAR_OR_DOUBLEBAR_OP ) => return lhs,
2659
- _ => { }
2653
+ if self . token == token:: BINOP ( token:: OR ) &&
2654
+ self . restrictions . contains ( RestrictionNoBarOp ) {
2655
+ return lhs;
2660
2656
}
2661
2657
2662
2658
let cur_opt = token_to_binop ( & self . token ) ;
@@ -2730,7 +2726,7 @@ impl<'a> Parser<'a> {
2730
2726
/// Parse an 'if' expression ('if' token already eaten)
2731
2727
pub fn parse_if_expr ( & mut self ) -> P < Expr > {
2732
2728
let lo = self . last_span . lo ;
2733
- let cond = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2729
+ let cond = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2734
2730
let thn = self . parse_block ( ) ;
2735
2731
let mut els: Option < P < Expr > > = None ;
2736
2732
let mut hi = thn. span . hi ;
@@ -2791,7 +2787,7 @@ impl<'a> Parser<'a> {
2791
2787
let lo = self . last_span . lo ;
2792
2788
let pat = self . parse_pat ( ) ;
2793
2789
self . expect_keyword ( keywords:: In ) ;
2794
- let expr = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2790
+ let expr = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2795
2791
let loop_block = self . parse_block ( ) ;
2796
2792
let hi = self . span . hi ;
2797
2793
@@ -2800,7 +2796,7 @@ impl<'a> Parser<'a> {
2800
2796
2801
2797
pub fn parse_while_expr ( & mut self , opt_ident : Option < ast:: Ident > ) -> P < Expr > {
2802
2798
let lo = self . last_span . lo ;
2803
- let cond = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2799
+ let cond = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2804
2800
let body = self . parse_block ( ) ;
2805
2801
let hi = body. span . hi ;
2806
2802
return self . mk_expr ( lo, hi, ExprWhile ( cond, body, opt_ident) ) ;
@@ -2815,7 +2811,7 @@ impl<'a> Parser<'a> {
2815
2811
2816
2812
fn parse_match_expr ( & mut self ) -> P < Expr > {
2817
2813
let lo = self . last_span . lo ;
2818
- let discriminant = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2814
+ let discriminant = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2819
2815
self . commit_expr_expecting ( & * discriminant, token:: LBRACE ) ;
2820
2816
let mut arms: Vec < Arm > = Vec :: new ( ) ;
2821
2817
while self . token != token:: RBRACE {
@@ -2834,7 +2830,7 @@ impl<'a> Parser<'a> {
2834
2830
guard = Some ( self . parse_expr ( ) ) ;
2835
2831
}
2836
2832
self . expect ( & token:: FAT_ARROW ) ;
2837
- let expr = self . parse_expr_res ( RESTRICT_STMT_EXPR ) ;
2833
+ let expr = self . parse_expr_res ( RestrictionStmtExpr ) ;
2838
2834
2839
2835
let require_comma =
2840
2836
!classify:: expr_is_simple_block ( & * expr)
@@ -2856,15 +2852,15 @@ impl<'a> Parser<'a> {
2856
2852
2857
2853
/// Parse an expression
2858
2854
pub fn parse_expr ( & mut self ) -> P < Expr > {
2859
- return self . parse_expr_res ( UNRESTRICTED ) ;
2855
+ return self . parse_expr_res ( Unrestricted ) ;
2860
2856
}
2861
2857
2862
- /// Parse an expression, subject to the given restriction
2863
- pub fn parse_expr_res ( & mut self , r : restriction ) -> P < Expr > {
2864
- let old = self . restriction ;
2865
- self . restriction = r;
2858
+ /// Parse an expression, subject to the given restrictions
2859
+ pub fn parse_expr_res ( & mut self , r : Restrictions ) -> P < Expr > {
2860
+ let old = self . restrictions ;
2861
+ self . restrictions = r;
2866
2862
let e = self . parse_assign_expr ( ) ;
2867
- self . restriction = old;
2863
+ self . restrictions = old;
2868
2864
return e;
2869
2865
}
2870
2866
@@ -3153,9 +3149,9 @@ impl<'a> Parser<'a> {
3153
3149
self . look_ahead ( 2 , |t| {
3154
3150
* t != token:: COMMA && * t != token:: RBRACKET
3155
3151
} ) {
3156
- let start = self . parse_expr_res ( RESTRICT_NO_BAR_OP ) ;
3152
+ let start = self . parse_expr_res ( RestrictionNoBarOp ) ;
3157
3153
self . eat ( & token:: DOTDOT ) ;
3158
- let end = self . parse_expr_res ( RESTRICT_NO_BAR_OP ) ;
3154
+ let end = self . parse_expr_res ( RestrictionNoBarOp ) ;
3159
3155
pat = PatRange ( start, end) ;
3160
3156
} else if is_plain_ident ( & self . token ) && !can_be_enum_or_struct {
3161
3157
let id = self . parse_ident ( ) ;
@@ -3441,7 +3437,7 @@ impl<'a> Parser<'a> {
3441
3437
check_expected_item ( self , found_attrs) ;
3442
3438
3443
3439
// Remainder are line-expr stmts.
3444
- let e = self . parse_expr_res ( RESTRICT_STMT_EXPR ) ;
3440
+ let e = self . parse_expr_res ( RestrictionStmtExpr ) ;
3445
3441
P ( spanned ( lo, e. span . hi , StmtExpr ( e, ast:: DUMMY_NODE_ID ) ) )
3446
3442
}
3447
3443
}
@@ -3450,7 +3446,7 @@ impl<'a> Parser<'a> {
3450
3446
3451
3447
/// Is this expression a successfully-parsed statement?
3452
3448
fn expr_is_complete ( & mut self , e : & Expr ) -> bool {
3453
- self . restriction == RESTRICT_STMT_EXPR &&
3449
+ self . restrictions . contains ( RestrictionStmtExpr ) &&
3454
3450
!classify:: expr_requires_semi_to_be_stmt ( e)
3455
3451
}
3456
3452
0 commit comments