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