Skip to content

Commit ac0c16d

Browse files
committed
Run rustfmt on /libsyntax/ext/tt/macro_parser.rs
1 parent bacb5c5 commit ac0c16d

File tree

1 file changed

+114
-78
lines changed

1 file changed

+114
-78
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 114 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ use codemap::Spanned;
9090
use errors::FatalError;
9191
use ext::tt::quoted::{self, TokenTree};
9292
use parse::{Directory, ParseSess};
93-
use parse::parser::{PathStyle, Parser};
94-
use parse::token::{self, DocComment, Token, Nonterminal};
93+
use parse::parser::{Parser, PathStyle};
94+
use parse::token::{self, DocComment, Nonterminal, Token};
9595
use print::pprust;
9696
use symbol::keywords;
9797
use tokenstream::TokenStream;
@@ -100,7 +100,7 @@ use util::small_vector::SmallVector;
100100
use std::mem;
101101
use std::rc::Rc;
102102
use std::collections::HashMap;
103-
use std::collections::hash_map::Entry::{Vacant, Occupied};
103+
use std::collections::hash_map::Entry::{Occupied, Vacant};
104104

105105
// To avoid costly uniqueness checks, we require that `MatchSeq` always has
106106
// a nonempty body.
@@ -182,7 +182,7 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
182182
match_lo: 0,
183183
match_cur: 0,
184184
match_hi: match_idx_hi,
185-
sp_lo: lo
185+
sp_lo: lo,
186186
})
187187
}
188188

@@ -206,25 +206,27 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
206206
#[derive(Debug, Clone)]
207207
pub enum NamedMatch {
208208
MatchedSeq(Rc<Vec<NamedMatch>>, syntax_pos::Span),
209-
MatchedNonterminal(Rc<Nonterminal>)
209+
MatchedNonterminal(Rc<Nonterminal>),
210210
}
211211

