1 // Copyright 2012 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.
13 use ext::base::ExtCtxt;
15 use ext::build::AstBuilder;
22 * Quasiquoting works via token trees.
24 * This is registered as a set of expression syntax extension called quote!
25 * that lifts its argument token-tree to an AST representing the
26 * construction of the same token tree, with ast::TTNonterminal nodes
27 * interpreted as antiquotes (splices).
34 use ext::base::ExtCtxt;
40 use ast::{TokenTree, Generics, Expr};
42 pub use parse::new_parser_from_tts;
43 pub use codemap::{BytePos, Span, dummy_spanned};
46 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> ;
49 impl ToTokens for TokenTree {
50 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
55 impl<T: ToTokens> ToTokens for Vec<T> {
56 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
57 let a = self.iter().flat_map(|t| t.to_tokens(cx).into_iter());
58 FromIterator::from_iter(a)
62 impl<T: ToTokens> ToTokens for Spanned<T> {
63 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
64 // FIXME: use the span?
65 self.node.to_tokens(cx)
69 impl<T: ToTokens> ToTokens for Option<T> {
70 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
72 &Some(ref t) => t.to_tokens(cx),
78 /* Should be (when bugs in default methods are fixed):
80 trait ToSource : ToTokens {
81 // Takes a thing and generates a string containing rust code for it.
82 pub fn to_source() -> String;
84 // If you can make source, you can definitely make tokens.
85 pub fn to_tokens(cx: &ExtCtxt) -> ~[TokenTree] {
86 cx.parse_tts(self.to_source())
92 // FIXME: Move this trait to pprust and get rid of *_to_str?
94 // Takes a thing and generates a string containing rust code for it.
95 fn to_source(&self) -> String;
98 // FIXME (Issue #16472): This should go away after ToToken impls
99 // are revised to go directly to token-trees.
100 trait ToSourceWithHygiene : ToSource {
101 // Takes a thing and generates a string containing rust code
102 // for it, encoding Idents as special byte sequences to
103 // maintain hygiene across serialization and deserialization.
104 fn to_source_with_hygiene(&self) -> String;
107 macro_rules! impl_to_source(
108 (P<$t:ty>, $pp:ident) => (
109 impl ToSource for P<$t> {
110 fn to_source(&self) -> String {
114 impl ToSourceWithHygiene for P<$t> {
115 fn to_source_with_hygiene(&self) -> String {
116 pprust::with_hygiene::$pp(&**self)
120 ($t:ty, $pp:ident) => (
121 impl ToSource for $t {
122 fn to_source(&self) -> String {
126 impl ToSourceWithHygiene for $t {
127 fn to_source_with_hygiene(&self) -> String {
128 pprust::with_hygiene::$pp(self)
134 fn slice_to_source<'a, T: ToSource>(sep: &'static str, xs: &'a [T]) -> String {
136 .map(|i| i.to_source())
137 .collect::<Vec<String>>()
142 fn slice_to_source_with_hygiene<'a, T: ToSourceWithHygiene>(
143 sep: &'static str, xs: &'a [T]) -> String {
145 .map(|i| i.to_source_with_hygiene())
146 .collect::<Vec<String>>()
151 macro_rules! impl_to_source_slice(
152 ($t:ty, $sep:expr) => (
153 impl<'a> ToSource for &'a [$t] {
154 fn to_source(&self) -> String {
155 slice_to_source($sep, *self)
159 impl<'a> ToSourceWithHygiene for &'a [$t] {
160 fn to_source_with_hygiene(&self) -> String {
161 slice_to_source_with_hygiene($sep, *self)
167 impl ToSource for ast::Ident {
168 fn to_source(&self) -> String {
169 token::get_ident(*self).get().to_string()
173 impl ToSourceWithHygiene for ast::Ident {
174 fn to_source_with_hygiene(&self) -> String {
175 self.encode_with_hygiene()
179 impl_to_source!(ast::Ty, ty_to_string)
180 impl_to_source!(ast::Block, block_to_string)
181 impl_to_source!(ast::Arg, arg_to_string)
182 impl_to_source!(Generics, generics_to_string)
183 impl_to_source!(P<ast::Item>, item_to_string)
184 impl_to_source!(P<ast::Method>, method_to_string)
185 impl_to_source!(P<ast::Stmt>, stmt_to_string)
186 impl_to_source!(P<ast::Expr>, expr_to_string)
187 impl_to_source!(P<ast::Pat>, pat_to_string)
188 impl_to_source!(ast::Arm, arm_to_string)
189 impl_to_source_slice!(ast::Ty, ", ")
190 impl_to_source_slice!(P<ast::Item>, "\n\n")
192 impl ToSource for ast::Attribute_ {
193 fn to_source(&self) -> String {
194 pprust::attribute_to_string(&dummy_spanned(self.clone()))
197 impl ToSourceWithHygiene for ast::Attribute_ {
198 fn to_source_with_hygiene(&self) -> String {
203 impl<'a> ToSource for &'a str {
204 fn to_source(&self) -> String {
205 let lit = dummy_spanned(ast::LitStr(
206 token::intern_and_get_ident(*self), ast::CookedStr));
207 pprust::lit_to_string(&lit)
210 impl<'a> ToSourceWithHygiene for &'a str {
211 fn to_source_with_hygiene(&self) -> String {
216 impl ToSource for () {
217 fn to_source(&self) -> String {
221 impl ToSourceWithHygiene for () {
222 fn to_source_with_hygiene(&self) -> String {
227 impl ToSource for bool {
228 fn to_source(&self) -> String {
229 let lit = dummy_spanned(ast::LitBool(*self));
230 pprust::lit_to_string(&lit)
233 impl ToSourceWithHygiene for bool {
234 fn to_source_with_hygiene(&self) -> String {
239 impl ToSource for char {
240 fn to_source(&self) -> String {
241 let lit = dummy_spanned(ast::LitChar(*self));
242 pprust::lit_to_string(&lit)
245 impl ToSourceWithHygiene for char {
246 fn to_source_with_hygiene(&self) -> String {
251 macro_rules! impl_to_source_int(
252 (signed, $t:ty, $tag:ident) => (
253 impl ToSource for $t {
254 fn to_source(&self) -> String {
255 let lit = ast::LitInt(*self as u64, ast::SignedIntLit(ast::$tag,
256 ast::Sign::new(*self)));
257 pprust::lit_to_string(&dummy_spanned(lit))
260 impl ToSourceWithHygiene for $t {
261 fn to_source_with_hygiene(&self) -> String {
266 (unsigned, $t:ty, $tag:ident) => (
267 impl ToSource for $t {
268 fn to_source(&self) -> String {
269 let lit = ast::LitInt(*self as u64, ast::UnsignedIntLit(ast::$tag));
270 pprust::lit_to_string(&dummy_spanned(lit))
273 impl ToSourceWithHygiene for $t {
274 fn to_source_with_hygiene(&self) -> String {
281 impl_to_source_int!(signed, int, TyI)
282 impl_to_source_int!(signed, i8, TyI8)
283 impl_to_source_int!(signed, i16, TyI16)
284 impl_to_source_int!(signed, i32, TyI32)
285 impl_to_source_int!(signed, i64, TyI64)
287 impl_to_source_int!(unsigned, uint, TyU)
288 impl_to_source_int!(unsigned, u8, TyU8)
289 impl_to_source_int!(unsigned, u16, TyU16)
290 impl_to_source_int!(unsigned, u32, TyU32)
291 impl_to_source_int!(unsigned, u64, TyU64)
293 // Alas ... we write these out instead. All redundant.
295 macro_rules! impl_to_tokens(
297 impl ToTokens for $t {
298 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
299 cx.parse_tts_with_hygiene(self.to_source_with_hygiene())
305 macro_rules! impl_to_tokens_lifetime(
307 impl<'a> ToTokens for $t {
308 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
309 cx.parse_tts_with_hygiene(self.to_source_with_hygiene())
315 impl_to_tokens!(ast::Ident)
316 impl_to_tokens!(P<ast::Item>)
317 impl_to_tokens!(P<ast::Pat>)
318 impl_to_tokens!(ast::Arm)
319 impl_to_tokens!(P<ast::Method>)
320 impl_to_tokens_lifetime!(&'a [P<ast::Item>])
321 impl_to_tokens!(ast::Ty)
322 impl_to_tokens_lifetime!(&'a [ast::Ty])
323 impl_to_tokens!(Generics)
324 impl_to_tokens!(P<ast::Stmt>)
325 impl_to_tokens!(P<ast::Expr>)
326 impl_to_tokens!(ast::Block)
327 impl_to_tokens!(ast::Arg)
328 impl_to_tokens!(ast::Attribute_)
329 impl_to_tokens_lifetime!(&'a str)
331 impl_to_tokens!(char)
332 impl_to_tokens!(bool)
338 impl_to_tokens!(uint)
344 pub trait ExtParseUtils {
345 fn parse_item(&self, s: String) -> P<ast::Item>;
346 fn parse_expr(&self, s: String) -> P<ast::Expr>;
347 fn parse_stmt(&self, s: String) -> P<ast::Stmt>;
348 fn parse_tts(&self, s: String) -> Vec<ast::TokenTree>;
351 trait ExtParseUtilsWithHygiene {
352 // FIXME (Issue #16472): This should go away after ToToken impls
353 // are revised to go directly to token-trees.
354 fn parse_tts_with_hygiene(&self, s: String) -> Vec<ast::TokenTree>;
357 impl<'a> ExtParseUtils for ExtCtxt<'a> {
359 fn parse_item(&self, s: String) -> P<ast::Item> {
360 let res = parse::parse_item_from_source_str(
361 "<quote expansion>".to_string(),
368 error!("parse error");
374 fn parse_stmt(&self, s: String) -> P<ast::Stmt> {
375 parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
382 fn parse_expr(&self, s: String) -> P<ast::Expr> {
383 parse::parse_expr_from_source_str("<quote expansion>".to_string(),
389 fn parse_tts(&self, s: String) -> Vec<ast::TokenTree> {
390 parse::parse_tts_from_source_str("<quote expansion>".to_string(),
397 impl<'a> ExtParseUtilsWithHygiene for ExtCtxt<'a> {
399 fn parse_tts_with_hygiene(&self, s: String) -> Vec<ast::TokenTree> {
400 use parse::with_hygiene::parse_tts_from_source_str;
401 parse_tts_from_source_str("<quote expansion>".to_string(),
411 pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
413 tts: &[ast::TokenTree])
414 -> Box<base::MacResult+'cx> {
415 let (cx_expr, expr) = expand_tts(cx, sp, tts);
416 let expanded = expand_wrapper(cx, sp, cx_expr, expr);
417 base::MacExpr::new(expanded)
420 pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
422 tts: &[ast::TokenTree])
423 -> Box<base::MacResult+'cx> {
424 let expanded = expand_parse_call(cx, sp, "parse_expr", Vec::new(), tts);
425 base::MacExpr::new(expanded)
428 pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
430 tts: &[ast::TokenTree])
431 -> Box<base::MacResult+'cx> {
432 let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
434 base::MacExpr::new(expanded)
437 pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
439 tts: &[ast::TokenTree])
440 -> Box<base::MacResult+'cx> {
441 let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
442 base::MacExpr::new(expanded)
445 pub fn expand_quote_arm(cx: &mut ExtCtxt,
447 tts: &[ast::TokenTree])
448 -> Box<base::MacResult+'static> {
449 let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
450 base::MacExpr::new(expanded)
453 pub fn expand_quote_ty(cx: &mut ExtCtxt,
455 tts: &[ast::TokenTree])
456 -> Box<base::MacResult+'static> {
457 let e_param_colons = cx.expr_lit(sp, ast::LitBool(false));
458 let expanded = expand_parse_call(cx, sp, "parse_ty",
459 vec!(e_param_colons), tts);
460 base::MacExpr::new(expanded)
463 pub fn expand_quote_method(cx: &mut ExtCtxt,
465 tts: &[ast::TokenTree])
466 -> Box<base::MacResult+'static> {
467 let e_param_colons = cx.expr_none(sp);
468 let expanded = expand_parse_call(cx, sp, "parse_method",
469 vec!(e_param_colons), tts);
470 base::MacExpr::new(expanded)
473 pub fn expand_quote_stmt(cx: &mut ExtCtxt,
475 tts: &[ast::TokenTree])
476 -> Box<base::MacResult+'static> {
477 let e_attrs = cx.expr_vec_ng(sp);
478 let expanded = expand_parse_call(cx, sp, "parse_stmt",
480 base::MacExpr::new(expanded)
483 fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
484 strs.iter().map(|str| str_to_ident((*str).as_slice())).collect()
487 fn id_ext(str: &str) -> ast::Ident {
491 // Lift an ident to the expr that evaluates to that ident.
492 fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
493 let e_str = cx.expr_str(sp, token::get_ident(ident));
494 cx.expr_method_call(sp,
495 cx.expr_ident(sp, id_ext("ext_cx")),
500 // Lift a name to the expr that evaluates to that name
501 fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> P<ast::Expr> {
502 let e_str = cx.expr_str(sp, token::get_ident(ident));
503 cx.expr_method_call(sp,
504 cx.expr_ident(sp, id_ext("ext_cx")),
509 fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
510 let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
511 cx.expr_path(cx.path_global(sp, idents))
514 fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
515 let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
516 cx.expr_path(cx.path_global(sp, idents))
519 fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> P<ast::Expr> {
520 let name = match bop {
525 PERCENT => "PERCENT",
532 mk_token_path(cx, sp, name)
535 fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
539 return cx.expr_call(sp, mk_token_path(cx, sp, "BINOP"), vec!(mk_binop(cx, sp, binop)));
542 return cx.expr_call(sp, mk_token_path(cx, sp, "BINOPEQ"),
543 vec!(mk_binop(cx, sp, binop)));
547 let e_byte = mk_name(cx, sp, i.ident());
549 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_BYTE"), vec!(e_byte));
553 let e_char = mk_name(cx, sp, i.ident());
555 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_CHAR"), vec!(e_char));
559 let e_int = mk_name(cx, sp, i.ident());
560 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INTEGER"), vec!(e_int));
563 LIT_FLOAT(fident) => {
564 let e_fident = mk_name(cx, sp, fident.ident());
565 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident));
569 return cx.expr_call(sp,
570 mk_token_path(cx, sp, "LIT_STR"),
571 vec!(mk_name(cx, sp, ident.ident())));
574 LIT_STR_RAW(ident, n) => {
575 return cx.expr_call(sp,
576 mk_token_path(cx, sp, "LIT_STR_RAW"),
577 vec!(mk_name(cx, sp, ident.ident()), cx.expr_uint(sp, n)));
581 return cx.expr_call(sp,
582 mk_token_path(cx, sp, "IDENT"),
583 vec!(mk_ident(cx, sp, ident), cx.expr_bool(sp, b)));
587 return cx.expr_call(sp,
588 mk_token_path(cx, sp, "LIFETIME"),
589 vec!(mk_ident(cx, sp, ident)));
592 DOC_COMMENT(ident) => {
593 return cx.expr_call(sp,
594 mk_token_path(cx, sp, "DOC_COMMENT"),
595 vec!(mk_name(cx, sp, ident.ident())));
598 INTERPOLATED(_) => fail!("quote! with interpolated token"),
603 let name = match *tok {
621 MOD_SEP => "MOD_SEP",
624 FAT_ARROW => "FAT_ARROW",
627 LBRACKET => "LBRACKET",
628 RBRACKET => "RBRACKET",
633 UNDERSCORE => "UNDERSCORE",
637 mk_token_path(cx, sp, name)
641 fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> {
643 ast::TTTok(sp, ref tok) => {
644 let e_sp = cx.expr_ident(sp, id_ext("_sp"));
645 let e_tok = cx.expr_call(sp,
646 mk_ast_path(cx, sp, "TTTok"),
647 vec!(e_sp, mk_token(cx, sp, tok)));
649 cx.expr_method_call(sp,
650 cx.expr_ident(sp, id_ext("tt")),
653 vec!(cx.stmt_expr(e_push))
656 ast::TTDelim(ref tts) => mk_tts(cx, sp, tts.as_slice()),
657 ast::TTSeq(..) => fail!("TTSeq in quote!"),
659 ast::TTNonterminal(sp, ident) => {
661 // tt.push_all_move($ident.to_tokens(ext_cx))
664 cx.expr_method_call(sp,
665 cx.expr_ident(sp, ident),
667 vec!(cx.expr_ident(sp, id_ext("ext_cx"))));
670 cx.expr_method_call(sp,
671 cx.expr_ident(sp, id_ext("tt")),
672 id_ext("push_all_move"),
675 vec!(cx.stmt_expr(e_push))
680 fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
681 -> Vec<P<ast::Stmt>> {
682 let mut ss = Vec::new();
683 for tt in tts.iter() {
684 ss.push_all_move(mk_tt(cx, sp, tt));
689 fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
690 -> (P<ast::Expr>, P<ast::Expr>) {
691 // NB: It appears that the main parser loses its mind if we consider
692 // $foo as a TTNonterminal during the main parse, so we have to re-parse
693 // under quote_depth > 0. This is silly and should go away; the _guess_ is
694 // it has to do with transition away from supporting old-style macros, so
695 // try removing it when enough of them are gone.
697 let mut p = cx.new_parser_from_tts(tts);
700 let cx_expr = p.parse_expr();
701 if !p.eat(&token::COMMA) {
702 p.fatal("expected token `,`");
705 let tts = p.parse_all_token_trees();
708 // We also bind a single value, sp, to ext_cx.call_site()
710 // This causes every span in a token-tree quote to be attributed to the
711 // call site of the extension using the quote. We can't really do much
712 // better since the source of the quote may well be in a library that
713 // was not even parsed by this compilation run, that the user has no
714 // source code for (eg. in libsyntax, which they're just _using_).
716 // The old quasiquoter had an elaborate mechanism for denoting input
717 // file locations from which quotes originated; unfortunately this
718 // relied on feeding the source string of the quote back into the
719 // compiler (which we don't really want to do) and, in any case, only
720 // pushed the problem a very small step further back: an error
721 // resulting from a parse of the resulting quote is still attributed to
722 // the site the string literal occurred, which was in a source file
723 // _other_ than the one the user has control over. For example, an
724 // error in a quote from the protocol compiler, invoked in user code
725 // using macro_rules! for example, will be attributed to the macro_rules.rs
726 // file in libsyntax, which the user might not even have source to (unless
727 // they happen to have a compiler on hand). Over all, the phase distinction
728 // just makes quotes "hard to attribute". Possibly this could be fixed
729 // by recreating some of the original qq machinery in the tt regime
730 // (pushing fake FileMaps onto the parser to account for original sites
731 // of quotes, for example) but at this point it seems not likely to be
734 let e_sp = cx.expr_method_call(sp,
735 cx.expr_ident(sp, id_ext("ext_cx")),
739 let stmt_let_sp = cx.stmt_let(sp, false,
743 let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
745 let mut vector = vec!(stmt_let_sp, stmt_let_tt);
746 vector.push_all_move(mk_tts(cx, sp, tts.as_slice()));
747 let block = cx.expr_block(
751 Some(cx.expr_ident(sp, id_ext("tt")))));
756 fn expand_wrapper(cx: &ExtCtxt,
758 cx_expr: P<ast::Expr>,
759 expr: P<ast::Expr>) -> P<ast::Expr> {
761 &["syntax", "ext", "quote", "rt"],
762 ].iter().map(|path| {
763 let path = path.iter().map(|s| s.to_string()).collect();
764 cx.view_use_glob(sp, ast::Inherited, ids_ext(path))
767 // Explicitly borrow to avoid moving from the invoker (#16992)
768 let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr));
769 let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow);
771 cx.expr_block(cx.block_all(sp, uses, vec!(stmt_let_ext_cx), Some(expr)))
774 fn expand_parse_call(cx: &ExtCtxt,
777 arg_exprs: Vec<P<ast::Expr>> ,
778 tts: &[ast::TokenTree]) -> P<ast::Expr> {
779 let (cx_expr, tts_expr) = expand_tts(cx, sp, tts);
781 let cfg_call = || cx.expr_method_call(
782 sp, cx.expr_ident(sp, id_ext("ext_cx")),
783 id_ext("cfg"), Vec::new());
785 let parse_sess_call = || cx.expr_method_call(
786 sp, cx.expr_ident(sp, id_ext("ext_cx")),
787 id_ext("parse_sess"), Vec::new());
789 let new_parser_call =
791 cx.expr_ident(sp, id_ext("new_parser_from_tts")),
792 vec!(parse_sess_call(), cfg_call(), tts_expr));
794 let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
797 expand_wrapper(cx, sp, cx_expr, expr)