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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![feature(proc_macro_diagnostic, proc_macro_span, proc_macro_def_site)]
#![recursion_limit = "512"]
extern crate core;
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
extern crate syn;
use class::{parse_class, ClassAttr};
use class_impl::{parse_impl, ImplAttr};
use proc_macro::Diagnostic;
use proc_macro2::{Span, TokenStream};
use protocol::{parse_protocol, ProtocolAttr};
use quote::quote;
use syn::parse2;
use syn::parse::{ParseStream, Parse};
mod class;
mod class_impl;
mod gensym;
mod ivar;
mod property;
mod protocol;
mod selector;
mod test;
mod util;
enum ObjrsAttr {
Impl(ImplAttr),
Class(ClassAttr),
Protocol(ProtocolAttr),
Selector(Span),
Ivar(Span),
}
impl Parse for ObjrsAttr {
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
use syn::token::Impl;
use util::{ivar, selector, class, protocol};
let lookahead = input.lookahead1();
if lookahead.peek(Impl) {
return input.parse().map(ObjrsAttr::Impl);
} else if lookahead.peek(class) {
return input.parse().map(ObjrsAttr::Class);
} else if lookahead.peek(protocol) {
return input.parse().map(ObjrsAttr::Protocol);
} else if lookahead.peek(selector) {
return Ok(ObjrsAttr::Selector(input.cursor().span()));
} else if lookahead.peek(ivar) {
return Ok(ObjrsAttr::Ivar(input.cursor().span()));
}
return Err(lookahead.error());
}
}
fn to_diagnostic(err: syn::parse::Error) -> Diagnostic {
return err.span().unstable().error(err.to_string());
}
fn shim(args: TokenStream, input: TokenStream) -> Result<TokenStream, Diagnostic> {
match parse2(args).map_err(to_diagnostic)? {
ObjrsAttr::Impl(attr) => return parse_impl(attr, input),
ObjrsAttr::Class(attr) => return parse_class(attr, input),
ObjrsAttr::Protocol(attr) => return parse_protocol(attr, input),
ObjrsAttr::Selector(span) => {
return Err(
span
.unstable()
.error("attribute must be enclosed in an impl block with a #[objrs(impl)] attribute"),
)
}
ObjrsAttr::Ivar(span) => {
return Err(
span
.unstable()
.error("attribute must be enclosed in a struct item with a #[objrs(class)] attribute"),
)
}
}
}
#[proc_macro_attribute]
pub fn objrs(
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
match shim(args.into(), input.into()) {
Ok(stream) => return stream.into(),
Err(diagnostic) => {
diagnostic.emit();
return proc_macro::TokenStream::new();
}
}
}
#[proc_macro]
pub fn selector(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
match selector::parse_selector_ref(input.into()) {
Ok(stream) => return stream.into(),
Err(diagnostic) => {
diagnostic.emit();
return quote!{{
extern crate objrs as __objrs_root;
__objrs_root::runtime::SEL(0 as *const _)
}}.into();
}
}
}