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 | /// An enum variant. |
| 6 | pub struct Variant { |
| 7 | /// Name of the variant. |
| 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 | /// Attributes tagged on the variant. |
| 11 | pub attrs: Vec<Attribute>, |
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 | /// Type of variant. |
| 14 | pub data: VariantData, |
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 | /// Explicit discriminant, e.g. `Foo = 1` |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame] | 17 | pub discriminant: Option<Expr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 18 | |
| 19 | pub eq_token: Option<tokens::Eq>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 20 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 23 | ast_enum! { |
| 24 | /// Data stored within an enum variant or struct. |
| 25 | pub enum VariantData { |
| 26 | /// Struct variant, e.g. `Point { x: f64, y: f64 }`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | Struct(Delimited<Field, tokens::Comma>, tokens::Brace), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 28 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 29 | /// Tuple variant, e.g. `Some(T)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | Tuple(Delimited<Field, tokens::Comma>, tokens::Paren), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 31 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | /// Unit variant, e.g. `None`. |
| 33 | Unit, |
| 34 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | impl VariantData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 38 | // TODO: expose this? |
| 39 | // /// Slice containing the fields stored in the variant. |
| 40 | // pub fn fields(&self) -> &Delimited<Field, tokens::Comma> { |
| 41 | // match *self { |
| 42 | // VariantData::Struct(ref fields, _) | |
| 43 | // VariantData::Tuple(ref fields, _) => fields, |
| 44 | // VariantData::Unit => &[], |
| 45 | // } |
| 46 | // } |
| 47 | // |
| 48 | // /// Mutable slice containing the fields stored in the variant. |
| 49 | // pub fn fields_mut(&mut self) -> &mut Delimited<Field, tokens::Comma> { |
| 50 | // match *self { |
| 51 | // VariantData::Struct(ref mut fields, _) | |
| 52 | // VariantData::Tuple(ref mut fields, _) => fields, |
| 53 | // VariantData::Unit => &mut [], |
| 54 | // } |
| 55 | // } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 58 | ast_struct! { |
| 59 | /// A field of a struct or enum variant. |
| 60 | pub struct Field { |
| 61 | /// Name of the field, if any. |
| 62 | /// |
| 63 | /// Fields of tuple structs have no names. |
| 64 | pub ident: Option<Ident>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 65 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 66 | /// Visibility of the field. |
| 67 | pub vis: Visibility, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 68 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 69 | /// Attributes tagged on the field. |
| 70 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 71 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 72 | /// Type of the field. |
| 73 | pub ty: Ty, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | |
| 75 | pub colon_token: Option<tokens::Colon>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 76 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 79 | ast_enum_of_structs! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 80 | /// Visibility level of an item. |
| 81 | pub enum Visibility { |
| 82 | /// Public, i.e. `pub`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 83 | pub Public(VisPublic { |
| 84 | pub pub_token: tokens::Pub, |
| 85 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 86 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 87 | /// Crate-visible, i.e. `pub(crate)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 88 | pub Crate(VisCrate { |
| 89 | pub pub_token: tokens::Pub, |
| 90 | pub paren_token: tokens::Paren, |
| 91 | pub crate_token: tokens::Crate, |
| 92 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 93 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 94 | /// Restricted, e.g. `pub(self)` or `pub(super)` or `pub(in some::module)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 95 | pub Restricted(VisRestricted { |
| 96 | pub pub_token: tokens::Pub, |
| 97 | pub paren_token: tokens::Paren, |
| 98 | pub in_token: Option<tokens::In>, |
| 99 | pub path: Box<Path>, |
| 100 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 101 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 102 | /// Inherited, i.e. private. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 103 | pub Inherited(VisInherited {}), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 104 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 105 | } |
| 106 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 107 | #[cfg(feature = "parsing")] |
| 108 | pub mod parsing { |
| 109 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 110 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 111 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 112 | use synom::tokens; |
| 113 | use synom::tokens::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 114 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 115 | impl Field { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 116 | named!(pub parse_struct -> Self, do_parse!( |
| 117 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 118 | vis: syn!(Visibility) >> |
| 119 | id: syn!(Ident) >> |
| 120 | colon: syn!(Colon) >> |
| 121 | ty: syn!(Ty) >> |
| 122 | (Field { |
| 123 | ident: Some(id), |
| 124 | vis: vis, |
| 125 | attrs: attrs, |
| 126 | ty: ty, |
| 127 | colon_token: Some(colon), |
| 128 | }) |
| 129 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 130 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 131 | named!(pub parse_tuple -> Self, do_parse!( |
| 132 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 133 | vis: syn!(Visibility) >> |
| 134 | ty: syn!(Ty) >> |
| 135 | (Field { |
| 136 | ident: None, |
| 137 | colon_token: None, |
| 138 | vis: vis, |
| 139 | attrs: attrs, |
| 140 | ty: ty, |
| 141 | }) |
| 142 | )); |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 145 | impl Synom for Visibility { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 146 | named!(parse -> Self, alt!( |
| 147 | do_parse!( |
| 148 | pub_token: syn!(Pub) >> |
| 149 | other: parens!(syn!(tokens::Crate)) >> |
| 150 | (Visibility::Crate(VisCrate { |
| 151 | crate_token: other.0, |
| 152 | paren_token: other.1, |
| 153 | pub_token: pub_token, |
| 154 | })) |
| 155 | ) |
| 156 | | |
| 157 | do_parse!( |
| 158 | pub_token: syn!(Pub) >> |
| 159 | other: parens!(syn!(Self_)) >> |
| 160 | (Visibility::Restricted(VisRestricted { |
| 161 | path: Box::new(other.0.into()), |
| 162 | in_token: None, |
| 163 | paren_token: other.1, |
| 164 | pub_token: pub_token, |
| 165 | })) |
| 166 | ) |
| 167 | | |
| 168 | do_parse!( |
| 169 | pub_token: syn!(Pub) >> |
| 170 | other: parens!(syn!(Super)) >> |
| 171 | (Visibility::Restricted(VisRestricted { |
| 172 | path: Box::new(other.0.into()), |
| 173 | in_token: None, |
| 174 | paren_token: other.1, |
| 175 | pub_token: pub_token, |
| 176 | })) |
| 177 | ) |
| 178 | | |
| 179 | do_parse!( |
| 180 | pub_token: syn!(Pub) >> |
| 181 | other: parens!(do_parse!( |
| 182 | in_tok: syn!(In) >> |
| 183 | restricted: call!(Path::parse_mod_style) >> |
| 184 | (in_tok, restricted) |
| 185 | )) >> |
| 186 | (Visibility::Restricted(VisRestricted { |
| 187 | path: Box::new((other.0).1), |
| 188 | in_token: Some((other.0).0), |
| 189 | paren_token: other.1, |
| 190 | pub_token: pub_token, |
| 191 | })) |
| 192 | ) |
| 193 | | |
| 194 | syn!(Pub) => { |tok| { |
| 195 | Visibility::Public(VisPublic { |
| 196 | pub_token: tok, |
| 197 | }) |
| 198 | } } |
| 199 | | |
| 200 | epsilon!() => { |_| Visibility::Inherited(VisInherited {}) } |
| 201 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 202 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #[cfg(feature = "printing")] |
| 206 | mod printing { |
| 207 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 208 | use quote::{Tokens, ToTokens}; |
| 209 | |
| 210 | impl ToTokens for Variant { |
| 211 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 212 | tokens.append_all(&self.attrs); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 213 | self.ident.to_tokens(tokens); |
| 214 | self.data.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 215 | if let Some(ref disc) = self.discriminant { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame^] | 216 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 217 | disc.to_tokens(tokens); |
| 218 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | impl ToTokens for VariantData { |
| 223 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 224 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 225 | VariantData::Struct(ref fields, ref brace) => { |
| 226 | brace.surround(tokens, |tokens| { |
| 227 | fields.to_tokens(tokens); |
| 228 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 229 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 230 | VariantData::Tuple(ref fields, ref paren) => { |
| 231 | paren.surround(tokens, |tokens| { |
| 232 | fields.to_tokens(tokens); |
| 233 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 234 | } |
| 235 | VariantData::Unit => {} |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | impl ToTokens for Field { |
| 241 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 242 | tokens.append_all(&self.attrs); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 243 | self.vis.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 244 | if let Some(ref ident) = self.ident { |
| 245 | ident.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame^] | 246 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 247 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 248 | self.ty.to_tokens(tokens); |
| 249 | } |
| 250 | } |
| 251 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 252 | impl ToTokens for VisPublic { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 253 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 254 | self.pub_token.to_tokens(tokens) |
| 255 | } |
| 256 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 257 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 258 | impl ToTokens for VisCrate { |
| 259 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 260 | self.pub_token.to_tokens(tokens); |
| 261 | self.paren_token.surround(tokens, |tokens| { |
| 262 | self.crate_token.to_tokens(tokens); |
| 263 | }) |
| 264 | } |
| 265 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 266 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 267 | impl ToTokens for VisRestricted { |
| 268 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 269 | self.pub_token.to_tokens(tokens); |
| 270 | self.paren_token.surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 271 | // XXX: If we have a path which is not "self" or "super", |
| 272 | // automatically add the "in" token. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 273 | self.in_token.to_tokens(tokens); |
| 274 | self.path.to_tokens(tokens); |
| 275 | }); |
| 276 | } |
| 277 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 278 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 279 | impl ToTokens for VisInherited { |
| 280 | fn to_tokens(&self, _tokens: &mut Tokens) { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 283 | } |