Skip to content

Instantly share code, notes, and snippets.

@knight42
Last active August 23, 2016 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knight42/7a760eee7979092635825d27e321a84a to your computer and use it in GitHub Desktop.
Save knight42/7a760eee7979092635825d27e321a84a to your computer and use it in GitHub Desktop.
doesn't work :/
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