• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![feature(plugin_registrar, quote, rustc_private)]
2 
3 extern crate syntax;
4 extern crate rustc;
5 extern crate rustc_plugin;
6 
7 use syntax::codemap::Span;
8 use syntax::parse::token;
9 use syntax::ast::{TokenTree, Ident};
10 use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager, IdentTT, get_single_str_from_tts};
11 use syntax::util::small_vector::SmallVector;
12 use rustc_plugin::Registry;
13 
expand_mod_path<'a>(cx: &'a mut ExtCtxt, sp: Span, ident: Ident, tts: Vec<TokenTree>) -> Box<MacResult + 'a>14 fn expand_mod_path<'a>(cx: &'a mut ExtCtxt, sp: Span, ident: Ident, tts: Vec<TokenTree>)
15             -> Box<MacResult + 'a> {
16     let path = match get_single_str_from_tts(cx, sp, &*tts, "mod_path!") {
17         Some(string) => string,
18         None => return DummyResult::expr(sp),
19     };
20     let path = &*path;
21 
22     MacEager::items(SmallVector::one(quote_item!(cx,
23 
24         #[path = $path]
25         pub mod $ident;
26 
27     ).unwrap()))
28 }
29 
30 #[plugin_registrar]
plugin_registrar(reg: &mut Registry)31 pub fn plugin_registrar(reg: &mut Registry) {
32     reg.register_syntax_extension(token::intern("mod_path"), IdentTT(Box::new(expand_mod_path), None, false));
33 }
34