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
//! This macro implements a syntax that emulates Python's
//! [`generator-expression`] syntax in a form more compatible with Rust's
//! usual syntax.
//!
//! This means that there are a few small differences between the Python syntax
//! and the syntax provided in this macro:
//!
//! * The pattern between the `for` and `in` tokens is a fully-fledged
//!   Rust pattern, which can be as simple as a simple token and as complex
//!   as struct destructuring.
//! * The expression defining the iterator after the `in` token
//!   must evaluate to either an `Iterator` or an `impl IntoIterator`.
//! * The conditional expression after the `if` token must evaluate to
//!   a boolean.
//! * You may use an `if let` clause instead of the usual `if` clause wherever
//!   `if` clauses are allowed. Any names introduced in the `if let` clause
//!   are available in any following clause.
//! * The expression in the beginning of the generator expression,
//!   the expression following the `in` token, and the expression following
//!   the `if` token, must all end with a semicolon (;). The only exception
//!   to this is the last expression following `in` or `if` in the macro,
//!   which may omit the trailing semicolon.
//!
//! The expression replaced by the `comp!()` macro invocation is a lazy
//! iterator whose lifetime is bound by any references it needs to capture.
//! This means that it can be `.collect()`ed into any container you like.
//!
//! Note though that, at least for now, all objects named in an `in` clause,
//! (except for the first `in` clause) must be either `Copy` or introduced by
//! the previous `for` or `if let` clauses. This is because the macro uses a
//! `move` closure (`FnOnce`) for each level of nesting, which may need to be
//! instantiated more than once without implicit cloning of the captured
//! objects.
//! Similarly, objects named in the "yield" expression (preceding the first
//! `for` clause) must be `Copy` types if they were not introduced by the final
//! `for` or `if let` clauses. This is because they may be used in multiple
//! output items.
//!
//! Specifying which objects should be cloned and where may be added in the
//! future, but will probably require a breaking change.
//!
//! This is a BNF description of the syntax used by this macro:
//!
//! ```bnf
//! comprehension ::=  expression ";" comp_for [comp_iter] [";"]
//! comp_iter     ::=  ";" (comp_for | comp_if | comp_if_let)
//! comp_for      ::=  "for" pattern "in" expression [comp_iter]
//! comp_if       ::=  "if" expression [comp_iter]
//! comp_if_let   ::=  "if" "let" pattern ("|" pattern)* "=" expression [comp_iter]
//! ```
//!
//! Just like in Python, you can nest as many `for`, `if`, and `if let`
//! clauses as you like.
//!
//! ## Examples
//!
//! Simple generator expression with a conditional:
//!
//! ```rust
//! use py_comp::comp;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Foo(i32);
//!
//! let arr = &[Foo(11), Foo(12)];
//!
//! // Notice the semicolons
//! let comp_vector = comp!(item; for item in arr; if item.0 % 10 == 2)
//!     .collect::<Vec<&Foo>>();
//!
//! assert_eq!(comp_vector, vec![&Foo(12)])
//! ```
//!
//! Triple cartesian product with conditions and patterns:
//!
//! ```rust
//! use py_comp::comp;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Foo(i32);
//!
//! // These need to be references to arrays because of how the closures
//! // that the macro expands to capture their environment.
//! let x = &[(Foo(11), "foo"), (Foo(12), "bar")];
//! let y = &[Foo(21), Foo(22)];
//! let z = &[Foo(31), Foo(32)];
//!
//! let xyz = comp!(
//!     (a, b, c);
//!     for (a, _text) in x;  // You can use any function parameter pattern.
//!     if a.0 % 10 == 2;
//!     for b in y;           // Obviously not every level requires a conditional.
//!     for c in z;
//!     if c.0 % 10 == 2;
//! )
//! .collect::<Vec<(&Foo, &Foo, &Foo)>>();
//!
//! // The result vector here is short for illustration purposes
//! // but can be as long as long as you need it to be.
//! assert_eq!(xyz, vec![(&Foo(12), &Foo(21), &Foo(32)), (&Foo(12), &Foo(22), &Foo(32))])
//! ```
//!
//! Flatten a triple-nested structure + complex expression:
//!
//! ```rust
//! use py_comp::comp;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct Foo(i32);
//!
//! let nested_3 = &[
//!     [
//!         [Foo(0), Foo(1), Foo(2)],
//!         [Foo(3), Foo(4), Foo(5)],
//!         [Foo(6), Foo(7), Foo(8)],
//!     ],
//!     [
//!         [Foo(9), Foo(10), Foo(11)],
//!         [Foo(12), Foo(13), Foo(14)],
//!         [Foo(15), Foo(16), Foo(17)],
//!     ],
//!     [
//!         [Foo(18), Foo(19), Foo(20)],
//!         [Foo(21), Foo(22), Foo(23)],
//!         [Foo(24), Foo(25), Foo(26)],
//!     ],
//! ];
//!
//! let nested_objects = comp!(
//!     {
//!         let inner = nested.0;
//!         Foo(inner + 1)
//!     };
//!     for nested_2 in nested_3;
//!     for nested_1 in nested_2;
//!     for nested in nested_1;
//! )
//! .collect::<Vec<Foo>>();
//!
//! let expected_values = (1..28).map(Foo).collect::<Vec<Foo>>();
//!
//! assert_eq!(expected_values, nested_objects);
//! ```
//!
//! [`generator-expression`]: https://docs.python.org/3/reference/expressions.html#generator-expressions
//!

