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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! 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)
//!
//! It also adds the [`assert_tokens!`] and [`assert_expansion!`] macros to
//! improve unit testability for `proc-macros`.
#![warn(clippy::pedantic, missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(rustdoc::all)]

use std::num::ParseIntError;
use std::str::FromStr;

#[cfg(doc)]
use proc_macro2::{Punct, Spacing};

#[cfg(feature = "proc-macro")]
extern crate proc_macro;

/// Parsing of simple rust structures without syn
#[cfg(feature = "parser")]
mod parser;
#[cfg(feature = "parser")]
pub use parser::TokenParser;

#[cfg(feature = "parser")]
#[macro_use]
mod assert;

#[cfg(feature = "parser")]
#[doc(hidden)]
pub mod __private;

mod sealed {
    pub trait Sealed {}

    macro_rules! sealed {
        [$($ty:ident),* $(,)?] => {$(
            impl Sealed for proc_macro::$ty {}
            impl Sealed for proc_macro2::$ty {}
        )*};
    }

    sealed![TokenStream, TokenTree, Punct, Literal, Group];
}

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

macro_rules! attr {
    (($($attr:tt)*), $($item:tt)+) => {
        $(#$attr)* $($item)+
    };
}

macro_rules! trait_def {
    ($item_attr:tt, $trait:ident, $($fn_attr:tt, $fn:ident, $({$($gen:tt)*})?, $args:tt, $($ret:ty)?),*) => {
        attr!($item_attr,
        pub trait $trait: crate::sealed::Sealed {
            $(attr!($fn_attr, fn $fn $($($gen)*)? $args $(-> $ret)?;);)*
        });
    };
}

macro_rules! trait_impl {
    ($trait:ident, $type:ident, $($fn_attr:tt, $fn:ident, $({$($gen:tt)*})?, $args:tt, $($ret:ty)?, $stmts:tt),*) => {
        impl $trait for $type {
            $(attr!($fn_attr, fn $fn $($($gen)*)? $args $(-> $ret)? $stmts);)*
        }
    };
}

macro_rules! impl_via_trait {
    ($(
        $(#$trait_attr:tt)*
        impl $trait:ident for $type:ident {
            $($(#$fn_attr:tt)*
            fn $fn:ident $({$($gen:tt)*})? ($($args:tt)*)  $(-> $ret:ty)? { $($stmts:tt)* })*
        }
    )+) => {
        once!($((trait_def!(($($trait_attr)*), $trait, $(($($fn_attr)*), $fn,$({$($gen)*})?, ($($args)*), $($ret)?),*);))+);
        #[cfg(feature = "proc-macro")]
        const _: () = {
            use proc_macro::*;
            $(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
        };
        #[cfg(feature = "proc-macro2")]
        const _:() = {
            use proc_macro2::*;
            $(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
        };
    };
    (
        mod $mod:ident, $mod2:ident {
            $(
                $(#$trait_attr:tt)*
                impl $trait:ident$($doc:literal)?, $trait2:ident$($doc2:literal)?  for $type:ident {
                    $($(#$fn_attr:tt)*
                    fn $fn:ident $({$($gen:tt)*})? ($($args:tt)*) $(-> $ret:ty)? { $($stmts:tt)* })*
                }
            )+
        }
    ) => {
        #[cfg(feature = "proc-macro")]
        once!(($(pub use $mod::$trait;)+));
        #[cfg(feature = "proc-macro")]
        mod $mod {
            use proc_macro::*;
            once!($((trait_def!(($($trait_attr)* $([doc=$doc])?), $trait, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?),*);))+);
            $(trait_impl!($trait, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
        }
        #[cfg(feature = "proc-macro2")]
        once!(($(pub use $mod2::$trait2;)+));
        #[cfg(feature = "proc-macro2")]
        mod $mod2 {
            use proc_macro2::*;
            once!($((trait_def!(($($trait_attr)*$([doc=$doc2])?), $trait2, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?),*);))+);
            $(trait_impl!($trait2, $type, $(($($fn_attr)*), $fn, $({$($gen)*})?, ($($args)*), $($ret)?, {$($stmts)*}),*);)+
        }
    };
}

impl_via_trait! {
    mod token_stream_ext, token_stream2_ext {
        /// Generic extensions for
        impl TokenStreamExt "[`proc_macro::TokenStream`]", TokenStream2Ext "[`proc_macro2::TokenStream`]" for TokenStream {
            /// Pushes a single [`TokenTree`] onto the token stream.
            fn push(&mut self, token: TokenTree) {
                self.extend(std::iter::once(token))
            }
            /// Creates a [`TokenParser`](crate::TokenParser) from this token stream.
            #[cfg(feature = "parser")]
            fn parser(self) -> crate::TokenParser<proc_macro2::token_stream::IntoIter> {
                #[allow(clippy::useless_conversion)]
                proc_macro2::TokenStream::from(self).into()
            }

            /// Creates a [`TokenParser`](crate::TokenParser) from this token stream.
            ///
            /// Allows to specify the length of the [peeker buffer](crate::TokenParser#peeking).
            #[cfg(feature = "parser")]
            fn parser_generic{<const PEEKER_LEN: usize>}(self) -> crate::TokenParser<proc_macro2::token_stream::IntoIter, PEEKER_LEN> {
                #[allow(clippy::useless_conversion)]
                proc_macro2::TokenStream::from(self).into()
            }
        }
    }
}

macro_rules! token_tree_ext {
    ($($a:literal, $token:literal, $is:ident, $as:ident, $into:ident, $variant:ident);+$(;)?) => {
        impl_via_trait! {
            mod token_tree_ext, token_tree2_ext {
                /// Generic extensions for
                impl TokenTreeExt "[`proc_macro::TokenTree`]", TokenTree2Ext "[`proc_macro2::TokenTree`]"  for TokenTree {
                    $(
                        #[doc = concat!("Tests if the token tree is ", $a, " ", $token, ".")]
                        #[must_use]
                        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, ".")]
                        #[must_use]
                        fn $as(&self) -> Option<&$variant> {
                            if let Self::$variant(inner) = &self {
                                Some(inner)
                            } else {
                                None
                            }
                        }
                        #[doc = concat!("Get the [`", stringify!($variant), "`] inside this token tree, or [`None`] if it isn't ", $a, " ", $token, ".")]
                        #[must_use]
                        fn $into(self) -> Option<$variant> {
                            if let Self::$variant(inner) = self {
                                Some(inner)
                            } else {
                                None
                            }
                        }
                    )*
                }
            }
        }
    };
}

token_tree_ext!(
    "a", "group", is_group, group, into_group, Group;
    "an", "ident", is_ident, ident, into_ident, Ident;
    "a", "punctuation", is_punct, punct, into_punct, Punct;
    "a", "literal", is_literal, literal, into_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, "`")]
                #[must_use]
                fn $name(&self) -> bool {
                    matches!(self, TokenTree::Punct(punct) if punct.$name())
                })*
                /// Tests if token is followed by some none punctuation token or whitespace.
                #[must_use]
                fn is_alone(&self) -> bool {
                    matches!(self, TokenTree::Punct(punct) if punct.is_alone())
                }
                /// Tests if token is followed by another punct and can potentially be combined into
                /// a multi-character operator.
                #[must_use]
                fn is_joint(&self) -> bool {
                    matches!(self, TokenTree::Punct(punct) if punct.is_joint())
                }
                /// If sets the [`spacing`](Punct::spacing) of a punct to [`Alone`](Spacing::Alone).
                #[must_use]
                fn alone(self) -> Self {
                    match self {
                        Self::Punct(p) => Self::Punct(p.alone()),
                        it => it
                    }
                }
            }
            impl TokenTreePunct for Punct {
                $(fn $name(&self) -> bool {
                    self.as_char() == $char
                })*
                fn is_alone(&self) -> bool {
                    self.spacing() == Spacing::Alone
                }
                fn is_joint(&self) -> bool {
                    self.spacing() == Spacing::Joint
                }
                fn alone(self) -> Self {
                    if self.is_alone() {
                        self
                    } else {
                        let mut this = Punct::new(self.as_char(), Spacing::Alone);
                        this.set_span(self.span());
                        this
                    }
                }
            }
        }
    };
}

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
];

