Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: allow stmt/expr macro invocations to be delimited by []. #12947

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1912,12 +1912,9 @@ impl Parser {
if self.token == token::NOT {
// MACRO INVOCATION expression
self.bump();
match self.token {
token::LPAREN | token::LBRACE => {}
_ => self.fatal("expected open delimiter")
};

let ket = token::flip_delimiter(&self.token);
let ket = token::close_delimiter_for(&self.token)
.unwrap_or_else(|| self.fatal("expected open delimiter"));
self.bump();

let tts = self.parse_seq_to_end(&ket,
Expand Down Expand Up @@ -2140,8 +2137,8 @@ impl Parser {
TTTok(p.span, p.bump_and_get())
}

match self.token {
token::EOF => {
match (&self.token, token::close_delimiter_for(&self.token)) {
(&token::EOF, _) => {
let open_braces = self.open_braces.clone();
for sp in open_braces.iter() {
self.span_note(*sp, "Did you mean to close this delimiter?");
Expand All @@ -2150,9 +2147,7 @@ impl Parser {
// if we give it one
self.fatal("this file contains an un-closed delimiter ");
}
token::LPAREN | token::LBRACE | token::LBRACKET => {
let close_delim = token::flip_delimiter(&self.token);

(_, Some(close_delim)) => {
// Parse the open delimiter.
self.open_braces.push(self.span);
let mut result = vec!(parse_any_tt_tok(self));
Expand Down Expand Up @@ -2188,13 +2183,12 @@ impl Parser {
// the interpolation of Matcher's
maybe_whole!(self, NtMatchers);
let name_idx = @Cell::new(0u);
match self.token {
token::LBRACE | token::LPAREN | token::LBRACKET => {
let other_delimiter = token::flip_delimiter(&self.token);
match token::close_delimiter_for(&self.token) {
Some(other_delimiter) => {
self.bump();
self.parse_matcher_subseq_upto(name_idx, &other_delimiter)
}
_ => self.fatal("expected open delimiter")
None => self.fatal("expected open delimiter")
}
}

Expand Down Expand Up @@ -3171,7 +3165,7 @@ impl Parser {
let pth = self.parse_path(NoTypesAllowed).path;
self.bump();

let id = if self.token == token::LPAREN || self.token == token::LBRACE {
let id = if token::close_delimiter_for(&self.token).is_some() {
token::special_idents::invalid // no special identifier
} else {
self.parse_ident()
Expand All @@ -3180,10 +3174,9 @@ impl Parser {
// check that we're pointing at delimiters (need to check
// again after the `if`, because of `parse_ident`
// consuming more tokens).
let (bra, ket) = match self.token {
token::LPAREN => (token::LPAREN, token::RPAREN),
token::LBRACE => (token::LBRACE, token::RBRACE),
_ => {
let (bra, ket) = match token::close_delimiter_for(&self.token) {
Some(ket) => (self.token.clone(), ket),
None => {
// we only expect an ident if we didn't parse one
// above.
let ident_str = if id == token::special_idents::invalid {
Expand Down Expand Up @@ -4769,15 +4762,14 @@ impl Parser {
token::special_idents::invalid // no special identifier
};
// eat a matched-delimiter token tree:
let tts = match self.token {
token::LPAREN | token::LBRACE => {
let ket = token::flip_delimiter(&self.token);
let tts = match token::close_delimiter_for(&self.token) {
Some(ket) => {
self.bump();
self.parse_seq_to_end(&ket,
seq_sep_none(),
|p| p.parse_token_tree())
}
_ => self.fatal("expected open delimiter")
None => self.fatal("expected open delimiter")
};
// single-variant-enum... :
let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
Expand Down
18 changes: 7 additions & 11 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,17 @@ pub fn can_begin_expr(t: &Token) -> bool {
}
}

/// what's the opposite delimiter?
pub fn flip_delimiter(t: &token::Token) -> token::Token {
/// Returns the matching close delimiter if this is an open delimiter,
/// otherwise `None`.
pub fn close_delimiter_for(t: &Token) -> Option<Token> {
match *t {
LPAREN => RPAREN,
LBRACE => RBRACE,
LBRACKET => RBRACKET,
RPAREN => LPAREN,
RBRACE => LBRACE,
RBRACKET => LBRACKET,
_ => fail!()
LPAREN => Some(RPAREN),
LBRACE => Some(RBRACE),
LBRACKET => Some(RBRACKET),
_ => None
}
}



pub fn is_lit(t: &Token) -> bool {
match *t {
LIT_CHAR(_) => true,
Expand Down
23 changes: 23 additions & 0 deletions src/test/run-pass/vec-macro-with-brackets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[feature(macro_rules)];

macro_rules! vec [
($($e:expr),*) => ({
let mut _temp = ::std::vec_ng::Vec::new();
$(_temp.push($e);)*
_temp
})
]

pub fn main() {
let my_vec = vec![1, 2, 3, 4, 5];
}