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