Skip to content

Commit 361d791

Browse files
committed
auto merge of #13234 : seanmonstar/rust/pr/12947, r=alexcrichton
this is useful for macros like vec! which construct containers rebase of #12947 because I'm impatient.
2 parents 1217cfb + be673e7 commit 361d791

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

src/libsyntax/parse/parser.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -1881,12 +1881,9 @@ impl<'a> Parser<'a> {
18811881
if self.token == token::NOT {
18821882
// MACRO INVOCATION expression
18831883
self.bump();
1884-
match self.token {
1885-
token::LPAREN | token::LBRACE => {}
1886-
_ => self.fatal("expected open delimiter")
1887-
};
18881884

1889-
let ket = token::flip_delimiter(&self.token);
1885+
let ket = token::close_delimiter_for(&self.token)
1886+
.unwrap_or_else(|| self.fatal("expected open delimiter"));
18901887
self.bump();
18911888

18921889
let tts = self.parse_seq_to_end(&ket,
@@ -2109,8 +2106,8 @@ impl<'a> Parser<'a> {
21092106
TTTok(p.span, p.bump_and_get())
21102107
}
21112108

2112-
match self.token {
2113-
token::EOF => {
2109+
match (&self.token, token::close_delimiter_for(&self.token)) {
2110+
(&token::EOF, _) => {
21142111
let open_braces = self.open_braces.clone();
21152112
for sp in open_braces.iter() {
21162113
self.span_note(*sp, "Did you mean to close this delimiter?");
@@ -2119,9 +2116,7 @@ impl<'a> Parser<'a> {
21192116
// if we give it one
21202117
self.fatal("this file contains an un-closed delimiter ");
21212118
}
2122-
token::LPAREN | token::LBRACE | token::LBRACKET => {
2123-
let close_delim = token::flip_delimiter(&self.token);
2124-
2119+
(_, Some(close_delim)) => {
21252120
// Parse the open delimiter.
21262121
self.open_braces.push(self.span);
21272122
let mut result = vec!(parse_any_tt_tok(self));
@@ -2157,13 +2152,12 @@ impl<'a> Parser<'a> {
21572152
// the interpolation of Matcher's
21582153
maybe_whole!(self, NtMatchers);
21592154
let mut name_idx = 0u;
2160-
match self.token {
2161-
token::LBRACE | token::LPAREN | token::LBRACKET => {
2162-
let other_delimiter = token::flip_delimiter(&self.token);
2155+
match token::close_delimiter_for(&self.token) {
2156+
Some(other_delimiter) => {
21632157
self.bump();
21642158
self.parse_matcher_subseq_upto(&mut name_idx, &other_delimiter)
21652159
}
2166-
_ => self.fatal("expected open delimiter")
2160+
None => self.fatal("expected open delimiter")
21672161
}
21682162
}
21692163

@@ -3138,7 +3132,7 @@ impl<'a> Parser<'a> {
31383132
let pth = self.parse_path(NoTypesAllowed).path;
31393133
self.bump();
31403134

3141-
let id = if self.token == token::LPAREN || self.token == token::LBRACE {
3135+
let id = if token::close_delimiter_for(&self.token).is_some() {
31423136
token::special_idents::invalid // no special identifier
31433137
} else {
31443138
self.parse_ident()
@@ -3147,10 +3141,9 @@ impl<'a> Parser<'a> {
31473141
// check that we're pointing at delimiters (need to check
31483142
// again after the `if`, because of `parse_ident`
31493143
// consuming more tokens).
3150-
let (bra, ket) = match self.token {
3151-
token::LPAREN => (token::LPAREN, token::RPAREN),
3152-
token::LBRACE => (token::LBRACE, token::RBRACE),
3153-
_ => {
3144+
let (bra, ket) = match token::close_delimiter_for(&self.token) {
3145+
Some(ket) => (self.token.clone(), ket),
3146+
None => {
31543147
// we only expect an ident if we didn't parse one
31553148
// above.
31563149
let ident_str = if id == token::special_idents::invalid {
@@ -4724,15 +4717,14 @@ impl<'a> Parser<'a> {
47244717
token::special_idents::invalid // no special identifier
47254718
};
47264719
// eat a matched-delimiter token tree:
4727-
let tts = match self.token {
4728-
token::LPAREN | token::LBRACE => {
4729-
let ket = token::flip_delimiter(&self.token);
4720+
let tts = match token::close_delimiter_for(&self.token) {
4721+
Some(ket) => {
47304722
self.bump();
47314723
self.parse_seq_to_end(&ket,
47324724
seq_sep_none(),
47334725
|p| p.parse_token_tree())
47344726
}
4735-
_ => self.fatal("expected open delimiter")
4727+
None => self.fatal("expected open delimiter")
47364728
};
47374729
// single-variant-enum... :
47384730
let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);

src/libsyntax/parse/token.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,17 @@ pub fn can_begin_expr(t: &Token) -> bool {
297297
}
298298
}
299299

300-
/// what's the opposite delimiter?
301-
pub fn flip_delimiter(t: &token::Token) -> token::Token {
300+
/// Returns the matching close delimiter if this is an open delimiter,
301+
/// otherwise `None`.
302+
pub fn close_delimiter_for(t: &Token) -> Option<Token> {
302303
match *t {
303-
LPAREN => RPAREN,
304-
LBRACE => RBRACE,
305-
LBRACKET => RBRACKET,
306-
RPAREN => LPAREN,
307-
RBRACE => LBRACE,
308-
RBRACKET => LBRACKET,
309-
_ => fail!()
304+
LPAREN => Some(RPAREN),
305+
LBRACE => Some(RBRACE),
306+
LBRACKET => Some(RBRACKET),
307+
_ => None
310308
}
311309
}
312310

313-
314-
315311
pub fn is_lit(t: &Token) -> bool {
316312
match *t {
317313
LIT_CHAR(_) => true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules)]
12+
13+
macro_rules! vec [
14+
($($e:expr),*) => ({
15+
let mut _temp = ::std::vec::Vec::new();
16+
$(_temp.push($e);)*
17+
_temp
18+
})
19+
]
20+
21+
pub fn main() {
22+
let my_vec = vec![1, 2, 3, 4, 5];
23+
}

0 commit comments

Comments
 (0)