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
/*!
Macro execution order is tricky. For example, the output of the following code goes against our
intuition of how functions should work:
```
macro_rules! expand_to_larch {
() => { larch };
}
macro_rules! recognize_tree {
(larch) => { println!("#1, the Larch.") };
(redwood) => { println!("#2, the Mighty Redwood.") };
(fir) => { println!("#3, the Fir.") };
(chestnut) => { println!("#4, the Horse Chestnut.") };
(pine) => { println!("#5, the Scots Pine.") };
($($other:tt)*) => { println!("I don't know; some kind of birch maybe?") };
}
macro_rules! name_a_larch {
() => {
recognize_tree!(expand_to_larch!())
};
}
fn main() {
name_a_larch!(); // Prints "I don't know; some kind of birch maybe?"
}
```
[The Little Book of Rust Macros][tlborm] (where the above example comes from) outlines *callbacks* -
a macro pattern that allows macro execution order to be specified:
```
# macro_rules! recognize_tree {
# (larch) => { println!("#1, the Larch.") };
# (redwood) => { println!("#2, the Mighty Redwood.") };
# (fir) => { println!("#3, the Fir.") };
# (chestnut) => { println!("#4, the Horse Chestnut.") };
# (pine) => { println!("#5, the Scots Pine.") };
# ($($other:tt)*) => { println!("I don't know; some kind of birch maybe?") };
# }
macro_rules! call_with_larch {
($callback:ident) => { $callback!(larch) };
}
macro_rules! name_a_larch {
() => {
call_with_larch!(recognize_tree)
};
}
fn main() {
name_a_larch!(); // Correctly prints "#1, the Larch." but is pretty hard to read
}
```
This syntax, while powerful, soon becomes confusing.
This macro allows far more readable macros to be written:
```
# use cps::cps;
#[cps]
macro_rules! expand_to_larch {
() => { larch };
}
#[cps]
macro_rules! recognize_tree {
(larch) => { println!("#1, the Larch.") };
// ...
($($other:tt)*) => { println!("I don't know; some kind of birch maybe?") };
}
#[cps]
macro_rules! name_a_larch {
() =>
let $tree:tt = expand_to_larch!() in
{
recognize_tree!($tree)
};
}
fn main() {
name_a_larch!(); // Prints "#1, the Larch."
}
```
Macros-by-example are hard, difficult to maintain, and you should always consider writing a proc-macro instead.
This library aims to make the macros that you *do* write more maintainable. Please recurse responsibly.
## Usage Notes
CPS converts iteration into recursion. Therefore when using this library you may reach the recursion limit (128 at the time of writing). You can raise this using `#![recursion_limit = "1024"]` but your build times may suffer.
Any macro `let` expression must have a macro on the right-hand side that was marked as `#[cps]`. The following example will not work:
```
# use cps::cps;
#[cps]
macro_rules! foo {
() => { BaseCase };
(bar) =>
let $x:tt = foo!() in
let $y:tt = stringify!($x) in // Issue: stringify is not a cps macro
{
$y
};
}
```
Instead, use the `cps` variants of builtin macros:
```
# use cps::cps;
#[cps]
macro_rules! foo {
() => { BaseCase };
(bar) =>
let $x:tt = foo!() in
let $y:tt = cps::stringify!($x) in // cps::stringify is a cps version of `stringify`
{
$y
};
}
```
*/
use TokenStream;
use ;
/// Manipulates a macro_rules! definition to add extended syntax to help in creating readable macros.
///
/// # Usage
///
/// CPS macros are a strict superset of Rust macro-rules. This means that any (\*) regular rust macro can
/// be prefaced by `#[cps]` and behave exactly the same.
///
/// The added syntax is the new `let` bindings allowed before the body of macro rules. These let statements
/// allow other `cps` macros to be evaluated *before* the body is evaluated. Let bindings are executed in order,
/// and can refer to the results of previous let binding results.
///
/// \* You may not begin a rule with the tokens `@_cps`
///
/// ## Evaluation Order
///
/// ```
/// # use cps::cps;
/// #[cps]
/// macro_rules! macro1 {
/// (a) => {
/// CaseA
/// };
/// (b) => {
/// CaseB
/// };
/// (CaseA) => {
/// MatchedCaseA
/// };
/// }
///
/// #[cps]
/// macro_rules! macro2 {
/// (a_b) =>
/// let $x:tt = macro1!(a) in
/// let $x2:tt = cps::stringify!($x) in
/// let $y:tt = macro1!(b) in
/// let $y2:tt = cps::stringify!($y) in
/// {
/// concat!($x2, $y2)
/// };
///
/// (sequential) =>
/// let $x:tt = macro1!(a) in
/// let $y:tt = macro1!($x) in
/// {
/// stringify!($y)
/// };
/// }
///
/// fn main() {
/// assert_eq!(macro2!(a_b), "CaseACaseB");
/// assert_eq!(macro2!(sequential), "MatchedCaseA");
/// }
/// ```
///
/// ## Macro indirection
///
/// The result of a previous let binding can be used as the name of a later let binding:
///
/// ```
/// # use cps::cps;
/// #[cps]
/// macro_rules! input_macro1 {
/// (next) => {
/// input_macro2
/// };
/// }
///
/// #[cps]
/// macro_rules! input_macro2 {
/// () => {
/// BaseCase2
/// };
/// }
///
/// #[cps]
/// macro_rules! macro1 {
/// ($cont1:ident) =>
/// let $cont2:ident = $cont1!(next) in
/// let $x:tt = $cont2!() in // Invoke the result of `$cont1!(next)`
/// {
/// stringify!($x)
/// };
/// }
///
/// fn main() {
/// assert_eq!(macro1!(input_macro1), "BaseCase2");
/// }
/// ```
///
/// [tlborm]: https://veykril.github.io/tlborm/decl-macros/patterns/callbacks.html
export_std_cps!;
export_std_cps!;
export_std_cps!;