212-
fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut res: I)
213-
-> NamedParseResult {
214-
fn n_rec<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, m: &TokenTree, res: &mut I,
215-
ret_val: &mut HashMap<Ident, Rc<NamedMatch>>)
216-
-> Result<(), (syntax_pos::Span, String)> {
212+
fn nameize<I: Iterator<Item = NamedMatch>>(
213+
sess: &ParseSess,
214+
ms: &[TokenTree],
215+
mut res: I,
216+
) -> NamedParseResult {
217+
fn n_rec<I: Iterator<Item = NamedMatch>>(
218+
sess: &ParseSess,
219+
m: &TokenTree,
220+
res: &mut I,
221+
ret_val: &mut HashMap<Ident, Rc<NamedMatch>>,
222+
) -> Result<(), (syntax_pos::Span, String)> {
217223
match *m {
218-
TokenTree::Sequence(_, ref seq) => {
219-
for next_m in &seq.tts {
220-
n_rec(sess, next_m, res.by_ref(), ret_val)?
221-
}
222-
}
223-
TokenTree::Delimited(_, ref delim) => {
224-
for next_m in &delim.tts {
225-
n_rec(sess, next_m, res.by_ref(), ret_val)?;
226-
}
227-
}
224+
TokenTree::Sequence(_, ref seq) => for next_m in &seq.tts {
225+
n_rec(sess, next_m, res.by_ref(), ret_val)?
226+
},
227+
TokenTree::Delimited(_, ref delim) => for next_m in &delim.tts {
228+
n_rec(sess, next_m, res.by_ref(), ret_val)?;
229+
},
228230
TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => {
229231
if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
230232
return Err((span, "missing fragment specifier".to_string()));
@@ -250,7 +252,7 @@ fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut
250252
let mut ret_val = HashMap::new();
251253
for m in ms {
252254
match n_rec(sess, m, res.by_ref(), &mut ret_val) {
253-
Ok(_) => {},
255+
Ok(_) => {}
254256
Err((sp, msg)) => return Error(sp, msg),
255257
}
256258
}
@@ -265,18 +267,21 @@ pub enum ParseResult<T> {
265267
/// indicates that no rules expected the given token.
266268
Failure(syntax_pos::Span, Token),
267269
/// Fatal error (malformed macro?). Abort compilation.
268-
Error(syntax_pos::Span, String)
270+
Error(syntax_pos::Span, String),
269271
}
270272

271273
pub fn parse_failure_msg(tok: Token) -> String {
272274
match tok {
273275
token::Eof => "unexpected end of macro invocation".to_string(),
274-
_ => format!("no rules expected the token `{}`", pprust::token_to_string(&tok)),
276+
_ => format!(
277+
"no rules expected the token `{}`",
278+
pprust::token_to_string(&tok)
279+
),
275280
}
276281
}
277282

278283
/// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
279-
fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
284+
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
280285
if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) {
281286
id1.name == id2.name
282287
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
@@ -290,14 +295,15 @@ fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
290295
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect()
291296
}
292297

293-
fn inner_parse_loop(sess: &ParseSess,
294-
cur_items: &mut SmallVector<Box<MatcherPos>>,
295-
next_items: &mut Vec<Box<MatcherPos>>,
296-
eof_items: &mut SmallVector<Box<MatcherPos>>,
297-
bb_items: &mut SmallVector<Box<MatcherPos>>,
298-
token: &Token,
299-
span: syntax_pos::Span)
300-
-> ParseResult<()> {
298+
fn inner_parse_loop(
299+
sess: &ParseSess,
300+
cur_items: &mut SmallVector<Box<MatcherPos>>,
301+
next_items: &mut Vec<Box<MatcherPos>>,
302+
eof_items: &mut SmallVector<Box<MatcherPos>>,
303+
bb_items: &mut SmallVector<Box<MatcherPos>>,
304+
token: &Token,
305+
span: syntax_pos::Span,
306+
) -> ParseResult<()> {
301307
while let Some(mut item) = cur_items.pop() {
302308
// When unzipped trees end, remove them
303309
while item.idx >= item.top_elts.len() {
@@ -306,7 +312,7 @@ fn inner_parse_loop(sess: &ParseSess,
306312
item.top_elts = elts;
307313
item.idx = idx + 1;
308314
}
309-
None => break
315+
None => break,
310316
}
311317
}
312318

@@ -341,11 +347,16 @@ fn inner_parse_loop(sess: &ParseSess,
341347
// Check if we need a separator
342348
if idx == len && item.sep.is_some() {
343349
// We have a separator, and it is the current token.
344-
if item.sep.as_ref().map(|sep| token_name_eq(token, sep)).unwrap_or(false) {
350+
if item.sep
351+
.as_ref()
352+
.map(|sep| token_name_eq(token, sep))
353+
.unwrap_or(false)
354+
{
345355
item.idx += 1;
346356
next_items.push(item);
347357
}
348-
} else { // we don't need a separator
358+
} else {
359+
// we don't need a separator
349360
item.match_cur = item.match_lo;
350361
item.idx = 0;
351362
cur_items.push(item);
@@ -418,12 +429,13 @@ fn inner_parse_loop(sess: &ParseSess,
418429
Success(())
419430
}
420431

421-
pub fn parse(sess: &ParseSess,
422-
tts: TokenStream,
423-
ms: &[TokenTree],
424-
directory: Option<Directory>,
425-
recurse_into_modules: bool)
426-
-> NamedParseResult {
432+
pub fn parse(
433+
sess: &ParseSess,
434+
tts: TokenStream,
435+
ms: &[TokenTree],
436+
directory: Option<Directory>,
437+
recurse_into_modules: bool,
438+
) -> NamedParseResult {
427439
let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true);
428440
let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo()));
429441
let mut next_items = Vec::new(); // or proceed normally
@@ -433,9 +445,16 @@ pub fn parse(sess: &ParseSess,
433445
let mut eof_items = SmallVector::new();
434446
assert!(next_items.is_empty());
435447

436-
match inner_parse_loop(sess, &mut cur_items, &mut next_items, &mut eof_items, &mut bb_items,
437-
&parser.token, parser.span) {
438-
Success(_) => {},
448+
match inner_parse_loop(
449+
sess,
450+
&mut cur_items,
451+
&mut next_items,
452+
&mut eof_items,
453+
&mut bb_items,
454+
&parser.token,
455+
parser.span,
456+
) {
457+
Success(_) => {}
439458
Failure(sp, tok) => return Failure(sp, tok),
440459
Error(sp, msg) => return Error(sp, msg),
441460
}
@@ -446,43 +465,56 @@ pub fn parse(sess: &ParseSess,
446465
/* error messages here could be improved with links to orig. rules */
447466
if token_name_eq(&parser.token, &token::Eof) {
448467
if eof_items.len() == 1 {
449-
let matches = eof_items[0].matches.iter_mut().map(|dv| {
450-
Rc::make_mut(dv).pop().unwrap()
451-
});
468+
let matches = eof_items[0]
469+
.matches
470+
.iter_mut()
471+
.map(|dv| Rc::make_mut(dv).pop().unwrap());
452472
return nameize(sess, ms, matches);
453473
} else if eof_items.len() > 1 {
454-
return Error(parser.span, "ambiguity: multiple successful parses".to_string());
474+
return Error(
475+
parser.span,
476+
"ambiguity: multiple successful parses".to_string(),
477+
);
455478
} else {
456479
return Failure(parser.span, token::Eof);
457480
}
458481
} else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
459-
let nts = bb_items.iter().map(|item| match item.top_elts.get_tt(item.idx) {
460-
TokenTree::MetaVarDecl(_, bind, name) => {
461-
format!("{} ('{}')", name, bind)
462-
}
463-
_ => panic!()
464-
}).collect::<Vec<String>>().join(" or ");
465-
466-
return Error(parser.span, format!(
467-
"local ambiguity: multiple parsing options: {}",
468-
match next_items.len() {
469-
0 => format!("built-in NTs {}.", nts),
470-
1 => format!("built-in NTs {} or 1 other option.", nts),
471-
n => format!("built-in NTs {} or {} other options.", nts, n),
472-
}
473-
));
482+
let nts = bb_items
483+
.iter()
484+
.map(|item| match item.top_elts.get_tt(item.idx) {
485+
TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind),
486+
_ => panic!(),
487+
})
488+
.collect::<Vec<String>>()
489+
.join(" or ");
490+
491+
return Error(
492+
parser.span,
493+
format!(
494+
"local ambiguity: multiple parsing options: {}",
495+
match next_items.len() {
496+
0 => format!("built-in NTs {}.", nts),
497+
1 => format!("built-in NTs {} or 1 other option.", nts),
498+
n => format!("built-in NTs {} or {} other options.", nts, n),
499+
}
500+
),
501+
);
474502
} else if bb_items.is_empty() && next_items.is_empty() {
475503
return Failure(parser.span, parser.token);
476504
} else if !next_items.is_empty() {
477505
/* Now process the next token */
478506
cur_items.extend(next_items.drain(..));
479507
parser.bump();
480-
} else /* bb_items.len() == 1 */ {
508+
} else
509+
/* bb_items.len() == 1 */
510+
{
481511
let mut item = bb_items.pop().unwrap();
482512
if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
483513
let match_cur = item.match_cur;
484-
item.push_match(match_cur,
485-
MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str()))));
514+
item.push_match(
515+
match_cur,
516+
MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str()))),
517+
);
486518
item.idx += 1;
487519
item.match_cur += 1;
488520
} else {
@@ -512,20 +544,21 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
512544
"expr" => token.can_begin_expr(),
513545
"ty" => token.can_begin_type(),
514546
"ident" => token.is_ident(),
515-
"vis" => match *token { // The follow-set of :vis + "priv" keyword + interpolated
547+
"vis" => match *token {
548+
// The follow-set of :vis + "priv" keyword + interpolated
516549
Token::Comma | Token::Ident(_) | Token::Interpolated(_) => true,
517550
_ => token.can_begin_type(),
518551
},
519552
"block" => match *token {
520553
Token::OpenDelim(token::Brace) => true,
521554
Token::Interpolated(ref nt) => match nt.0 {
522-
token::NtItem(_) |
523-
token::NtPat(_) |
524-
token::NtTy(_) |
525-
token::NtIdent(_) |
526-
token::NtMeta(_) |
527-
token::NtPath(_) |
528-
token::NtVis(_) => false, // none of these may start with '{'.
555+
token::NtItem(_)
556+
| token::NtPat(_)
557+
| token::NtTy(_)
558+
| token::NtIdent(_)
559+
| token::NtMeta(_)
560+
| token::NtPath(_)
561+
| token::NtVis(_) => false, // none of these may start with '{'.
529562
_ => true,
530563
},
531564
_ => false,
@@ -591,12 +624,15 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
591624
"ident" => match p.token {
592625
token::Ident(sn) => {
593626
p.bump();
594-
token::NtIdent(Spanned::<Ident>{node: sn, span: p.prev_span})
627+
token::NtIdent(Spanned::<Ident> {
628+
node: sn,
629+
span: p.prev_span,
630+
})
595631
}
596632
_ => {
597633
let token_str = pprust::token_to_string(&p.token);
598-
p.fatal(&format!("expected ident, found {}",
599-
&token_str[..])).emit();
634+
p.fatal(&format!("expected ident, found {}", &token_str[..]))
635+
.emit();
600636
FatalError.raise()
601637
}
602638
},
@@ -606,6 +642,6 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
606642
"lifetime" => token::NtLifetime(p.expect_lifetime()),
607643
// this is not supposed to happen, since it has been checked
608644
// when compiling the macro.
609-
_ => p.span_bug(sp, "invalid fragment specifier")
645+
_ => p.span_bug(sp, "invalid fragment specifier"),
610646
}
611647
}

0 commit comments

Comments
 (0)