Implement pattern guards

The syntax is

    alt x {
        mypat where mycond { ... }
    }

The condition may refer to any of the variables bound by the pattern.
When a guard fails, pattern-matching continues with the next pattern.

Closes #857
This commit is contained in:
Marijn Haverbeke 2011-08-22 14:38:48 +02:00
parent a2466233b4
commit 7d08678b74
9 changed files with 96 additions and 29 deletions

View file

@ -1360,8 +1360,12 @@ fn parse_alt_expr(p: &parser) -> @ast::expr {
let arms: [ast::arm] = [];
while p.peek() != token::RBRACE {
let pats = parse_pats(p);
let guard = none;
if eat_word(p, "when") {
guard = some(parse_expr(p));
}
let blk = parse_block(p);
arms += [{pats: pats, body: blk}];
arms += [{pats: pats, guard: guard, body: blk}];
}
let hi = p.get_hi_pos();
p.bump();