blob: 4f5b4cb041c47759055b10cdfc8f9a117873a95d [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf38cdf62016-09-23 19:07:09 -07009use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnayf38cdf62016-09-23 19:07:09 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_struct! {
13 /// An enum variant.
David Tolnay461d98e2018-01-07 11:07:19 -080014 ///
15 /// *This type is available if Syn is built with the `"derive"` or `"full"`
16 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070017 pub struct Variant {
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// Attributes tagged on the variant.
19 pub attrs: Vec<Attribute>,
Clar Charrd22b5702017-03-10 15:24:56 -050020
David Tolnay4a3f59a2017-12-28 21:21:12 -050021 /// Name of the variant.
22 pub ident: Ident,
23
David Tolnaye3d41b72017-12-31 15:24:00 -050024 /// Content stored in the variant.
25 pub fields: Fields,
Clar Charrd22b5702017-03-10 15:24:56 -050026
David Tolnay05658502018-01-07 09:56:37 -080027 /// Explicit discriminant: `Variant = 1`
David Tolnaye67902a2017-12-28 22:12:00 -050028 pub discriminant: Option<(Token![=], Expr)>,
Alex Crichton62a0a592017-05-22 13:58:53 -070029 }
David Tolnayf38cdf62016-09-23 19:07:09 -070030}
31
David Tolnaye3d41b72017-12-31 15:24:00 -050032ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -070033 /// Data stored within an enum variant or struct.
David Tolnay614a0142018-01-07 10:25:43 -080034 ///
David Tolnay461d98e2018-01-07 11:07:19 -080035 /// *This type is available if Syn is built with the `"derive"` or `"full"`
36 /// feature.*
37 ///
David Tolnay614a0142018-01-07 10:25:43 -080038 /// # Syntax tree enum
39 ///
40 /// This type is a [syntax tree enum].
41 ///
42 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnaye3d41b72017-12-31 15:24:00 -050043 pub enum Fields {
44 /// Named fields of a struct or struct variant such as `Point { x: f64,
45 /// y: f64 }`.
David Tolnay461d98e2018-01-07 11:07:19 -080046 ///
47 /// *This type is available if Syn is built with the `"derive"` or
48 /// `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -050049 pub Named(FieldsNamed {
50 pub brace_token: token::Brace,
David Tolnaybdafb102018-01-01 19:39:10 -080051 pub named: Punctuated<Field, Token![,]>,
David Tolnaye3d41b72017-12-31 15:24:00 -050052 }),
Clar Charrd22b5702017-03-10 15:24:56 -050053
David Tolnaye3d41b72017-12-31 15:24:00 -050054 /// Unnamed fields of a tuple struct or tuple variant such as `Some(T)`.
David Tolnay461d98e2018-01-07 11:07:19 -080055 ///
56 /// *This type is available if Syn is built with the `"derive"` or
57 /// `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -050058 pub Unnamed(FieldsUnnamed {
59 pub paren_token: token::Paren,
David Tolnaybdafb102018-01-01 19:39:10 -080060 pub unnamed: Punctuated<Field, Token![,]>,
David Tolnaye3d41b72017-12-31 15:24:00 -050061 }),
Clar Charrd22b5702017-03-10 15:24:56 -050062
David Tolnaye3d41b72017-12-31 15:24:00 -050063 /// Unit struct or unit variant such as `None`.
64 pub Unit,
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }
David Tolnayf38cdf62016-09-23 19:07:09 -070066}
67
Nika Layzell5680dcc2018-01-16 15:14:27 -050068impl Fields {
69 /// Get an iterator over the [`Field`] items in this object. This iterator
70 /// can be used to iterate over a named or unnamed struct or variant's
71 /// fields uniformly.
72 ///
73 /// [`Field`]: struct.Field.html
David Tolnay96a09d92018-01-16 22:24:03 -080074 pub fn iter(&self) -> punctuated::Iter<Field, Token![,]> {
Nika Layzell5680dcc2018-01-16 15:14:27 -050075 match *self {
David Tolnay96a09d92018-01-16 22:24:03 -080076 Fields::Unit => punctuated::Iter::private_empty(),
77 Fields::Named(ref f) => f.named.iter(),
78 Fields::Unnamed(ref f) => f.unnamed.iter(),
Nika Layzell5680dcc2018-01-16 15:14:27 -050079 }
80 }
81}
82
83impl<'a> IntoIterator for &'a Fields {
84 type Item = &'a Field;
David Tolnay96a09d92018-01-16 22:24:03 -080085 type IntoIter = punctuated::Iter<'a, Field, Token![,]>;
Nika Layzell5680dcc2018-01-16 15:14:27 -050086
87 fn into_iter(self) -> Self::IntoIter {
88 self.iter()
89 }
90}
91
Alex Crichton62a0a592017-05-22 13:58:53 -070092ast_struct! {
93 /// A field of a struct or enum variant.
David Tolnay461d98e2018-01-07 11:07:19 -080094 ///
95 /// *This type is available if Syn is built with the `"derive"` or `"full"`
96 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub struct Field {
David Tolnay4a3f59a2017-12-28 21:21:12 -050098 /// Attributes tagged on the field.
99 pub attrs: Vec<Attribute>,
100
101 /// Visibility of the field.
102 pub vis: Visibility,
103
Alex Crichton62a0a592017-05-22 13:58:53 -0700104 /// Name of the field, if any.
105 ///
106 /// Fields of tuple structs have no names.
107 pub ident: Option<Ident>,
Clar Charrd22b5702017-03-10 15:24:56 -0500108
David Tolnay4a3f59a2017-12-28 21:21:12 -0500109 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500110
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 /// Type of the field.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800112 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700114}
115
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700116ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800117 /// The visibility level of an item: inherited or `pub` or
118 /// `pub(restricted)`.
David Tolnay614a0142018-01-07 10:25:43 -0800119 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800120 /// *This type is available if Syn is built with the `"derive"` or `"full"`
121 /// feature.*
122 ///
David Tolnay614a0142018-01-07 10:25:43 -0800123 /// # Syntax tree enum
124 ///
125 /// This type is a [syntax tree enum].
126 ///
127 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 pub enum Visibility {
David Tolnay05658502018-01-07 09:56:37 -0800129 /// A public visibility level: `pub`.
David Tolnay461d98e2018-01-07 11:07:19 -0800130 ///
131 /// *This type is available if Syn is built with the `"derive"` or
132 /// `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700133 pub Public(VisPublic {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub pub_token: Token![pub],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700135 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500136
David Tolnay05658502018-01-07 09:56:37 -0800137 /// A crate-level visibility: `pub(crate)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800138 ///
139 /// *This type is available if Syn is built with the `"derive"` or
140 /// `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700141 pub Crate(VisCrate {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800142 pub pub_token: Token![pub],
David Tolnay32954ef2017-12-26 22:43:16 -0500143 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800144 pub crate_token: Token![crate],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700145 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500146
David Tolnay05658502018-01-07 09:56:37 -0800147 /// A visibility level restricted to some path: `pub(self)` or
148 /// `pub(super)` or `pub(in some::module)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800149 ///
150 /// *This type is available if Syn is built with the `"derive"` or
151 /// `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700152 pub Restricted(VisRestricted {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800153 pub pub_token: Token![pub],
David Tolnay32954ef2017-12-26 22:43:16 -0500154 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 pub in_token: Option<Token![in]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 pub path: Box<Path>,
157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
David Tolnay05658502018-01-07 09:56:37 -0800159 /// An inherited visibility, which usually means private.
David Tolnayfcfb9002017-12-28 22:04:29 -0500160 pub Inherited,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700162}
163
David Tolnayf38cdf62016-09-23 19:07:09 -0700164#[cfg(feature = "parsing")]
165pub mod parsing {
166 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700167
Michael Layzell92639a52017-06-01 00:07:44 -0400168 use synom::Synom;
David Tolnayf38cdf62016-09-23 19:07:09 -0700169
David Tolnaye3d41b72017-12-31 15:24:00 -0500170 impl Synom for Variant {
171 named!(parse -> Self, do_parse!(
172 attrs: many0!(Attribute::parse_outer) >>
173 id: syn!(Ident) >>
174 fields: alt!(
175 syn!(FieldsNamed) => { Fields::Named }
176 |
177 syn!(FieldsUnnamed) => { Fields::Unnamed }
178 |
179 epsilon!() => { |_| Fields::Unit }
180 ) >>
181 disr: option!(tuple!(punct!(=), syn!(Expr))) >>
182 (Variant {
183 ident: id,
184 attrs: attrs,
185 fields: fields,
186 discriminant: disr,
187 })
188 ));
189
190 fn description() -> Option<&'static str> {
191 Some("enum variant")
192 }
193 }
194
195 impl Synom for FieldsNamed {
196 named!(parse -> Self, map!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500197 braces!(call!(Punctuated::parse_terminated_with, Field::parse_named)),
David Tolnaye3d41b72017-12-31 15:24:00 -0500198 |(brace, fields)| FieldsNamed {
199 brace_token: brace,
David Tolnaybdafb102018-01-01 19:39:10 -0800200 named: fields,
David Tolnaye3d41b72017-12-31 15:24:00 -0500201 }
202 ));
David Tolnay79777332018-01-07 10:04:42 -0800203
204 fn description() -> Option<&'static str> {
205 Some("named fields in a struct or struct variant")
206 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500207 }
208
209 impl Synom for FieldsUnnamed {
210 named!(parse -> Self, map!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500211 parens!(call!(Punctuated::parse_terminated_with, Field::parse_unnamed)),
David Tolnaye3d41b72017-12-31 15:24:00 -0500212 |(paren, fields)| FieldsUnnamed {
213 paren_token: paren,
David Tolnaybdafb102018-01-01 19:39:10 -0800214 unnamed: fields,
David Tolnaye3d41b72017-12-31 15:24:00 -0500215 }
216 ));
David Tolnay79777332018-01-07 10:04:42 -0800217
218 fn description() -> Option<&'static str> {
219 Some("unnamed fields in a tuple struct or tuple variant")
220 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500221 }
222
Alex Crichton954046c2017-05-30 21:49:42 -0700223 impl Field {
David Tolnaye3d41b72017-12-31 15:24:00 -0500224 named!(pub parse_named -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500225 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400226 vis: syn!(Visibility) >>
227 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800228 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800229 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400230 (Field {
231 ident: Some(id),
232 vis: vis,
233 attrs: attrs,
234 ty: ty,
235 colon_token: Some(colon),
236 })
237 ));
David Tolnayf38cdf62016-09-23 19:07:09 -0700238
David Tolnaye3d41b72017-12-31 15:24:00 -0500239 named!(pub parse_unnamed -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500240 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400241 vis: syn!(Visibility) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800242 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400243 (Field {
244 ident: None,
245 colon_token: None,
246 vis: vis,
247 attrs: attrs,
248 ty: ty,
249 })
250 ));
Michael Layzell416724e2017-05-24 21:12:34 -0400251 }
252
Alex Crichton954046c2017-05-30 21:49:42 -0700253 impl Synom for Visibility {
Michael Layzell92639a52017-06-01 00:07:44 -0400254 named!(parse -> Self, alt!(
255 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800256 pub_token: keyword!(pub) >>
257 other: parens!(keyword!(crate)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400258 (Visibility::Crate(VisCrate {
Michael Layzell92639a52017-06-01 00:07:44 -0400259 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500260 paren_token: other.0,
261 crate_token: other.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400262 }))
263 )
264 |
265 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800266 pub_token: keyword!(pub) >>
267 other: parens!(keyword!(self)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400268 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400269 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500270 paren_token: other.0,
271 in_token: None,
272 path: Box::new(other.1.into()),
Michael Layzell92639a52017-06-01 00:07:44 -0400273 }))
274 )
275 |
276 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800277 pub_token: keyword!(pub) >>
278 other: parens!(keyword!(super)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400279 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400280 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500281 paren_token: other.0,
282 in_token: None,
283 path: Box::new(other.1.into()),
Michael Layzell92639a52017-06-01 00:07:44 -0400284 }))
285 )
286 |
287 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 pub_token: keyword!(pub) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400289 other: parens!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800290 in_tok: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400291 restricted: call!(Path::parse_mod_style) >>
292 (in_tok, restricted)
293 )) >>
294 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400295 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500296 paren_token: other.0,
297 in_token: Some((other.1).0),
298 path: Box::new((other.1).1),
Michael Layzell92639a52017-06-01 00:07:44 -0400299 }))
300 )
301 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800302 keyword!(pub) => { |tok| {
Michael Layzell92639a52017-06-01 00:07:44 -0400303 Visibility::Public(VisPublic {
304 pub_token: tok,
305 })
306 } }
307 |
David Tolnayfcfb9002017-12-28 22:04:29 -0500308 epsilon!() => { |_| Visibility::Inherited }
Michael Layzell92639a52017-06-01 00:07:44 -0400309 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800310
311 fn description() -> Option<&'static str> {
David Tolnay05658502018-01-07 09:56:37 -0800312 Some("visibility qualifier such as `pub`")
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800313 }
Alex Crichton954046c2017-05-30 21:49:42 -0700314 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700315}
316
317#[cfg(feature = "printing")]
318mod printing {
319 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500320 use quote::{ToTokens, Tokens};
David Tolnayf38cdf62016-09-23 19:07:09 -0700321
322 impl ToTokens for Variant {
323 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700324 tokens.append_all(&self.attrs);
David Tolnayf38cdf62016-09-23 19:07:09 -0700325 self.ident.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500326 self.fields.to_tokens(tokens);
David Tolnaye67902a2017-12-28 22:12:00 -0500327 if let Some((ref eq_token, ref disc)) = self.discriminant {
328 eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400329 disc.to_tokens(tokens);
330 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700331 }
332 }
333
David Tolnaye3d41b72017-12-31 15:24:00 -0500334 impl ToTokens for FieldsNamed {
David Tolnayf38cdf62016-09-23 19:07:09 -0700335 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye3d41b72017-12-31 15:24:00 -0500336 self.brace_token.surround(tokens, |tokens| {
David Tolnaybdafb102018-01-01 19:39:10 -0800337 self.named.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500338 });
339 }
340 }
341
342 impl ToTokens for FieldsUnnamed {
343 fn to_tokens(&self, tokens: &mut Tokens) {
344 self.paren_token.surround(tokens, |tokens| {
David Tolnaybdafb102018-01-01 19:39:10 -0800345 self.unnamed.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500346 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700347 }
348 }
349
350 impl ToTokens for Field {
351 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700352 tokens.append_all(&self.attrs);
David Tolnay47a877c2016-10-01 16:50:55 -0700353 self.vis.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400354 if let Some(ref ident) = self.ident {
355 ident.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700356 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400357 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700358 self.ty.to_tokens(tokens);
359 }
360 }
361
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700362 impl ToTokens for VisPublic {
David Tolnay47a877c2016-10-01 16:50:55 -0700363 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700364 self.pub_token.to_tokens(tokens)
365 }
366 }
Arnaviond32b2942017-04-29 17:18:02 -0700367
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700368 impl ToTokens for VisCrate {
369 fn to_tokens(&self, tokens: &mut Tokens) {
370 self.pub_token.to_tokens(tokens);
371 self.paren_token.surround(tokens, |tokens| {
372 self.crate_token.to_tokens(tokens);
373 })
374 }
375 }
Arnaviond32b2942017-04-29 17:18:02 -0700376
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700377 impl ToTokens for VisRestricted {
378 fn to_tokens(&self, tokens: &mut Tokens) {
379 self.pub_token.to_tokens(tokens);
380 self.paren_token.surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400381 // XXX: If we have a path which is not "self" or "super",
382 // automatically add the "in" token.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700383 self.in_token.to_tokens(tokens);
384 self.path.to_tokens(tokens);
385 });
386 }
387 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700388}