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:
parent
a2466233b4
commit
7d08678b74
9 changed files with 96 additions and 29 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue