blob: 3d1924ad74b137b76bbe083359eefebd495c259a [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 Tolnay8095c302018-03-31 19:34:17 +020074 pub fn iter(&self) -> punctuated::Iter<Field> {
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 Tolnay8095c302018-03-31 19:34:17 +020085 type IntoIter = punctuated::Iter<'a, Field>;
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 Tolnayc8ecb362018-04-01 11:01:09 +0200137 /// A crate-level visibility: `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 crate_token: Token![crate],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700143 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500144
David Tolnay05658502018-01-07 09:56:37 -0800145 /// A visibility level restricted to some path: `pub(self)` or
David Tolnayc8ecb362018-04-01 11:01:09 +0200146 /// `pub(super)` or `pub(crate)` or `pub(in some::module)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"derive"` or
149 /// `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700150 pub Restricted(VisRestricted {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800151 pub pub_token: Token![pub],
David Tolnay32954ef2017-12-26 22:43:16 -0500152 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800153 pub in_token: Option<Token![in]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700154 pub path: Box<Path>,
155 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500156
David Tolnay05658502018-01-07 09:56:37 -0800157 /// An inherited visibility, which usually means private.
David Tolnayfcfb9002017-12-28 22:04:29 -0500158 pub Inherited,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700160}
161
David Tolnayf38cdf62016-09-23 19:07:09 -0700162#[cfg(feature = "parsing")]
163pub mod parsing {
164 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700165
Michael Layzell92639a52017-06-01 00:07:44 -0400166 use synom::Synom;
David Tolnayf38cdf62016-09-23 19:07:09 -0700167
David Tolnaye3d41b72017-12-31 15:24:00 -0500168 impl Synom for Variant {
169 named!(parse -> Self, do_parse!(
170 attrs: many0!(Attribute::parse_outer) >>
171 id: syn!(Ident) >>
172 fields: alt!(
173 syn!(FieldsNamed) => { Fields::Named }
174 |
175 syn!(FieldsUnnamed) => { Fields::Unnamed }
176 |
177 epsilon!() => { |_| Fields::Unit }
178 ) >>
179 disr: option!(tuple!(punct!(=), syn!(Expr))) >>
180 (Variant {
181 ident: id,
182 attrs: attrs,
183 fields: fields,
184 discriminant: disr,
185 })
186 ));
187
188 fn description() -> Option<&'static str> {
189 Some("enum variant")
190 }
191 }
192
193 impl Synom for FieldsNamed {
194 named!(parse -> Self, map!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500195 braces!(call!(Punctuated::parse_terminated_with, Field::parse_named)),
David Tolnaye3d41b72017-12-31 15:24:00 -0500196 |(brace, fields)| FieldsNamed {
197 brace_token: brace,
David Tolnaybdafb102018-01-01 19:39:10 -0800198 named: fields,
David Tolnaye3d41b72017-12-31 15:24:00 -0500199 }
200 ));
David Tolnay79777332018-01-07 10:04:42 -0800201
202 fn description() -> Option<&'static str> {
203 Some("named fields in a struct or struct variant")
204 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500205 }
206
207 impl Synom for FieldsUnnamed {
208 named!(parse -> Self, map!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500209 parens!(call!(Punctuated::parse_terminated_with, Field::parse_unnamed)),
David Tolnaye3d41b72017-12-31 15:24:00 -0500210 |(paren, fields)| FieldsUnnamed {
211 paren_token: paren,
David Tolnaybdafb102018-01-01 19:39:10 -0800212 unnamed: fields,
David Tolnaye3d41b72017-12-31 15:24:00 -0500213 }
214 ));
David Tolnay79777332018-01-07 10:04:42 -0800215
216 fn description() -> Option<&'static str> {
217 Some("unnamed fields in a tuple struct or tuple variant")
218 }
David Tolnaye3d41b72017-12-31 15:24:00 -0500219 }
220
Alex Crichton954046c2017-05-30 21:49:42 -0700221 impl Field {
David Tolnaye3d41b72017-12-31 15:24:00 -0500222 named!(pub parse_named -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500223 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400224 vis: syn!(Visibility) >>
225 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800226 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800227 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400228 (Field {
229 ident: Some(id),
230 vis: vis,
231 attrs: attrs,
232 ty: ty,
233 colon_token: Some(colon),
234 })
235 ));
David Tolnayf38cdf62016-09-23 19:07:09 -0700236
David Tolnaye3d41b72017-12-31 15:24:00 -0500237 named!(pub parse_unnamed -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500238 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400239 vis: syn!(Visibility) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800240 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400241 (Field {
242 ident: None,
243 colon_token: None,
244 vis: vis,
245 attrs: attrs,
246 ty: ty,
247 })
248 ));
Michael Layzell416724e2017-05-24 21:12:34 -0400249 }
250
Alex Crichton954046c2017-05-30 21:49:42 -0700251 impl Synom for Visibility {
Michael Layzell92639a52017-06-01 00:07:44 -0400252 named!(parse -> Self, alt!(
253 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800254 pub_token: keyword!(pub) >>
255 other: parens!(keyword!(crate)) >>
David Tolnayfe58b5a2018-03-31 18:20:40 +0200256 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400257 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500258 paren_token: other.0,
David Tolnayfe58b5a2018-03-31 18:20:40 +0200259 in_token: None,
260 path: Box::new(other.1.into()),
Michael Layzell92639a52017-06-01 00:07:44 -0400261 }))
262 )
263 |
David Tolnayf36af872018-06-30 23:07:21 -0700264 do_parse!(
265 crate_token: keyword!(crate) >>
266 not!(punct!(::)) >>
267 (Visibility::Crate(VisCrate {
268 crate_token: crate_token,
269 }))
270 )
David Tolnay36175562018-03-28 13:51:02 +0200271 |
Michael Layzell92639a52017-06-01 00:07:44 -0400272 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800273 pub_token: keyword!(pub) >>
274 other: parens!(keyword!(self)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400275 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400276 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500277 paren_token: other.0,
278 in_token: None,
279 path: Box::new(other.1.into()),
Michael Layzell92639a52017-06-01 00:07:44 -0400280 }))
281 )
282 |
283 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800284 pub_token: keyword!(pub) >>
285 other: parens!(keyword!(super)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400286 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400287 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500288 paren_token: other.0,
289 in_token: None,
290 path: Box::new(other.1.into()),
Michael Layzell92639a52017-06-01 00:07:44 -0400291 }))
292 )
293 |
294 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub_token: keyword!(pub) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400296 other: parens!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800297 in_tok: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400298 restricted: call!(Path::parse_mod_style) >>
299 (in_tok, restricted)
300 )) >>
301 (Visibility::Restricted(VisRestricted {
Michael Layzell92639a52017-06-01 00:07:44 -0400302 pub_token: pub_token,
David Tolnay8875fca2017-12-31 13:52:37 -0500303 paren_token: other.0,
304 in_token: Some((other.1).0),
305 path: Box::new((other.1).1),
Michael Layzell92639a52017-06-01 00:07:44 -0400306 }))
307 )
308 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800309 keyword!(pub) => { |tok| {
Michael Layzell92639a52017-06-01 00:07:44 -0400310 Visibility::Public(VisPublic {
311 pub_token: tok,
312 })
313 } }
314 |
David Tolnayfcfb9002017-12-28 22:04:29 -0500315 epsilon!() => { |_| Visibility::Inherited }
Michael Layzell92639a52017-06-01 00:07:44 -0400316 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800317
318 fn description() -> Option<&'static str> {
David Tolnay05658502018-01-07 09:56:37 -0800319 Some("visibility qualifier such as `pub`")
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800320 }
Alex Crichton954046c2017-05-30 21:49:42 -0700321 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700322}
323
324#[cfg(feature = "printing")]
325mod printing {
326 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700327 use proc_macro2::TokenStream;
328 use quote::{ToTokens, TokenStreamExt};
David Tolnayf38cdf62016-09-23 19:07:09 -0700329
330 impl ToTokens for Variant {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700331 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700332 tokens.append_all(&self.attrs);
David Tolnayf38cdf62016-09-23 19:07:09 -0700333 self.ident.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500334 self.fields.to_tokens(tokens);
David Tolnaye67902a2017-12-28 22:12:00 -0500335 if let Some((ref eq_token, ref disc)) = self.discriminant {
336 eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400337 disc.to_tokens(tokens);
338 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700339 }
340 }
341
David Tolnaye3d41b72017-12-31 15:24:00 -0500342 impl ToTokens for FieldsNamed {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700343 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3d41b72017-12-31 15:24:00 -0500344 self.brace_token.surround(tokens, |tokens| {
David Tolnaybdafb102018-01-01 19:39:10 -0800345 self.named.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500346 });
347 }
348 }
349
350 impl ToTokens for FieldsUnnamed {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700351 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye3d41b72017-12-31 15:24:00 -0500352 self.paren_token.surround(tokens, |tokens| {
David Tolnaybdafb102018-01-01 19:39:10 -0800353 self.unnamed.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -0500354 });
David Tolnayf38cdf62016-09-23 19:07:09 -0700355 }
356 }
357
358 impl ToTokens for Field {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700359 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700360 tokens.append_all(&self.attrs);
David Tolnay47a877c2016-10-01 16:50:55 -0700361 self.vis.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400362 if let Some(ref ident) = self.ident {
363 ident.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700364 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400365 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700366 self.ty.to_tokens(tokens);
367 }
368 }
369
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700370 impl ToTokens for VisPublic {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700371 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700372 self.pub_token.to_tokens(tokens)
373 }
374 }
Arnaviond32b2942017-04-29 17:18:02 -0700375
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700376 impl ToTokens for VisCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700377 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayfe58b5a2018-03-31 18:20:40 +0200378 self.crate_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700379 }
380 }
Arnaviond32b2942017-04-29 17:18:02 -0700381
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700382 impl ToTokens for VisRestricted {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700383 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700384 self.pub_token.to_tokens(tokens);
385 self.paren_token.surround(tokens, |tokens| {
David Tolnayfe58b5a2018-03-31 18:20:40 +0200386 // XXX: If we have a path which is not "self" or "super" or
387 // "crate", automatically add the "in" token.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700388 self.in_token.to_tokens(tokens);
389 self.path.to_tokens(tokens);
390 });
391 }
392 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700393}