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
use super::*;
use delimited::Delimited;

ast_struct! {
    /// An enum variant.
    pub struct Variant {
        /// Name of the variant.
        pub ident: Ident,

        /// Attributes tagged on the variant.
        pub attrs: Vec<Attribute>,

        /// Type of variant.
        pub data: VariantData,

        /// Explicit discriminant, e.g. `Foo = 1`
        pub discriminant: Option<Expr>,

        pub eq_token: Option<tokens::Eq>,
    }
}

ast_enum! {
    /// Data stored within an enum variant or struct.
    pub enum VariantData {
        /// Struct variant, e.g. `Point { x: f64, y: f64 }`.
        Struct(Delimited<Field, tokens::Comma>, tokens::Brace),

        /// Tuple variant, e.g. `Some(T)`.
        Tuple(Delimited<Field, tokens::Comma>, tokens::Paren),

        /// Unit variant, e.g. `None`.
        Unit,
    }
}

impl VariantData {
    // TODO: expose this?
    // /// Slice containing the fields stored in the variant.
    // pub fn fields(&self) -> &Delimited<Field, tokens::Comma> {
    //     match *self {
    //         VariantData::Struct(ref fields, _) |
    //         VariantData::Tuple(ref fields, _) => fields,
    //         VariantData::Unit => &[],
    //     }
    // }
    //
    // /// Mutable slice containing the fields stored in the variant.
    // pub fn fields_mut(&mut self) -> &mut Delimited<Field, tokens::Comma> {
    //     match *self {
    //         VariantData::Struct(ref mut fields, _) |
    //         VariantData::Tuple(ref mut fields, _) => fields,
    //         VariantData::Unit => &mut [],
    //     }
    // }
}

ast_struct! {
    /// A field of a struct or enum variant.
    pub struct Field {
        /// Name of the field, if any.
        ///
        /// Fields of tuple structs have no names.
        pub ident: Option<Ident>,

        /// Visibility of the field.
        pub vis: Visibility,

        /// Attributes tagged on the field.
        pub attrs: Vec<Attribute>,

        /// Type of the field.
        pub ty: Ty,

        pub colon_token: Option<tokens::Colon>,
    }
}

ast_enum_of_structs! {
    /// Visibility level of an item.
    pub enum Visibility {
        /// Public, i.e. `pub`.
        pub Public(VisPublic {
            pub pub_token: tokens::Pub,
        }),

        /// Crate-visible, i.e. `pub(crate)`.
        pub Crate(VisCrate {
            pub pub_token: tokens::Pub,
            pub paren_token: tokens::Paren,
            pub crate_token: tokens::Crate,
        }),

        /// Restricted, e.g. `pub(self)` or `pub(super)` or `pub(in some::module)`.
        pub Restricted(VisRestricted {
            pub pub_token: tokens::Pub,
            pub paren_token: tokens::Paren,
            pub in_token: Option<tokens::In>,
            pub path: Box<Path>,
        }),

        /// Inherited, i.e. private.
        pub Inherited(VisInherited {}),
    }
}

#[cfg(feature = "parsing")]
pub mod parsing {
    use super::*;

    use synom::Synom;
    use synom::tokens;
    use synom::tokens::*;

    impl Field {
        named!(pub parse_struct -> Self, do_parse!(
            attrs: many0!(call!(Attribute::parse_outer)) >>
            vis: syn!(Visibility) >>
            id: syn!(Ident) >>
            colon: syn!(Colon) >>
            ty: syn!(Ty) >>
            (Field {
                ident: Some(id),
                vis: vis,
                attrs: attrs,
                ty: ty,
                colon_token: Some(colon),
            })
        ));

        named!(pub parse_tuple -> Self, do_parse!(
            attrs: many0!(call!(Attribute::parse_outer)) >>
            vis: syn!(Visibility) >>
            ty: syn!(Ty) >>
            (Field {
                ident: None,
                colon_token: None,
                vis: vis,
                attrs: attrs,
                ty: ty,
            })
        ));
    }

