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
//!
//! Definition of `GroupDeterminer` and related macros.
//!

use super::super::utils::is_valid_stream;
use proc_macro2::{TokenStream, TokenTree};
use syn::parse::{Parse, ParseStream};

use super::CommandGroup;

pub type DetermineGroupPredicate = fn(ParseStream) -> bool;

#[derive(Clone, Copy)]
union FnPointer {
    fun: DetermineGroupPredicate,
    raw: *const (),
}

unsafe impl std::marker::Send for FnPointer {}

unsafe impl std::marker::Sync for FnPointer {}

///
/// `GroupDeterminer` is used to determine any `CommandGroup` or separator (for ex. `,`) in `ParseStream`
///
#[derive(Clone)]
pub struct GroupDeterminer {
    group_type: Option<CommandGroup>,
    check_input_fn: FnPointer,
    validate_parsed: bool,
    length: usize,
}

///
/// Creates `GroupDeterminer` for given `CommandGroup`s with provided tokens.
///
#[macro_export]
macro_rules! define_group_determiners {
    ($($group_type: ident => $($token: expr),+ => $length: expr),+) => {{
        [
            $crate::define_determiner_with_no_group!(Token![,] => 0),
            $(
                $crate::define_group_determiner!(
                    $crate::expr_chain::group::CommandGroup::$group_type => $($token),+ => $length
                )
            ),*,
            $crate::expr_chain::group::GroupDeterminer::new_const(
                None,
                $crate::handler::Handler::peek_handler as *const (),
                true,
                0
            )
        ]
    }};
}

///
/// Creates `GroupDeterminer` with no `CommandGroup` and provided tokens.
///
#[macro_export]
macro_rules! define_determiner_with_no_group {
    ($($token: expr),+ => $length: expr) => {{
        let check_tokens = $crate::define_tokens_checker!($($token),+);
        $crate::expr_chain::group::GroupDeterminer::new_const(
            None,
            check_tokens as *const (),
            true,
            $length
        )
    }}
}

