diff options
| author | Marijn Haverbeke <marijnh@gmail.com> | 2011-07-11 14:13:20 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <marijnh@gmail.com> | 2011-07-11 15:32:41 +0200 |
| commit | 7595fe5153cfc3e703cec4516616c51af3223bc6 (patch) | |
| tree | b35cdf3f29627c1d5173f8058ac9f0222bf15b17 /src/comp/syntax/parse/parser.rs | |
| parent | 12cb128a0a8cfffeccae910c72831562bd9c3b93 (diff) | |
| download | rust-7595fe5153cfc3e703cec4516616c51af3223bc6.tar.gz rust-7595fe5153cfc3e703cec4516616c51af3223bc6.zip | |
Implement record patterns
Closes #469.
Diffstat (limited to 'src/comp/syntax/parse/parser.rs')
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 567aa995fd6..29b12763d91 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1455,6 +1455,45 @@ fn parse_pat(&parser p) -> @ast::pat { } } } + case (token::LBRACE) { + p.bump(); + auto fields = ~[]; + auto etc = false; + auto first = true; + while (p.peek() != token::RBRACE) { + if (p.peek() == token::DOT) { + p.bump(); + expect(p, token::DOT); + expect(p, token::DOT); + if (p.peek() != token::RBRACE) { + p.fatal("expecting }, found " + + token::to_str(p.get_reader(), p.peek())); + } + etc = true; + break; + } + if (first) { first = false; } + else { expect(p, token::COMMA); } + auto fieldname = parse_ident(p); + auto subpat; + if (p.peek() == token::COLON) { + p.bump(); + subpat = parse_pat(p); + } else { + if (p.get_bad_expr_words().contains_key(fieldname)) { + p.fatal("found " + fieldname + + " in binding position"); + } + subpat = @rec(id=p.get_id(), + node=ast::pat_bind(fieldname), + span=rec(lo=lo, hi=hi)); + } + fields += ~[rec(ident=fieldname, pat=subpat)]; + } + hi = p.get_hi_pos(); + p.bump(); + pat = ast::pat_rec(fields, etc); + } case (?tok) { if (!is_ident(tok) || is_word(p, "true") || is_word(p, "false")) { auto lit = parse_lit(p); |