    impl Synom for Visibility {
        named!(parse -> Self, alt!(
            do_parse!(
                pub_token: syn!(Pub) >>
                other: parens!(syn!(tokens::Crate)) >>
                (Visibility::Crate(VisCrate {
                    crate_token: other.0,
                    paren_token: other.1,
                    pub_token: pub_token,
                }))
            )
            |
            do_parse!(
                pub_token: syn!(Pub) >>
                other: parens!(syn!(Self_)) >>
                (Visibility::Restricted(VisRestricted {
                    path: Box::new(other.0.into()),
                    in_token: None,
                    paren_token: other.1,
                    pub_token: pub_token,
                }))
            )
            |
            do_parse!(
                pub_token: syn!(Pub) >>
                other: parens!(syn!(Super)) >>
                (Visibility::Restricted(VisRestricted {
                    path: Box::new(other.0.into()),
                    in_token: None,
                    paren_token: other.1,
                    pub_token: pub_token,
                }))
            )
            |
            do_parse!(
                pub_token: syn!(Pub) >>
                other: parens!(do_parse!(
                    in_tok: syn!(In) >>
                    restricted: call!(Path::parse_mod_style) >>
                    (in_tok, restricted)
                )) >>
                (Visibility::Restricted(VisRestricted {
                    path: Box::new((other.0).1),
                    in_token: Some((other.0).0),
                    paren_token: other.1,
                    pub_token: pub_token,
                }))
            )
            |
            syn!(Pub) => { |tok| {
                Visibility::Public(VisPublic {
                    pub_token: tok,
                })
            } }
            |
            epsilon!() => { |_| Visibility::Inherited(VisInherited {}) }
        ));
    }
}

#[cfg(feature = "printing")]
mod printing {
    use super::*;
    use quote::{Tokens, ToTokens};

    impl ToTokens for Variant {
        fn to_tokens(&self, tokens: &mut Tokens) {
            tokens.append_all(&self.attrs);
            self.ident.to_tokens(tokens);
            self.data.to_tokens(tokens);
            if let Some(ref disc) = self.discriminant {
                TokensOrDefault(&self.eq_token).to_tokens(tokens);
                disc.to_tokens(tokens);
            }
        }
    }

    impl ToTokens for VariantData {
        fn to_tokens(&self, tokens: &mut Tokens) {
            match *self {
                VariantData::Struct(ref fields, ref brace) => {
                    brace.surround(tokens, |tokens| {
                        fields.to_tokens(tokens);
                    });
                }
                VariantData::Tuple(ref fields, ref paren) => {
                    paren.surround(tokens, |tokens| {
                        fields.to_tokens(tokens);
                    });
                }
                VariantData::Unit => {}
            }
        }
    }

    impl ToTokens for Field {
        fn to_tokens(&self, tokens: &mut Tokens) {
            tokens.append_all(&self.attrs);
            self.vis.to_tokens(tokens);
            if let Some(ref ident) = self.ident {
                ident.to_tokens(tokens);
                TokensOrDefault(&self.colon_token).to_tokens(tokens);
            }
            self.ty.to_tokens(tokens);
        }
    }

    impl ToTokens for VisPublic {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.pub_token.to_tokens(tokens)
        }
    }

    impl ToTokens for VisCrate {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.pub_token.to_tokens(tokens);
            self.paren_token.surround(tokens, |tokens| {
                self.crate_token.to_tokens(tokens);
            })
        }
    }

    impl ToTokens for VisRestricted {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.pub_token.to_tokens(tokens);
            self.paren_token.surround(tokens, |tokens| {
                // XXX: If we have a path which is not "self" or "super",
                // automatically add the "in" token.
                self.in_token.to_tokens(tokens);
                self.path.to_tokens(tokens);
            });
        }
    }

    impl ToTokens for VisInherited {
        fn to_tokens(&self, _tokens: &mut Tokens) {
        }
    }
}