macro_rules! delimited {
    ($($delimiter:ident as $name:ident : $doc:literal),*) => {
        impl_via_trait!{
            /// Trait to test for delimiters of groups
            impl Delimited for TokenTree {
                $(#[doc = concat!("Tests if the token is a group with ", $doc)]
                #[must_use]
                fn $name(&self) -> bool {
                    matches!(self, TokenTree::Group(group) if group.$name())
                })*
            }
            impl Delimited for Group {
                $(#[doc = concat!("Tests if a group has ", $doc)]
                #[must_use]
                fn $name(&self) -> bool {
                    matches!(self.delimiter(), Delimiter::$delimiter)
                })*
            }
        }
    };
}

delimited![
    Parenthesis as is_parenthesized: " parentheses (`( ... )`)",
    Brace as is_braced: " braces (`{ ... }`)",
    Bracket as is_bracketed: " brackets (`[ ... ]`)",
    None as is_implicitly_delimited: " no delimiters (`Ø ... Ø`)"
];

impl_via_trait! {
    /// Trait to parse literals
    impl TokenTreeLiteral for TokenTree {
        /// Tests if the token is a string literal.
        #[must_use]
        fn is_string(&self) -> bool {
            self.literal().is_some_and(TokenTreeLiteral::is_string)
        }

        /// Returns the string contents if it is a string literal.
        #[must_use]
        fn string(&self) -> Option<String> {
            self.literal().and_then(TokenTreeLiteral::string)
        }
    }

    impl TokenTreeLiteral for Literal {
        fn is_string(&self) -> bool {
            let s = self.to_string();
            s.starts_with('"') || s.starts_with("r\"") || s.starts_with("r#")
        }
        fn string(&self) -> Option<String> {
            let lit = self.to_string();
            if lit.starts_with('"') {
                Some(resolve_escapes(&lit[1..lit.len() - 1]))
            } else if lit.starts_with('r') {
                let pounds = lit.chars().skip(1).take_while(|&c| c == '#').count();
                Some(lit[2 + pounds..lit.len() - pounds - 1].to_owned())
            } else {
                None
            }
        }
    }
}

