]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/quote.rs
Auto merge of #28651 - dotdash:exhaustive_match, r=eddyb
[rust.git] / src / libsyntax / ext / quote.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 use ast;
12 use codemap::Span;
13 use ext::base::ExtCtxt;
14 use ext::base;
15 use ext::build::AstBuilder;
16 use parse::token::*;
17 use parse::token;
18 use ptr::P;
19
20 ///  Quasiquoting works via token trees.
21 ///
22 ///  This is registered as a set of expression syntax extension called quote!
23 ///  that lifts its argument token-tree to an AST representing the
24 ///  construction of the same token tree, with token::SubstNt interpreted
25 ///  as antiquotes (splices).
26
27 pub mod rt {
28     use ast;
29     use codemap::Spanned;
30     use ext::base::ExtCtxt;
31     use parse::{self, token, classify};
32     use ptr::P;
33     use std::rc::Rc;
34
35     use ast::{TokenTree, Expr};
36
37     pub use parse::new_parser_from_tts;
38     pub use codemap::{BytePos, Span, dummy_spanned, DUMMY_SP};
39
40     pub trait ToTokens {
41         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree>;
42     }
43
44     impl ToTokens for TokenTree {
45         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
46             vec!(self.clone())
47         }
48     }
49
50     impl<T: ToTokens> ToTokens for Vec<T> {
51         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
52             self.iter().flat_map(|t| t.to_tokens(cx)).collect()
53         }
54     }
55
56     impl<T: ToTokens> ToTokens for Spanned<T> {
57         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
58             // FIXME: use the span?
59             self.node.to_tokens(cx)
60         }
61     }
62
63     impl<T: ToTokens> ToTokens for Option<T> {
64         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
65             match self {
66                 &Some(ref t) => t.to_tokens(cx),
67                 &None => Vec::new(),
68             }
69         }
70     }
71
72     impl ToTokens for ast::Ident {
73         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
74             vec![ast::TtToken(DUMMY_SP, token::Ident(*self, token::Plain))]
75         }
76     }
77
78     impl ToTokens for ast::Path {
79         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
80             vec![ast::TtToken(DUMMY_SP, token::Interpolated(token::NtPath(Box::new(self.clone()))))]
81         }
82     }
83
84     impl ToTokens for ast::Ty {
85         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
86             vec![ast::TtToken(self.span, token::Interpolated(token::NtTy(P(self.clone()))))]
87         }
88     }
89
90     impl ToTokens for ast::Block {
91         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
92             vec![ast::TtToken(self.span, token::Interpolated(token::NtBlock(P(self.clone()))))]
93         }
94     }
95
96     impl ToTokens for ast::Generics {
97         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
98             vec![ast::TtToken(DUMMY_SP, token::Interpolated(token::NtGenerics(self.clone())))]
99         }
100     }
101
102     impl ToTokens for ast::WhereClause {
103         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
104             vec![ast::TtToken(DUMMY_SP, token::Interpolated(token::NtWhereClause(self.clone())))]
105         }
106     }
107
108     impl ToTokens for P<ast::Item> {
109         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
110             vec![ast::TtToken(self.span, token::Interpolated(token::NtItem(self.clone())))]
111         }
112     }
113
114     impl ToTokens for P<ast::ImplItem> {
115         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
116             vec![ast::TtToken(self.span, token::Interpolated(token::NtImplItem(self.clone())))]
117         }
118     }
119
120     impl ToTokens for P<ast::TraitItem> {
121         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
122             vec![ast::TtToken(self.span, token::Interpolated(token::NtTraitItem(self.clone())))]
123         }
124     }
125
126     impl ToTokens for P<ast::Stmt> {
127         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
128             let mut tts = vec![
129                 ast::TtToken(self.span, token::Interpolated(token::NtStmt(self.clone())))
130             ];
131
132             // Some statements require a trailing semicolon.
133             if classify::stmt_ends_with_semi(&self.node) {
134                 tts.push(ast::TtToken(self.span, token::Semi));
135             }
136
137             tts
138         }
139     }
140
141     impl ToTokens for P<ast::Expr> {
142         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
143             vec![ast::TtToken(self.span, token::Interpolated(token::NtExpr(self.clone())))]
144         }
145     }
146
147     impl ToTokens for P<ast::Pat> {
148         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
149             vec![ast::TtToken(self.span, token::Interpolated(token::NtPat(self.clone())))]
150         }
151     }
152
153     impl ToTokens for ast::Arm {
154         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
155             vec![ast::TtToken(DUMMY_SP, token::Interpolated(token::NtArm(self.clone())))]
156         }
157     }
158
159     macro_rules! impl_to_tokens_slice {
160         ($t: ty, $sep: expr) => {
161             impl ToTokens for [$t] {
162                 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
163                     let mut v = vec![];
164                     for (i, x) in self.iter().enumerate() {
165                         if i > 0 {
166                             v.push_all(&$sep);
167                         }
168                         v.extend(x.to_tokens(cx));
169                     }
170                     v
171                 }
172             }
173         };
174     }
175
176     impl_to_tokens_slice! { ast::Ty, [ast::TtToken(DUMMY_SP, token::Comma)] }
177     impl_to_tokens_slice! { P<ast::Item>, [] }
178
179     impl ToTokens for P<ast::MetaItem> {
180         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
181             vec![ast::TtToken(DUMMY_SP, token::Interpolated(token::NtMeta(self.clone())))]
182         }
183     }
184
185     impl ToTokens for ast::Attribute {
186         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
187             let mut r = vec![];
188             // FIXME: The spans could be better
189             r.push(ast::TtToken(self.span, token::Pound));
190             if self.node.style == ast::AttrInner {
191                 r.push(ast::TtToken(self.span, token::Not));
192             }
193             r.push(ast::TtDelimited(self.span, Rc::new(ast::Delimited {
194                 delim: token::Bracket,
195                 open_span: self.span,
196                 tts: self.node.value.to_tokens(cx),
197                 close_span: self.span,
198             })));
199             r
200         }
201     }
202
203     impl ToTokens for str {
204         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
205             let lit = ast::LitStr(
206                 token::intern_and_get_ident(self), ast::CookedStr);
207             dummy_spanned(lit).to_tokens(cx)
208         }
209     }
210
211     impl ToTokens for () {
212         fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
213             vec![ast::TtDelimited(DUMMY_SP, Rc::new(ast::Delimited {
214                 delim: token::Paren,
215                 open_span: DUMMY_SP,
216                 tts: vec![],
217                 close_span: DUMMY_SP,
218             }))]
219         }
220     }
221
222     impl ToTokens for ast::Lit {
223         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
224             // FIXME: This is wrong
225             P(ast::Expr {
226                 id: ast::DUMMY_NODE_ID,
227                 node: ast::ExprLit(P(self.clone())),
228                 span: DUMMY_SP,
229             }).to_tokens(cx)
230         }
231     }
232
233     impl ToTokens for bool {
234         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
235             dummy_spanned(ast::LitBool(*self)).to_tokens(cx)
236         }
237     }
238
239     impl ToTokens for char {
240         fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
241             dummy_spanned(ast::LitChar(*self)).to_tokens(cx)
242         }
243     }
244
245     macro_rules! impl_to_tokens_int {
246         (signed, $t:ty, $tag:expr) => (
247             impl ToTokens for $t {
248                 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
249                     let lit = ast::LitInt(*self as u64, ast::SignedIntLit($tag,
250                                                                           ast::Sign::new(*self)));
251                     dummy_spanned(lit).to_tokens(cx)
252                 }
253             }
254         );
255         (unsigned, $t:ty, $tag:expr) => (
256             impl ToTokens for $t {
257                 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
258                     let lit = ast::LitInt(*self as u64, ast::UnsignedIntLit($tag));
259                     dummy_spanned(lit).to_tokens(cx)
260                 }
261             }
262         );
263     }
264
265     impl_to_tokens_int! { signed, isize, ast::TyIs }
266     impl_to_tokens_int! { signed, i8,  ast::TyI8 }
267     impl_to_tokens_int! { signed, i16, ast::TyI16 }
268     impl_to_tokens_int! { signed, i32, ast::TyI32 }
269     impl_to_tokens_int! { signed, i64, ast::TyI64 }
270
271     impl_to_tokens_int! { unsigned, usize, ast::TyUs }
272     impl_to_tokens_int! { unsigned, u8,   ast::TyU8 }
273     impl_to_tokens_int! { unsigned, u16,  ast::TyU16 }
274     impl_to_tokens_int! { unsigned, u32,  ast::TyU32 }
275     impl_to_tokens_int! { unsigned, u64,  ast::TyU64 }
276
277     pub trait ExtParseUtils {
278         fn parse_item(&self, s: String) -> P<ast::Item>;
279         fn parse_expr(&self, s: String) -> P<ast::Expr>;
280         fn parse_stmt(&self, s: String) -> P<ast::Stmt>;
281         fn parse_tts(&self, s: String) -> Vec<ast::TokenTree>;
282     }
283
284     impl<'a> ExtParseUtils for ExtCtxt<'a> {
285
286         fn parse_item(&self, s: String) -> P<ast::Item> {
287             parse::parse_item_from_source_str(
288                 "<quote expansion>".to_string(),
289                 s,
290                 self.cfg(),
291                 self.parse_sess()).expect("parse error")
292         }
293
294         fn parse_stmt(&self, s: String) -> P<ast::Stmt> {
295             parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
296                                               s,
297                                               self.cfg(),
298                                               self.parse_sess()).expect("parse error")
299         }
300
301         fn parse_expr(&self, s: String) -> P<ast::Expr> {
302             parse::parse_expr_from_source_str("<quote expansion>".to_string(),
303                                               s,
304                                               self.cfg(),
305                                               self.parse_sess())
306         }
307
308         fn parse_tts(&self, s: String) -> Vec<ast::TokenTree> {
309             parse::parse_tts_from_source_str("<quote expansion>".to_string(),
310                                              s,
311                                              self.cfg(),
312                                              self.parse_sess())
313         }
314     }
315 }
316
317 pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
318                                 sp: Span,
319                                 tts: &[ast::TokenTree])
320                                 -> Box<base::MacResult+'cx> {
321     let (cx_expr, expr) = expand_tts(cx, sp, tts);
322     let expanded = expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"]]);
323     base::MacEager::expr(expanded)
324 }
325
326 pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
327                               sp: Span,
328                               tts: &[ast::TokenTree])
329                               -> Box<base::MacResult+'cx> {
330     let expanded = expand_parse_call(cx, sp, "parse_expr", vec!(), tts);
331     base::MacEager::expr(expanded)
332 }
333
334 pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
335                               sp: Span,
336                               tts: &[ast::TokenTree])
337                               -> Box<base::MacResult+'cx> {
338     let expanded = expand_parse_call(cx, sp, "parse_item", vec!(), tts);
339     base::MacEager::expr(expanded)
340 }
341
342 pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
343                              sp: Span,
344                              tts: &[ast::TokenTree])
345                              -> Box<base::MacResult+'cx> {
346     let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
347     base::MacEager::expr(expanded)
348 }
349
350 pub fn expand_quote_arm(cx: &mut ExtCtxt,
351                         sp: Span,
352                         tts: &[ast::TokenTree])
353                         -> Box<base::MacResult+'static> {
354     let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
355     base::MacEager::expr(expanded)
356 }
357
358 pub fn expand_quote_ty(cx: &mut ExtCtxt,
359                        sp: Span,
360                        tts: &[ast::TokenTree])
361                        -> Box<base::MacResult+'static> {
362     let expanded = expand_parse_call(cx, sp, "parse_ty", vec!(), tts);
363     base::MacEager::expr(expanded)
364 }
365
366 pub fn expand_quote_stmt(cx: &mut ExtCtxt,
367                          sp: Span,
368                          tts: &[ast::TokenTree])
369                          -> Box<base::MacResult+'static> {
370     let expanded = expand_parse_call(cx, sp, "parse_stmt", vec!(), tts);
371     base::MacEager::expr(expanded)
372 }
373
374 pub fn expand_quote_attr(cx: &mut ExtCtxt,
375                          sp: Span,
376                          tts: &[ast::TokenTree])
377                          -> Box<base::MacResult+'static> {
378     let expanded = expand_parse_call(cx, sp, "parse_attribute",
379                                     vec!(cx.expr_bool(sp, true)), tts);
380
381     base::MacEager::expr(expanded)
382 }
383
384 pub fn expand_quote_matcher(cx: &mut ExtCtxt,
385                             sp: Span,
386                             tts: &[ast::TokenTree])
387                             -> Box<base::MacResult+'static> {
388     let (cx_expr, tts) = parse_arguments_to_quote(cx, tts);
389     let mut vector = mk_stmts_let(cx, sp);
390     vector.extend(statements_mk_tts(cx, &tts[..], true));
391     let block = cx.expr_block(
392         cx.block_all(sp,
393                      vector,
394                      Some(cx.expr_ident(sp, id_ext("tt")))));
395
396     let expanded = expand_wrapper(cx, sp, cx_expr, block, &[&["syntax", "ext", "quote", "rt"]]);
397     base::MacEager::expr(expanded)
398 }
399
400 fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
401     strs.iter().map(|str| str_to_ident(&(*str))).collect()
402 }
403
404 fn id_ext(str: &str) -> ast::Ident {
405     str_to_ident(str)
406 }
407
408 // Lift an ident to the expr that evaluates to that ident.
409 fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
410     let e_str = cx.expr_str(sp, ident.name.as_str());
411     cx.expr_method_call(sp,
412                         cx.expr_ident(sp, id_ext("ext_cx")),
413                         id_ext("ident_of"),
414                         vec!(e_str))
415 }
416
417 // Lift a name to the expr that evaluates to that name
418 fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
419     let e_str = cx.expr_str(sp, ident.name.as_str());
420     cx.expr_method_call(sp,
421                         cx.expr_ident(sp, id_ext("ext_cx")),
422                         id_ext("name_of"),
423                         vec!(e_str))
424 }
425
426 fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
427     let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
428     cx.expr_path(cx.path_global(sp, idents))
429 }
430
431 fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
432     let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
433     cx.expr_path(cx.path_global(sp, idents))
434 }
435
436 fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOpToken) -> P<ast::Expr> {
437     let name = match bop {
438         token::Plus     => "Plus",
439         token::Minus    => "Minus",
440         token::Star     => "Star",
441         token::Slash    => "Slash",
442         token::Percent  => "Percent",
443         token::Caret    => "Caret",
444         token::And      => "And",
445         token::Or       => "Or",
446         token::Shl      => "Shl",
447         token::Shr      => "Shr"
448     };
449     mk_token_path(cx, sp, name)
450 }
451
452 fn mk_delim(cx: &ExtCtxt, sp: Span, delim: token::DelimToken) -> P<ast::Expr> {
453     let name = match delim {
454         token::Paren     => "Paren",
455         token::Bracket   => "Bracket",
456         token::Brace     => "Brace",
457     };
458     mk_token_path(cx, sp, name)
459 }
460
461 #[allow(non_upper_case_globals)]
462 fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
463     macro_rules! mk_lit {
464         ($name: expr, $suffix: expr, $($args: expr),*) => {{
465             let inner = cx.expr_call(sp, mk_token_path(cx, sp, $name), vec![$($args),*]);
466             let suffix = match $suffix {
467                 Some(name) => cx.expr_some(sp, mk_name(cx, sp, ast::Ident::with_empty_ctxt(name))),
468                 None => cx.expr_none(sp)
469             };
470             cx.expr_call(sp, mk_token_path(cx, sp, "Literal"), vec![inner, suffix])
471         }}
472     }
473     match *tok {
474         token::BinOp(binop) => {
475             return cx.expr_call(sp, mk_token_path(cx, sp, "BinOp"), vec!(mk_binop(cx, sp, binop)));
476         }
477         token::BinOpEq(binop) => {
478             return cx.expr_call(sp, mk_token_path(cx, sp, "BinOpEq"),
479                                 vec!(mk_binop(cx, sp, binop)));
480         }
481
482         token::OpenDelim(delim) => {
483             return cx.expr_call(sp, mk_token_path(cx, sp, "OpenDelim"),
484                                 vec![mk_delim(cx, sp, delim)]);
485         }
486         token::CloseDelim(delim) => {
487             return cx.expr_call(sp, mk_token_path(cx, sp, "CloseDelim"),
488                                 vec![mk_delim(cx, sp, delim)]);
489         }
490
491         token::Literal(token::Byte(i), suf) => {
492             let e_byte = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
493             return mk_lit!("Byte", suf, e_byte);
494         }
495
496         token::Literal(token::Char(i), suf) => {
497             let e_char = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
498             return mk_lit!("Char", suf, e_char);
499         }
500
501         token::Literal(token::Integer(i), suf) => {
502             let e_int = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
503             return mk_lit!("Integer", suf, e_int);
504         }
505
506         token::Literal(token::Float(fident), suf) => {
507             let e_fident = mk_name(cx, sp, ast::Ident::with_empty_ctxt(fident));
508             return mk_lit!("Float", suf, e_fident);
509         }
510
511         token::Literal(token::Str_(ident), suf) => {
512             return mk_lit!("Str_", suf, mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident)))
513         }
514
515         token::Literal(token::StrRaw(ident, n), suf) => {
516             return mk_lit!("StrRaw", suf, mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident)),
517                            cx.expr_usize(sp, n))
518         }
519
520         token::Ident(ident, style) => {
521             return cx.expr_call(sp,
522                                 mk_token_path(cx, sp, "Ident"),
523                                 vec![mk_ident(cx, sp, ident),
524                                      match style {
525                                         ModName => mk_token_path(cx, sp, "ModName"),
526                                         Plain   => mk_token_path(cx, sp, "Plain"),
527                                      }]);
528         }
529
530         token::Lifetime(ident) => {
531             return cx.expr_call(sp,
532                                 mk_token_path(cx, sp, "Lifetime"),
533                                 vec!(mk_ident(cx, sp, ident)));
534         }
535
536         token::DocComment(ident) => {
537             return cx.expr_call(sp,
538                                 mk_token_path(cx, sp, "DocComment"),
539                                 vec!(mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident))));
540         }
541
542         token::MatchNt(name, kind, namep, kindp) => {
543             return cx.expr_call(sp,
544                                 mk_token_path(cx, sp, "MatchNt"),
545                                 vec!(mk_ident(cx, sp, name),
546                                      mk_ident(cx, sp, kind),
547                                      match namep {
548                                         ModName => mk_token_path(cx, sp, "ModName"),
549                                         Plain   => mk_token_path(cx, sp, "Plain"),
550                                      },
551                                      match kindp {
552                                         ModName => mk_token_path(cx, sp, "ModName"),
553                                         Plain   => mk_token_path(cx, sp, "Plain"),
554                                      }));
555         }
556
557         token::Interpolated(_) => panic!("quote! with interpolated token"),
558
559         _ => ()
560     }
561
562     let name = match *tok {
563         token::Eq           => "Eq",
564         token::Lt           => "Lt",
565         token::Le           => "Le",
566         token::EqEq         => "EqEq",
567         token::Ne           => "Ne",
568         token::Ge           => "Ge",
569         token::Gt           => "Gt",
570         token::AndAnd       => "AndAnd",
571         token::OrOr         => "OrOr",
572         token::Not          => "Not",
573         token::Tilde        => "Tilde",
574         token::At           => "At",
575         token::Dot          => "Dot",
576         token::DotDot       => "DotDot",
577         token::Comma        => "Comma",
578         token::Semi         => "Semi",
579         token::Colon        => "Colon",
580         token::ModSep       => "ModSep",
581         token::RArrow       => "RArrow",
582         token::LArrow       => "LArrow",
583         token::FatArrow     => "FatArrow",
584         token::Pound        => "Pound",
585         token::Dollar       => "Dollar",
586         token::Question     => "Question",
587         token::Underscore   => "Underscore",
588         token::Eof          => "Eof",
589         _                   => panic!("unhandled token in quote!"),
590     };
591     mk_token_path(cx, sp, name)
592 }
593
594 fn statements_mk_tt(cx: &ExtCtxt, tt: &ast::TokenTree, matcher: bool) -> Vec<P<ast::Stmt>> {
595     match *tt {
596         ast::TtToken(sp, SubstNt(ident, _)) => {
597             // tt.extend($ident.to_tokens(ext_cx))
598
599             let e_to_toks =
600                 cx.expr_method_call(sp,
601                                     cx.expr_ident(sp, ident),
602                                     id_ext("to_tokens"),
603                                     vec!(cx.expr_ident(sp, id_ext("ext_cx"))));
604             let e_to_toks =
605                 cx.expr_method_call(sp, e_to_toks, id_ext("into_iter"), vec![]);
606
607             let e_push =
608                 cx.expr_method_call(sp,
609                                     cx.expr_ident(sp, id_ext("tt")),
610                                     id_ext("extend"),
611                                     vec!(e_to_toks));
612
613             vec!(cx.stmt_expr(e_push))
614         }
615         ref tt @ ast::TtToken(_, MatchNt(..)) if !matcher => {
616             let mut seq = vec![];
617             for i in 0..tt.len() {
618                 seq.push(tt.get_tt(i));
619             }
620             statements_mk_tts(cx, &seq[..], matcher)
621         }
622         ast::TtToken(sp, ref tok) => {
623             let e_sp = cx.expr_ident(sp, id_ext("_sp"));
624             let e_tok = cx.expr_call(sp,
625                                      mk_ast_path(cx, sp, "TtToken"),
626                                      vec!(e_sp, expr_mk_token(cx, sp, tok)));
627             let e_push =
628                 cx.expr_method_call(sp,
629                                     cx.expr_ident(sp, id_ext("tt")),
630                                     id_ext("push"),
631                                     vec!(e_tok));
632             vec!(cx.stmt_expr(e_push))
633         },
634         ast::TtDelimited(_, ref delimed) => {
635             statements_mk_tt(cx, &delimed.open_tt(), matcher).into_iter()
636                 .chain(delimed.tts.iter()
637                                   .flat_map(|tt| statements_mk_tt(cx, tt, matcher)))
638                 .chain(statements_mk_tt(cx, &delimed.close_tt(), matcher))
639                 .collect()
640         },
641         ast::TtSequence(sp, ref seq) => {
642             if !matcher {
643                 panic!("TtSequence in quote!");
644             }
645
646             let e_sp = cx.expr_ident(sp, id_ext("_sp"));
647
648             let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
649             let mut tts_stmts = vec![stmt_let_tt];
650             tts_stmts.extend(statements_mk_tts(cx, &seq.tts[..], matcher));
651             let e_tts = cx.expr_block(cx.block(sp, tts_stmts,
652                                                    Some(cx.expr_ident(sp, id_ext("tt")))));
653             let e_separator = match seq.separator {
654                 Some(ref sep) => cx.expr_some(sp, expr_mk_token(cx, sp, sep)),
655                 None => cx.expr_none(sp),
656             };
657             let e_op = match seq.op {
658                 ast::ZeroOrMore => mk_ast_path(cx, sp, "ZeroOrMore"),
659                 ast::OneOrMore => mk_ast_path(cx, sp, "OneOrMore"),
660             };
661             let fields = vec![cx.field_imm(sp, id_ext("tts"), e_tts),
662                               cx.field_imm(sp, id_ext("separator"), e_separator),
663                               cx.field_imm(sp, id_ext("op"), e_op),
664                               cx.field_imm(sp, id_ext("num_captures"),
665                                                cx.expr_usize(sp, seq.num_captures))];
666             let seq_path = vec![id_ext("syntax"), id_ext("ast"), id_ext("SequenceRepetition")];
667             let e_seq_struct = cx.expr_struct(sp, cx.path_global(sp, seq_path), fields);
668             let e_rc_new = cx.expr_call_global(sp, vec![id_ext("std"),
669                                                         id_ext("rc"),
670                                                         id_ext("Rc"),
671                                                         id_ext("new")],
672                                                    vec![e_seq_struct]);
673             let e_tok = cx.expr_call(sp,
674                                      mk_ast_path(cx, sp, "TtSequence"),
675                                      vec!(e_sp, e_rc_new));
676             let e_push =
677                 cx.expr_method_call(sp,
678                                     cx.expr_ident(sp, id_ext("tt")),
679                                     id_ext("push"),
680                                     vec!(e_tok));
681             vec!(cx.stmt_expr(e_push))
682         }
683     }
684 }
685
686 fn parse_arguments_to_quote(cx: &ExtCtxt, tts: &[ast::TokenTree])
687                             -> (P<ast::Expr>, Vec<ast::TokenTree>) {
688     // NB: It appears that the main parser loses its mind if we consider
689     // $foo as a SubstNt during the main parse, so we have to re-parse
690     // under quote_depth > 0. This is silly and should go away; the _guess_ is
691     // it has to do with transition away from supporting old-style macros, so
692     // try removing it when enough of them are gone.
693
694     let mut p = cx.new_parser_from_tts(tts);
695     p.quote_depth += 1;
696
697     let cx_expr = p.parse_expr();
698     if !panictry!(p.eat(&token::Comma)) {
699         panic!(p.fatal("expected token `,`"));
700     }
701
702     let tts = panictry!(p.parse_all_token_trees());
703     p.abort_if_errors();
704
705     (cx_expr, tts)
706 }
707
708 fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec<P<ast::Stmt>> {
709     // We also bind a single value, sp, to ext_cx.call_site()
710     //
711     // This causes every span in a token-tree quote to be attributed to the
712     // call site of the extension using the quote. We can't really do much
713     // better since the source of the quote may well be in a library that
714     // was not even parsed by this compilation run, that the user has no
715     // source code for (eg. in libsyntax, which they're just _using_).
716     //
717     // The old quasiquoter had an elaborate mechanism for denoting input
718     // file locations from which quotes originated; unfortunately this
719     // relied on feeding the source string of the quote back into the
720     // compiler (which we don't really want to do) and, in any case, only
721     // pushed the problem a very small step further back: an error
722     // resulting from a parse of the resulting quote is still attributed to
723     // the site the string literal occurred, which was in a source file
724     // _other_ than the one the user has control over. For example, an
725     // error in a quote from the protocol compiler, invoked in user code
726     // using macro_rules! for example, will be attributed to the macro_rules.rs
727     // file in libsyntax, which the user might not even have source to (unless
728     // they happen to have a compiler on hand). Over all, the phase distinction
729     // just makes quotes "hard to attribute". Possibly this could be fixed
730     // by recreating some of the original qq machinery in the tt regime
731     // (pushing fake FileMaps onto the parser to account for original sites
732     // of quotes, for example) but at this point it seems not likely to be
733     // worth the hassle.
734
735     let e_sp = cx.expr_method_call(sp,
736                                    cx.expr_ident(sp, id_ext("ext_cx")),
737                                    id_ext("call_site"),
738                                    Vec::new());
739
740     let stmt_let_sp = cx.stmt_let(sp, false,
741                                   id_ext("_sp"),
742                                   e_sp);
743
744     let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
745
746     vec!(stmt_let_sp, stmt_let_tt)
747 }
748
749 fn statements_mk_tts(cx: &ExtCtxt, tts: &[ast::TokenTree], matcher: bool) -> Vec<P<ast::Stmt>> {
750     let mut ss = Vec::new();
751     for tt in tts {
752         ss.extend(statements_mk_tt(cx, tt, matcher));
753     }
754     ss
755 }
756
757 fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
758               -> (P<ast::Expr>, P<ast::Expr>) {
759     let (cx_expr, tts) = parse_arguments_to_quote(cx, tts);
760
761     let mut vector = mk_stmts_let(cx, sp);
762     vector.extend(statements_mk_tts(cx, &tts[..], false));
763     let block = cx.expr_block(
764         cx.block_all(sp,
765                      vector,
766                      Some(cx.expr_ident(sp, id_ext("tt")))));
767
768     (cx_expr, block)
769 }
770
771 fn expand_wrapper(cx: &ExtCtxt,
772                   sp: Span,
773                   cx_expr: P<ast::Expr>,
774                   expr: P<ast::Expr>,
775                   imports: &[&[&str]]) -> P<ast::Expr> {
776     // Explicitly borrow to avoid moving from the invoker (#16992)
777     let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr));
778     let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow);
779
780     let stmts = imports.iter().map(|path| {
781         // make item: `use ...;`
782         let path = path.iter().map(|s| s.to_string()).collect();
783         cx.stmt_item(sp, cx.item_use_glob(sp, ast::Inherited, ids_ext(path)))
784     }).chain(Some(stmt_let_ext_cx)).collect();
785
786     cx.expr_block(cx.block_all(sp, stmts, Some(expr)))
787 }
788
789 fn expand_parse_call(cx: &ExtCtxt,
790                      sp: Span,
791                      parse_method: &str,
792                      arg_exprs: Vec<P<ast::Expr>> ,
793                      tts: &[ast::TokenTree]) -> P<ast::Expr> {
794     let (cx_expr, tts_expr) = expand_tts(cx, sp, tts);
795
796     let cfg_call = || cx.expr_method_call(
797         sp, cx.expr_ident(sp, id_ext("ext_cx")),
798         id_ext("cfg"), Vec::new());
799
800     let parse_sess_call = || cx.expr_method_call(
801         sp, cx.expr_ident(sp, id_ext("ext_cx")),
802         id_ext("parse_sess"), Vec::new());
803
804     let new_parser_call =
805         cx.expr_call(sp,
806                      cx.expr_ident(sp, id_ext("new_parser_from_tts")),
807                      vec!(parse_sess_call(), cfg_call(), tts_expr));
808
809     let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
810                                    arg_exprs);
811
812     if parse_method == "parse_attribute" {
813         expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"],
814                                                 &["syntax", "parse", "attr"]])
815     } else {
816         expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"]])
817     }
818 }