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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{Expr, Ident, Token, braced};
// ── Parsing ─────────────────────────────────────────────────────────────────
/// Top-level item inside `tmpl! { }` or after the template ident in `tmpl!(tmpl, ...)`
enum TopItem {
/// `key = value` — simple parameter
Param { key: Ident, value: Expr },
/// `name { arm, arm, ... }` — implementation block
ImplBlock { name: Ident, arms: Vec<Arm> },
}
/// An arm inside an implementation block
enum Arm {
/// `key = value` — single-param arm
Single { key: Ident, value: Expr },
/// `{ key = value, key = value, ... }` — multi-param arm
Multi(Vec<(Ident, Expr)>),
}
// ── Parse implementations ──────────────────────────────────────────────────
impl Parse for TopItem {
fn parse(input: ParseStream) -> syn::Result<Self> {
let ident: Ident = input.parse()?;
let ahead = input.lookahead1();
if ahead.peek(Token![=]) {
let _eq: Token![=] = input.parse()?;
let value: Expr = input.parse()?;
Ok(TopItem::Param { key: ident, value })
} else if ahead.peek(syn::token::Brace) {
let content;
braced!(content in input);
let arms: Vec<Arm> = content
.parse_terminated(Arm::parse, Token![,])?
.into_iter()
.collect();
Ok(TopItem::ImplBlock { name: ident, arms })
} else {
Err(ahead.error())
}
}
}
impl Parse for Arm {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(syn::token::Brace) {
let content;
braced!(content in input);
let params: Vec<(Ident, Expr)> = content
.parse_terminated(Self::parse_single, Token![,])?
.into_iter()
.collect();
Ok(Arm::Multi(params))
} else {
let (key, value) = Self::parse_single(input)?;
Ok(Arm::Single { key, value })
}
}
}
impl Arm {
fn parse_single(input: ParseStream) -> syn::Result<(Ident, Expr)> {
let key: Ident = input.parse()?;
let _eq: Token![=] = input.parse()?;
let value: Expr = input.parse()?;
Ok((key, value))
}
}
// ── Macro entry point ──────────────────────────────────────────────────────
fn split_input(input: ParseStream) -> syn::Result<(Ident, Vec<TopItem>)> {
// Try to parse: `ident , items` (explicit template variable)
// If the first token is an ident followed by `,`, it's explicit.
// Otherwise, default to `tmpl` and parse as items.
let first: Ident = input.parse()?;
if input.peek(Token![,]) {
let _comma: Token![,] = input.parse()?;
let items: Vec<TopItem> = input
.parse_terminated(TopItem::parse, Token![,])?
.into_iter()
.collect();
Ok((first, items))
} else {
// The first ident is actually the start of the first item.
// We need to re-parse. Use `syn::Result` to signal this.
// Tricky: we've already consumed `first`. Since syn::parse uses
// ParseStream which is a cursor, we actually can't rewind easily.
//
// Solution: use a custom approach — treat the first ident as
// the start of an item, and parse the rest as items.
let items = {
// Re-build: first ident + rest
let mut items: Vec<TopItem> = Vec::new();
// Parse the first item starting with `first`
let ahead = input.lookahead1();
if ahead.peek(Token![=]) {
let _eq: Token![=] = input.parse()?;
let value: Expr = input.parse()?;
items.push(TopItem::Param { key: first, value });
} else if ahead.peek(syn::token::Brace) {
let content;
braced!(content in input);
let arms: Vec<Arm> = content
.parse_terminated(Arm::parse, Token![,])?
.into_iter()
.collect();
items.push(TopItem::ImplBlock { name: first, arms });
} else {
return Err(ahead.error());
}
// Parse remaining items
while !input.is_empty() {
let _comma: Token![,] = input.parse()?;
if input.is_empty() {
break; // trailing comma
}
items.push(input.parse()?);
}
items
};
Ok((Ident::new("tmpl", proc_macro2::Span::call_site()), items))
}
}
#[proc_macro]
pub fn tmpl(input: TokenStream) -> TokenStream {
match syn::parse::Parser::parse(split_input, input) {
Ok((template_var, items)) => {
let stmts = generate(&template_var, &items);
let expanded = quote! { { #(#stmts)* } };
expanded.into()
}
Err(e) => e.to_compile_error().into(),
}
}
// ── Code generation ────────────────────────────────────────────────────────
fn generate(template_var: &Ident, items: &[TopItem]) -> Vec<proc_macro2::TokenStream> {
let mut stmts: Vec<proc_macro2::TokenStream> = Vec::new();
for item in items {
match item {
TopItem::Param { key, value } => {
let key_str = key.to_string();
stmts.push(quote! {
#template_var.insert_param(
#key_str.to_string(),
::std::string::ToString::to_string(&#value),
);
});
}
TopItem::ImplBlock { name, arms } => {
let name_str = name.to_string();
// Generate push statements for each arm
let push_stmts: Vec<proc_macro2::TokenStream> =
arms.iter().map(|arm| gen_arm_push(name, arm)).collect();
stmts.push(quote! {
let #name = #template_var.add_impl(#name_str.to_string());
#(#push_stmts)*
});
}
}
}
stmts
}
fn gen_arm_push(name: &Ident, arm: &Arm) -> proc_macro2::TokenStream {
match arm {
Arm::Single { key, value } => {
let key_str = key.to_string();
quote! {
#name.push(::std::collections::HashMap::from([
(#key_str.to_string(), ::std::string::ToString::to_string(&#value)),
]));
}
}
Arm::Multi(params) => {
let entries: Vec<_> = params
.iter()
.map(|(key, value)| {
let k = key.to_string();
quote! {
(#k.to_string(), ::std::string::ToString::to_string(&#value))
}
})
.collect();
quote! {
#name.push(::std::collections::HashMap::from([
#(#entries),*
]));
}
}
}
}
|