// Implemented following https://doc.rust-lang.org/reference/tokens.html#string-literals
// #[allow(clippy::needless_continue)]
fn resolve_escapes(mut s: &str) -> String {
    let mut out = String::new();
    while !s.is_empty() {
        if s.starts_with('\\') {
            match s.as_bytes()[1] {
                b'x' => {
                    out.push(
                        char::from_u32(u32::from_str_radix(&s[2..=3], 16).expect("valid escape"))
                            .expect("valid escape"),
                    );
                    s = &s[4..];
                }
                b'u' => {
                    let len = s[3..].find('}').expect("valid escape");
                    out.push(
                        char::from_u32(u32::from_str_radix(&s[3..len], 16).expect("valid escape"))
                            .expect("valid escape"),
                    );
                    s = &s[3 + len..];
                }
                b'n' => {
                    out.push('\n');
                    s = &s[2..];
                }
                b'r' => {
                    out.push('\r');
                    s = &s[2..];
                }
                b't' => {
                    out.push('\t');
                    s = &s[2..];
                }
                b'\\' => {
                    out.push('\\');
                    s = &s[2..];
                }
                b'0' => {
                    out.push('\0');
                    s = &s[2..];
                }
                b'\'' => {
                    out.push('\'');
                    s = &s[2..];
                }
                b'"' => {
                    out.push('"');
                    s = &s[2..];
                }
                b'\n' => {
                    s = &s[..s[2..]
                        .find(|c: char| !c.is_ascii_whitespace())
                        .unwrap_or(s.len())];
                }
                c => unreachable!(
                    "TokenStream string literals should only contain valid escapes, found `\\{c}`"
                ),
            }
        } else {
            let len = s.find('\\').unwrap_or(s.len());
            out.push_str(&s[..len]);
            s = &s[len..];
        }
    }
    out
}

#[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!(matches!(
            tokens.next().unwrap().group().unwrap().to_string().as_str(),
            "{ group }" | "{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\""
        );
    }

    #[test]
    fn test() {}
}