1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![cfg_attr(not(feature="with-syntex"), feature(quote, rustc_private))]

#[cfg(feature="with-syntex")] extern crate quasi;
#[cfg(feature="with-syntex")] extern crate syntex_syntax as syntax;
#[cfg(feature="with-syntex")] include!(concat!(env!("OUT_DIR"), "/lib.rs"));

#[cfg(not(feature="with-syntex"))] extern crate syntax;
#[cfg(not(feature="with-syntex"))] include!("lib.in.rs");

#[cfg(not(feature = "with-syntex"))]
extern crate rustc_plugin;

#[cfg(feature = "with-syntex")]
extern crate syntex;

#[cfg(feature = "with-syntex")]
fn syntex_registry() -> syntex::Registry {
    let mut reg = syntex::Registry::new();

    reg.add_attr("feature(custom_derive)");
    reg.add_attr("feature(custom_attribute)");

    reg.add_macro("mock", generate_mock);
    reg.add_decorator("derive_Mock", derive_mock);

    reg
}

#[cfg(not(feature = "with-syntex"))]
pub fn register(reg: &mut rustc_plugin::Registry) {
    use syntax::symbol::Symbol;
    use syntax::ext::base::MultiDecorator;
    use syntax::feature_gate::AttributeType;

    reg.register_macro("mock", generate_mock);
    reg.register_syntax_extension(Symbol::intern("derive_Mock"),
                                  MultiDecorator(Box::new(derive_mock)));
    reg.register_attribute("derive_Mock".to_owned(), AttributeType::Whitelisted);
}

#[cfg(feature = "with-syntex")]
pub fn expand_str(src: &str) -> Result<String, syntex::Error> {
    let src = src.to_owned();

    let expand_thread = move || {
        syntex_registry().expand_str("", "", &src)
    };

    syntex::with_extra_stack(expand_thread)
}

#[cfg(feature = "with-syntex")]
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
    where S: AsRef<std::path::Path>,
          D: AsRef<std::path::Path>,
{
    let src = src.as_ref().to_owned();
    let dst = dst.as_ref().to_owned();

    let expand_thread = move || {
        syntex_registry().expand("", src, dst)
    };

    syntex::with_extra_stack(expand_thread)
}