]> git.lizzy.rs Git - rust.git/blob - src/macros.rs
Optimise common `=> {{` macro pattern
[rust.git] / src / macros.rs
1 // Copyright 2015 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 // Format list-like macro invocations. These are invocations whose token trees
12 // can be interpreted as expressions and separated by commas.
13 // Note that these token trees do not actually have to be interpreted as
14 // expressions by the compiler. An example of an invocation we would reformat is
15 // foo!( x, y, z ). The token x may represent an identifier in the code, but we
16 // interpreted as an expression.
17 // Macro uses which are not-list like, such as bar!(key => val), will not be
18 // reformatted.
19 // List-like invocations with parentheses will be formatted as function calls,
20 // and those with brackets will be formatted as array literals.
21
22 use std::collections::HashMap;
23 use syntax::ast;
24 use syntax::codemap::{BytePos, Span};
25 use syntax::parse::new_parser_from_tts;
26 use syntax::parse::parser::Parser;
27 use syntax::parse::token::{BinOpToken, DelimToken, Token};
28 use syntax::print::pprust;
29 use syntax::symbol;
30 use syntax::tokenstream::{Cursor, ThinTokenStream, TokenStream, TokenTree};
31 use syntax::util::ThinVec;
32
33 use codemap::SpanUtils;
34 use comment::{contains_comment, remove_trailing_white_spaces, FindUncommented};
35 use expr::{rewrite_array, rewrite_call_inner};
36 use rewrite::{Rewrite, RewriteContext};
37 use shape::{Indent, Shape};
38 use utils::{format_visibility, mk_sp};
39
40 const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
41
42 // FIXME: use the enum from libsyntax?
43 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
44 enum MacroStyle {
45     Parens,
46     Brackets,
47     Braces,
48 }
49
50 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
51 pub enum MacroPosition {
52     Item,
53     Statement,
54     Expression,
55     Pat,
56 }
57
58 impl MacroStyle {
59     fn opener(&self) -> &'static str {
60         match *self {
61             MacroStyle::Parens => "(",
62             MacroStyle::Brackets => "[",
63             MacroStyle::Braces => "{",
64         }
65     }
66 }
67
68 #[derive(Debug)]
69 pub enum MacroArg {
70     Expr(ast::Expr),
71     Ty(ast::Ty),
72     Pat(ast::Pat),
73 }
74
75 impl Rewrite for MacroArg {
76     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
77         match *self {
78             MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
79             MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
80             MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
81         }
82     }
83 }
84
85 fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
86     macro_rules! parse_macro_arg {
87         ($macro_arg: ident, $parser: ident) => {
88             let mut cloned_parser = (*parser).clone();
89             match cloned_parser.$parser() {
90                 Ok(x) => {
91                     if parser.sess.span_diagnostic.has_errors() {
92                         parser.sess.span_diagnostic.reset_err_count();
93                     } else {
94                         // Parsing succeeded.
95                         *parser = cloned_parser;
96                         return Some(MacroArg::$macro_arg((*x).clone()));
97                     }
98                 }
99                 Err(mut e) => {
100                     e.cancel();
101                     parser.sess.span_diagnostic.reset_err_count();
102                 }
103             }
104         }
105     }
106
107     parse_macro_arg!(Expr, parse_expr);
108     parse_macro_arg!(Ty, parse_ty);
109     parse_macro_arg!(Pat, parse_pat);
110
111     None
112 }
113
114 pub fn rewrite_macro(
115     mac: &ast::Mac,
116     extra_ident: Option<ast::Ident>,
117     context: &RewriteContext,
118     shape: Shape,
119     position: MacroPosition,
120 ) -> Option<String> {
121     let context = &mut context.clone();
122     context.inside_macro = true;
123     if context.config.use_try_shorthand() {
124         if let Some(expr) = convert_try_mac(mac, context) {
125             context.inside_macro = false;
126             return expr.rewrite(context, shape);
127         }
128     }
129
130     let original_style = macro_style(mac, context);
131
132     let macro_name = match extra_ident {
133         None => format!("{}!", mac.node.path),
134         Some(ident) => {
135             if ident == symbol::keywords::Invalid.ident() {
136                 format!("{}!", mac.node.path)
137             } else {
138                 format!("{}! {}", mac.node.path, ident)
139             }
140         }
141     };
142
143     let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
144         MacroStyle::Brackets
145     } else {
146         original_style
147     };
148
149     let ts: TokenStream = mac.node.stream();
150     if ts.is_empty() && !contains_comment(context.snippet(mac.span)) {
151         return match style {
152             MacroStyle::Parens if position == MacroPosition::Item => {
153                 Some(format!("{}();", macro_name))
154             }
155             MacroStyle::Parens => Some(format!("{}()", macro_name)),
156             MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
157             MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
158         };
159     }
160
161     let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
162     let mut arg_vec = Vec::new();
163     let mut vec_with_semi = false;
164     let mut trailing_comma = false;
165
166     if MacroStyle::Braces != style {
167         loop {
168             match parse_macro_arg(&mut parser) {
169                 Some(arg) => arg_vec.push(arg),
170                 None => return Some(context.snippet(mac.span).to_owned()),
171             }
172
173             match parser.token {
174                 Token::Eof => break,
175                 Token::Comma => (),
176                 Token::Semi => {
177                     // Try to parse `vec![expr; expr]`
178                     if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
179                         parser.bump();
180                         if parser.token != Token::Eof {
181                             match parse_macro_arg(&mut parser) {
182                                 Some(arg) => {
183                                     arg_vec.push(arg);
184                                     parser.bump();
185                                     if parser.token == Token::Eof && arg_vec.len() == 2 {
186                                         vec_with_semi = true;
187                                         break;
188                                     }
189                                 }
190                                 None => return Some(context.snippet(mac.span).to_owned()),
191                             }
192                         }
193                     }
194                     return Some(context.snippet(mac.span).to_owned());
195                 }
196                 _ => return Some(context.snippet(mac.span).to_owned()),
197             }
198
199             parser.bump();
200
201             if parser.token == Token::Eof {
202                 trailing_comma = true;
203                 break;
204             }
205         }
206     }
207
208     match style {
209         MacroStyle::Parens => {
210             // Format macro invocation as function call, forcing no trailing
211             // comma because not all macros support them.
212             rewrite_call_inner(
213                 context,
214                 &macro_name,
215                 &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..],
216                 mac.span,
217                 shape,
218                 context.config.width_heuristics().fn_call_width,
219                 trailing_comma,
220             ).map(|rw| match position {
221                 MacroPosition::Item => format!("{};", rw),
222                 _ => rw,
223             })
224         }
225         MacroStyle::Brackets => {
226             let mac_shape = shape.offset_left(macro_name.len())?;
227             // Handle special case: `vec![expr; expr]`
228             if vec_with_semi {
229                 let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
230                     ("[ ", " ]")
231                 } else {
232                     ("[", "]")
233                 };
234                 // 6 = `vec!` + `; `
235                 let total_overhead = lbr.len() + rbr.len() + 6;
236                 let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
237                 let lhs = arg_vec[0].rewrite(context, nested_shape)?;
238                 let rhs = arg_vec[1].rewrite(context, nested_shape)?;
239                 if !lhs.contains('\n') && !rhs.contains('\n')
240                     && lhs.len() + rhs.len() + total_overhead <= shape.width
241                 {
242                     Some(format!("{}{}{}; {}{}", macro_name, lbr, lhs, rhs, rbr))
243                 } else {
244                     Some(format!(
245                         "{}{}\n{}{};\n{}{}\n{}{}",
246                         macro_name,
247                         lbr,
248                         nested_shape.indent.to_string(context.config),
249                         lhs,
250                         nested_shape.indent.to_string(context.config),
251                         rhs,
252                         shape.indent.to_string(context.config),
253                         rbr
254                     ))
255                 }
256             } else {
257                 // If we are rewriting `vec!` macro or other special macros,
258                 // then we can rewrite this as an usual array literal.
259                 // Otherwise, we must preserve the original existence of trailing comma.
260                 if FORCED_BRACKET_MACROS.contains(&macro_name.as_str()) {
261                     context.inside_macro = false;
262                     trailing_comma = false;
263                 }
264                 // Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
265                 let sp = mk_sp(
266                     context
267                         .codemap
268                         .span_after(mac.span, original_style.opener()),
269                     mac.span.hi() - BytePos(1),
270                 );
271                 let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..];
272                 let rewrite = rewrite_array(arg_vec, sp, context, mac_shape, trailing_comma)?;
273
274                 Some(format!("{}{}", macro_name, rewrite))
275             }
276         }
277         MacroStyle::Braces => {
278             // Skip macro invocations with braces, for now.
279             indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
280         }
281     }
282 }
283
284 pub fn rewrite_macro_def(
285     context: &RewriteContext,
286     indent: Indent,
287     def: &ast::MacroDef,
288     ident: ast::Ident,
289     vis: &ast::Visibility,
290     span: Span,
291 ) -> Option<String> {
292     let snippet = Some(remove_trailing_white_spaces(context.snippet(span)));
293
294     let mut parser = MacroParser::new(def.stream().into_trees());
295     let parsed_def = match parser.parse() {
296         Some(def) => def,
297         None => return snippet,
298     };
299
300     let mut result = if def.legacy {
301         String::from("macro_rules!")
302     } else {
303         format!("{}macro", format_visibility(vis))
304     };
305
306     result += " ";
307     result += &ident.name.as_str();
308
309     let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
310
311     let mac_indent = if multi_branch_style {
312         result += " {";
313         indent.block_indent(context.config)
314     } else {
315         indent
316     };
317
318     let mac_indent_str = mac_indent.to_string(context.config);
319
320     for branch in parsed_def.branches {
321         // Only attempt to format function-like macros.
322         if branch.args_paren_kind != DelimToken::Paren {
323             // FIXME(#1539): implement for non-sugared macros.
324             return snippet;
325         }
326
327         let args = format!("({})", format_macro_args(branch.args)?);
328
329         if multi_branch_style {
330             result += "\n";
331             result += &mac_indent_str;
332             result += &args;
333             result += " =>";
334         } else {
335             result += &args;
336         }
337
338         // The macro body is the most interesting part. It might end up as various
339         // AST nodes, but also has special variables (e.g, `$foo`) which can't be
340         // parsed as regular Rust code (and note that these can be escaped using
341         // `$$`). We'll try and format like an AST node, but we'll substitute
342         // variables for new names with the same length first.
343
344         let old_body = context.snippet(branch.body).trim();
345         let (body_str, substs) = match replace_names(old_body) {
346             Some(result) => result,
347             None => return snippet,
348         };
349
350         let mut config = context.config.clone();
351         config.set().hide_parse_errors(true);
352
353         result += " {";
354
355         let has_block_body = old_body.starts_with("{");
356
357         let body_indent = if has_block_body {
358             mac_indent
359         } else {
360             // We'll hack the indent below, take this into account when formatting,
361             let body_indent = mac_indent.block_indent(&config);
362             let new_width = config.max_width() - body_indent.width();
363             config.set().max_width(new_width);
364             body_indent
365         };
366
367         // First try to format as items, then as statements.
368         let new_body = match ::format_snippet(&body_str, &config) {
369             Some(new_body) => new_body,
370             None => match ::format_code_block(&body_str, &config) {
371                 Some(new_body) => new_body,
372                 None => return snippet,
373             },
374         };
375
376         // Indent the body since it is in a block.
377         let indent_str = body_indent.to_string(&config);
378         let mut new_body = new_body
379             .trim_right()
380             .lines()
381             .fold(String::new(), |mut s, l| {
382                 if !l.is_empty() {
383                     s += &indent_str;
384                 }
385                 s + l + "\n"
386             });
387
388         // Undo our replacement of macro variables.
389         // FIXME: this could be *much* more efficient.
390         for (old, new) in &substs {
391             if old_body.find(new).is_some() {
392                 debug!(
393                     "rewrite_macro_def: bailing matching variable: `{}` in `{}`",
394                     new, ident
395                 );
396                 return snippet;
397             }
398             new_body = new_body.replace(new, old);
399         }
400
401         if has_block_body {
402             result += new_body.trim();
403         } else {
404             result += "\n";
405             result += &new_body;
406             result += &mac_indent_str;
407         }
408
409         result += "}";
410         if def.legacy {
411             result += ";";
412         }
413         result += "\n";
414     }
415
416     if multi_branch_style {
417         result += &indent.to_string(context.config);
418         result += "}";
419     }
420
421     Some(result)
422 }
423
424 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
425 // aren't causing problems.
426 // This should also work for escaped `$` variables, where we leave earlier `$`s.
427 fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
428     // Each substitution will require five or six extra bytes.
429     let mut result = String::with_capacity(input.len() + 64);
430     let mut substs = HashMap::new();
431     let mut dollar_count = 0;
432     let mut cur_name = String::new();
433
434     for c in input.chars() {
435         if c == '$' {
436             dollar_count += 1;
437         } else if dollar_count == 0 {
438             result.push(c);
439         } else if !c.is_alphanumeric() && !cur_name.is_empty() {
440             // Terminates a name following one or more dollars.
441             let mut new_name = String::new();
442             let mut old_name = String::new();
443             old_name.push('$');
444             for _ in 0..(dollar_count - 1) {
445                 new_name.push('$');
446                 old_name.push('$');
447             }
448             new_name.push('z');
449             new_name.push_str(&cur_name);
450             old_name.push_str(&cur_name);
451
452             result.push_str(&new_name);
453             substs.insert(old_name, new_name);
454
455             result.push(c);
456
457             dollar_count = 0;
458             cur_name = String::new();
459         } else if c == '(' && cur_name.is_empty() {
460             // FIXME: Support macro def with repeat.
461             return None;
462         } else if c.is_alphanumeric() {
463             cur_name.push(c);
464         }
465     }
466
467     // FIXME: duplicate code
468     if !cur_name.is_empty() {
469         let mut new_name = String::new();
470         let mut old_name = String::new();
471         old_name.push('$');
472         for _ in 0..(dollar_count - 1) {
473             new_name.push('$');
474             old_name.push('$');
475         }
476         new_name.push('z');
477         new_name.push_str(&cur_name);
478         old_name.push_str(&cur_name);
479
480         result.push_str(&new_name);
481         substs.insert(old_name, new_name);
482     }
483
484     debug!("replace_names `{}` {:?}", result, substs);
485
486     Some((result, substs))
487 }
488
489 // This is a bit sketchy. The token rules probably need tweaking, but it works
490 // for some common cases. I hope the basic logic is sufficient. Note that the
491 // meaning of some tokens is a bit different here from usual Rust, e.g., `*`
492 // and `(`/`)` have special meaning.
493 //
494 // We always try and format on one line.
495 fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
496     let mut result = String::with_capacity(128);
497     let mut insert_space = SpaceState::Never;
498
499     for tok in (toks.into(): TokenStream).trees() {
500         match tok {
501             TokenTree::Token(_, t) => {
502                 if !result.is_empty() && force_space_before(&t) {
503                     insert_space = SpaceState::Always;
504                 }
505                 if force_no_space_before(&t) {
506                     insert_space = SpaceState::Never;
507                 }
508                 match (insert_space, ident_like(&t)) {
509                     (SpaceState::Always, _)
510                     | (SpaceState::Punctuation, false)
511                     | (SpaceState::Ident, true) => {
512                         result.push(' ');
513                     }
514                     _ => {}
515                 }
516                 result.push_str(&pprust::token_to_string(&t));
517                 insert_space = next_space(&t);
518             }
519             TokenTree::Delimited(_, d) => {
520                 if let SpaceState::Always = insert_space {
521                     result.push(' ');
522                 }
523                 let formatted = format_macro_args(d.tts)?;
524                 match d.delim {
525                     DelimToken::Paren => {
526                         result.push_str(&format!("({})", formatted));
527                         insert_space = SpaceState::Always;
528                     }
529                     DelimToken::Bracket => {
530                         result.push_str(&format!("[{}]", formatted));
531                         insert_space = SpaceState::Always;
532                     }
533                     DelimToken::Brace => {
534                         result.push_str(&format!(" {{ {} }}", formatted));
535                         insert_space = SpaceState::Always;
536                     }
537                     DelimToken::NoDelim => {
538                         result.push_str(&format!("{}", formatted));
539                         insert_space = SpaceState::Always;
540                     }
541                 }
542             }
543         }
544     }
545
546     Some(result)
547 }
548
549 // We should insert a space if the next token is a:
550 #[derive(Copy, Clone)]
551 enum SpaceState {
552     Never,
553     Punctuation,
554     Ident, // Or ident/literal-like thing.
555     Always,
556 }
557
558 fn force_space_before(tok: &Token) -> bool {
559     match *tok {
560         Token::Eq
561         | Token::Lt
562         | Token::Le
563         | Token::EqEq
564         | Token::Ne
565         | Token::Ge
566         | Token::Gt
567         | Token::AndAnd
568         | Token::OrOr
569         | Token::Not
570         | Token::Tilde
571         | Token::BinOpEq(_)
572         | Token::At
573         | Token::RArrow
574         | Token::LArrow
575         | Token::FatArrow
576         | Token::Pound
577         | Token::Dollar => true,
578         Token::BinOp(bot) => bot != BinOpToken::Star,
579         _ => false,
580     }
581 }
582
583 fn force_no_space_before(tok: &Token) -> bool {
584     match *tok {
585         Token::Semi | Token::Comma | Token::Dot => true,
586         Token::BinOp(bot) => bot == BinOpToken::Star,
587         _ => false,
588     }
589 }
590 fn ident_like(tok: &Token) -> bool {
591     match *tok {
592         Token::Ident(_) | Token::Literal(..) | Token::Lifetime(_) => true,
593         _ => false,
594     }
595 }
596
597 fn next_space(tok: &Token) -> SpaceState {
598     match *tok {
599         Token::Not
600         | Token::Tilde
601         | Token::At
602         | Token::Comma
603         | Token::Dot
604         | Token::DotDot
605         | Token::DotDotDot
606         | Token::DotDotEq
607         | Token::DotEq
608         | Token::Question
609         | Token::Underscore
610         | Token::BinOp(_) => SpaceState::Punctuation,
611
612         Token::ModSep
613         | Token::Pound
614         | Token::Dollar
615         | Token::OpenDelim(_)
616         | Token::CloseDelim(_)
617         | Token::Whitespace => SpaceState::Never,
618
619         Token::Literal(..) | Token::Ident(_) | Token::Lifetime(_) => SpaceState::Ident,
620
621         _ => SpaceState::Always,
622     }
623 }
624
625 /// Tries to convert a macro use into a short hand try expression. Returns None
626 /// when the macro is not an instance of try! (or parsing the inner expression
627 /// failed).
628 pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
629     if &format!("{}", mac.node.path)[..] == "try" {
630         let ts: TokenStream = mac.node.tts.clone().into();
631         let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
632
633         Some(ast::Expr {
634             id: ast::NodeId::new(0), // dummy value
635             node: ast::ExprKind::Try(parser.parse_expr().ok()?),
636             span: mac.span, // incorrect span, but shouldn't matter too much
637             attrs: ThinVec::new(),
638         })
639     } else {
640         None
641     }
642 }
643
644 fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
645     let snippet = context.snippet(mac.span);
646     let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
647     let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
648     let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
649
650     if paren_pos < bracket_pos && paren_pos < brace_pos {
651         MacroStyle::Parens
652     } else if bracket_pos < brace_pos {
653         MacroStyle::Brackets
654     } else {
655         MacroStyle::Braces
656     }
657 }
658
659 /// Indent each line according to the specified `indent`.
660 /// e.g.
661 /// ```rust
662 /// foo!{
663 /// x,
664 /// y,
665 /// foo(
666 ///     a,
667 ///     b,
668 ///     c,
669 /// ),
670 /// }
671 /// ```
672 /// will become
673 /// ```rust
674 /// foo!{
675 ///     x,
676 ///     y,
677 ///     foo(
678 ///         a,
679 ///         b,
680 ///         c,
681 //      ),
682 /// }
683 /// ```
684 fn indent_macro_snippet(
685     context: &RewriteContext,
686     macro_str: &str,
687     indent: Indent,
688 ) -> Option<String> {
689     let mut lines = macro_str.lines();
690     let first_line = lines.next().map(|s| s.trim_right())?;
691     let mut trimmed_lines = Vec::with_capacity(16);
692
693     let min_prefix_space_width = lines
694         .filter_map(|line| {
695             let prefix_space_width = if is_empty_line(line) {
696                 None
697             } else {
698                 Some(get_prefix_space_width(context, line))
699             };
700             trimmed_lines.push((line.trim(), prefix_space_width));
701             prefix_space_width
702         })
703         .min()?;
704
705     Some(
706         String::from(first_line) + "\n"
707             + &trimmed_lines
708                 .iter()
709                 .map(|&(line, prefix_space_width)| match prefix_space_width {
710                     Some(original_indent_width) => {
711                         let new_indent_width = indent.width()
712                             + original_indent_width
713                                 .checked_sub(min_prefix_space_width)
714                                 .unwrap_or(0);
715                         let new_indent = Indent::from_width(context.config, new_indent_width);
716                         format!("{}{}", new_indent.to_string(context.config), line.trim())
717                     }
718                     None => String::new(),
719                 })
720                 .collect::<Vec<_>>()
721                 .join("\n"),
722     )
723 }
724
725 fn get_prefix_space_width(context: &RewriteContext, s: &str) -> usize {
726     let mut width = 0;
727     for c in s.chars() {
728         match c {
729             ' ' => width += 1,
730             '\t' => width += context.config.tab_spaces(),
731             _ => return width,
732         }
733     }
734     width
735 }
736
737 fn is_empty_line(s: &str) -> bool {
738     s.is_empty() || s.chars().all(char::is_whitespace)
739 }
740
741 // A very simple parser that just parses a macros 2.0 definition into its branches.
742 // Currently we do not attempt to parse any further than that.
743 #[derive(new)]
744 struct MacroParser {
745     toks: Cursor,
746 }
747
748 impl MacroParser {
749     // (`(` ... `)` `=>` `{` ... `}`)*
750     fn parse(&mut self) -> Option<Macro> {
751         let mut branches = vec![];
752         while self.toks.look_ahead(1).is_some() {
753             branches.push(self.parse_branch()?);
754         }
755
756         Some(Macro { branches })
757     }
758
759     // `(` ... `)` `=>` `{` ... `}`
760     fn parse_branch(&mut self) -> Option<MacroBranch> {
761         let (args_paren_kind, args) = match self.toks.next()? {
762             TokenTree::Token(..) => return None,
763             TokenTree::Delimited(_, ref d) => (d.delim, d.tts.clone()),
764         };
765         match self.toks.next()? {
766             TokenTree::Token(_, Token::FatArrow) => {}
767             _ => return None,
768         }
769         let body = match self.toks.next()? {
770             TokenTree::Token(..) => return None,
771             TokenTree::Delimited(sp, _) => {
772                 let data = sp.data();
773                 Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt)
774             }
775         };
776         if let Some(TokenTree::Token(_, Token::Semi)) = self.toks.look_ahead(0) {
777             self.toks.next();
778         }
779         Some(MacroBranch {
780             args,
781             args_paren_kind,
782             body,
783         })
784     }
785 }
786
787 // A parsed macros 2.0 macro definition.
788 struct Macro {
789     branches: Vec<MacroBranch>,
790 }
791
792 // FIXME: it would be more efficient to use references to the token streams
793 // rather than clone them, if we can make the borrowing work out.
794 struct MacroBranch {
795     args: ThinTokenStream,
796     args_paren_kind: DelimToken,
797     body: Span,
798 }
799
800 #[cfg(test)]
801 mod test {
802     use super::*;
803     use syntax::parse::{parse_stream_from_source_str, ParseSess};
804     use syntax::codemap::{FileName, FilePathMapping};
805
806     fn format_macro_args_str(s: &str) -> String {
807         let input = parse_stream_from_source_str(
808             FileName::Custom("stdin".to_owned()),
809             s.to_owned(),
810             &ParseSess::new(FilePathMapping::empty()),
811             None,
812         );
813         format_macro_args(input.into()).unwrap()
814     }
815
816     #[test]
817     fn test_format_macro_args() {
818         assert_eq!(format_macro_args_str(""), "".to_owned());
819         assert_eq!(format_macro_args_str("$ x : ident"), "$x: ident".to_owned());
820         assert_eq!(
821             format_macro_args_str("$ m1 : ident , $ m2 : ident , $ x : ident"),
822             "$m1: ident, $m2: ident, $x: ident".to_owned()
823         );
824         assert_eq!(
825             format_macro_args_str("$($beginning:ident),*;$middle:ident;$($end:ident),*"),
826             "$($beginning: ident),*; $middle: ident; $($end: ident),*".to_owned()
827         );
828         assert_eq!(
829             format_macro_args_str(
830                 "$ name : ident ( $ ( $ dol : tt $ var : ident ) * ) $ ( $ body : tt ) *"
831             ),
832             "$name: ident($($dol: tt $var: ident)*) $($body: tt)*".to_owned()
833         );
834     }
835 }