blob: a5f7d8cee73b7a5b299ce75b38be445249f1301e [file] [log] [blame]
David Tolnayf38cdf62016-09-23 19:07:09 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayf38cdf62016-09-23 19:07:09 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_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 Charrd22b5702017-03-10 15:24:56 -05009
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// Visibility of the struct or enum.
11 pub vis: Visibility,
Clar Charrd22b5702017-03-10 15:24:56 -050012
Alex Crichton62a0a592017-05-22 13:58:53 -070013 /// Attributes tagged on the whole struct or enum.
14 pub attrs: Vec<Attribute>,
Clar Charrd22b5702017-03-10 15:24:56 -050015
Alex Crichton62a0a592017-05-22 13:58:53 -070016 /// Generics required to complete the definition.
17 pub generics: Generics,
Clar Charrd22b5702017-03-10 15:24:56 -050018
Alex Crichton62a0a592017-05-22 13:58:53 -070019 /// Data within the struct or enum.
20 pub body: Body,
21 }
David Tolnayf38cdf62016-09-23 19:07:09 -070022}
23
Clar Charrd22b5702017-03-10 15:24:56 -050024
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// Body of a derived struct or enum.
27 pub enum Body {
28 /// It's an enum.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 pub Enum(BodyEnum {
30 pub enum_token: tokens::Enum,
31 pub brace_token: tokens::Brace,
32 pub variants: Delimited<Variant, tokens::Comma>,
33 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070034
35 /// It's a struct.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070036 pub Struct(BodyStruct {
37 pub data: VariantData,
38 pub struct_token: tokens::Struct,
39 pub semi_token: Option<tokens::Semi>,
40 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070041 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070042
43 do_not_generate_to_tokens
David Tolnayf38cdf62016-09-23 19:07:09 -070044}
45
46#[cfg(feature = "parsing")]
47pub mod parsing {
48 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -070049
Michael Layzell92639a52017-06-01 00:07:44 -040050 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -070051 use synom::tokens::*;
52
53 impl Synom for DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -040054 named!(parse -> Self, do_parse!(
55 attrs: many0!(call!(Attribute::parse_outer)) >>
56 vis: syn!(Visibility) >>
57 which: alt!(
58 syn!(Struct) => { Ok }
59 |
60 // weird hack to get around exhaustiveness check below
61 syn!(Enum) => { |e| Err((e, 1)) }
62 ) >>
63 id: syn!(Ident) >>
64 generics: syn!(Generics) >>
65 item: switch!(value!(which),
66 Ok(s) => map!(struct_body, move |(wh, body, semi)| DeriveInput {
67 ident: id,
68 vis: vis,
69 attrs: attrs,
70 generics: Generics {
71 where_clause: wh,
72 .. generics
73 },
74 body: Body::Struct(BodyStruct {
75 struct_token: s,
76 data: body,
77 semi_token: semi,
78 }),
79 })
80 |
81 Err((e, 1)) => map!(enum_body, move |(wh, body, brace)| DeriveInput {
82 ident: id,
83 vis: vis,
84 attrs: attrs,
85 generics: Generics {
86 where_clause: wh,
87 .. generics
88 },
89 body: Body::Enum(BodyEnum {
90 variants: body,
91 brace_token: brace,
92 enum_token: e,
93 }),
94 })
95 ) >>
96 (item)
97 ));
Alex Crichton954046c2017-05-30 21:49:42 -070098
99 fn description() -> Option<&'static str> {
100 Some("derive input")
101 }
102 }
103
104
105 named!(struct_body -> (WhereClause, VariantData, Option<tokens::Semi>), alt!(
106 do_parse!(
107 wh: syn!(WhereClause) >>
108 body: struct_like_body >>
109 (wh, VariantData::Struct(body.0, body.1), None)
110 )
111 |
112 do_parse!(
113 body: tuple_like_body >>
114 wh: syn!(WhereClause) >>
115 semi: syn!(Semi) >>
116 (wh, VariantData::Tuple(body.0, body.1), Some(semi))
117 )
118 |
119 do_parse!(
120 wh: syn!(WhereClause) >>
121 semi: syn!(Semi) >>
122 (wh, VariantData::Unit, Some(semi))
123 )
David Tolnayf38cdf62016-09-23 19:07:09 -0700124 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700125
126 named!(enum_body -> (WhereClause, Delimited<Variant, tokens::Comma>, tokens::Brace), do_parse!(
127 wh: syn!(WhereClause) >>
128 data: braces!(Delimited::parse_terminated) >>
129 (wh, data.0, data.1)
130 ));
131
132 impl Synom for Variant {
Michael Layzell92639a52017-06-01 00:07:44 -0400133 named!(parse -> Self, do_parse!(
134 attrs: many0!(call!(Attribute::parse_outer)) >>
135 id: syn!(Ident) >>
136 data: alt!(
137 struct_like_body => { |(d, b)| VariantData::Struct(d, b) }
138 |
139 tuple_like_body => { |(d, b)| VariantData::Tuple(d, b) }
140 |
141 epsilon!() => { |_| VariantData::Unit }
142 ) >>
143 disr: option!(do_parse!(
144 eq: syn!(Eq) >>
145 disr: discriminant >>
146 (eq, disr)
147 )) >>
148 (Variant {
149 ident: id,
150 attrs: attrs,
151 data: data,
152 eq_token: disr.as_ref().map(|p| tokens::Eq((p.0).0)),
153 discriminant: disr.map(|p| p.1),
154 })
155 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700156 }
157
158 #[cfg(not(feature = "full"))]
159 named!(discriminant -> ConstExpr, syn!(ConstExpr));
160
161 #[cfg(feature = "full")]
162 named!(discriminant -> ConstExpr, alt!(
163 terminated!(syn!(ConstExpr), after_discriminant)
164 |
165 terminated!(syn!(Expr), after_discriminant) => { ConstExpr::Other }
166 ));
167
168 #[cfg(feature = "full")]
169 named!(after_discriminant -> (), peek!(alt!(
170 syn!(Comma) => { |_| () }
171 |
172 input_end!() => { |_| () }
173 )));
174
175 named!(struct_like_body -> (Delimited<Field, tokens::Comma>, tokens::Brace),
176 braces!(call!(Delimited::parse_terminated_with, Field::parse_struct)));
177
178 named!(tuple_like_body -> (Delimited<Field, tokens::Comma>, tokens::Paren),
179 parens!(call!(Delimited::parse_terminated_with, Field::parse_tuple)));
David Tolnayf38cdf62016-09-23 19:07:09 -0700180}
181
David Tolnayc2dfbf42016-09-23 23:52:15 -0700182#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -0700183mod printing {
184 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -0700185 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700186 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -0700187 use quote::{Tokens, ToTokens};
188
David Tolnay0e837402016-12-22 17:25:55 -0500189 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700190 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700191 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700192 attr.to_tokens(tokens);
193 }
David Tolnay47a877c2016-10-01 16:50:55 -0700194 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700195 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700196 Body::Enum(ref d) => d.enum_token.to_tokens(tokens),
197 Body::Struct(ref d) => d.struct_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700198 }
199 self.ident.to_tokens(tokens);
200 self.generics.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700201 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700202 Body::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700203 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 data.brace_token.surround(tokens, |tokens| {
205 data.variants.to_tokens(tokens);
206 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700207 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700208 Body::Struct(ref data) => {
209 match data.data {
210 VariantData::Struct(..) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700211 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700212 data.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -0700213 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700214 VariantData::Tuple(..) => {
215 data.data.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700216 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700217 }
218 VariantData::Unit => {
219 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700220 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700221 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700222 data.semi_token.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700223 }
224 }
225 }
226 }
227}