Add support for destructuring vectors in match expressions

This commit is contained in:
Jakub Wieczorek 2012-12-08 20:22:43 +00:00 committed by Graydon Hoare
parent 5bf7ba0773
commit 1968cb315a
20 changed files with 521 additions and 10 deletions

View file

@ -1794,6 +1794,39 @@ impl Parser {
};
}
fn parse_pat_vec_elements(refutable: bool) -> (~[@pat], Option<@pat>) {
let mut elements = ~[];
let mut tail = None;
let mut first = true;
while self.token != token::RBRACKET {
if first { first = false; }
else { self.expect(token::COMMA); }
let mut is_tail = false;
if self.token == token::DOTDOT {
self.bump();
is_tail = true;
}
let subpat = self.parse_pat(refutable);
if is_tail {
match subpat {
@{ node: pat_wild, _ } => (),
@{ node: pat_ident(_, _, _), _ } => (),
@{ span, _ } => self.span_fatal(
span, ~"expected an identifier or `_`"
)
}
tail = Some(subpat);
break;
}
elements.push(subpat);
}
return (elements, tail);
}
fn parse_pat_fields(refutable: bool) -> (~[ast::field_pat], bool) {
let mut fields = ~[];
let mut etc = false;
@ -1929,6 +1962,13 @@ impl Parser {
pat = pat_tup(fields);
}
}
token::LBRACKET => {
self.bump();
let (elements, tail) = self.parse_pat_vec_elements(refutable);
hi = self.span.hi;
self.expect(token::RBRACKET);
pat = ast::pat_vec(elements, tail);
}
copy tok => {
if !is_ident_or_path(tok)
|| self.is_keyword(~"true")