Skip to main content

typed_quote/tokens/
punct.rs

1use super::*;
2
3macro_rules! imps {
4    (
5        [$($Punct:ident)+]
6        $This:ident
7        $imp:tt
8    ) => {
9        $(
10            const _: () = {
11                use self::$Punct as $This;
12
13                crate::expand_or! {$imp}
14            };
15        )+
16    };
17}
18
19macro_rules! punct {
20    ({
21        #$attr:tt
22        [
23            $($Punct:ident ($PUNCT_value:literal)),+ $(,)?
24        ];
25
26        use $This:ident;
27
28        $($imp:tt)*
29    }) => {
30        $(
31            #$attr
32            pub struct $Punct<S: MaybeSpan>(pub S);
33
34            #[cfg(any(feature = "proc-macro", feature = "proc-macro2"))]
35            impl $Punct<NoSpan> {
36                const CHAR: char = $PUNCT_value;
37            }
38        )+
39        imps! {
40            [$($Punct)+]
41            $This
42            [$($imp)*]
43        }
44    };
45}
46
47punct!({
48    #[derive(Debug, Clone, Copy)]
49    [
50        Pound('#'),
51        Comma(','),
52        Dot('.'),
53        Semi(';'),
54        Colon(':'),
55        Add('+'),
56        And('&'),
57        At('@'),
58        Bang('!'),
59        Caret('^'),
60        Div('/'),
61        Eq('='),
62        Gt('>'),
63        Lt('<'),
64        Or('|'),
65        Question('?'),
66        Rem('%'),
67        Star('*'),
68        Sub('-'),
69        Dollar('$'),
70        Tilde('~'),
71    ];
72
73    use PUNCT;
74
75    impl<S: MaybeSpan> sealed::IntoTokenTree for PUNCT<S> {}
76    impl<S: MaybeSpan> IntoTokenTree for PUNCT<S> {
77        crate::impl_into_token_tree!(|self| pm::TokenTree::Punct(
78            (
79                pm::Punct::new(PUNCT::<NoSpan>::CHAR, pm::Spacing::Alone),
80                self.0
81            )
82                .into_st()
83        ));
84    }
85
86    impl<S: MaybeSpan> sealed::ToTokenTree for PUNCT<S> {}
87    impl<S: MaybeSpan> ToTokenTree for PUNCT<S> {
88        crate::impl_to_token_tree! {copy}
89    }
90
91    impl<S: MaybeSpan> sealed::IntoTokens for PUNCT<S> {}
92    impl<S: MaybeSpan> IntoTokens for PUNCT<S> {
93        crate::impl_into_tokens! {tt}
94    }
95
96    impl<S: MaybeSpan> sealed::ToTokens for PUNCT<S> {}
97    impl<S: MaybeSpan> ToTokens for PUNCT<S> {
98        crate::impl_to_tokens! {copy}
99    }
100
101    impl<SO: MaybeSpan> sealed::WithSpan for PUNCT<SO> {}
102    impl<SO: MaybeSpan> WithSpan for PUNCT<SO> {
103        type WithDefaultSpan<S: crate::Span> = PUNCT<SO::WithDefaultSpan<S>>;
104
105        fn with_default_span<S: crate::Span>(self, span: S) -> Self::WithDefaultSpan<S> {
106            PUNCT(self.0.with_default_span(span))
107        }
108
109        type WithReplacedSpan<S: crate::Span> = PUNCT<SO::WithReplacedSpan<S>>;
110
111        fn with_replaced_span<S: crate::Span>(self, span: S) -> Self::WithReplacedSpan<S> {
112            PUNCT(self.0.with_replaced_span(span))
113        }
114    }
115
116    impl<SO: MaybeSpan> sealed::RefWithSpan for PUNCT<SO> {}
117    impl<SO: MaybeSpan> RefWithSpan for PUNCT<SO> {
118        crate::impl_ref_with_span! {copy}
119    }
120});