rune_macros/
parse.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::{
    add_trait_bounds,
    context::{Context, ParseKind, Tokens},
};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned as _;

/// Derive implementation of the Parse macro.
pub struct Derive {
    input: syn::DeriveInput,
}

impl syn::parse::Parse for Derive {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        Ok(Self {
            input: input.parse()?,
        })
    }
}

impl Derive {
    pub(super) fn expand(self, cx: &Context) -> Result<TokenStream, ()> {
        let attr = cx.type_attrs(&self.input.attrs);
        let tokens = cx.tokens_with_module(attr.module.as_ref());

        let mut expander = Expander { cx, tokens };

        match &self.input.data {
            syn::Data::Struct(st) => expander.expand_struct(&self.input, st),
            syn::Data::Enum(en) => {
                expander.cx.error(syn::Error::new_spanned(
                    en.enum_token,
                    "not supported on enums",
                ));

                Err(())
            }
            syn::Data::Union(un) => {
                expander.cx.error(syn::Error::new_spanned(
                    un.union_token,
                    "not supported on unions",
                ));

                Err(())
            }
        }
    }
}

struct Expander<'cx> {
    cx: &'cx Context,
    tokens: Tokens,
}

impl Expander<'_> {
    /// Expand on a struct.
    fn expand_struct(
        &mut self,
        input: &syn::DeriveInput,
        st: &syn::DataStruct,
    ) -> Result<TokenStream, ()> {
        self.expand_struct_fields(input, &st.fields)
    }

    /// Expand field decoding.
    fn expand_struct_fields(
        &mut self,
        input: &syn::DeriveInput,
        fields: &syn::Fields,
    ) -> Result<TokenStream, ()> {
        match fields {
            syn::Fields::Named(named) => self.expand_struct_named(input, named),
            syn::Fields::Unnamed(..) => {
                self.cx.error(syn::Error::new_spanned(
                    fields,
                    "Tuple structs are not supported",
                ));
                Err(())
            }
            syn::Fields::Unit => {
                self.cx.error(syn::Error::new_spanned(
                    fields,
                    "Unit structs are not supported",
                ));
                Err(())
            }
        }
    }

    /// Expand named fields.
    fn expand_struct_named(
        &mut self,
        input: &syn::DeriveInput,
        named: &syn::FieldsNamed,
    ) -> Result<TokenStream, ()> {
        let ident = &input.ident;
        let mut fields = Vec::new();

        let mut meta_args = Vec::new();
        let mut meta_parse = Vec::new();
        let mut meta_fields = Vec::new();

        let ty_attrs = self.cx.type_attrs(&input.attrs);
        let mut skipped = 0;

        for (i, field) in named.named.iter().enumerate() {
            let field_attrs = self.cx.field_attrs(&field.attrs);
            let ident = self.cx.field_ident(field)?;

            if field_attrs.id.is_some() {
                fields.push(quote_spanned! { field.span() => #ident: Default::default() });
                skipped += 1;
                continue;
            }

            let parse_impl = if let Some(parse_with) = field_attrs.parse_with {
                quote_spanned!(field.span() => #parse_with(parser)?)
            } else {
                quote_spanned!(field.span() => parser.parse()?)
            };

            if field_attrs.meta.is_none() {
                fields.push(quote_spanned! { field.span() => #ident: #parse_impl });
                continue;
            }

            if i - skipped != meta_fields.len() {
                self.cx.error(syn::Error::new_spanned(
                    field,
                    "The first sequence of fields may have `#[rune(meta)]`, \
                        but field is outside of that sequence.",
                ));
                return Err(());
            }

            let ident = self.cx.field_ident(field)?;
            let ty = &field.ty;
            meta_args.push(quote_spanned!(field.span() => #ident: #ty));
            meta_parse.push(quote_spanned!(field.span() => let #ident: #ty = #parse_impl));
            fields.push(quote_spanned! { field.span() => #ident });
            meta_fields.push(ident);
        }

        let parser_ident = &if fields.is_empty() {
            quote!(_parser)
        } else {
            quote!(parser)
        };

        let parse = &self.tokens.parse;
        let parser = &self.tokens.parser;
        let compile_error = &self.tokens.compile_error;
        let result = &self.tokens.result;

        let mut generics = input.generics.clone();

        add_trait_bounds(&mut generics, parse);

        let (impl_generics, type_generics, where_generics) = generics.split_for_impl();

        let inner = if let ParseKind::MetaOnly = ty_attrs.parse {
            None
        } else {
            Some(quote_spanned! {
                named.span() =>
                #[automatically_derived]
                impl #impl_generics #parse for #ident #type_generics #where_generics {
                    fn parse(parser: &mut #parser<'_>) -> #result<Self, #compile_error> {
                        #(#meta_parse;)*
                        Self::parse_with_meta(parser, #(#meta_fields,)*)
                     }
                }
            })
        };

        let output = if !meta_args.is_empty() {
            quote_spanned! { named.span() =>
                #[automatically_derived]
                impl #ident {
                    #[doc = "Parse #ident and attach the given meta"]
                    pub fn parse_with_meta(#parser_ident: &mut #parser<'_>, #(#meta_args,)*)
                        -> #result<Self, #compile_error>
                    {
                        Ok(Self {
                            #(#fields,)*
                        })
                    }
                }

                #inner
            }
        } else {
            quote_spanned! { named.span() =>
                #[automatically_derived]
                impl #impl_generics #parse for #ident #type_generics #where_generics {
                    fn parse(#parser_ident: &mut #parser<'_>) -> #result<Self, #compile_error> {
                        Ok(Self {
                            #(#fields,)*
                        })
                    }
                }
            }
        };

        Ok(output)
    }
}