///
/// Creates function which checks if `ParseStream` next values are provided tokens.
///
#[macro_export]
macro_rules! define_tokens_checker {
    ($token1: expr, $token2:expr, $token3:expr) => {{
        fn check_tokens(input: ::syn::parse::ParseStream<'_>) -> bool {
            input.peek($token1) && input.peek2($token2) && input.peek3($token3)
        }
        check_tokens
    }};
    ($token1: expr, $token2:expr) => {{
        fn check_tokens(input: ::syn::parse::ParseStream<'_>) -> bool {
            input.peek($token1) && input.peek2($token2)
        }
        check_tokens
    }};
    ($token: expr) => {{
        fn check_tokens(input: ::syn::parse::ParseStream<'_>) -> bool { input.peek($token) }
        check_tokens
    }};
    ($($token: expr),+) => {{
        fn check_tokens(input: ::syn::parse::ParseStream<'_>) -> bool {
            let input = input.fork();
            $(
                input.peek($token) && $crate::expr_chain::utils::skip(&input)
            )&&+
        }
        check_tokens
    }};
}

///
/// Creates `GroupDeterminer` with given (`CommandGroup` => tokens => length => ?Check parsed tokens? (optional bool))
///
/// # Example:
/// ```
/// use join_impl::expr_chain::group::CommandGroup;
/// use join_impl::define_group_determiner;
/// use syn::Token;
///
/// fn main() {
///     let then_determiner = define_group_determiner!(CommandGroup::Then => Token![->] => 2); // last param is optional, true by default
/// }
/// ```
///
#[macro_export]
macro_rules! define_group_determiner {
    ($group_type: expr => $($tokens: expr),+ => $length: expr => $validate_parsed: expr) => {{
        let check_tokens = $crate::define_tokens_checker!($($tokens),*);
        $crate::expr_chain::group::GroupDeterminer::new_const(
            Some($group_type),
            check_tokens as *const (),
            $validate_parsed,
            $length
        )
    }};
    ($group_type: expr => $($tokens: expr),+ => $length: expr) => {
        $crate::define_group_determiner!(
            $group_type => $($tokens),+ => $length => true
        )
    };
}

impl GroupDeterminer {
    ///
    /// Constructs new `GroupDeterminer` using `const fn` (can be used to create const or static item).
    ///
    /// # Example:
    /// ```
    /// extern crate join_impl;
    /// extern crate syn;
    ///
    /// use syn::Token;
    /// use syn::parse::ParseStream;
    /// use join_impl::expr_chain::group::GroupDeterminer;
    ///
    /// fn check_input(input: ParseStream) -> bool { input.peek(Token![,]) }
    ///
    /// fn main() {
    ///     let first_comma_determiner = GroupDeterminer::new_const(
    ///         None, // Because comma is not an action group
    ///         check_input as *const (),
    ///         false,
    ///         1
    ///     );
    /// }
    /// ```
    ///
    pub const fn new_const(
        group_type: Option<CommandGroup>,
        check_input_fn: *const (),
        validate_parsed: bool,
        length: usize,
    ) -> Self {
        Self {
            group_type,
            check_input_fn: FnPointer {
                raw: check_input_fn,
            },
            validate_parsed,
            length,
        }
    }

    ///
    /// Constructs new `GroupDeterminer`.
    ///
    /// # Example:
    /// ```
    /// extern crate join_impl;
    /// extern crate syn;
    ///
    /// use syn::Token;
    /// use syn::parse::ParseStream;
    /// use join_impl::expr_chain::group::GroupDeterminer;
    ///
    /// fn check_input(input: ParseStream) -> bool { input.peek(Token![,]) }
    ///
    /// fn main() {
    ///     let first_comma_determiner = GroupDeterminer::new(
    ///         None, // Because comma is not an action group
    ///         check_input,
    ///         false,
    ///         1
    ///     );
    /// }
    /// ```
    ///
    pub fn new(
        group_type: impl Into<Option<CommandGroup>>,
        check_input_fn: DetermineGroupPredicate,
        validate_parsed: bool,
        length: usize,
    ) -> Self {
        Self {
            group_type: group_type.into(),
            check_input_fn: FnPointer {
                fun: check_input_fn,
            },
            validate_parsed,
            length,
        }
    }

    ///
    /// Returns type of group of `GroupDeterminer`.
    ///
    pub fn get_group_type(&self) -> Option<CommandGroup> {
        self.group_type
    }

    ///
    /// Checks if input next tokens are of self group type.
    ///  
    pub fn check_input(&self, input: ParseStream<'_>) -> bool {
        (unsafe { self.check_input_fn.fun })(input)
    }

    ///
    /// Checks already parsed tokens. In many cases it's used to check if
    /// parsed tokens are valid expression. in this case we can say for sure that
    /// we found separator.
    ///
    pub fn check_parsed<T: Parse>(&self, input: TokenStream) -> bool {
        !self.validate_parsed || is_valid_stream::<T>(input)
    }

    ///
    /// Used to parse `length` tokens of type `TokenTree` from input `ParseStream`.
    ///
    pub fn erase_input<'b>(&self, input: ParseStream<'b>) -> syn::Result<ParseStream<'b>> {
        (0..self.length()).try_for_each(|_| input.parse::<TokenTree>().map(|_| ()))?;
        Ok(input)
    }

    ///
    /// Returns value of `length` field.
    ///
    pub fn length(&self) -> usize {
        self.length
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;
    use super::*;

    #[test]
    fn it_creates_comma_determiner() {
        fn check_comma(input: ParseStream) -> bool {
            input.peek(::syn::Token![,])
        }

        let first_comma_determiner = GroupDeterminer::new_const(
            None, // Because comma is not a command group
            check_comma as *const (),
            false,
            1,
        );

        assert_eq!(first_comma_determiner.get_group_type(), None);
        assert!(first_comma_determiner.check_parsed::<::syn::Expr>(::quote::quote! { , }));
        assert_eq!(first_comma_determiner.length(), 1);
    }

    #[test]
    fn it_creates_then_determiner() {
        fn check_then(input: ParseStream) -> bool {
            input.peek(::syn::Token![=>])
        }

        let then_determiner = GroupDeterminer::new(CommandGroup::Then, check_then, true, 2);

        assert_eq!(then_determiner.get_group_type(), Some(CommandGroup::Then));
        assert!(then_determiner.check_parsed::<::syn::Expr>(::quote::quote! { 23 }));
        assert_eq!(then_determiner.length(), 2);
    }
}