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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! A utility macro to prefix each of several statements with the given tokens.
//!
//! Doing this with items is straightforward: the `item`
//! [fragment specifier](https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.meta.specifier)
//! in [`macro_rules!`](https://doc.rust-lang.org/reference/macros-by-example.html) works as expected.
//! With statements however, things get tricky. Since the `stmt`
//! [fragment specifier](https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.meta.specifier)
//! also matches lone semicolons (which are no-op statements), and doesn't match trailing
//! semicolons, the solution isn't trivial.
//!
//! After getting around the limitation, you'll be surprised to find the Rust compiler gets really
//! confused with cryptic errors despite everything seeming right. After much trial and error, I
//! found a procedural macro with special logic for
//! [`Delimiter::None`](proc_macro::Delimiter::None) seems to work right.
//!
//! This macro is implemented as [`prefix_stmts!`]. It's a wrapper around the actual implementation,
//! which first separates input statements through the `stmt`
//! [fragment specifier](https://doc.rust-lang.org/reference/macros-by-example.html#r-macro.decl.meta.specifier),
//! and then forwards them to a procedural macro
//! ([`emit_prefixed_tokens_except_starts_with_semicolon`](prefix_stmts_proc_macro::emit_prefixed_tokens_except_starts_with_semicolon)),
//! which does exactly what the name says (with the [`Delimiter::None`](proc_macro::Delimiter::None)
//! trick).
//!
//! If you find a way to do this without a procedural macro, please do let me know!
//!
//! ```rust
//! use prefix_stmts::prefix_stmts;
//!
//! fn void() {}
//!
//! let mut count: usize = 0;
//!
//! prefix_stmts! {
//! [ count += 1; ]
//!
//! ;;;;;;;;;;;;;;;;; // Lone semicolons (noop, don't prefix)
//!
//! pub(crate) fn foo() {}
//!
//! let _: usize = 42;
//!
//! void();
//!
//! { foo();; void() };;;
//!
//! { ( { void() } );; foo() }
//! }
//!
//! assert_eq!(count, 5);
//! ```
//!
//! My whole motivation for making this was allowing several statements to be prefixed with
//! [`cfg`](https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index)
//! attributes without repeating the attribute for each statement.
//!
//! ```rust
//! use prefix_stmts::prefix_stmts;
//!
//! prefix_stmts! {
//! [ #[cfg( all() )] ]
//!
//! const FOO: bool = true;
//! }
//!
//! prefix_stmts! {
//! [ #[cfg( not( all() ) )] ]
//!
//! const FOO: bool = false;
//! }
//!
//! assert_eq!(FOO, true);
//! ```
//!
//! The Minimum Supported Rust Version (MSRV) for this crate is
//! [Rust 1.45](https://blog.rust-lang.org/2020/07/16/Rust-1.45.0/), which added support for
//! [statements emitted by procedural macros](https://blog.rust-lang.org/2020/07/16/Rust-1.45.0/#stabilizing-function-like-procedural-macros-in-expressions-patterns-and-statements).
//! Do mind that Rust <= [1.47](https://blog.rust-lang.org/2020/10/08/Rust-1.47/) has issues with
//! hygiene from the procedural macro, so using at least
//! [Rust 1.48](https://blog.rust-lang.org/2020/11/19/Rust-1.48/) is recommended.
extern crate proc_macro;
/// Prefixes each statement with the given prefix tokens. First provide the tokens to prefix wrapped
/// in `[ brackets ]`, then follow with the statements.
///
/// ```rust
/// use prefix_stmts::prefix_stmts;
///
/// prefix_stmts! {
/// [ #[cfg( all() )] ]
///
/// const FOO: bool = true;
/// }
///
/// prefix_stmts! {
/// [ #[cfg( not( all() ) )] ]
///
/// const FOO: bool = false;
/// }
///
/// assert_eq!(FOO, true);
/// ```
///
/// See the [crate documentation](crate) for more details.