repetitive 0.2.0

Macro for generating repetitive code
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Ident, Token, parse::ParseStream};

use super::*;

/// Contains both the dot and the method ident.
#[derive(Debug, Clone, Copy)]
pub enum Method {
    // Unary Operators
    Neg(Span),
    Not(Span),

    // Binary Operators
    Add(Span),
    Sub(Span),
    Mul(Span),
    Div(Span),
    Rem(Span),

    BitAnd(Span),
    BitOr(Span),
    BitXor(Span),
    Shl(Span),
    Shr(Span),

    // Comparison Operators
    Eq(Span),
    Ne(Span),
    Lt(Span),
    Gt(Span),
    Le(Span),
    Ge(Span),

    Min(Span),
    Max(Span),
    Clamp(Span),

    // List
    Len(Span),
    Index(Span),
    Enumerate(Span),
    Zip(Span),
    Chain(Span),
    Contains(Span),
    ConcatIdent(Span),
    ConcatString(Span),
}

impl ContextParse for Method {
    fn ctx_parse(input: ParseStream, ctx: &mut Context) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let dot = <Token![.]>::ctx_parse(input, ctx)?;

        let ident = match Ident::ctx_parse(input, ctx) {
            Ok(ident) => {
                ctx.push_method_call(dot, Some(ident.clone()));

                ident
            }
            Err(err) => {
                ctx.push_method_call(dot, None);

                return Err(err);
            }
        };

        Ok(match ident.to_string().as_str() {
            // Unary Operators
            "neg" => Self::Neg(ident.span()),
            "not" => Self::Not(ident.span()),

            // Binary Operators
            "add" => Self::Add(ident.span()),
            "sub" => Self::Sub(ident.span()),
            "mul" => Self::Mul(ident.span()),
            "div" => Self::Div(ident.span()),
            "rem" => Self::Rem(ident.span()),

            "bitand" => Self::BitAnd(ident.span()),
            "bitor" => Self::BitOr(ident.span()),
            "bitxor" => Self::BitXor(ident.span()),
            "shl" => Self::Shl(ident.span()),
            "shr" => Self::Shr(ident.span()),

            // Comparison Operators
            "eq" => Self::Eq(ident.span()),
            "ne" => Self::Ne(ident.span()),
            "lt" => Self::Lt(ident.span()),
            "gt" => Self::Gt(ident.span()),
            "le" => Self::Le(ident.span()),
            "ge" => Self::Ge(ident.span()),

            "min" => Self::Min(ident.span()),
            "max" => Self::Max(ident.span()),
            "clamp" => Self::Clamp(ident.span()),

            // List
            "len" => Self::Len(ident.span()),
            "index" => Self::Index(ident.span()),
            "enumerate" => Self::Enumerate(ident.span()),
            "zip" => Self::Zip(ident.span()),
            "chain" => Self::Chain(ident.span()),
            "contains" => Self::Contains(ident.span()),
            "concat_ident" => Self::ConcatIdent(ident.span()),
            "concat_string" => Self::ConcatString(ident.span()),

            _ => {
                return Err(Error::ParseError(syn::Error::new(
                    ident.span(),
                    "unknown method",
                )));
            }
        })
    }
}

