@@ -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 ) ;
@@ -2696,15 +2692,16 @@ impl<'a> Parser<'a> {
2696
2692
pub fn parse_assign_expr ( & mut self ) -> P < Expr > {
2697
2693
let lo = self . span . lo ;
2698
2694
let lhs = self . parse_binops ( ) ;
2695
+ let restrictions = self . restrictions & RestrictionNoStructLiteral ;
2699
2696
match self . token {
2700
2697
token:: EQ => {
2701
2698
self . bump ( ) ;
2702
- let rhs = self . parse_expr ( ) ;
2699
+ let rhs = self . parse_expr_res ( restrictions ) ;
2703
2700
self . mk_expr ( lo, rhs. span . hi , ExprAssign ( lhs, rhs) )
2704
2701
}
2705
2702
token:: BINOPEQ ( op) => {
2706
2703
self . bump ( ) ;
2707
- let rhs = self . parse_expr ( ) ;
2704
+ let rhs = self . parse_expr_res ( restrictions ) ;
2708
2705
let aop = match op {
2709
2706
token:: PLUS => BiAdd ,
2710
2707
token:: MINUS => BiSub ,
@@ -2730,7 +2727,7 @@ impl<'a> Parser<'a> {
2730
2727
/// Parse an 'if' expression ('if' token already eaten)
2731
2728
pub fn parse_if_expr ( & mut self ) -> P < Expr > {
2732
2729
let lo = self . last_span . lo ;
2733
- let cond = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2730
+ let cond = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2734
2731
let thn = self . parse_block ( ) ;
2735
2732
let mut els: Option < P < Expr > > = None ;
2736
2733
let mut hi = thn. span . hi ;
@@ -2791,7 +2788,7 @@ impl<'a> Parser<'a> {
2791
2788
let lo = self . last_span . lo ;
2792
2789
let pat = self . parse_pat ( ) ;
2793
2790
self . expect_keyword ( keywords:: In ) ;
2794
- let expr = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2791
+ let expr = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2795
2792
let loop_block = self . parse_block ( ) ;
2796
2793
let hi = self . span . hi ;
2797
2794
@@ -2800,7 +2797,7 @@ impl<'a> Parser<'a> {
2800
2797
2801
2798
pub fn parse_while_expr ( & mut self , opt_ident : Option < ast:: Ident > ) -> P < Expr > {
2802
2799
let lo = self . last_span . lo ;
2803
- let cond = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2800
+ let cond = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2804
2801
let body = self . parse_block ( ) ;
2805
2802
let hi = body. span . hi ;
2806
2803
return self . mk_expr ( lo, hi, ExprWhile ( cond, body, opt_ident) ) ;
@@ -2815,7 +2812,7 @@ impl<'a> Parser<'a> {
2815
2812
2816
2813
fn parse_match_expr ( & mut self ) -> P < Expr > {
2817
2814
let lo = self . last_span . lo ;
2818
- let discriminant = self . parse_expr_res ( RESTRICT_NO_STRUCT_LITERAL ) ;
2815
+ let discriminant = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2819
2816
self . commit_expr_expecting ( & * discriminant, token:: LBRACE ) ;
2820
2817
let mut arms: Vec < Arm > = Vec :: new ( ) ;
2821
2818
while self . token != token:: RBRACE {
@@ -2834,7 +2831,7 @@ impl<'a> Parser<'a> {
2834
2831
guard = Some ( self . parse_expr ( ) ) ;
2835
2832
}
2836
2833
self . expect ( & token:: FAT_ARROW ) ;
2837
- let expr = self . parse_expr_res ( RESTRICT_STMT_EXPR ) ;
2834
+ let expr = self . parse_expr_res ( RestrictionStmtExpr ) ;
2838
2835
2839
2836
let require_comma =
2840
2837
!classify:: expr_is_simple_block ( & * expr)
@@ -2856,15 +2853,15 @@ impl<'a> Parser<'a> {
2856
2853
2857
2854
/// Parse an expression
2858
2855
pub fn parse_expr ( & mut self ) -> P < Expr > {
2859
- return self . parse_expr_res ( UNRESTRICTED ) ;
2856
+ return self . parse_expr_res ( Unrestricted ) ;
2860
2857
}
2861
2858
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;
2859
+ /// Parse an expression, subject to the given restrictions
2860
+ pub fn parse_expr_res ( & mut self , r : Restrictions ) -> P < Expr > {
2861
+ let old = self . restrictions ;
2862
+ self . restrictions = r;
2866
2863
let e = self . parse_assign_expr ( ) ;
2867
- self . restriction = old;
2864
+ self . restrictions = old;
2868
2865
return e;
2869
2866
}
2870
2867
@@ -3153,9 +3150,9 @@ impl<'a> Parser<'a> {
3153
3150
self . look_ahead ( 2 , |t| {
3154
3151
* t != token:: COMMA && * t != token:: RBRACKET
3155
3152
} ) {
3156
- let start = self . parse_expr_res ( RESTRICT_NO_BAR_OP ) ;
3153
+ let start = self . parse_expr_res ( RestrictionNoBarOp ) ;
3157
3154
self . eat ( & token:: DOTDOT ) ;
3158
- let end = self . parse_expr_res ( RESTRICT_NO_BAR_OP ) ;
3155
+ let end = self . parse_expr_res ( RestrictionNoBarOp ) ;
3159
3156
pat = PatRange ( start, end) ;
3160
3157
} else if is_plain_ident ( & self . token ) && !can_be_enum_or_struct {
3161
3158
let id = self . parse_ident ( ) ;
@@ -3441,7 +3438,7 @@ impl<'a> Parser<'a> {
3441
3438
check_expected_item ( self , found_attrs) ;
3442
3439
3443
3440
// Remainder are line-expr stmts.
3444
- let e = self . parse_expr_res ( RESTRICT_STMT_EXPR ) ;
3441
+ let e = self . parse_expr_res ( RestrictionStmtExpr ) ;
3445
3442
P ( spanned ( lo, e. span . hi , StmtExpr ( e, ast:: DUMMY_NODE_ID ) ) )
3446
3443
}
3447
3444
}
@@ -3450,7 +3447,7 @@ impl<'a> Parser<'a> {
3450
3447
3451
3448
/// Is this expression a successfully-parsed statement?
3452
3449
fn expr_is_complete ( & mut self , e : & Expr ) -> bool {
3453
- self . restriction == RESTRICT_STMT_EXPR &&
3450
+ self . restrictions . contains ( RestrictionStmtExpr ) &&
3454
3451
!classify:: expr_requires_semi_to_be_stmt ( e)
3455
3452
}
3456
3453
0 commit comments