David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 1 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2 | use delimited::Delimited; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_struct! { |
| 5 | /// Struct or enum sent to a `proc_macro_derive` macro. |
| 6 | pub struct DeriveInput { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame^] | 7 | /// Attributes tagged on the whole struct or enum. |
| 8 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 9 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 10 | /// Visibility of the struct or enum. |
| 11 | pub vis: Visibility, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 12 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame^] | 13 | /// Name of the struct or enum. |
| 14 | pub ident: Ident, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 15 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 16 | /// Generics required to complete the definition. |
| 17 | pub generics: Generics, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 18 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | /// Data within the struct or enum. |
| 20 | pub body: Body, |
| 21 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 24 | ast_enum_of_structs! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 25 | /// Body of a derived struct or enum. |
| 26 | pub enum Body { |
| 27 | /// It's an enum. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 28 | pub Enum(BodyEnum { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 29 | pub enum_token: Token![enum], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 30 | pub brace_token: token::Brace, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 31 | pub variants: Delimited<Variant, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 32 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 33 | |
| 34 | /// It's a struct. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 35 | pub Struct(BodyStruct { |
| 36 | pub data: VariantData, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 37 | pub struct_token: Token![struct], |
| 38 | pub semi_token: Option<Token![;]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 39 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 41 | |
| 42 | do_not_generate_to_tokens |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #[cfg(feature = "parsing")] |
| 46 | pub mod parsing { |
| 47 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 48 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 49 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 50 | |
| 51 | impl Synom for DeriveInput { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 52 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 53 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 54 | vis: syn!(Visibility) >> |
| 55 | which: alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 56 | keyword!(struct) => { Ok } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 57 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 58 | keyword!(enum) => { Err } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 59 | ) >> |
| 60 | id: syn!(Ident) >> |
| 61 | generics: syn!(Generics) >> |
| 62 | item: switch!(value!(which), |
| 63 | Ok(s) => map!(struct_body, move |(wh, body, semi)| DeriveInput { |
| 64 | ident: id, |
| 65 | vis: vis, |
| 66 | attrs: attrs, |
| 67 | generics: Generics { |
| 68 | where_clause: wh, |
| 69 | .. generics |
| 70 | }, |
| 71 | body: Body::Struct(BodyStruct { |
| 72 | struct_token: s, |
| 73 | data: body, |
| 74 | semi_token: semi, |
| 75 | }), |
| 76 | }) |
| 77 | | |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 78 | Err(e) => map!(enum_body, move |(wh, body, brace)| DeriveInput { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 79 | ident: id, |
| 80 | vis: vis, |
| 81 | attrs: attrs, |
| 82 | generics: Generics { |
| 83 | where_clause: wh, |
| 84 | .. generics |
| 85 | }, |
| 86 | body: Body::Enum(BodyEnum { |
| 87 | variants: body, |
| 88 | brace_token: brace, |
| 89 | enum_token: e, |
| 90 | }), |
| 91 | }) |
| 92 | ) >> |
| 93 | (item) |
| 94 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 95 | |
| 96 | fn description() -> Option<&'static str> { |
| 97 | Some("derive input") |
| 98 | } |
| 99 | } |
| 100 | |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 101 | named!(struct_body -> (Option<WhereClause>, VariantData, Option<Token![;]>), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 102 | do_parse!( |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 103 | wh: option!(syn!(WhereClause)) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 104 | body: struct_like_body >> |
| 105 | (wh, VariantData::Struct(body.0, body.1), None) |
| 106 | ) |
| 107 | | |
| 108 | do_parse!( |
| 109 | body: tuple_like_body >> |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 110 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 111 | semi: punct!(;) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 112 | (wh, VariantData::Tuple(body.0, body.1), Some(semi)) |
| 113 | ) |
| 114 | | |
| 115 | do_parse!( |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 116 | wh: option!(syn!(WhereClause)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 117 | semi: punct!(;) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 118 | (wh, VariantData::Unit, Some(semi)) |
| 119 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 120 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 121 | |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 122 | named!(enum_body -> (Option<WhereClause>, Delimited<Variant, Token![,]>, token::Brace), do_parse!( |
| 123 | wh: option!(syn!(WhereClause)) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 124 | data: braces!(Delimited::parse_terminated) >> |
| 125 | (wh, data.0, data.1) |
| 126 | )); |
| 127 | |
| 128 | impl Synom for Variant { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 129 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 130 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 131 | id: syn!(Ident) >> |
| 132 | data: alt!( |
| 133 | struct_like_body => { |(d, b)| VariantData::Struct(d, b) } |
| 134 | | |
| 135 | tuple_like_body => { |(d, b)| VariantData::Tuple(d, b) } |
| 136 | | |
| 137 | epsilon!() => { |_| VariantData::Unit } |
| 138 | ) >> |
| 139 | disr: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 140 | eq: punct!(=) >> |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame] | 141 | disr: syn!(Expr) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 142 | (eq, disr) |
| 143 | )) >> |
| 144 | (Variant { |
| 145 | ident: id, |
| 146 | attrs: attrs, |
| 147 | data: data, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 148 | eq_token: disr.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 149 | discriminant: disr.map(|p| p.1), |
| 150 | }) |
| 151 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 152 | } |
| 153 | |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 154 | named!(struct_like_body -> (Delimited<Field, Token![,]>, token::Brace), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 155 | braces!(call!(Delimited::parse_terminated_with, Field::parse_struct))); |
| 156 | |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 157 | named!(tuple_like_body -> (Delimited<Field, Token![,]>, token::Paren), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 158 | parens!(call!(Delimited::parse_terminated_with, Field::parse_tuple))); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 159 | } |
| 160 | |
David Tolnay | c2dfbf4 | 2016-09-23 23:52:15 -0700 | [diff] [blame] | 161 | #[cfg(feature = "printing")] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 162 | mod printing { |
| 163 | use super::*; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 164 | use attr::FilterAttrs; |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 165 | use data::VariantData; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 166 | use quote::{ToTokens, Tokens}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 167 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 168 | impl ToTokens for DeriveInput { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 169 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 170 | for attr in self.attrs.outer() { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 171 | attr.to_tokens(tokens); |
| 172 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 173 | self.vis.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 174 | match self.body { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 175 | Body::Enum(ref d) => d.enum_token.to_tokens(tokens), |
| 176 | Body::Struct(ref d) => d.struct_token.to_tokens(tokens), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 177 | } |
| 178 | self.ident.to_tokens(tokens); |
| 179 | self.generics.to_tokens(tokens); |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 180 | match self.body { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 181 | Body::Enum(ref data) => { |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 182 | self.generics.where_clause.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | data.brace_token.surround(tokens, |tokens| { |
| 184 | data.variants.to_tokens(tokens); |
| 185 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 186 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 187 | Body::Struct(ref data) => { |
| 188 | match data.data { |
| 189 | VariantData::Struct(..) => { |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 190 | self.generics.where_clause.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 191 | data.data.to_tokens(tokens); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 192 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 193 | VariantData::Tuple(..) => { |
| 194 | data.data.to_tokens(tokens); |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 195 | self.generics.where_clause.to_tokens(tokens); |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 196 | } |
| 197 | VariantData::Unit => { |
| 198 | self.generics.where_clause.to_tokens(tokens); |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 199 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 200 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 201 | data.semi_token.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |