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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![deny(missing_debug_implementations)]
#![feature(quote, concat_idents, plugin_registrar, rustc_private)]
#![feature(custom_attribute)]
extern crate rustc_plugin;
extern crate syntax;
use rustc_plugin::Registry;
use syntax::ext::base::{SyntaxExtension, MultiItemDecorator, ExtCtxt};
use syntax::ext::quote::rt::Span;
use syntax::ast::{self};
use syntax::ext::base::Annotatable;
use syntax::symbol::Symbol;
mod function;
mod ident_ext;
use function::Function;
use ident_ext::IdentExt;
struct Theory;
impl MultiItemDecorator for Theory {
fn expand(
&self,
ecx: &mut ExtCtxt,
sp: Span,
_: &ast::MetaItem,
item: &Annotatable,
push: &mut FnMut(Annotatable)
) {
let function = Function::from(item).unwrap_or_else(|item_sp| {
ecx.span_err(sp, "this attribute can only be used on functions...");
ecx.span_fatal(item_sp, "...but was applied to the item above.");
});
let original_fn_name = function.ident();
for (data_fn_name, tokens) in function.attrs()
.into_iter()
.filter(|s| s.check_name("data"))
.map(|v| v.clone().tokens.as_tree())
.filter(|&(_, k)| !k)
.map(|(s, _)| s)
.enumerate()
.map(|(i, a)| (original_fn_name.append(format!("_{}", i)),a)) {
push(Annotatable::Item(quote_item!(ecx,
#[test]
fn $data_fn_name() {
$original_fn_name$tokens;
}
).expect("function")));
}
}
}
struct Data;
impl MultiItemDecorator for Data {
fn expand(
&self,
ecx: &mut ExtCtxt,
sp: Span,
_: &ast::MetaItem,
item: &Annotatable,
_: &mut FnMut(Annotatable)
) {
Function::from(item).unwrap_or_else(|item_sp| {
ecx.span_err(sp, "this attribute can only be used on functions...");
ecx.span_fatal(item_sp, "...but was applied to the item above.");
});
}
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
let theory = Theory;
reg.register_syntax_extension(Symbol::intern("theory"), SyntaxExtension::MultiDecorator(Box::new(theory)));
let data = Data;
reg.register_syntax_extension(Symbol::intern("data"), SyntaxExtension::MultiDecorator(Box::new(data)));
}