Skip to content

Commit 99293b1

Browse files
committed
Convert restriction enum into bitflags
This makes having multiple restrictions at once cleaner. Also drop NO_DOUBLEBAR restriction since it is never used.
1 parent 63eaba2 commit 99293b1

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ use std::mem;
8888
use std::rc::Rc;
8989
use std::iter;
9090

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+
}
9998
}
10099

101100
type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
@@ -314,7 +313,7 @@ pub struct Parser<'a> {
314313
pub buffer_start: int,
315314
pub buffer_end: int,
316315
pub tokens_consumed: uint,
317-
pub restriction: restriction,
316+
pub restrictions: Restrictions,
318317
pub quote_depth: uint, // not (yet) related to the quasiquoter
319318
pub reader: Box<Reader+'a>,
320319
pub interner: Rc<token::IdentInterner>,
@@ -383,7 +382,7 @@ impl<'a> Parser<'a> {
383382
buffer_start: 0,
384383
buffer_end: 0,
385384
tokens_consumed: 0,
386-
restriction: UNRESTRICTED,
385+
restrictions: Unrestricted,
387386
quote_depth: 0,
388387
obsolete_set: HashSet::new(),
389388
mod_path_stack: Vec::new(),
@@ -2189,7 +2188,7 @@ impl<'a> Parser<'a> {
21892188
if self.token == token::LBRACE {
21902189
// This is a struct literal, unless we're prohibited
21912190
// from parsing struct literals here.
2192-
if self.restriction != RESTRICT_NO_STRUCT_LITERAL {
2191+
if !self.restrictions.contains(RestrictionNoStructLiteral) {
21932192
// It's a struct literal.
21942193
self.bump();
21952194
let mut fields = Vec::new();
@@ -2651,12 +2650,9 @@ impl<'a> Parser<'a> {
26512650

26522651
// Prevent dynamic borrow errors later on by limiting the
26532652
// 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;
26602656
}
26612657

26622658
let cur_opt = token_to_binop(&self.token);
@@ -2730,7 +2726,7 @@ impl<'a> Parser<'a> {
27302726
/// Parse an 'if' expression ('if' token already eaten)
27312727
pub fn parse_if_expr(&mut self) -> P<Expr> {
27322728
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);
27342730
let thn = self.parse_block();
27352731
let mut els: Option<P<Expr>> = None;
27362732
let mut hi = thn.span.hi;
@@ -2791,7 +2787,7 @@ impl<'a> Parser<'a> {
27912787
let lo = self.last_span.lo;
27922788
let pat = self.parse_pat();
27932789
self.expect_keyword(keywords::In);
2794-
let expr = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
2790+
let expr = self.parse_expr_res(RestrictionNoStructLiteral);
27952791
let loop_block = self.parse_block();
27962792
let hi = self.span.hi;
27972793

@@ -2800,7 +2796,7 @@ impl<'a> Parser<'a> {
28002796

28012797
pub fn parse_while_expr(&mut self, opt_ident: Option<ast::Ident>) -> P<Expr> {
28022798
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);
28042800
let body = self.parse_block();
28052801
let hi = body.span.hi;
28062802
return self.mk_expr(lo, hi, ExprWhile(cond, body, opt_ident));
@@ -2815,7 +2811,7 @@ impl<'a> Parser<'a> {
28152811

28162812
fn parse_match_expr(&mut self) -> P<Expr> {
28172813
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);
28192815
self.commit_expr_expecting(&*discriminant, token::LBRACE);
28202816
let mut arms: Vec<Arm> = Vec::new();
28212817
while self.token != token::RBRACE {
@@ -2834,7 +2830,7 @@ impl<'a> Parser<'a> {
28342830
guard = Some(self.parse_expr());
28352831
}
28362832
self.expect(&token::FAT_ARROW);
2837-
let expr = self.parse_expr_res(RESTRICT_STMT_EXPR);
2833+
let expr = self.parse_expr_res(RestrictionStmtExpr);
28382834

28392835
let require_comma =
28402836
!classify::expr_is_simple_block(&*expr)
@@ -2856,15 +2852,15 @@ impl<'a> Parser<'a> {
28562852

28572853
/// Parse an expression
28582854
pub fn parse_expr(&mut self) -> P<Expr> {
2859-
return self.parse_expr_res(UNRESTRICTED);
2855+
return self.parse_expr_res(Unrestricted);
28602856
}
28612857

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;
28662862
let e = self.parse_assign_expr();
2867-
self.restriction = old;
2863+
self.restrictions = old;
28682864
return e;
28692865
}
28702866

@@ -3153,9 +3149,9 @@ impl<'a> Parser<'a> {
31533149
self.look_ahead(2, |t| {
31543150
*t != token::COMMA && *t != token::RBRACKET
31553151
}) {
3156-
let start = self.parse_expr_res(RESTRICT_NO_BAR_OP);
3152+
let start = self.parse_expr_res(RestrictionNoBarOp);
31573153
self.eat(&token::DOTDOT);
3158-
let end = self.parse_expr_res(RESTRICT_NO_BAR_OP);
3154+
let end = self.parse_expr_res(RestrictionNoBarOp);
31593155
pat = PatRange(start, end);
31603156
} else if is_plain_ident(&self.token) && !can_be_enum_or_struct {
31613157
let id = self.parse_ident();
@@ -3441,7 +3437,7 @@ impl<'a> Parser<'a> {
34413437
check_expected_item(self, found_attrs);
34423438

34433439
// Remainder are line-expr stmts.
3444-
let e = self.parse_expr_res(RESTRICT_STMT_EXPR);
3440+
let e = self.parse_expr_res(RestrictionStmtExpr);
34453441
P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
34463442
}
34473443
}
@@ -3450,7 +3446,7 @@ impl<'a> Parser<'a> {
34503446

34513447
/// Is this expression a successfully-parsed statement?
34523448
fn expr_is_complete(&mut self, e: &Expr) -> bool {
3453-
self.restriction == RESTRICT_STMT_EXPR &&
3449+
self.restrictions.contains(RestrictionStmtExpr) &&
34543450
!classify::expr_requires_semi_to_be_stmt(e)
34553451
}
34563452

0 commit comments

Comments
 (0)