Last active
August 23, 2016 07:05
-
-
Save knight42/7a760eee7979092635825d27e321a84a to your computer and use it in GitHub Desktop.
doesn't work :/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use syntax::ast; | |
use syntax::ptr::P; | |
use syntax::codemap; | |
use syntax::parse::token; | |
use syntax::tokenstream::TokenTree; | |
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager}; | |
use syntax::ext::build::AstBuilder; | |
use syntax_pos::Span; | |
use rustc_plugin::Registry; | |
use syntax::util::small_vector::SmallVector; | |
// Ideally, it will expand | |
// | |
// ```rust | |
// choose! { | |
// test_a | |
// test_b | |
// } | |
// ``` | |
// to | |
// ```rust | |
// #[cfg(feature = "a")] | |
// mod test_a; | |
// #[cfg(feature = "b")] | |
// mod test_b; | |
// ``` | |
// | |
// but the modules contain nothing in the expanded code at present | |
fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> { | |
let mut test_mods: SmallVector<P<ast::Item>> = SmallVector::many(vec![]); | |
for arg in args { | |
let mut attrs = vec![]; | |
let text = match arg { | |
&TokenTree::Token(_, token::Ident(s)) => s.to_string(), | |
_ => { | |
return DummyResult::any(sp); | |
} | |
}; | |
let cfg_str = token::InternedString::new("cfg"); | |
let feat_str = token::InternedString::new("feature"); | |
attrs.push(cx.attribute(sp, | |
cx.meta_list(sp, | |
cfg_str, | |
vec![cx.meta_name_value(sp, | |
feat_str, | |
ast::LitKind::Str(token::intern_and_get_ident(text.trim_left_matches("test_")), ast::StrStyle::Cooked))]))); | |
test_mods.push(P(ast::Item { | |
ident: cx.ident_of(text.as_str()), | |
attrs: attrs, | |
id: ast::DUMMY_NODE_ID, | |
node: ast::ItemKind::Mod( | |
// === How to include the specified module file here? === | |
ast::Mod { | |
inner: codemap::DUMMY_SP, | |
items: vec![], | |
} | |
), | |
vis: ast::Visibility::Inherited, | |
span: sp, | |
})) | |
} | |
MacEager::items(test_mods) | |
} | |
#[plugin_registrar] | |
pub fn plugin_registrar(reg: &mut Registry) { | |
reg.register_macro("choose", choose); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment