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 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 7 | /// Attributes tagged on the variant. |
| 8 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 9 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 10 | /// Name of the variant. |
| 11 | pub ident: Ident, |
| 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` |
David Tolnay | e67902a | 2017-12-28 22:12:00 -0500 | [diff] [blame] | 17 | pub discriminant: Option<(Token![=], Expr)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 21 | ast_enum! { |
| 22 | /// Data stored within an enum variant or struct. |
| 23 | pub enum VariantData { |
| 24 | /// Struct variant, e.g. `Point { x: f64, y: f64 }`. |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 25 | Struct(Delimited<Field, Token![,]>, token::Brace), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 26 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 27 | /// Tuple variant, e.g. `Some(T)`. |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 28 | Tuple(Delimited<Field, Token![,]>, token::Paren), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 29 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | /// Unit variant, e.g. `None`. |
| 31 | Unit, |
| 32 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | impl VariantData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | // TODO: expose this? |
| 37 | // /// Slice containing the fields stored in the variant. |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 38 | // pub fn fields(&self) -> &Delimited<Field, token::Comma> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 39 | // match *self { |
| 40 | // VariantData::Struct(ref fields, _) | |
| 41 | // VariantData::Tuple(ref fields, _) => fields, |
| 42 | // VariantData::Unit => &[], |
| 43 | // } |
| 44 | // } |
| 45 | // |
| 46 | // /// Mutable slice containing the fields stored in the variant. |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 47 | // pub fn fields_mut(&mut self) -> &mut Delimited<Field, token::Comma> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 48 | // match *self { |
| 49 | // VariantData::Struct(ref mut fields, _) | |
| 50 | // VariantData::Tuple(ref mut fields, _) => fields, |
| 51 | // VariantData::Unit => &mut [], |
| 52 | // } |
| 53 | // } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 56 | ast_struct! { |
| 57 | /// A field of a struct or enum variant. |
| 58 | pub struct Field { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 59 | /// Attributes tagged on the field. |
| 60 | pub attrs: Vec<Attribute>, |
| 61 | |
| 62 | /// Visibility of the field. |
| 63 | pub vis: Visibility, |
| 64 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 65 | /// Name of the field, if any. |
| 66 | /// |
| 67 | /// Fields of tuple structs have no names. |
| 68 | pub ident: Option<Ident>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 69 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 70 | pub colon_token: Option<Token![:]>, |
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. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 73 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 77 | ast_enum_of_structs! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 78 | /// Visibility level of an item. |
| 79 | pub enum Visibility { |
| 80 | /// Public, i.e. `pub`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 81 | pub Public(VisPublic { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 82 | pub pub_token: Token![pub], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 83 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 84 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 85 | /// Crate-visible, i.e. `pub(crate)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 86 | pub Crate(VisCrate { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 87 | pub pub_token: Token![pub], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 88 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 89 | pub crate_token: Token![crate], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 90 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 91 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 92 | /// 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] | 93 | pub Restricted(VisRestricted { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 94 | pub pub_token: Token![pub], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 95 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 96 | pub in_token: Option<Token![in]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 97 | pub path: Box<Path>, |
| 98 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 99 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 100 | /// Inherited, i.e. private. |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 101 | pub Inherited, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 102 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 105 | #[cfg(feature = "parsing")] |
| 106 | pub mod parsing { |
| 107 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 108 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 109 | use synom::Synom; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 110 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 111 | impl Field { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 112 | named!(pub parse_struct -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 113 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 114 | vis: syn!(Visibility) >> |
| 115 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 116 | colon: punct!(:) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 117 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 118 | (Field { |
| 119 | ident: Some(id), |
| 120 | vis: vis, |
| 121 | attrs: attrs, |
| 122 | ty: ty, |
| 123 | colon_token: Some(colon), |
| 124 | }) |
| 125 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 126 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 127 | named!(pub parse_tuple -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 128 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 129 | vis: syn!(Visibility) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 130 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 131 | (Field { |
| 132 | ident: None, |
| 133 | colon_token: None, |
| 134 | vis: vis, |
| 135 | attrs: attrs, |
| 136 | ty: ty, |
| 137 | }) |
| 138 | )); |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 141 | impl Synom for Visibility { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 142 | named!(parse -> Self, alt!( |
| 143 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 144 | pub_token: keyword!(pub) >> |
| 145 | other: parens!(keyword!(crate)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 146 | (Visibility::Crate(VisCrate { |
| 147 | crate_token: other.0, |
| 148 | paren_token: other.1, |
| 149 | pub_token: pub_token, |
| 150 | })) |
| 151 | ) |
| 152 | | |
| 153 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 154 | pub_token: keyword!(pub) >> |
| 155 | other: parens!(keyword!(self)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 156 | (Visibility::Restricted(VisRestricted { |
| 157 | path: Box::new(other.0.into()), |
| 158 | in_token: None, |
| 159 | paren_token: other.1, |
| 160 | pub_token: pub_token, |
| 161 | })) |
| 162 | ) |
| 163 | | |
| 164 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 165 | pub_token: keyword!(pub) >> |
| 166 | other: parens!(keyword!(super)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 167 | (Visibility::Restricted(VisRestricted { |
| 168 | path: Box::new(other.0.into()), |
| 169 | in_token: None, |
| 170 | paren_token: other.1, |
| 171 | pub_token: pub_token, |
| 172 | })) |
| 173 | ) |
| 174 | | |
| 175 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 176 | pub_token: keyword!(pub) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 177 | other: parens!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 178 | in_tok: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 179 | restricted: call!(Path::parse_mod_style) >> |
| 180 | (in_tok, restricted) |
| 181 | )) >> |
| 182 | (Visibility::Restricted(VisRestricted { |
| 183 | path: Box::new((other.0).1), |
| 184 | in_token: Some((other.0).0), |
| 185 | paren_token: other.1, |
| 186 | pub_token: pub_token, |
| 187 | })) |
| 188 | ) |
| 189 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 190 | keyword!(pub) => { |tok| { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 191 | Visibility::Public(VisPublic { |
| 192 | pub_token: tok, |
| 193 | }) |
| 194 | } } |
| 195 | | |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 196 | epsilon!() => { |_| Visibility::Inherited } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 197 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame^] | 198 | |
| 199 | fn description() -> Option<&'static str> { |
| 200 | Some("visibility qualifier, e.g. `pub`") |
| 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 | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 208 | use quote::{ToTokens, Tokens}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 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); |
David Tolnay | e67902a | 2017-12-28 22:12:00 -0500 | [diff] [blame] | 215 | if let Some((ref eq_token, ref disc)) = self.discriminant { |
| 216 | 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 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 278 | } |