rune_macros/quote/
inner.rs

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
use proc_macro2 as p;

pub(crate) const S: Punct = Punct::new("::");
pub(crate) const MACROS: RuneModule = RuneModule("macros");
pub(crate) const AST: RuneModule = RuneModule("ast");

use crate::RUNE;
pub(crate) trait ToTokens {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span);
}

impl ToTokens for &'static str {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        stream.extend(Some(p::TokenTree::Ident(p::Ident::new(self, span))))
    }
}

impl ToTokens for char {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        let mut p = p::Punct::new(self, p::Spacing::Alone);
        p.set_span(span);
        stream.extend(Some(p::TokenTree::Punct(p)));
    }
}

impl ToTokens for p::Literal {
    fn to_tokens(mut self, stream: &mut p::TokenStream, span: p::Span) {
        self.set_span(span);
        stream.extend(Some(p::TokenTree::Literal(self)));
    }
}

macro_rules! impl_tuple {
    () => {};

    ($f_ident:ident $f_var:ident, $($ident:ident $var:ident),* $(,)?) => {
        impl<$f_ident, $( $ident,)*> ToTokens for ($f_ident, $($ident,)*)
        where
            $f_ident: ToTokens,
            $($ident: ToTokens,)*
        {
            fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
                let ($f_var, $($var,)*) = self;
                $f_var.to_tokens(stream, span);
                $($var.to_tokens(stream, span);)*
            }
        }

        impl_tuple!($($ident $var,)*);
    }
}

impl ToTokens for () {
    fn to_tokens(self, _: &mut p::TokenStream, _: p::Span) {}
}

impl_tuple!(A a, B b, C c, D d, E e, F f, G g, H h);

impl ToTokens for p::Ident {
    fn to_tokens(self, stream: &mut p::TokenStream, _: p::Span) {
        stream.extend(std::iter::once(p::TokenTree::Ident(self)));
    }
}

impl ToTokens for p::TokenStream {
    fn to_tokens(self, stream: &mut p::TokenStream, _: p::Span) {
        stream.extend(self);
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct RuneModule(&'static str);

impl ToTokens for RuneModule {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (RUNE, S, self.0).to_tokens(stream, span);
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct ToTokensFn;

impl ToTokens for ToTokensFn {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (MACROS, S, "ToTokens", S, "to_tokens").to_tokens(stream, span);
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct Kind(pub(crate) &'static str);

impl ToTokens for Kind {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (AST, S, "Kind", S, self.0).to_tokens(stream, span);
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct Delimiter(pub(crate) &'static str);

impl Delimiter {
    /// Convert from a proc macro.
    pub(crate) fn from_proc_macro(d: p::Delimiter) -> Option<Self> {
        match d {
            p::Delimiter::Parenthesis => Some(Delimiter("Parenthesis")),
            p::Delimiter::Brace => Some(Delimiter("Brace")),
            p::Delimiter::Bracket => Some(Delimiter("Bracket")),
            p::Delimiter::None => None,
        }
    }
}

impl ToTokens for Delimiter {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (AST, S, "Delimiter", S).to_tokens(stream, span);
        self.0.to_tokens(stream, span);
    }
}

/// Construct a joined punctuation out of the given string.
#[derive(Clone, Copy)]
pub(crate) struct Punct(&'static str, p::Spacing);

impl Punct {
    pub(crate) const fn new(s: &'static str) -> Punct {
        Punct(s, p::Spacing::Alone)
    }
}

impl ToTokens for Punct {
    fn to_tokens(self, stream: &mut proc_macro2::TokenStream, span: p::Span) {
        let mut it = self.0.chars();
        let last = it.next_back();

        for c in it {
            let mut p = p::Punct::new(c, p::Spacing::Joint);
            p.set_span(span);
            stream.extend(Some(p::TokenTree::Punct(p)));
        }

        if let Some(c) = last {
            let mut p = p::Punct::new(c, self.1);
            p.set_span(span);
            stream.extend(Some(p::TokenTree::Punct(p)));
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Group<T>(p::Delimiter, T);

/// `(T)`.
pub(crate) fn p<T>(inner: T) -> Group<T> {
    Group(p::Delimiter::Parenthesis, inner)
}

/// `{T}`.
pub(crate) fn braced<T>(inner: T) -> Group<T> {
    Group(p::Delimiter::Brace, inner)
}

impl<T> ToTokens for Group<T>
where
    T: ToTokens,
{
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        let mut inner = p::TokenStream::new();
        self.1.to_tokens(&mut inner, span);

        let mut group = p::Group::new(self.0, inner);
        group.set_span(span);
        stream.extend(Some(p::TokenTree::Group(group)));
    }
}

/// An identifier constructor.
pub(crate) struct NewIdent<'a>(pub(crate) &'static str, pub(crate) &'a str);

impl ToTokens for NewIdent<'_> {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (self.0, '.', "ident", p(p::Literal::string(self.1)), '?').to_tokens(stream, span);
    }
}

/// An identifier constructor.
pub(crate) struct NewLit(pub(crate) &'static str, pub(crate) p::Literal);

impl ToTokens for NewLit {
    fn to_tokens(self, stream: &mut p::TokenStream, span: p::Span) {
        (self.0, '.', "lit", p(self.1), '?').to_tokens(stream, span);
    }
}