#![warn(clippy::all)]

use doc_comment::doctest;

doctest!("../Readme.md");

/// Check that the type of the expression passed here implements IntoIterator.
#[doc(hidden)]
#[inline(always)]
pub fn __py_comp_assert_impl_into_iter<T: IntoIterator>(_: &T) {}

/// A Python-like lazy generator-expression
///
/// For details see [module level documentation][super]
///
/// [super]: ../py_comp/index.html
#[macro_export(local_inner_macros)]
macro_rules! comp {
    // @parse_if if
    (@parse_if
        $item_expr: expr;
        if $condition: expr
    ) => {
        if $condition {
            Some($item_expr)
        } else {
            None
        }
    };

    // @parse_if if-let
    (@parse_if
        $item_expr: expr;
        if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr
    ) => {
        if let $( $if_let_pattern )|+ = $if_let_expr {
            Some($item_expr)
        } else {
            None
        }
    };

    // @parse_if if for ...
    // This case returns to the main macro parsing.
    (@parse_if
        $item_expr: expr;
        if $condition: expr;
        for $($rest: tt)*
    ) => {
        if $condition {
            Some(comp!($item_expr; for $($rest)*))
        } else {
            None
        }
    };

    // @parse_if if-let for ...
    // This case returns to the main macro parsing.
    (@parse_if
        $item_expr: expr;
        if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr;
        for $($rest: tt)*
    ) => {
        if let $( $if_let_pattern )|+ = $if_let_expr {
            Some(comp!($item_expr; for $($rest)*))
        } else {
            None
        }
    };

    // @parse_if if if ...
    (@parse_if
        $item_expr: expr;
        if $condition: expr;
        if $($rest: tt)*
    ) => {
        if $condition {
            comp!(@parse_if $item_expr; if $($rest)*)
        } else {
            None
        }
    };

    // @parse_if if-let if ...
    (@parse_if
        $item_expr: expr;
        if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr;
        if $($rest: tt)*
    ) => {
        if let $( $if_let_pattern )|+ = $if_let_expr {
            comp!(@parse_if $item_expr; if $($rest)*)
        } else {
            None
        }
    };

    // for in
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr $(;)?
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .map(move |$pattern| $item_expr)
    }};

    // for in $( if $( if-let )* )+
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr
        $(
            ; if $condition: expr
            $( ; if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr )*
        )+
        $(;)?
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .filter_map(move |$pattern|
                comp!(@parse_if
                    $item_expr
                    $(
                        ; if $condition
                        $( ; if let $( $if_let_pattern )|+ = $if_let_expr )*
                    )+
                )
            )
    }};

    // for in $( if-let $( if )* )+
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr
        $(
            ; if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr
            $( ; if $condition: expr )*
        )+
        $(;)?
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .filter_map(move |$pattern|
                comp!(@parse_if
                    $item_expr
                    $(
                        ; if let $( $if_let_pattern )|+ = $if_let_expr
                        $( ; if $condition )*
                    )+
                )
            )
    }};

    // for in for ...
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr;
        for $($rest: tt)*
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .flat_map(move |$pattern|
                comp!($item_expr; for $($rest)*)
            )
    }};

    // for in $( if $( if-let )* )+ for ...
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr;
        $(
            if $condition: expr;
            $( if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr; )*
        )+
        for $($rest: tt)*
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .filter_map(move |$pattern|
                comp!(@parse_if
                    $item_expr;
                    $(
                        if $condition;
                        $( if let $( $if_let_pattern )|+ = $if_let_expr; )*
                    )+
                    for $($rest)*
                )
            )
            .flatten()
    }};

    // for in $( if-let $( if )* )+ for ...
    (
        $item_expr: expr;
        for $pattern: pat in $into_iterator: expr;
        $(
            if let $( $if_let_pattern: pat )|+ = $if_let_expr: expr;
            $( if $condition: expr; )*
        )+
        for $($rest: tt)*
    ) => {{
        let into_iterator = $into_iterator;
        $crate::__py_comp_assert_impl_into_iter(&into_iterator);
        into_iterator
            .into_iter()
            .filter_map(move |$pattern|
                comp!(@parse_if
                    $item_expr;
                    $(
                        if let $( $if_let_pattern )|+ = $if_let_expr;
                        $( if $condition; )*
                    )+
                    for $($rest)*
                )
            )
            .flatten()
    }};
}