diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2013-02-17 15:41:47 -0800 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2013-02-17 15:52:05 -0800 |
| commit | 612553cb3921f428668602afda1b106e0fd54d73 (patch) | |
| tree | cc68e58ac9ed47482d768890d3152cf6c783b7d5 /src/libsyntax/parse | |
| parent | a6945f2a45d56ef692cd8f2955dcef4e4c10d50a (diff) | |
| download | rust-612553cb3921f428668602afda1b106e0fd54d73.tar.gz rust-612553cb3921f428668602afda1b106e0fd54d73.zip | |
syntax: Allow 1-tuple expressions
This is for greater uniformity (for example, macros that generate tuples). rustc already supported 1-tuple patterns, but there was no way to construct a 1-tuple term.
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 799f0d40a46..b5c68d6715e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1061,6 +1061,9 @@ pub impl Parser { if self.token == token::LPAREN { self.bump(); + // (e) is parenthesized e + // (e,) is a tuple with only one field, e + let mut one_tuple = false; if self.token == token::RPAREN { hi = self.span.hi; self.bump(); @@ -1069,12 +1072,18 @@ pub impl Parser { } let mut es = ~[self.parse_expr()]; while self.token == token::COMMA { - self.bump(); es.push(self.parse_expr()); + self.bump(); + if self.token != token::RPAREN { + es.push(self.parse_expr()); + } + else { + one_tuple = true; + } } hi = self.span.hi; self.expect(token::RPAREN); - return if es.len() == 1 { + return if es.len() == 1 && !one_tuple { self.mk_expr(lo, self.span.hi, expr_paren(es[0])) } else { @@ -2158,11 +2167,13 @@ pub impl Parser { pat = pat_lit(expr); } else { let mut fields = ~[self.parse_pat(refutable)]; - while self.token == token::COMMA { - self.bump(); - fields.push(self.parse_pat(refutable)); + if self.look_ahead(1) != token::RPAREN { + while self.token == token::COMMA { + self.bump(); + fields.push(self.parse_pat(refutable)); + } } - if vec::len(fields) == 1u { self.expect(token::COMMA); } + if fields.len() == 1 { self.expect(token::COMMA); } hi = self.span.hi; self.expect(token::RPAREN); pat = pat_tup(fields); |
