summaryrefslogtreecommitdiffstats
path: root/vendor/time-macros/src/quote.rs
blob: 4d3dcbca030b9dfdec7a73cf5d8edd99b812127a (plain)
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
macro_rules! quote {
    () => (::proc_macro::TokenStream::new());
    ($($x:tt)*) => {{
        let mut ts = ::proc_macro::TokenStream::new();
        let ts_mut = &mut ts;
        quote_inner!(ts_mut $($x)*);
        ts
    }};
}

#[cfg(any(feature = "formatting", feature = "parsing"))]
macro_rules! quote_append {
    ($ts:ident $($x:tt)*) => {{
        quote_inner!($ts $($x)*);
    }};
}

macro_rules! quote_group {
    ({ $($x:tt)* }) => {
        ::proc_macro::TokenTree::Group(::proc_macro::Group::new(
            ::proc_macro::Delimiter::Brace,
            quote!($($x)*)
        ))
    };
}

macro_rules! sym {
    ($ts:ident $x:tt $y:tt) => {
        $ts.extend([
            ::proc_macro::TokenTree::from(::proc_macro::Punct::new(
                $x,
                ::proc_macro::Spacing::Joint,
            )),
            ::proc_macro::TokenTree::from(::proc_macro::Punct::new(
                $y,
                ::proc_macro::Spacing::Alone,
            )),
        ]);
    };
    ($ts:ident $x:tt) => {
        $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Punct::new(
            $x,
            ::proc_macro::Spacing::Alone,
        ))]);
    };
}

macro_rules! quote_inner {
    // Base case
    ($ts:ident) => {};

    // Single or double symbols
    ($ts:ident :: $($tail:tt)*) => { sym!($ts ':' ':'); quote_inner!($ts $($tail)*); };
    ($ts:ident .. $($tail:tt)*) => { sym!($ts '.' '.'); quote_inner!($ts $($tail)*); };
    ($ts:ident : $($tail:tt)*) => { sym!($ts ':'); quote_inner!($ts $($tail)*); };
    ($ts:ident = $($tail:tt)*) => { sym!($ts '='); quote_inner!($ts $($tail)*); };
    ($ts:ident ; $($tail:tt)*) => { sym!($ts ';'); quote_inner!($ts $($tail)*); };
    ($ts:ident , $($tail:tt)*) => { sym!($ts ','); quote_inner!($ts $($tail)*); };
    ($ts:ident . $($tail:tt)*) => { sym!($ts '.'); quote_inner!($ts $($tail)*); };
    ($ts:ident & $($tail:tt)*) => { sym!($ts '&'); quote_inner!($ts $($tail)*); };
    ($ts:ident << $($tail:tt)*) => { sym!($ts '<' '<'); quote_inner!($ts $($tail)*); };
    ($ts:ident < $($tail:tt)*) => { sym!($ts '<'); quote_inner!($ts $($tail)*); };
    ($ts:ident >> $($tail:tt)*) => { sym!($ts '>' '>'); quote_inner!($ts $($tail)*); };
    ($ts:ident > $($tail:tt)*) => { sym!($ts '>'); quote_inner!($ts $($tail)*); };
    ($ts:ident -> $($tail:tt)*) => { sym!($ts '-' '>'); quote_inner!($ts $($tail)*); };
    ($ts:ident ? $($tail:tt)*) => { sym!($ts '?'); quote_inner!($ts $($tail)*); };
    ($ts:ident ! $($tail:tt)*) => { sym!($ts '!'); quote_inner!($ts $($tail)*); };
    ($ts:ident | $($tail:tt)*) => { sym!($ts '|'); quote_inner!($ts $($tail)*); };
    ($ts:ident * $($tail:tt)*) => { sym!($ts '*'); quote_inner!($ts $($tail)*); };
    ($ts:ident + $($tail:tt)*) => { sym!($ts '+'); quote_inner!($ts $($tail)*); };

    // Identifier
    ($ts:ident $i:ident $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Ident::new(
            &stringify!($i),
            ::proc_macro::Span::mixed_site(),
        ))]);
        quote_inner!($ts $($tail)*);
    };

    // Literal
    ($ts:ident 0 $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Literal::usize_unsuffixed(0))]);
        quote_inner!($ts $($tail)*);
    };
    ($ts:ident $l:literal $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Literal::string(&$l))]);
        quote_inner!($ts $($tail)*);
    };

    // Lifetime
    ($ts:ident $l:lifetime $($tail:tt)*) => {
        $ts.extend([
            ::proc_macro::TokenTree::from(
                ::proc_macro::Punct::new('\'', ::proc_macro::Spacing::Joint)
            ),
            ::proc_macro::TokenTree::from(::proc_macro::Ident::new(
                stringify!($l).trim_start_matches(|c| c == '\''),
                ::proc_macro::Span::mixed_site(),
            )),
        ]);
        quote_inner!($ts $($tail)*);
    };

    // Groups
    ($ts:ident ($($inner:tt)*) $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
            ::proc_macro::Delimiter::Parenthesis,
            quote!($($inner)*)
        ))]);
        quote_inner!($ts $($tail)*);
    };
    ($ts:ident [$($inner:tt)*] $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
            ::proc_macro::Delimiter::Bracket,
            quote!($($inner)*)
        ))]);
        quote_inner!($ts $($tail)*);
    };
    ($ts:ident {$($inner:tt)*} $($tail:tt)*) => {
        $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
            ::proc_macro::Delimiter::Brace,
            quote!($($inner)*)
        ))]);
        quote_inner!($ts $($tail)*);
    };

    // Interpolated values
    // TokenTree by default
    ($ts:ident #($e:expr) $($tail:tt)*) => {
        $ts.extend([$crate::to_tokens::ToTokenTree::into_token_tree($e)]);
        quote_inner!($ts $($tail)*);
    };
    // Allow a TokenStream by request. It's more expensive, so avoid if possible.
    ($ts:ident #S($e:expr) $($tail:tt)*) => {
        $crate::to_tokens::ToTokenStream::append_to($e, $ts);
        quote_inner!($ts $($tail)*);
    };
}