]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | // force-host | |
12 | ||
13 | #![feature(plugin_registrar)] | |
c34b1796 | 14 | #![feature(box_syntax, rustc_private)] |
1a4d82fc JJ |
15 | |
16 | extern crate syntax; | |
3157f602 | 17 | extern crate syntax_pos; |
1a4d82fc | 18 | extern crate rustc; |
92a42be0 | 19 | extern crate rustc_plugin; |
1a4d82fc JJ |
20 | |
21 | use std::borrow::ToOwned; | |
22 | use syntax::ast; | |
1a4d82fc | 23 | use syntax::ext::build::AstBuilder; |
c34b1796 | 24 | use syntax::ext::base::{TTMacroExpander, ExtCtxt, MacResult, MacEager, NormalTT}; |
1a4d82fc JJ |
25 | use syntax::parse::token; |
26 | use syntax::print::pprust; | |
27 | use syntax::ptr::P; | |
3157f602 XL |
28 | use syntax_pos::Span; |
29 | use syntax::tokenstream; | |
92a42be0 | 30 | use rustc_plugin::Registry; |
1a4d82fc JJ |
31 | |
32 | struct Expander { | |
9e0c209e | 33 | args: Vec<ast::NestedMetaItem>, |
1a4d82fc JJ |
34 | } |
35 | ||
36 | impl TTMacroExpander for Expander { | |
37 | fn expand<'cx>(&self, | |
38 | ecx: &'cx mut ExtCtxt, | |
39 | sp: Span, | |
3157f602 | 40 | _: &[tokenstream::TokenTree]) -> Box<MacResult+'cx> { |
9e0c209e | 41 | let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i)) |
c1a9b12d | 42 | .collect::<Vec<_>>().join(", "); |
85aaf69f | 43 | let interned = token::intern_and_get_ident(&args[..]); |
c34b1796 | 44 | MacEager::expr(ecx.expr_str(sp, interned)) |
1a4d82fc JJ |
45 | } |
46 | } | |
47 | ||
48 | #[plugin_registrar] | |
49 | pub fn plugin_registrar(reg: &mut Registry) { | |
a7813a04 | 50 | let args = reg.args().to_owned(); |
1a4d82fc | 51 | reg.register_syntax_extension(token::intern("plugin_args"), |
c34b1796 AL |
52 | // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. |
53 | NormalTT(Box::new(Expander { args: args, }), None, false)); | |
1a4d82fc | 54 | } |