blob: a0c1c8d24dcc4164f549b19c69ff6af4ba2c3975 [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 |
David Tolnay92a56512017-11-10 00:02:14 -080060 syn!(Enum) => { Err }
Michael Layzell92639a52017-06-01 00:07:44 -040061 ) >>
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 Tolnay92a56512017-11-10 00:02:14 -080080 Err(e) => map!(enum_body, move |(wh, body, brace)| DeriveInput {
Michael Layzell92639a52017-06-01 00:07:44 -040081 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 Crichton954046c2017-05-30 21:49:42 -070097
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 Tolnayf38cdf62016-09-23 19:07:09 -0700123 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700124
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 Layzell92639a52017-06-01 00:07:44 -0400132 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 Layzelld7ee9102017-06-07 10:02:19 -0400144 disr: syn!(Expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400145 (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 Crichton954046c2017-05-30 21:49:42 -0700155 }
156
Alex Crichton954046c2017-05-30 21:49:42 -0700157 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 Tolnayf38cdf62016-09-23 19:07:09 -0700162}
163
David Tolnayc2dfbf42016-09-23 23:52:15 -0700164#[cfg(feature = "printing")]
David Tolnayf38cdf62016-09-23 19:07:09 -0700165mod printing {
166 use super::*;
David Tolnay4a51dc72016-10-01 00:40:31 -0700167 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700168 use data::VariantData;
David Tolnayf38cdf62016-09-23 19:07:09 -0700169 use quote::{Tokens, ToTokens};
170
David Tolnay0e837402016-12-22 17:25:55 -0500171 impl ToTokens for DeriveInput {
David Tolnayf38cdf62016-09-23 19:07:09 -0700172 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4a51dc72016-10-01 00:40:31 -0700173 for attr in self.attrs.outer() {
David Tolnayf38cdf62016-09-23 19:07:09 -0700174 attr.to_tokens(tokens);
175 }
David Tolnay47a877c2016-10-01 16:50:55 -0700176 self.vis.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700177 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700178 Body::Enum(ref d) => d.enum_token.to_tokens(tokens),
179 Body::Struct(ref d) => d.struct_token.to_tokens(tokens),
David Tolnayf38cdf62016-09-23 19:07:09 -0700180 }
181 self.ident.to_tokens(tokens);
182 self.generics.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700183 match self.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700184 Body::Enum(ref data) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700185 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700186 data.brace_token.surround(tokens, |tokens| {
187 data.variants.to_tokens(tokens);
188 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700189 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700190 Body::Struct(ref data) => {
191 match data.data {
192 VariantData::Struct(..) => {
David Tolnay28c1db62016-10-27 22:48:18 -0700193 self.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700194 data.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -0700195 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700196 VariantData::Tuple(..) => {
197 data.data.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700198 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700199 }
200 VariantData::Unit => {
201 self.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -0700202 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700203 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 data.semi_token.to_tokens(tokens);
David Tolnayf38cdf62016-09-23 19:07:09 -0700205 }
206 }
207 }
208 }
209}