-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
165 lines (148 loc) · 5.03 KB
/
build.rs
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
use proc_macro2::TokenStream;
use quote::{format_ident, quote, TokenStreamExt};
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use syn_codegen::{Data, Definitions, Node, Type};
// Manual blacklist; see https://github.com/dtolnay/syn/issues/607#issuecomment-475905135.
fn has_spanned(ty: &str) -> bool {
!matches!(
ty,
"DataStruct" | "DataEnum" | "DataUnion" | "QSelf" | "LocalInit"
)
}
fn definition_tokens(definitions: &Definitions, tokens: &mut TokenStream) {
for node in &definitions.types {
node_tokens(node, tokens);
}
for syn_token in definitions.tokens.keys() {
let ident = format_ident!("{}", syn_token);
tokens.append_all(quote! {
impl ToJS for syn::token::#ident {
fn to_js(&self) -> JsValue {
js!(#ident {
span: self.span()
})
}
}
});
}
}
fn node_tokens(node: &Node, tokens: &mut TokenStream) {
let ident = format_ident!("{}", node.ident);
let data = match &node.data {
Data::Private => match node.ident.as_str() {
"LitStr" | "LitByteStr" | "LitByte" | "LitChar" => {
quote! {
js!(#ident {
value: self.value(),
suffix: self.suffix(),
span: self.span()
})
}
}
"LitInt" | "LitFloat" => {
quote! {
js!(#ident {
digits: self.base10_digits(),
suffix: self.suffix(),
span: self.span()
})
}
}
_ => unreachable!(),
},
Data::Struct(fields) => {
let mut fields = fields.iter().collect::<Vec<_>>();
fields.sort_by_key(|(_field, ty)| match ty {
// Move groups down or they will be the target of any locations.
Type::Group(_) | Type::Punctuated(_) => 2,
// Tokens are the smallest, so make sure they're matched when searching for locations first.
Type::Std(_) | Type::Token(_) => 0,
// All the rest is somewhere in the middle.
_ => 1,
});
let fields = fields
.into_iter()
.map(|(field, _ty)| {
let field = format_ident!("{}", field);
quote! {
#field: self.#field
}
})
.chain(if has_spanned(&node.ident) {
Some(quote! {
span: self.span()
})
} else {
None
});
quote! {
js!(#ident {
#(#fields,)*
})
}
}
Data::Enum(variants) => {
let matches = variants.iter().map(|(variant, types)| {
let variant = format_ident!("{}", variant);
let variant = quote! {
#ident::#variant
};
let variant_path = quote! {
syn::#variant
};
match types.len() {
0 => quote! {
#variant_path => js!(#variant {})
},
1 => quote! {
#variant_path(x) => x.to_js()
},
len => {
let payload = (0..len).map(|i| format_ident!("x{}", i));
let payload = quote! { #(#payload),* };
quote! {
#variant_path(#payload) => js!(#variant { span: self.span() } [#payload])
}
}
}
});
let wildcard = if node.exhaustive {
None
} else {
Some(quote!(unreachable!()))
};
if matches.len() == 0 {
quote!(#wildcard)
} else {
let wildcard = wildcard.map(|w| quote!(_ => #w));
quote! {
match self {
#(#matches,)*
#wildcard
}
}
}
}
};
tokens.append_all(quote! {
impl ToJS for syn::#ident {
fn to_js(&self) -> JsValue {
#data
}
}
});
}
fn main() {
let definitions: Definitions = serde_json::from_str(include_str!("syn/syn.json")).unwrap();
let mut generated = TokenStream::new();
definition_tokens(&definitions, &mut generated);
let path = &Path::new(&env::var_os("OUT_DIR").unwrap()).join("to_js.rs");
{
let mut out = File::create(path).unwrap();
writeln!(out, "{}", generated).unwrap();
}
let _ = std::process::Command::new("rustfmt").arg(path).status();
}