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;
23 * Quasiquoting works via token trees.
25 * This is registered as a set of expression syntax extension called quote!
26 * that lifts its argument token-tree to an AST representing the
27 * construction of the same token tree, with ast::TTNonterminal nodes
28 * interpreted as antiquotes (splices).
35 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};
48 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> ;
51 impl ToTokens for TokenTree {
52 fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
57 impl<T: ToTokens> ToTokens for Vec<T> {
58 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
59 let a = self.iter().flat_map(|t| t.to_tokens(cx).move_iter());
60 FromIterator::from_iter(a)
64 impl<T: ToTokens> ToTokens for Spanned<T> {
65 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
66 // FIXME: use the span?
67 self.node.to_tokens(cx)
71 impl<T: ToTokens> ToTokens for Option<T> {
72 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
74 &Some(ref t) => t.to_tokens(cx),
80 /* Should be (when bugs in default methods are fixed):
82 trait ToSource : ToTokens {
83 // Takes a thing and generates a string containing rust code for it.
84 pub fn to_source() -> String;
86 // If you can make source, you can definitely make tokens.
87 pub fn to_tokens(cx: &ExtCtxt) -> ~[TokenTree] {
88 cx.parse_tts(self.to_source())
94 // FIXME: Move this trait to pprust and get rid of *_to_str?
96 // Takes a thing and generates a string containing rust code for it.
97 fn to_source(&self) -> String;
100 // FIXME (Issue #16472): This should go away after ToToken impls
101 // are revised to go directly to token-trees.
102 trait ToSourceWithHygiene : ToSource {
103 // Takes a thing and generates a string containing rust code
104 // for it, encoding Idents as special byte sequences to
105 // maintain hygiene across serialization and deserialization.
106 fn to_source_with_hygiene(&self) -> String;
109 macro_rules! impl_to_source(
110 (Gc<$t:ty>, $pp:ident) => (
111 impl ToSource for Gc<$t> {
112 fn to_source(&self) -> String {
116 impl ToSourceWithHygiene for Gc<$t> {
117 fn to_source_with_hygiene(&self) -> String {
118 pprust::with_hygiene::$pp(&**self)
122 ($t:ty, $pp:ident) => (
123 impl ToSource for $t {
124 fn to_source(&self) -> String {
128 impl ToSourceWithHygiene for $t {
129 fn to_source_with_hygiene(&self) -> String {
130 pprust::with_hygiene::$pp(self)
136 fn slice_to_source<'a, T: ToSource>(sep: &'static str, xs: &'a [T]) -> String {
138 .map(|i| i.to_source())
139 .collect::<Vec<String>>()
144 fn slice_to_source_with_hygiene<'a, T: ToSourceWithHygiene>(
145 sep: &'static str, xs: &'a [T]) -> String {
147 .map(|i| i.to_source_with_hygiene())
148 .collect::<Vec<String>>()
153 macro_rules! impl_to_source_slice(
154 ($t:ty, $sep:expr) => (
155 impl<'a> ToSource for &'a [$t] {
156 fn to_source(&self) -> String {
157 slice_to_source($sep, *self)
161 impl<'a> ToSourceWithHygiene for &'a [$t] {
162 fn to_source_with_hygiene(&self) -> String {
163 slice_to_source_with_hygiene($sep, *self)
169 impl ToSource for ast::Ident {
170 fn to_source(&self) -> String {
171 token::get_ident(*self).get().to_string()
175 impl ToSourceWithHygiene for ast::Ident {
176 fn to_source_with_hygiene(&self) -> String {
177 self.encode_with_hygiene()
181 impl_to_source!(ast::Ty, ty_to_string)
182 impl_to_source!(ast::Block, block_to_string)
183 impl_to_source!(ast::Arg, arg_to_string)
184 impl_to_source!(Generics, generics_to_string)
185 impl_to_source!(Gc<ast::Item>, item_to_string)
186 impl_to_source!(Gc<ast::Method>, method_to_string)
187 impl_to_source!(Gc<ast::Stmt>, stmt_to_string)
188 impl_to_source!(Gc<ast::Expr>, expr_to_string)
189 impl_to_source!(Gc<ast::Pat>, pat_to_string)
190 impl_to_source!(ast::Arm, arm_to_string)
191 impl_to_source_slice!(ast::Ty, ", ")
192 impl_to_source_slice!(Gc<ast::Item>, "\n\n")
194 impl ToSource for ast::Attribute_ {
195 fn to_source(&self) -> String {
196 pprust::attribute_to_string(&dummy_spanned(*self))
199 impl ToSourceWithHygiene for ast::Attribute_ {
200 fn to_source_with_hygiene(&self) -> String {
205 impl<'a> ToSource for &'a str {
206 fn to_source(&self) -> String {
207 let lit = dummy_spanned(ast::LitStr(
208 token::intern_and_get_ident(*self), ast::CookedStr));
209 pprust::lit_to_string(&lit)
212 impl<'a> ToSourceWithHygiene for &'a str {
213 fn to_source_with_hygiene(&self) -> String {
218 impl ToSource for () {
219 fn to_source(&self) -> String {
223 impl ToSourceWithHygiene for () {
224 fn to_source_with_hygiene(&self) -> String {
229 impl ToSource for bool {
230 fn to_source(&self) -> String {
231 let lit = dummy_spanned(ast::LitBool(*self));
232 pprust::lit_to_string(&lit)
235 impl ToSourceWithHygiene for bool {
236 fn to_source_with_hygiene(&self) -> String {
241 impl ToSource for char {
242 fn to_source(&self) -> String {
243 let lit = dummy_spanned(ast::LitChar(*self));
244 pprust::lit_to_string(&lit)
247 impl ToSourceWithHygiene for char {
248 fn to_source_with_hygiene(&self) -> String {
253 macro_rules! impl_to_source_int(
254 (signed, $t:ty, $tag:ident) => (
255 impl ToSource for $t {
256 fn to_source(&self) -> String {
257 let lit = ast::LitInt(*self as u64, ast::SignedIntLit(ast::$tag,
258 ast::Sign::new(*self)));
259 pprust::lit_to_string(&dummy_spanned(lit))
262 impl ToSourceWithHygiene for $t {
263 fn to_source_with_hygiene(&self) -> String {
268 (unsigned, $t:ty, $tag:ident) => (
269 impl ToSource for $t {
270 fn to_source(&self) -> String {
271 let lit = ast::LitInt(*self as u64, ast::UnsignedIntLit(ast::$tag));
272 pprust::lit_to_string(&dummy_spanned(lit))
275 impl ToSourceWithHygiene for $t {
276 fn to_source_with_hygiene(&self) -> String {
283 impl_to_source_int!(signed, int, TyI)
284 impl_to_source_int!(signed, i8, TyI8)
285 impl_to_source_int!(signed, i16, TyI16)
286 impl_to_source_int!(signed, i32, TyI32)
287 impl_to_source_int!(signed, i64, TyI64)
289 impl_to_source_int!(unsigned, uint, TyU)
290 impl_to_source_int!(unsigned, u8, TyU8)
291 impl_to_source_int!(unsigned, u16, TyU16)
292 impl_to_source_int!(unsigned, u32, TyU32)
293 impl_to_source_int!(unsigned, u64, TyU64)
295 // Alas ... we write these out instead. All redundant.
297 macro_rules! impl_to_tokens(
299 impl ToTokens for $t {
300 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
301 cx.parse_tts_with_hygiene(self.to_source_with_hygiene())
307 macro_rules! impl_to_tokens_lifetime(
309 impl<'a> ToTokens for $t {
310 fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
311 cx.parse_tts_with_hygiene(self.to_source_with_hygiene())
317 impl_to_tokens!(ast::Ident)
318 impl_to_tokens!(Gc<ast::Item>)
319 impl_to_tokens!(Gc<ast::Pat>)
320 impl_to_tokens!(ast::Arm)
321 impl_to_tokens!(Gc<ast::Method>)
322 impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
323 impl_to_tokens!(ast::Ty)
324 impl_to_tokens_lifetime!(&'a [ast::Ty])
325 impl_to_tokens!(Generics)
326 impl_to_tokens!(Gc<ast::Stmt>)
327 impl_to_tokens!(Gc<ast::Expr>)
328 impl_to_tokens!(ast::Block)
329 impl_to_tokens!(ast::Arg)
330 impl_to_tokens!(ast::Attribute_)
331 impl_to_tokens_lifetime!(&'a str)
333 impl_to_tokens!(char)
334 impl_to_tokens!(bool)
340 impl_to_tokens!(uint)
346 pub trait ExtParseUtils {
347 fn parse_item(&self, s: String) -> Gc<ast::Item>;
348 fn parse_expr(&self, s: String) -> Gc<ast::Expr>;
349 fn parse_stmt(&self, s: String) -> Gc<ast::Stmt>;
350 fn parse_tts(&self, s: String) -> Vec<ast::TokenTree>;
353 trait ExtParseUtilsWithHygiene {
354 // FIXME (Issue #16472): This should go away after ToToken impls
355 // are revised to go directly to token-trees.
356 fn parse_tts_with_hygiene(&self, s: String) -> Vec<ast::TokenTree>;
359 impl<'a> ExtParseUtils for ExtCtxt<'a> {
361 fn parse_item(&self, s: String) -> Gc<ast::Item> {
362 let res = parse::parse_item_from_source_str(
363 "<quote expansion>".to_string(),
370 error!("parse error");
376 fn parse_stmt(&self, s: String) -> Gc<ast::Stmt> {
377 parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
384 fn parse_expr(&self, s: String) -> Gc<ast::Expr> {
385 parse::parse_expr_from_source_str("<quote expansion>".to_string(),
391 fn parse_tts(&self, s: String) -> Vec<ast::TokenTree> {
392 parse::parse_tts_from_source_str("<quote expansion>".to_string(),
399 impl<'a> ExtParseUtilsWithHygiene for ExtCtxt<'a> {
401 fn parse_tts_with_hygiene(&self, s: String) -> Vec<ast::TokenTree> {
402 use parse::with_hygiene::parse_tts_from_source_str;
403 parse_tts_from_source_str("<quote expansion>".to_string(),
413 pub fn expand_quote_tokens(cx: &mut ExtCtxt,
415 tts: &[ast::TokenTree])
416 -> Box<base::MacResult> {
417 let (cx_expr, expr) = expand_tts(cx, sp, tts);
418 let expanded = expand_wrapper(cx, sp, cx_expr, expr);
419 base::MacExpr::new(expanded)
422 pub fn expand_quote_expr(cx: &mut ExtCtxt,
424 tts: &[ast::TokenTree]) -> Box<base::MacResult> {
425 let expanded = expand_parse_call(cx, sp, "parse_expr", Vec::new(), tts);
426 base::MacExpr::new(expanded)
429 pub fn expand_quote_item(cx: &mut ExtCtxt,
431 tts: &[ast::TokenTree])
432 -> Box<base::MacResult> {
433 let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
435 base::MacExpr::new(expanded)
438 pub fn expand_quote_pat(cx: &mut ExtCtxt,
440 tts: &[ast::TokenTree])
441 -> Box<base::MacResult> {
442 let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
443 base::MacExpr::new(expanded)
446 pub fn expand_quote_arm(cx: &mut ExtCtxt,
448 tts: &[ast::TokenTree])
449 -> Box<base::MacResult> {
450 let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
451 base::MacExpr::new(expanded)
454 pub fn expand_quote_ty(cx: &mut ExtCtxt,
456 tts: &[ast::TokenTree])
457 -> Box<base::MacResult> {
458 let e_param_colons = cx.expr_lit(sp, ast::LitBool(false));
459 let expanded = expand_parse_call(cx, sp, "parse_ty",
460 vec!(e_param_colons), tts);
461 base::MacExpr::new(expanded)
464 pub fn expand_quote_method(cx: &mut ExtCtxt,
466 tts: &[ast::TokenTree])
467 -> Box<base::MacResult> {
468 let e_param_colons = cx.expr_none(sp);
469 let expanded = expand_parse_call(cx, sp, "parse_method",
470 vec!(e_param_colons), tts);
471 base::MacExpr::new(expanded)
474 pub fn expand_quote_stmt(cx: &mut ExtCtxt,
476 tts: &[ast::TokenTree])
477 -> Box<base::MacResult> {
478 let e_attrs = cx.expr_vec_ng(sp);
479 let expanded = expand_parse_call(cx, sp, "parse_stmt",
481 base::MacExpr::new(expanded)
484 fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
485 strs.iter().map(|str| str_to_ident((*str).as_slice())).collect()
488 fn id_ext(str: &str) -> ast::Ident {
492 // Lift an ident to the expr that evaluates to that ident.
493 fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
494 let e_str = cx.expr_str(sp, token::get_ident(ident));
495 cx.expr_method_call(sp,
496 cx.expr_ident(sp, id_ext("ext_cx")),
501 // Lift a name to the expr that evaluates to that name
502 fn mk_name(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> Gc<ast::Expr> {
503 let e_str = cx.expr_str(sp, token::get_ident(ident));
504 cx.expr_method_call(sp,
505 cx.expr_ident(sp, id_ext("ext_cx")),
510 fn mk_ast_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> {
511 let idents = vec!(id_ext("syntax"), id_ext("ast"), id_ext(name));
512 cx.expr_path(cx.path_global(sp, idents))
515 fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> Gc<ast::Expr> {
516 let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("token"), id_ext(name));
517 cx.expr_path(cx.path_global(sp, idents))
520 fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::BinOp) -> Gc<ast::Expr> {
521 let name = match bop {
526 PERCENT => "PERCENT",
533 mk_token_path(cx, sp, name)
536 fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> Gc<ast::Expr> {
540 return cx.expr_call(sp, mk_token_path(cx, sp, "BINOP"), vec!(mk_binop(cx, sp, binop)));
543 return cx.expr_call(sp, mk_token_path(cx, sp, "BINOPEQ"),
544 vec!(mk_binop(cx, sp, binop)));
548 let e_byte = mk_name(cx, sp, i.ident());
550 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_BYTE"), vec!(e_byte));
554 let e_char = mk_name(cx, sp, i.ident());
556 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_CHAR"), vec!(e_char));
560 let e_int = mk_name(cx, sp, i.ident());
561 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_INTEGER"), vec!(e_int));
564 LIT_FLOAT(fident) => {
565 let e_fident = mk_name(cx, sp, fident.ident());
566 return cx.expr_call(sp, mk_token_path(cx, sp, "LIT_FLOAT"), vec!(e_fident));
570 return cx.expr_call(sp,
571 mk_token_path(cx, sp, "LIT_STR"),
572 vec!(mk_name(cx, sp, ident.ident())));
575 LIT_STR_RAW(ident, n) => {
576 return cx.expr_call(sp,
577 mk_token_path(cx, sp, "LIT_STR_RAW"),
578 vec!(mk_name(cx, sp, ident.ident()), cx.expr_uint(sp, n)));
582 return cx.expr_call(sp,
583 mk_token_path(cx, sp, "IDENT"),
584 vec!(mk_ident(cx, sp, ident), cx.expr_bool(sp, b)));
588 return cx.expr_call(sp,
589 mk_token_path(cx, sp, "LIFETIME"),
590 vec!(mk_ident(cx, sp, ident)));
593 DOC_COMMENT(ident) => {
594 return cx.expr_call(sp,
595 mk_token_path(cx, sp, "DOC_COMMENT"),
596 vec!(mk_name(cx, sp, ident.ident())));
599 INTERPOLATED(_) => fail!("quote! with interpolated token"),
604 let name = match *tok {
622 MOD_SEP => "MOD_SEP",
625 FAT_ARROW => "FAT_ARROW",
628 LBRACKET => "LBRACKET",
629 RBRACKET => "RBRACKET",
634 UNDERSCORE => "UNDERSCORE",
638 mk_token_path(cx, sp, name)
642 fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::TokenTree) -> Vec<Gc<ast::Stmt>> {
644 ast::TTTok(sp, ref tok) => {
645 let e_sp = cx.expr_ident(sp, id_ext("_sp"));
646 let e_tok = cx.expr_call(sp,
647 mk_ast_path(cx, sp, "TTTok"),
648 vec!(e_sp, mk_token(cx, sp, tok)));
650 cx.expr_method_call(sp,
651 cx.expr_ident(sp, id_ext("tt")),
654 vec!(cx.stmt_expr(e_push))
657 ast::TTDelim(ref tts) => mk_tts(cx, sp, tts.as_slice()),
658 ast::TTSeq(..) => fail!("TTSeq in quote!"),
660 ast::TTNonterminal(sp, ident) => {
662 // tt.push_all_move($ident.to_tokens(ext_cx))
665 cx.expr_method_call(sp,
666 cx.expr_ident(sp, ident),
668 vec!(cx.expr_ident(sp, id_ext("ext_cx"))));
671 cx.expr_method_call(sp,
672 cx.expr_ident(sp, id_ext("tt")),
673 id_ext("push_all_move"),
676 vec!(cx.stmt_expr(e_push))
681 fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
682 -> Vec<Gc<ast::Stmt>> {
683 let mut ss = Vec::new();
684 for tt in tts.iter() {
685 ss.push_all_move(mk_tt(cx, sp, tt));
690 fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
691 -> (Gc<ast::Expr>, Gc<ast::Expr>) {
692 // NB: It appears that the main parser loses its mind if we consider
693 // $foo as a TTNonterminal during the main parse, so we have to re-parse
694 // under quote_depth > 0. This is silly and should go away; the _guess_ is
695 // it has to do with transition away from supporting old-style macros, so
696 // try removing it when enough of them are gone.
698 let mut p = cx.new_parser_from_tts(tts);
701 let cx_expr = p.parse_expr();
702 if !p.eat(&token::COMMA) {
703 p.fatal("expected token `,`");
706 let tts = p.parse_all_token_trees();
709 // We also bind a single value, sp, to ext_cx.call_site()
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_).
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
735 let e_sp = cx.expr_method_call(sp,
736 cx.expr_ident(sp, id_ext("ext_cx")),
740 let stmt_let_sp = cx.stmt_let(sp, false,
744 let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
746 let mut vector = vec!(stmt_let_sp, stmt_let_tt);
747 vector.push_all_move(mk_tts(cx, sp, tts.as_slice()));
748 let block = cx.expr_block(
752 Some(cx.expr_ident(sp, id_ext("tt")))));
757 fn expand_wrapper(cx: &ExtCtxt,
759 cx_expr: Gc<ast::Expr>,
760 expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
762 &["syntax", "ext", "quote", "rt"],
763 ].iter().map(|path| {
764 let path = path.iter().map(|s| s.to_string()).collect();
765 cx.view_use_glob(sp, ast::Inherited, ids_ext(path))
768 let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr);
770 cx.expr_block(cx.block_all(sp, uses, vec!(stmt_let_ext_cx), Some(expr)))
773 fn expand_parse_call(cx: &ExtCtxt,
776 arg_exprs: Vec<Gc<ast::Expr>>,
777 tts: &[ast::TokenTree]) -> Gc<ast::Expr> {
778 let (cx_expr, tts_expr) = expand_tts(cx, sp, tts);
780 let cfg_call = || cx.expr_method_call(
781 sp, cx.expr_ident(sp, id_ext("ext_cx")),
782 id_ext("cfg"), Vec::new());
784 let parse_sess_call = || cx.expr_method_call(
785 sp, cx.expr_ident(sp, id_ext("ext_cx")),
786 id_ext("parse_sess"), Vec::new());
788 let new_parser_call =
790 cx.expr_ident(sp, id_ext("new_parser_from_tts")),
791 vec!(parse_sess_call(), cfg_call(), tts_expr));
793 let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
796 expand_wrapper(cx, sp, cx_expr, expr)