From 9a269a3aa8fe8140ad3f2fc2cfdfd68d6b40ec86 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 8 Dec 2011 11:56:16 +0100 Subject: Allow binding of nested patterns See src/test/run-pass/nested-patterns.rs for some examples. The syntax is boundvar@subpattern Which will match the subpattern as usual, but also bind boundvar to the whole matched value. Closes #838 --- src/comp/syntax/parse/parser.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/comp/syntax/parse/parser.rs') diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 2bf7865bf81..5748637fde4 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1429,10 +1429,9 @@ fn parse_pat(p: parser) -> @ast::pat { if p.get_bad_expr_words().contains_key(fieldname) { p.fatal("found " + fieldname + " in binding position"); } - subpat = - @{id: p.get_id(), - node: ast::pat_bind(fieldname), - span: ast_util::mk_sp(lo, hi)}; + subpat = @{id: p.get_id(), + node: ast::pat_bind(fieldname, none), + span: ast_util::mk_sp(lo, hi)}; } fields += [{ident: fieldname, pat: subpat}]; } @@ -1479,7 +1478,9 @@ fn parse_pat(p: parser) -> @ast::pat { _ { true } } { hi = p.get_hi_pos(); - pat = ast::pat_bind(parse_value_ident(p)); + let name = parse_value_ident(p); + let sub = eat(p, token::AT) ? some(parse_pat(p)) : none; + pat = ast::pat_bind(name, sub); } else { let tag_path = parse_path_and_ty_param_substs(p); hi = tag_path.span.hi; -- cgit 1.4.1-3-g733a5