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.
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.
11 use ast::{self, Arg, Arm, Block, Expr, Item, Pat, Path, Stmt, TokenTree, Ty};
13 use ext::base::ExtCtxt;
15 use ext::build::AstBuilder;
16 use parse::parser::{Parser, PathParsingMode};
21 /// Quasiquoting works via token trees.
23 /// This is registered as a set of expression syntax extension called quote!
24 /// that lifts its argument token-tree to an AST representing the
25 /// construction of the same token tree, with token::SubstNt interpreted
26 /// as antiquotes (splices).
31 use ext::base::ExtCtxt;
32 use parse::{self, token, classify};
36 use ast::{TokenTree, Expr};
38 pub use parse::new_parser_from_tts;
39 pub use codemap::{BytePos, Span, dummy_spanned, DUMMY_SP};
42 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree>;
45 impl ToTokens for TokenTree {
46 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
51 impl<T: ToTokens> ToTokens for Vec<T> {
52 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
53 self.iter().flat_map(|t| t.to_tokens(cx)).collect()
57 impl<T: ToTokens> ToTokens for Spanned<T> {
58 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
59 // FIXME: use the span?
60 self.node.to_tokens(cx)
64 impl<T: ToTokens> ToTokens for Option<T> {
65 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
67 Some(ref t) => t.to_tokens(cx),
73 impl ToTokens for ast::Ident {
74 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
75 vec![TokenTree::Token(DUMMY_SP, token::Ident(*self, token::Plain))]
79 impl ToTokens for ast::Path {
80 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
81 vec![TokenTree::Token(DUMMY_SP,
82 token::Interpolated(token::NtPath(Box::new(self.clone()))))]
86 impl ToTokens for ast::Ty {
87 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
88 vec![TokenTree::Token(self.span, token::Interpolated(token::NtTy(P(self.clone()))))]
92 impl ToTokens for ast::Block {
93 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
94 vec![TokenTree::Token(self.span, token::Interpolated(token::NtBlock(P(self.clone()))))]
98 impl ToTokens for ast::Generics {
99 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
100 vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtGenerics(self.clone())))]
104 impl ToTokens for ast::WhereClause {
105 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
106 vec![TokenTree::Token(DUMMY_SP,
107 token::Interpolated(token::NtWhereClause(self.clone())))]
111 impl ToTokens for P<ast::Item> {
112 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
113 vec![TokenTree::Token(self.span, token::Interpolated(token::NtItem(self.clone())))]
117 impl ToTokens for P<ast::ImplItem> {
118 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
119 vec![TokenTree::Token(self.span, token::Interpolated(token::NtImplItem(self.clone())))]
123 impl ToTokens for P<ast::TraitItem> {
124 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
125 vec![TokenTree::Token(self.span, token::Interpolated(token::NtTraitItem(self.clone())))]
129 impl ToTokens for P<ast::Stmt> {
130 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
132 TokenTree::Token(self.span, token::Interpolated(token::NtStmt(self.clone())))
135 // Some statements require a trailing semicolon.
136 if classify::stmt_ends_with_semi(&self.node) {
137 tts.push(TokenTree::Token(self.span, token::Semi));
144 impl ToTokens for P<ast::Expr> {
145 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
146 vec![TokenTree::Token(self.span, token::Interpolated(token::NtExpr(self.clone())))]
150 impl ToTokens for P<ast::Pat> {
151 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
152 vec![TokenTree::Token(self.span, token::Interpolated(token::NtPat(self.clone())))]
156 impl ToTokens for ast::Arm {
157 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
158 vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtArm(self.clone())))]
162 impl ToTokens for ast::Arg {
163 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
164 vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtArg(self.clone())))]
168 impl ToTokens for P<ast::Block> {
169 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
170 vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtBlock(self.clone())))]
174 macro_rules! impl_to_tokens_slice {
175 ($t: ty, $sep: expr) => {
176 impl ToTokens for [$t] {
177 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
179 for (i, x) in self.iter().enumerate() {
183 v.extend(x.to_tokens(cx));
191 impl_to_tokens_slice! { ast::Ty, [TokenTree::Token(DUMMY_SP, token::Comma)] }
192 impl_to_tokens_slice! { P<ast::Item>, [] }
193 impl_to_tokens_slice! { ast::Arg, [TokenTree::Token(DUMMY_SP, token::Comma)] }
195 impl ToTokens for P<ast::MetaItem> {
196 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
197 vec![TokenTree::Token(DUMMY_SP, token::Interpolated(token::NtMeta(self.clone())))]
201 impl ToTokens for ast::Attribute {
202 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
204 // FIXME: The spans could be better
205 r.push(TokenTree::Token(self.span, token::Pound));
206 if self.node.style == ast::AttrStyle::Inner {
207 r.push(TokenTree::Token(self.span, token::Not));
209 r.push(TokenTree::Delimited(self.span, Rc::new(ast::Delimited {
210 delim: token::Bracket,
211 open_span: self.span,
212 tts: self.node.value.to_tokens(cx),
213 close_span: self.span,
219 impl ToTokens for str {
220 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
221 let lit = ast::LitStr(
222 token::intern_and_get_ident(self), ast::CookedStr);
223 dummy_spanned(lit).to_tokens(cx)
227 impl ToTokens for () {
228 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
229 vec![TokenTree::Delimited(DUMMY_SP, Rc::new(ast::Delimited {
233 close_span: DUMMY_SP,
238 impl ToTokens for ast::Lit {
239 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
240 // FIXME: This is wrong
242 id: ast::DUMMY_NODE_ID,
243 node: ast::ExprLit(P(self.clone())),
249 impl ToTokens for bool {
250 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
251 dummy_spanned(ast::LitBool(*self)).to_tokens(cx)
255 impl ToTokens for char {
256 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
257 dummy_spanned(ast::LitChar(*self)).to_tokens(cx)
261 macro_rules! impl_to_tokens_int {
262 (signed, $t:ty, $tag:expr) => (
263 impl ToTokens for $t {
264 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
265 let lit = ast::LitInt(*self as u64, ast::SignedIntLit($tag,
266 ast::Sign::new(*self)));
267 dummy_spanned(lit).to_tokens(cx)
271 (unsigned, $t:ty, $tag:expr) => (
272 impl ToTokens for $t {
273 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
274 let lit = ast::LitInt(*self as u64, ast::UnsignedIntLit($tag));
275 dummy_spanned(lit).to_tokens(cx)
281 impl_to_tokens_int! { signed, isize, ast::TyIs }
282 impl_to_tokens_int! { signed, i8, ast::TyI8 }
283 impl_to_tokens_int! { signed, i16, ast::TyI16 }
284 impl_to_tokens_int! { signed, i32, ast::TyI32 }
285 impl_to_tokens_int! { signed, i64, ast::TyI64 }
287 impl_to_tokens_int! { unsigned, usize, ast::TyUs }
288 impl_to_tokens_int! { unsigned, u8, ast::TyU8 }
289 impl_to_tokens_int! { unsigned, u16, ast::TyU16 }
290 impl_to_tokens_int! { unsigned, u32, ast::TyU32 }
291 impl_to_tokens_int! { unsigned, u64, ast::TyU64 }
293 pub trait ExtParseUtils {
294 fn parse_item(&self, s: String) -> P<ast::Item>;
295 fn parse_expr(&self, s: String) -> P<ast::Expr>;
296 fn parse_stmt(&self, s: String) -> P<ast::Stmt>;
297 fn parse_tts(&self, s: String) -> Vec<TokenTree>;
300 impl<'a> ExtParseUtils for ExtCtxt<'a> {
302 fn parse_item(&self, s: String) -> P<ast::Item> {
303 parse::parse_item_from_source_str(
304 "<quote expansion>".to_string(),
307 self.parse_sess()).expect("parse error")
310 fn parse_stmt(&self, s: String) -> P<ast::Stmt> {
311 parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
314 self.parse_sess()).expect("parse error")
317 fn parse_expr(&self, s: String) -> P<ast::Expr> {
318 parse::parse_expr_from_source_str("<quote expansion>".to_string(),
324 fn parse_tts(&self, s: String) -> Vec<TokenTree> {
325 parse::parse_tts_from_source_str("<quote expansion>".to_string(),
333 // These panicking parsing functions are used by the quote_*!() syntax extensions,
334 // but shouldn't be used otherwise.
335 pub fn parse_expr_panic(parser: &mut Parser) -> P<Expr> {
336 panictry!(parser.parse_expr())
339 pub fn parse_item_panic(parser: &mut Parser) -> Option<P<Item>> {
340 panictry!(parser.parse_item())
343 pub fn parse_pat_panic(parser: &mut Parser) -> P<Pat> {
344 panictry!(parser.parse_pat())
347 pub fn parse_arm_panic(parser: &mut Parser) -> Arm {
348 panictry!(parser.parse_arm())
351 pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> {
352 panictry!(parser.parse_ty())
355 pub fn parse_stmt_panic(parser: &mut Parser) -> Option<P<Stmt>> {
356 panictry!(parser.parse_stmt())
359 pub fn parse_attribute_panic(parser: &mut Parser, permit_inner: bool) -> ast::Attribute {
360 panictry!(parser.parse_attribute(permit_inner))
363 pub fn parse_arg_panic(parser: &mut Parser) -> Arg {
364 panictry!(parser.parse_arg())
367 pub fn parse_block_panic(parser: &mut Parser) -> P<Block> {
368 panictry!(parser.parse_block())
371 pub fn parse_meta_item_panic(parser: &mut Parser) -> P<ast::MetaItem> {
372 panictry!(parser.parse_meta_item())
375 pub fn parse_path_panic(parser: &mut Parser, mode: PathParsingMode) -> ast::Path {
376 panictry!(parser.parse_path(mode))
379 pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
382 -> Box<base::MacResult+'cx> {
383 let (cx_expr, expr) = expand_tts(cx, sp, tts);
384 let expanded = expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"]]);
385 base::MacEager::expr(expanded)
388 pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
391 -> Box<base::MacResult+'cx> {
392 let expanded = expand_parse_call(cx, sp, "parse_expr_panic", vec!(), tts);
393 base::MacEager::expr(expanded)
396 pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
399 -> Box<base::MacResult+'cx> {
400 let expanded = expand_parse_call(cx, sp, "parse_item_panic", vec!(), tts);
401 base::MacEager::expr(expanded)
404 pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
407 -> Box<base::MacResult+'cx> {
408 let expanded = expand_parse_call(cx, sp, "parse_pat_panic", vec!(), tts);
409 base::MacEager::expr(expanded)
412 pub fn expand_quote_arm(cx: &mut ExtCtxt,
415 -> Box<base::MacResult+'static> {
416 let expanded = expand_parse_call(cx, sp, "parse_arm_panic", vec!(), tts);
417 base::MacEager::expr(expanded)
420 pub fn expand_quote_ty(cx: &mut ExtCtxt,
423 -> Box<base::MacResult+'static> {
424 let expanded = expand_parse_call(cx, sp, "parse_ty_panic", vec!(), tts);
425 base::MacEager::expr(expanded)
428 pub fn expand_quote_stmt(cx: &mut ExtCtxt,
431 -> Box<base::MacResult+'static> {
432 let expanded = expand_parse_call(cx, sp, "parse_stmt_panic", vec!(), tts);
433 base::MacEager::expr(expanded)
436 pub fn expand_quote_attr(cx: &mut ExtCtxt,
439 -> Box<base::MacResult+'static> {
440 let expanded = expand_parse_call(cx, sp, "parse_attribute_panic",
441 vec!(cx.expr_bool(sp, true)), tts);
443 base::MacEager::expr(expanded)
446 pub fn expand_quote_arg(cx: &mut ExtCtxt,
449 -> Box<base::MacResult+'static> {
450 let expanded = expand_parse_call(cx, sp, "parse_arg_panic", vec!(), tts);
451 base::MacEager::expr(expanded)
454 pub fn expand_quote_block(cx: &mut ExtCtxt,
457 -> Box<base::MacResult+'static> {
458 let expanded = expand_parse_call(cx, sp, "parse_block_panic", vec!(), tts);
459 base::MacEager::expr(expanded)
462 pub fn expand_quote_meta_item(cx: &mut ExtCtxt,
465 -> Box<base::MacResult+'static> {
466 let expanded = expand_parse_call(cx, sp, "parse_meta_item_panic", vec!(), tts);
467 base::MacEager::expr(expanded)
470 pub fn expand_quote_path(cx: &mut ExtCtxt,
473 -> Box<base::MacResult+'static> {
474 let mode = mk_parser_path(cx, sp, "LifetimeAndTypesWithoutColons");
475 let expanded = expand_parse_call(cx, sp, "parse_path_panic", vec!(mode), tts);
476 base::MacEager::expr(expanded)
479 pub fn expand_quote_matcher(cx: &mut ExtCtxt,
482 -> Box<base::MacResult+'static> {
483 let (cx_expr, tts) = parse_arguments_to_quote(cx, tts);
484 let mut vector = mk_stmts_let(cx, sp);
485 vector.extend(statements_mk_tts(cx, &tts[..], true));
486 let block = cx.expr_block(
489 Some(cx.expr_ident(sp, id_ext("tt")))));
491 let expanded = expand_wrapper(cx, sp, cx_expr, block, &[&["syntax", "ext", "quote", "rt"]]);
492 base::MacEager::expr(expanded)
495 fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
496 strs.iter().map(|str| str_to_ident(&(*str))).collect()
499 fn id_ext(str: &str) -> ast::Ident {
503 // Lift an ident to the expr that evaluates to that ident.
504 fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
505 let e_str = cx.expr_str(sp, ident.name.as_str());
506 cx.expr_method_call(sp,
507 cx.expr_ident(sp, id_ext("ext_cx")),
512 // Lift a name to the expr that evaluates to that name
513 fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
514 let e_str = cx.expr_str(sp, ident.name.as_str());
515 cx.expr_method_call(sp,
516 cx.expr_ident(sp, id_ext("ext_cx")),
521 fn mk_tt_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
522 let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext("TokenTree"), id_ext(name));
523 cx.expr_path(cx.path_global(sp, idents))
526 fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
527 let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
528 cx.expr_path(cx.path_global(sp, idents))
531 fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
532 let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
533 cx.expr_path(cx.path_global(sp, idents))
536 fn mk_parser_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
537 let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("parser"), id_ext(name));
538 cx.expr_path(cx.path_global(sp, idents))
541 fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOpToken) -> P<ast::Expr> {
542 let name = match bop {
543 token::Plus => "Plus",
544 token::Minus => "Minus",
545 token::Star => "Star",
546 token::Slash => "Slash",
547 token::Percent => "Percent",
548 token::Caret => "Caret",
554 mk_token_path(cx, sp, name)
557 fn mk_delim(cx: &ExtCtxt, sp: Span, delim: token::DelimToken) -> P<ast::Expr> {
558 let name = match delim {
559 token::Paren => "Paren",
560 token::Bracket => "Bracket",
561 token::Brace => "Brace",
563 mk_token_path(cx, sp, name)
566 #[allow(non_upper_case_globals)]
567 fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
568 macro_rules! mk_lit {
569 ($name: expr, $suffix: expr, $($args: expr),*) => {{
570 let inner = cx.expr_call(sp, mk_token_path(cx, sp, $name), vec![$($args),*]);
571 let suffix = match $suffix {
572 Some(name) => cx.expr_some(sp, mk_name(cx, sp, ast::Ident::with_empty_ctxt(name))),
573 None => cx.expr_none(sp)
575 cx.expr_call(sp, mk_token_path(cx, sp, "Literal"), vec![inner, suffix])
579 token::BinOp(binop) => {
580 return cx.expr_call(sp, mk_token_path(cx, sp, "BinOp"), vec!(mk_binop(cx, sp, binop)));
582 token::BinOpEq(binop) => {
583 return cx.expr_call(sp, mk_token_path(cx, sp, "BinOpEq"),
584 vec!(mk_binop(cx, sp, binop)));
587 token::OpenDelim(delim) => {
588 return cx.expr_call(sp, mk_token_path(cx, sp, "OpenDelim"),
589 vec![mk_delim(cx, sp, delim)]);
591 token::CloseDelim(delim) => {
592 return cx.expr_call(sp, mk_token_path(cx, sp, "CloseDelim"),
593 vec![mk_delim(cx, sp, delim)]);
596 token::Literal(token::Byte(i), suf) => {
597 let e_byte = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
598 return mk_lit!("Byte", suf, e_byte);
601 token::Literal(token::Char(i), suf) => {
602 let e_char = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
603 return mk_lit!("Char", suf, e_char);
606 token::Literal(token::Integer(i), suf) => {
607 let e_int = mk_name(cx, sp, ast::Ident::with_empty_ctxt(i));
608 return mk_lit!("Integer", suf, e_int);
611 token::Literal(token::Float(fident), suf) => {
612 let e_fident = mk_name(cx, sp, ast::Ident::with_empty_ctxt(fident));
613 return mk_lit!("Float", suf, e_fident);
616 token::Literal(token::Str_(ident), suf) => {
617 return mk_lit!("Str_", suf, mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident)))
620 token::Literal(token::StrRaw(ident, n), suf) => {
621 return mk_lit!("StrRaw", suf, mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident)),
622 cx.expr_usize(sp, n))
625 token::Ident(ident, style) => {
626 return cx.expr_call(sp,
627 mk_token_path(cx, sp, "Ident"),
628 vec![mk_ident(cx, sp, ident),
630 ModName => mk_token_path(cx, sp, "ModName"),
631 Plain => mk_token_path(cx, sp, "Plain"),
635 token::Lifetime(ident) => {
636 return cx.expr_call(sp,
637 mk_token_path(cx, sp, "Lifetime"),
638 vec!(mk_ident(cx, sp, ident)));
641 token::DocComment(ident) => {
642 return cx.expr_call(sp,
643 mk_token_path(cx, sp, "DocComment"),
644 vec!(mk_name(cx, sp, ast::Ident::with_empty_ctxt(ident))));
647 token::MatchNt(name, kind, namep, kindp) => {
648 return cx.expr_call(sp,
649 mk_token_path(cx, sp, "MatchNt"),
650 vec!(mk_ident(cx, sp, name),
651 mk_ident(cx, sp, kind),
653 ModName => mk_token_path(cx, sp, "ModName"),
654 Plain => mk_token_path(cx, sp, "Plain"),
657 ModName => mk_token_path(cx, sp, "ModName"),
658 Plain => mk_token_path(cx, sp, "Plain"),
662 token::Interpolated(_) => panic!("quote! with interpolated token"),
667 let name = match *tok {
671 token::EqEq => "EqEq",
675 token::AndAnd => "AndAnd",
676 token::OrOr => "OrOr",
678 token::Tilde => "Tilde",
681 token::DotDot => "DotDot",
682 token::Comma => "Comma",
683 token::Semi => "Semi",
684 token::Colon => "Colon",
685 token::ModSep => "ModSep",
686 token::RArrow => "RArrow",
687 token::LArrow => "LArrow",
688 token::FatArrow => "FatArrow",
689 token::Pound => "Pound",
690 token::Dollar => "Dollar",
691 token::Question => "Question",
692 token::Underscore => "Underscore",
694 _ => panic!("unhandled token in quote!"),
696 mk_token_path(cx, sp, name)
699 fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, matcher: bool) -> Vec<P<ast::Stmt>> {
701 TokenTree::Token(sp, SubstNt(ident, _)) => {
702 // tt.extend($ident.to_tokens(ext_cx))
705 cx.expr_method_call(sp,
706 cx.expr_ident(sp, ident),
708 vec!(cx.expr_ident(sp, id_ext("ext_cx"))));
710 cx.expr_method_call(sp, e_to_toks, id_ext("into_iter"), vec![]);
713 cx.expr_method_call(sp,
714 cx.expr_ident(sp, id_ext("tt")),
718 vec!(cx.stmt_expr(e_push))
720 ref tt @ TokenTree::Token(_, MatchNt(..)) if !matcher => {
721 let mut seq = vec![];
722 for i in 0..tt.len() {
723 seq.push(tt.get_tt(i));
725 statements_mk_tts(cx, &seq[..], matcher)
727 TokenTree::Token(sp, ref tok) => {
728 let e_sp = cx.expr_ident(sp, id_ext("_sp"));
729 let e_tok = cx.expr_call(sp,
730 mk_tt_path(cx, sp, "Token"),
731 vec!(e_sp, expr_mk_token(cx, sp, tok)));
733 cx.expr_method_call(sp,
734 cx.expr_ident(sp, id_ext("tt")),
737 vec!(cx.stmt_expr(e_push))
739 TokenTree::Delimited(_, ref delimed) => {
740 statements_mk_tt(cx, &delimed.open_tt(), matcher).into_iter()
741 .chain(delimed.tts.iter()
742 .flat_map(|tt| statements_mk_tt(cx, tt, matcher)))
743 .chain(statements_mk_tt(cx, &delimed.close_tt(), matcher))
746 TokenTree::Sequence(sp, ref seq) => {
748 panic!("TokenTree::Sequence in quote!");
751 let e_sp = cx.expr_ident(sp, id_ext("_sp"));
753 let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
754 let mut tts_stmts = vec![stmt_let_tt];
755 tts_stmts.extend(statements_mk_tts(cx, &seq.tts[..], matcher));
756 let e_tts = cx.expr_block(cx.block(sp, tts_stmts,
757 Some(cx.expr_ident(sp, id_ext("tt")))));
758 let e_separator = match seq.separator {
759 Some(ref sep) => cx.expr_some(sp, expr_mk_token(cx, sp, sep)),
760 None => cx.expr_none(sp),
762 let e_op = match seq.op {
763 ast::ZeroOrMore => mk_ast_path(cx, sp, "ZeroOrMore"),
764 ast::OneOrMore => mk_ast_path(cx, sp, "OneOrMore"),
766 let fields = vec![cx.field_imm(sp, id_ext("tts"), e_tts),
767 cx.field_imm(sp, id_ext("separator"), e_separator),
768 cx.field_imm(sp, id_ext("op"), e_op),
769 cx.field_imm(sp, id_ext("num_captures"),
770 cx.expr_usize(sp, seq.num_captures))];
771 let seq_path = vec![id_ext("syntax"), id_ext("ast"), id_ext("SequenceRepetition")];
772 let e_seq_struct = cx.expr_struct(sp, cx.path_global(sp, seq_path), fields);
773 let e_rc_new = cx.expr_call_global(sp, vec![id_ext("std"),
778 let e_tok = cx.expr_call(sp,
779 mk_tt_path(cx, sp, "Sequence"),
780 vec!(e_sp, e_rc_new));
782 cx.expr_method_call(sp,
783 cx.expr_ident(sp, id_ext("tt")),
786 vec!(cx.stmt_expr(e_push))
791 fn parse_arguments_to_quote(cx: &ExtCtxt, tts: &[TokenTree])
792 -> (P<ast::Expr>, Vec<TokenTree>) {
793 // NB: It appears that the main parser loses its mind if we consider
794 // $foo as a SubstNt during the main parse, so we have to re-parse
795 // under quote_depth > 0. This is silly and should go away; the _guess_ is
796 // it has to do with transition away from supporting old-style macros, so
797 // try removing it when enough of them are gone.
799 let mut p = cx.new_parser_from_tts(tts);
802 let cx_expr = panictry!(p.parse_expr());
803 if !panictry!(p.eat(&token::Comma)) {
804 panic!(p.fatal("expected token `,`"));
807 let tts = panictry!(p.parse_all_token_trees());
813 fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec<P<ast::Stmt>> {
814 // We also bind a single value, sp, to ext_cx.call_site()
816 // This causes every span in a token-tree quote to be attributed to the
817 // call site of the extension using the quote. We can't really do much
818 // better since the source of the quote may well be in a library that
819 // was not even parsed by this compilation run, that the user has no
820 // source code for (eg. in libsyntax, which they're just _using_).
822 // The old quasiquoter had an elaborate mechanism for denoting input
823 // file locations from which quotes originated; unfortunately this
824 // relied on feeding the source string of the quote back into the
825 // compiler (which we don't really want to do) and, in any case, only
826 // pushed the problem a very small step further back: an error
827 // resulting from a parse of the resulting quote is still attributed to
828 // the site the string literal occurred, which was in a source file
829 // _other_ than the one the user has control over. For example, an
830 // error in a quote from the protocol compiler, invoked in user code
831 // using macro_rules! for example, will be attributed to the macro_rules.rs
832 // file in libsyntax, which the user might not even have source to (unless
833 // they happen to have a compiler on hand). Over all, the phase distinction
834 // just makes quotes "hard to attribute". Possibly this could be fixed
835 // by recreating some of the original qq machinery in the tt regime
836 // (pushing fake FileMaps onto the parser to account for original sites
837 // of quotes, for example) but at this point it seems not likely to be
840 let e_sp = cx.expr_method_call(sp,
841 cx.expr_ident(sp, id_ext("ext_cx")),
845 let stmt_let_sp = cx.stmt_let(sp, false,
849 let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
851 vec!(stmt_let_sp, stmt_let_tt)
854 fn statements_mk_tts(cx: &ExtCtxt, tts: &[TokenTree], matcher: bool) -> Vec<P<ast::Stmt>> {
855 let mut ss = Vec::new();
857 ss.extend(statements_mk_tt(cx, tt, matcher));
862 fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[TokenTree])
863 -> (P<ast::Expr>, P<ast::Expr>) {
864 let (cx_expr, tts) = parse_arguments_to_quote(cx, tts);
866 let mut vector = mk_stmts_let(cx, sp);
867 vector.extend(statements_mk_tts(cx, &tts[..], false));
868 let block = cx.expr_block(
871 Some(cx.expr_ident(sp, id_ext("tt")))));
876 fn expand_wrapper(cx: &ExtCtxt,
878 cx_expr: P<ast::Expr>,
880 imports: &[&[&str]]) -> P<ast::Expr> {
881 // Explicitly borrow to avoid moving from the invoker (#16992)
882 let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr));
883 let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow);
885 let stmts = imports.iter().map(|path| {
886 // make item: `use ...;`
887 let path = path.iter().map(|s| s.to_string()).collect();
888 cx.stmt_item(sp, cx.item_use_glob(sp, ast::Inherited, ids_ext(path)))
889 }).chain(Some(stmt_let_ext_cx)).collect();
891 cx.expr_block(cx.block_all(sp, stmts, Some(expr)))
894 fn expand_parse_call(cx: &ExtCtxt,
897 arg_exprs: Vec<P<ast::Expr>> ,
898 tts: &[TokenTree]) -> P<ast::Expr> {
899 let (cx_expr, tts_expr) = expand_tts(cx, sp, tts);
901 let cfg_call = || cx.expr_method_call(
902 sp, cx.expr_ident(sp, id_ext("ext_cx")),
903 id_ext("cfg"), Vec::new());
905 let parse_sess_call = || cx.expr_method_call(
906 sp, cx.expr_ident(sp, id_ext("ext_cx")),
907 id_ext("parse_sess"), Vec::new());
909 let new_parser_call =
911 cx.expr_ident(sp, id_ext("new_parser_from_tts")),
912 vec!(parse_sess_call(), cfg_call(), tts_expr));
914 let path = vec![id_ext("syntax"), id_ext("ext"), id_ext("quote"), id_ext(parse_method)];
915 let mut args = vec![cx.expr_mut_addr_of(sp, new_parser_call)];
916 args.extend(arg_exprs);
917 let expr = cx.expr_call_global(sp, path, args);
919 if parse_method == "parse_attribute" {
920 expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"],
921 &["syntax", "parse", "attr"]])
923 expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"]])