#[cfg(feature = "doc")]
pub fn paste_method_doc(idents: &[(Token![.], Option<Ident>)]) -> TokenStream {
    let ident_calls = idents.iter().map(|(dot, ident)| {
        match ident
            .as_ref()
            .map(|ident| ident.to_string())
            .as_ref()
            .map(|ident| ident.as_str())
        {
            // Unary Operators
            Some("neg") => quote! { val #dot #ident() },
            Some("not") => quote! { val #dot #ident() },

            // Binary Operators
            Some("add") => quote! { val #dot #ident(other) },
            Some("sub") => quote! { val #dot #ident(other) },
            Some("mul") => quote! { val #dot #ident(other) },
            Some("div") => quote! { val #dot #ident(other) },
            Some("rem") => quote! { val #dot #ident(other) },

            Some("bitand") => quote! { val #dot #ident(other) },
            Some("bitor") => quote! { val #dot #ident(other) },
            Some("bitxor") => quote! { val #dot #ident(other) },
            Some("shl") => quote! { val #dot #ident(other) },
            Some("shr") => quote! { val #dot #ident(other) },

            // Comparison Operators
            Some("eq") => quote! { val #dot #ident(other) },
            Some("ne") => quote! { val #dot #ident(other) },
            Some("lt") => quote! { val #dot #ident(other) },
            Some("gt") => quote! { val #dot #ident(other) },
            Some("le") => quote! { val #dot #ident(other) },
            Some("ge") => quote! { val #dot #ident(other) },

            Some("min") => quote! { val #dot #ident(other) },
            Some("max") => quote! { val #dot #ident(other) },
            Some("clamp") => quote! { val #dot #ident(min, max) },

            // List
            Some("len") => quote! { val #dot #ident() },
            Some("index") => quote! { val #dot #ident(index) },
            Some("enumerate") => quote! { val #dot #ident() },
            Some("zip") => quote! { val #dot #ident(other) },
            Some("chain") => quote! { val #dot #ident(other) },
            Some("contains") => quote! { val #dot #ident(value) },
            Some("concat_ident") => quote! { val #dot #ident() },
            Some("concat_string") => quote! { val #dot #ident() },

            Some(_) => quote! { val #dot #ident },
            None => quote! { val #dot },
        }
    });

    let neg_doc = fn_doc("neg", NEG_DOC, &idents);
    let not_doc = fn_doc("not", NOT_DOC, &idents);

    let add_doc = fn_doc("add", ADD_DOC, &idents);
    let sub_doc = fn_doc("sub", SUB_DOC, &idents);
    let mul_doc = fn_doc("mul", MUL_DOC, &idents);
    let div_doc = fn_doc("div", DIV_DOC, &idents);
    let rem_doc = fn_doc("rem", REM_DOC, &idents);
    let bitand_doc = fn_doc("bitand", BITAND_DOC, &idents);
    let bitor_doc = fn_doc("bitor", BITOR_DOC, &idents);
    let bitxor_doc = fn_doc("bitxor", BITXOR_DOC, &idents);
    let shl_doc = fn_doc("shl", SHL_DOC, &idents);
    let shr_doc = fn_doc("shr", SHR_DOC, &idents);

    let eq_doc = fn_doc("eq", EQ_DOC, &idents);
    let ne_doc = fn_doc("ne", NE_DOC, &idents);
    let lt_doc = fn_doc("lt", LT_DOC, &idents);
    let gt_doc = fn_doc("gt", GT_DOC, &idents);
    let le_doc = fn_doc("le", LE_DOC, &idents);
    let ge_doc = fn_doc("ge", GE_DOC, &idents);

    let min_doc = fn_doc("min", MIN_DOC, &idents);
    let max_doc = fn_doc("max", MAX_DOC, &idents);
    let clamp_doc = fn_doc("clamp", CLAMP_DOC, &idents);

    let len_doc = fn_doc("len", LEN_DOC, &idents);
    let index_doc = fn_doc("index", INDEX_DOC, &idents);
    let enumerate_doc = fn_doc("enumerate", ENUMERATE_DOC, &idents);
    let zip_doc = fn_doc("zip", ZIP_DOC, &idents);
    let chain_doc = fn_doc("chain", CHAIN_DOC, &idents);
    let contains_doc = fn_doc("contains", CONTAINS_DOC, &idents);
    let concat_ident_doc = fn_doc("concat_ident", CONCAT_IDENT_DOC, &idents);
    let concat_string_doc = fn_doc("concat_string", CONCAT_STRING_DOC, &idents);

    quote! {
        const _: () = {
            #[allow(dead_code)]
            #[derive(Debug, Clone, Copy)]
            struct Fragment;

            impl Fragment {
                #[allow(dead_code)]
                #[doc = #neg_doc]
                const fn neg(self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #not_doc]
                const fn not(self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #add_doc]
                const fn add(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #sub_doc]
                const fn sub(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #mul_doc]
                const fn mul(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #div_doc]
                const fn div(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #rem_doc]
                const fn rem(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #bitand_doc]
                const fn bitand(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #bitor_doc]
                const fn bitor(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #bitxor_doc]
                const fn bitxor(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #shl_doc]
                const fn shl(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #shr_doc]
                const fn shr(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #eq_doc]
                const fn eq(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #ne_doc]
                const fn ne(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #lt_doc]
                const fn lt(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #gt_doc]
                const fn gt(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #le_doc]
                const fn le(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #ge_doc]
                const fn ge(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #min_doc]
                const fn min(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #max_doc]
                const fn max(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #clamp_doc]
                const fn clamp(self, min: Self, max: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #len_doc]
                const fn len(self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #index_doc]
                const fn index(self, index: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #enumerate_doc]
                const fn enumerate(self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #zip_doc]
                const fn zip(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #chain_doc]
                const fn chain(self, other: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #contains_doc]
                const fn contains(self, value: Self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #concat_ident_doc]
                const fn concat_ident(self) -> Self { Self }

                #[allow(dead_code)]
                #[doc = #concat_string_doc]
                const fn concat_string(self) -> Self { Self }
            }

            #[allow(dead_code)]
            let val = Fragment;
            #[allow(dead_code)]
            let value = Fragment;
            #[allow(dead_code)]
            let other = Fragment;
            #[allow(dead_code)]
            let index = Fragment;
            #[allow(dead_code)]
            let min = Fragment;
            #[allow(dead_code)]
            let max = Fragment;

            #(#ident_calls;)*
        };
    }
}

fn fn_doc(name: &str, doc: &'static str, idents: &[(Token![.], Option<Ident>)]) -> &'static str {
    if idents.iter().any(|(_, ident)| {
        ident
            .as_ref()
            .map(|ident| ident.to_string())
            .as_ref()
            .map(|ident| ident.as_str())
            == Some(name)
    }) {
        doc
    } else {
        ""
    }
}

const NEG_DOC: &str = "Negates a fragment. This works for `int` and `float` fragments.";

const NOT_DOC: &str = "Negates a fragment. This works for `bool` fragments.";

const ADD_DOC: &str = "
    Adds two fragments together.
    This works for `int`, `float`, `str` and `ident` fragments.

    For `str`/`ident` fragments, this concatenates the left with the right `str`/`ident`/`char`.
    ";

const SUB_DOC: &str = "Subtracts two fragments. This works for `int` and `float` fragments.";

const MUL_DOC: &str = "
    Multiplies two fragments.
    This works for `int`, `float`, `str`, `ident` and `char` fragments.

    For `str`/`ident`/`char` fragments, this repeats the left with the right `int`.
    ";

const DIV_DOC: &str = "Divides two fragments. This works for `int` and `float` fragments.";

const REM_DOC: &str = "Remains two fragments. This works for `int` and `float` fragments.";

const BITAND_DOC: &str = "Bitwise and of two fragments. This works for `int` and `bool` fragments.";

const BITOR_DOC: &str = "Bitwise or of two fragments. This works for `int` and `bool` fragments.";

const BITXOR_DOC: &str = "Bitwise xor of two fragments. This works for `int` and `bool` fragments.";

const SHL_DOC: &str = "Left shifts two fragments. This works for `int` fragments.";

const SHR_DOC: &str = "Right shifts two fragments. This works for `int` fragments.";

const EQ_DOC: &str =
    "Checks if two fragments are equal. This works for all fragments but `tokens`.";

const NE_DOC: &str =
    "Checks if two fragments are not equal. This works for all fragments but `tokens`.";

const LT_DOC: &str = "Checks if the first fragment is less than the second. This works for all fragments but `list` and `tokens`.";

const GT_DOC: &str = "Checks if the first fragment is greater than the second. This works for all fragments but `list` and `tokens`.";

const LE_DOC: &str = "Checks if the first fragment is less than or equal to the second. This works for all fragments but `list` and `tokens`.";

const GE_DOC: &str = "Checks if the first fragment is greater than or equal to the second. This works for all fragments but `list` and `tokens`.";

const MIN_DOC: &str = "
    Returns the minimum of two fragments.
    This uses the `<` / `>` operators, and thus works for more than just numbers.
    ";

const MAX_DOC: &str = "
    Returns the maximum of two fragments.
    This uses the `<` / `>` operators, and thus works for more than just numbers.
    ";

const CLAMP_DOC: &str = "
    Clamps a fragment between two other fragments.
    This uses the `<` / `>` operators, and thus works for more than just numbers.
    ";

const LEN_DOC: &str = "Returns the length of a fragment. This works for `str`, `ident`, `char`, `list` and `tokens` fragments.";

const INDEX_DOC: &str =
    "Returns the element at the given index. This only works for `list` fragments.";

const ENUMERATE_DOC: &str =
    "Returns a list of tuple-lists over the elements of the input list, as `[idx, val]`.";

const ZIP_DOC: &str =
    "Returns a list of tuple-lists over the elements of two fragments, as `[val1, val2]`.";

const CHAIN_DOC: &str =
    "Returns a list that has the elements of the first list, then of the second list.";

const CONTAINS_DOC: &str = "Checks if the specified value is contained in the list.";

const CONCAT_IDENT_DOC: &str =
    "Concatenates the items of a list into an `ident`, this works exactly like `@[...]`.";

const CONCAT_STRING_DOC: &str =
    "Concatenates the items of a list into a `str`, this works exactly like `@[...]`.";