David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 9 | use super::*; |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 10 | use punctuated::{Iter, Punctuated}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 11 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 12 | ast_struct! { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 13 | /// Data structure sent to a `proc_macro_derive` macro. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 14 | /// |
| 15 | /// *This type is available if Syn is built with the `"derive"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 16 | pub struct DeriveInput { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 17 | /// Attributes tagged on the whole struct or enum. |
| 18 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 19 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 20 | /// Visibility of the struct or enum. |
| 21 | pub vis: Visibility, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 22 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 23 | /// Name of the struct or enum. |
| 24 | pub ident: Ident, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 25 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 26 | /// Generics required to complete the definition. |
| 27 | pub generics: Generics, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 28 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 29 | /// Data within the struct or enum. |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 30 | pub data: Data, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 31 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 34 | ast_enum_of_structs! { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 35 | /// The storage of a struct, enum or union data structure. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 36 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 37 | /// *This type is available if Syn is built with the `"derive"` feature.* |
| 38 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 39 | /// # Syntax tree enum |
| 40 | /// |
| 41 | /// This type is a [syntax tree enum]. |
| 42 | /// |
| 43 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 44 | pub enum Data { |
David Tolnay | d2aa883 | 2018-01-07 01:05:09 -0800 | [diff] [blame] | 45 | /// A struct input to a `proc_macro_derive` macro. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 46 | /// |
| 47 | /// *This type is available if Syn is built with the `"derive"` |
| 48 | /// feature.* |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 49 | pub Struct(DataStruct { |
| 50 | pub struct_token: Token![struct], |
| 51 | pub fields: Fields, |
| 52 | pub semi_token: Option<Token![;]>, |
| 53 | }), |
| 54 | |
David Tolnay | d2aa883 | 2018-01-07 01:05:09 -0800 | [diff] [blame] | 55 | /// An enum input to a `proc_macro_derive` macro. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 56 | /// |
| 57 | /// *This type is available if Syn is built with the `"derive"` |
| 58 | /// feature.* |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 59 | pub Enum(DataEnum { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 60 | pub enum_token: Token![enum], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 61 | pub brace_token: token::Brace, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 62 | pub variants: Punctuated<Variant, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 63 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 64 | |
David Tolnay | d2aa883 | 2018-01-07 01:05:09 -0800 | [diff] [blame] | 65 | /// A tagged union input to a `proc_macro_derive` macro. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 66 | /// |
| 67 | /// *This type is available if Syn is built with the `"derive"` |
| 68 | /// feature.* |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 69 | pub Union(DataUnion { |
| 70 | pub union_token: Token![union], |
| 71 | pub fields: FieldsNamed, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 72 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 73 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | |
| 75 | do_not_generate_to_tokens |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 78 | impl Fields { |
| 79 | /// Returns an iterator over the fields |
Sean Griffin | c8ef484 | 2018-01-16 10:39:41 -0700 | [diff] [blame] | 80 | pub fn iter(&self) -> Iter<Field, Token![,]> { |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 81 | match *self { |
Sean Griffin | c8ef484 | 2018-01-16 10:39:41 -0700 | [diff] [blame] | 82 | Fields::Unit => Iter::empty(), |
| 83 | Fields::Named(FieldsNamed { ref named, .. }) => named.iter(), |
| 84 | Fields::Unnamed(FieldsUnnamed { ref unnamed, .. }) => unnamed.iter(), |
Sean Griffin | ca93a77 | 2018-01-15 13:33:17 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 87 | } |
Sean Griffin | ca93a77 | 2018-01-15 13:33:17 -0700 | [diff] [blame] | 88 | |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 89 | impl<'a> IntoIterator for &'a Fields { |
| 90 | type Item = &'a Field; |
Sean Griffin | c8ef484 | 2018-01-16 10:39:41 -0700 | [diff] [blame] | 91 | type IntoIter = Iter<'a, Field, Token![,]>; |
Sean Griffin | dadd0b3 | 2018-01-16 10:35:40 -0700 | [diff] [blame] | 92 | |
| 93 | fn into_iter(self) -> Self::IntoIter { |
| 94 | self.iter() |
Sean Griffin | ca93a77 | 2018-01-15 13:33:17 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 98 | #[cfg(feature = "parsing")] |
| 99 | pub mod parsing { |
| 100 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 101 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 102 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 103 | |
| 104 | impl Synom for DeriveInput { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 105 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 106 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 107 | vis: syn!(Visibility) >> |
| 108 | which: alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 109 | keyword!(struct) => { Ok } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 110 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 111 | keyword!(enum) => { Err } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 112 | ) >> |
| 113 | id: syn!(Ident) >> |
| 114 | generics: syn!(Generics) >> |
| 115 | item: switch!(value!(which), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 116 | Ok(s) => map!(data_struct, move |(wh, fields, semi)| DeriveInput { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 117 | ident: id, |
| 118 | vis: vis, |
| 119 | attrs: attrs, |
| 120 | generics: Generics { |
| 121 | where_clause: wh, |
| 122 | .. generics |
| 123 | }, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 124 | data: Data::Struct(DataStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 125 | struct_token: s, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 126 | fields: fields, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 127 | semi_token: semi, |
| 128 | }), |
| 129 | }) |
| 130 | | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 131 | Err(e) => map!(data_enum, move |(wh, brace, variants)| DeriveInput { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 132 | ident: id, |
| 133 | vis: vis, |
| 134 | attrs: attrs, |
| 135 | generics: Generics { |
| 136 | where_clause: wh, |
| 137 | .. generics |
| 138 | }, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 139 | data: Data::Enum(DataEnum { |
| 140 | variants: variants, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 141 | brace_token: brace, |
| 142 | enum_token: e, |
| 143 | }), |
| 144 | }) |
| 145 | ) >> |
| 146 | (item) |
| 147 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 148 | |
| 149 | fn description() -> Option<&'static str> { |
| 150 | Some("derive input") |
| 151 | } |
| 152 | } |
| 153 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 154 | named!(data_struct -> (Option<WhereClause>, Fields, Option<Token![;]>), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 155 | do_parse!( |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 156 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 157 | fields: syn!(FieldsNamed) >> |
| 158 | (wh, Fields::Named(fields), None) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 159 | ) |
| 160 | | |
| 161 | do_parse!( |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 162 | fields: syn!(FieldsUnnamed) >> |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 163 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 164 | semi: punct!(;) >> |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 165 | (wh, Fields::Unnamed(fields), Some(semi)) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 166 | ) |
| 167 | | |
| 168 | do_parse!( |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 169 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 170 | semi: punct!(;) >> |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 171 | (wh, Fields::Unit, Some(semi)) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 172 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 173 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 174 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 175 | named!(data_enum -> (Option<WhereClause>, token::Brace, Punctuated<Variant, Token![,]>), do_parse!( |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 176 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 177 | data: braces!(Punctuated::parse_terminated) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 178 | (wh, data.0, data.1) |
| 179 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 180 | } |
| 181 | |
David Tolnay | c2dfbf4 | 2016-09-23 23:52:15 -0700 | [diff] [blame] | 182 | #[cfg(feature = "printing")] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 183 | mod printing { |
| 184 | use super::*; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 185 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 186 | use quote::{ToTokens, Tokens}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 187 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 188 | impl ToTokens for DeriveInput { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 189 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 190 | for attr in self.attrs.outer() { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 191 | attr.to_tokens(tokens); |
| 192 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 193 | self.vis.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 194 | match self.data { |
| 195 | Data::Struct(ref d) => d.struct_token.to_tokens(tokens), |
| 196 | Data::Enum(ref d) => d.enum_token.to_tokens(tokens), |
| 197 | Data::Union(ref d) => d.union_token.to_tokens(tokens), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 198 | } |
| 199 | self.ident.to_tokens(tokens); |
| 200 | self.generics.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 201 | match self.data { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 202 | Data::Struct(ref data) => match data.fields { |
| 203 | Fields::Named(ref fields) => { |
| 204 | self.generics.where_clause.to_tokens(tokens); |
| 205 | fields.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 206 | } |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 207 | Fields::Unnamed(ref fields) => { |
| 208 | fields.to_tokens(tokens); |
| 209 | self.generics.where_clause.to_tokens(tokens); |
| 210 | TokensOrDefault(&data.semi_token).to_tokens(tokens); |
| 211 | } |
| 212 | Fields::Unit => { |
| 213 | self.generics.where_clause.to_tokens(tokens); |
| 214 | TokensOrDefault(&data.semi_token).to_tokens(tokens); |
| 215 | } |
| 216 | }, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 217 | Data::Enum(ref data) => { |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 218 | self.generics.where_clause.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 219 | data.brace_token.surround(tokens, |tokens| { |
| 220 | data.variants.to_tokens(tokens); |
| 221 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 222 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 223 | Data::Union(ref data) => { |
| 224 | self.generics.where_clause.to_tokens(tokens); |
| 225 | data.fields.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |