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
//! Some useful functions on `proc_macro` and `proc_macro2` types
//!
//! E.g. [pushing tokens onto TokenStream](TokenStreamExt::push) and [testing
//! for specific punctuation on TokenTree and Punct](TokenTreePunct)
#![warn(clippy::pedantic)]
#![deny(missing_docs)]

// TODO consider splitting up the traits

macro_rules! once {
    (($($tts:tt)*) $($tail:tt)*) => {
        $($tts)*
    };
}

macro_rules! impl_via_trait {
    (
        $(
            $(#$trait_attr:tt)*
            impl $trait:ident for $type:ident {
                $(#$first_attr:tt)*
                $(type $types:ident = $type_assignment:ty; $(#$type_attr:tt)* )*
                $(fn $function:ident($($args:tt)*) $(-> $ret:ty)? { $($stmts:tt)* } $(#$fn_attr:tt)* )*
            }
        )+
    ) => {
        once!($(($(#$trait_attr)* pub trait $trait {
            $(#$first_attr)*
            $(type $types; $(#$type_attr)*)*
            $(fn $function($($args)*) $(-> $ret)?; $(#$fn_attr)*)*
        }))?);
        $(#[cfg(feature = "proc-macro2")]
        const _:() = {
            use proc_macro2::*;
            impl $trait for $type {
                $(type $types = $type_assignment;)*
                $(fn $function($($args)*) $(-> $ret)? {
                    $($stmts)*
                })*
            }
        };
        #[cfg(use_proc_macro)]
        const _:() = {
            use proc_macro::*;
            impl $trait for $type {
                $(fn $function($($args)*) $(-> $ret)? {
                    $($stmts)*
                })*
            }
        };)+
    };
}

impl_via_trait! {
    /// Generic extensions for
    #[cfg_attr(feature = "proc-macro2", doc = "[`proc_macro2::TokenStream`]")]
    #[cfg_attr(all(proc_macro, feature = "proc-macro2"), doc = "and")]
    #[cfg_attr(proc_macro, doc = "[`proc_macro::TokenStream`]")]
    #[cfg_attr(not(any(proc_macro, feature = "proc-macro2")), doc = "`proc_macro::TokenStream`")]
    impl TokenStreamExt for TokenStream {
        /// TokenTree to support both proc_macro and proc_macro2
        type TokenTree = TokenTree;
        /// Pushes a single [`Self::TokenTree`] onto the token stream
        fn push(&mut self, token: Self::TokenTree) {
            self.extend(std::iter::once(token))
        }
    }
}

macro_rules! token_tree_ext {
    ($($a:literal, $token:literal, $is:ident, $as:ident, $variant:ident);+$(;)?) => {
        impl_via_trait! {
            /// Generic extensions for
            #[cfg_attr(feature = "proc-macro2", doc = "[`proc_macro2::TokenTree`]")]
            #[cfg_attr(all(proc_macro, feature = "proc-macro2"), doc = "and")]
            #[cfg_attr(proc_macro, doc = "[`proc_macro::TokenTree`]")]
            #[cfg_attr(not(any(proc_macro, feature = "proc-macro2")), doc = "`proc_macro::TokenTree`")]
            impl TokenTreeExt for TokenTree {
                $(
                    #[doc = concat!(stringify!($variant), " of this TokenTree, necessary to support both TokenTrees")]
                    type $variant = $variant;
                )*
                $(
                    #[doc = concat!("Tests if the token tree is ", $a, " ", $token, ".")]
                    fn $is(&self) -> bool {
                        matches!(self, Self::$variant(_))
                    }
                    #[doc = concat!("Get the `", stringify!($variant), "` inside this token tree, or [`None`] if it isn't ", $a, " ", $token, ".")]
                    fn $as(self) -> Option<<Self as TokenTreeExt>::$variant> {
                        if let Self::$variant(inner) = self {
                            Some(inner)
                        } else {
                            None
                        }
                    }
                )*
            }
        }
    };
}

token_tree_ext!(
    "a", "group", is_group, group, Group;
    "an", "ident", is_ident, ident, Ident;
    "a", "punctuation", is_punct, punct, Punct;
    "a", "literal", is_literal, literal, Literal;
);

macro_rules! punctuations {
    ($($char:literal as $name:ident),*) => {
        impl_via_trait!{
            /// Trait to test for punctuation
            impl TokenTreePunct for TokenTree {
                $(#[doc = concat!("Tests if the token is `", $char, "`")]
                fn $name(&self) -> bool {
                    matches!(self, TokenTree::Punct(punct) if punct.as_char() == $char)
                })*
            }
            impl TokenTreePunct for Punct {
                $(fn $name(&self) -> bool {
                    self.as_char() == $char
                })*
            }
        }
    };
}

punctuations![
    '=' as is_equals,
    '<' as is_less_than,
    '>' as is_greater_than,
    '!' as is_exclamation,
    '~' as is_tilde,
    '+' as is_plus,
    '-' as is_minus,
    '*' as is_asterix, // TODO naming
    '/' as is_slash,
    '%' as is_percent,
    '^' as is_caret,
    '&' as is_and,
    '|' as is_pipe,
    '@' as is_at,
    '.' as is_dot,
    ',' as is_comma,
    ';' as is_semi,
    ':' as is_colon,
    '#' as is_pound,
    '$' as is_dollar,
    '?' as is_question,
    '\'' as is_quote // TODO naming
];

#[cfg(all(test, feature = "proc-macro2"))]
mod test {
    use proc_macro2::{Punct, Spacing, TokenTree};
    use quote::quote;

    use super::*;

    #[test]
    fn punctuation() {
        let mut tokens = quote! {=<>!$~+-*/%^|@.,;:#$?'a}.into_iter();
        assert!(tokens.next().unwrap().is_equals());
        assert!(tokens.next().unwrap().is_less_than());
        assert!(tokens.next().unwrap().is_greater_than());
        assert!(tokens.next().unwrap().is_exclamation());
        assert!(tokens.next().unwrap().is_dollar());
        assert!(tokens.next().unwrap().is_tilde());
        assert!(tokens.next().unwrap().is_plus());
        assert!(tokens.next().unwrap().is_minus());
        assert!(tokens.next().unwrap().is_asterix());
        assert!(tokens.next().unwrap().is_slash());
        assert!(tokens.next().unwrap().is_percent());
        assert!(tokens.next().unwrap().is_caret());
        assert!(tokens.next().unwrap().is_pipe());
        assert!(tokens.next().unwrap().is_at());
        assert!(tokens.next().unwrap().is_dot());
        assert!(tokens.next().unwrap().is_comma());
        assert!(tokens.next().unwrap().is_semi());
        assert!(tokens.next().unwrap().is_colon());
        assert!(tokens.next().unwrap().is_pound());
        assert!(tokens.next().unwrap().is_dollar());
        assert!(tokens.next().unwrap().is_question());
        assert!(tokens.next().unwrap().is_quote());
    }

    #[test]
    fn token_stream_ext() {
        let mut tokens = quote!(a);
        tokens.push(TokenTree::Punct(Punct::new(',', Spacing::Alone)));
        assert_eq!(tokens.to_string(), "a ,");
    }

    #[test]
    fn token_tree_ext() {
        let mut tokens = quote!({group} ident + "literal").into_iter().peekable();
        assert!(tokens.peek().unwrap().is_group());
        assert_eq!(
            tokens.next().unwrap().group().unwrap().to_string(),
            "{ group }"
        );
        assert!(tokens.peek().unwrap().is_ident());
        assert_eq!(tokens.next().unwrap().ident().unwrap().to_string(), "ident");
        assert!(tokens.peek().unwrap().is_punct());
        assert_eq!(tokens.next().unwrap().punct().unwrap().to_string(), "+");
        assert!(tokens.peek().unwrap().is_literal());
        assert_eq!(
            tokens.next().unwrap().literal().unwrap().to_string(),
            "\"literal\""
        );
    }
}