about summary refs log tree commit diff
path: root/src/comp/syntax/parse
diff options
context:
space:
mode:
authorMarijn Haverbeke <marijnh@gmail.com>2011-08-22 14:38:48 +0200
committerMarijn Haverbeke <marijnh@gmail.com>2011-08-22 17:49:31 +0200
commit7d08678b740d779d9f0e1e6d15d7cf6ad4e1b57a (patch)
tree71a73aa6e74449cab2048957f79a5ca05138e22b /src/comp/syntax/parse
parenta2466233b4217cc8da4d2ebcd5f7c0b11db5b861 (diff)
downloadrust-7d08678b740d779d9f0e1e6d15d7cf6ad4e1b57a.tar.gz
rust-7d08678b740d779d9f0e1e6d15d7cf6ad4e1b57a.zip
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
Diffstat (limited to 'src/comp/syntax/parse')
-rw-r--r--src/comp/syntax/parse/parser.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs
index d20f345b3af..b20c7905013 100644
--- a/src/comp/syntax/parse/parser.rs
+++ b/src/comp/syntax/parse/parser.rs
@@ -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();