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
//!
//! `GroupDeterminer` is used to determine any `ActionGroup` or separator (for ex. `,`) in `ParseStream`
//!

use proc_macro2::{TokenStream, TokenTree};
use syn::parse::ParseStream;

use super::ActionGroup;

pub struct GroupDeterminer {
    group_type: Option<ActionGroup>,
    check_input_fn: Box<dyn Fn(ParseStream) -> bool>,
    check_parsed_fn: Option<Box<dyn Fn(TokenStream) -> bool>>,
    length: usize,
}

#[macro_export]
macro_rules! instant_and_deferred_determiners {
    ($($group_type: ident => $($tokens: expr),+; $length: expr),+) => {
        vec![
            $(
                crate::group_determiner!(
                    crate::expr_chain::ActionGroup::Instant(crate::expr_chain::CommandGroup::$group_type) => $($tokens),+; $length
                ),
                crate::group_determiner!(
                    crate::expr_chain::ActionGroup::Deferred(crate::expr_chain::CommandGroup::$group_type) => Token![~], $($tokens),+; $length + 1
                )
            ),*
        ]
    };
}

#[macro_export]
macro_rules! tokens_checker {
    ($token1: expr, $token2:expr, $token3:expr) => {
        Box::new(|input: ::syn::parse::ParseStream<'_>| {
            input.peek($token1) && input.peek2($token2) && input.peek3($token3)
        })
    };
    ($token1: expr, $token2:expr) => {
        Box::new(|input: ::syn::parse::ParseStream<'_>| input.peek($token1) && input.peek2($token2))
    };
    ($token: expr) => {
        Box::new(|input: ::syn::parse::ParseStream<'_>| input.peek($token))
    };
}

#[macro_export]
macro_rules! group_determiner {
    ($group_type: expr => $($tokens: expr),+; $length: expr; $check_parsed_fn: expr) => {{
        let check_input_fn = crate::tokens_checker!($($tokens),*);
        crate::expr_chain::GroupDeterminer::new(
            $group_type,
            check_input_fn,
            Some($check_parsed_fn),
            $length
        )
    }};
    ($group_type: expr => $($tokens: expr),+; $length: expr) => {
        crate::group_determiner!(
            $group_type => $($tokens),+; $length; Box::new(is_valid_expr)
        )
    };
}

impl GroupDeterminer {
    ///
    /// Constructs new `GroupDeterminer`.
    /// Example:
    /// ```
    /// extern crate union_impl;
    /// extern crate syn;
    ///
    /// use syn::Token;
    /// use union_impl::expr_chain::group::GroupDeterminer;
    ///
    /// fn main() {
    ///     let first_comma_determiner = GroupDeterminer::new(
    ///         None, // Because comma is not an action group
    ///         Box::new(|input| input.peek(Token![,])),
    ///         None,
    ///         1
    ///     );
    /// }
    /// ```
    ///
    pub fn new(
        group_type: impl Into<Option<ActionGroup>>,
        check_input_fn: Box<dyn Fn(ParseStream) -> bool>,
        check_parsed_fn: Option<Box<dyn Fn(TokenStream) -> bool>>,
        length: usize,
    ) -> Self {
        GroupDeterminer {
            group_type: group_type.into(),
            check_input_fn,
            check_parsed_fn,
            length,
        }
    }

    ///
    /// Returns type of group of `GroupDeterminer`.
    ///

    pub fn get_group_type(&self) -> Option<ActionGroup> {
        self.group_type
    }

    ///
    /// Checks if input next tokens are of self group type.
    ///  

    pub fn check_input(&self, input: ParseStream) -> bool {
        (self.check_input_fn)(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(&self, input: TokenStream) -> bool {
        self.check_parsed_fn
            .as_ref()
            .map(|checker| checker(input))
            .unwrap_or(true)
    }

    ///
    /// Used to parse `length` tokens of type `TokenTree` from input `ParseStream`.
    ///
    pub fn erase_input<'a>(&self, input: ParseStream<'a>) -> syn::Result<ParseStream<'a>> {
        (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::super::utils::is_valid_expr;
    use super::super::*;
    use super::*;

    #[test]
    fn it_creates_comma_determiner() {
        let first_comma_determiner = GroupDeterminer::new(
            None, // Because comma is not an action group
            Box::new(|input| input.peek(::syn::Token![,])),
            None,
            1,
        );

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

    #[test]
    fn it_creates_then_determiner() {
        let then_determiner = GroupDeterminer::new(
            Some(ActionGroup::Instant(CommandGroup::Then)), // Because comma is not an action group
            Box::new(|input| input.peek(::syn::Token![=>])),
            Some(Box::new(is_valid_expr)),
            2,
        );

        assert_eq!(
            then_determiner.get_group_type(),
            Some(ActionGroup::Instant(CommandGroup::Then))
        );
        assert!(then_determiner.check_parsed(::quote::quote! { 23 }));
        assert_eq!(then_determiner.length(), 2);
    }
}