David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 3 | ast_struct! { |
| 4 | /// An enum variant. |
| 5 | pub struct Variant { |
| 6 | /// Name of the variant. |
| 7 | pub ident: Ident, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 8 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 9 | /// Attributes tagged on the variant. |
| 10 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 11 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 12 | /// Type of variant. |
| 13 | pub data: VariantData, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 14 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 15 | /// Explicit discriminant, e.g. `Foo = 1` |
| 16 | pub discriminant: Option<ConstExpr>, |
| 17 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 20 | ast_enum! { |
| 21 | /// Data stored within an enum variant or struct. |
| 22 | pub enum VariantData { |
| 23 | /// Struct variant, e.g. `Point { x: f64, y: f64 }`. |
| 24 | Struct(Vec<Field>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 25 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 26 | /// Tuple variant, e.g. `Some(T)`. |
| 27 | Tuple(Vec<Field>), |
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 | /// Unit variant, e.g. `None`. |
| 30 | Unit, |
| 31 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | impl VariantData { |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 35 | /// Slice containing the fields stored in the variant. |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 36 | pub fn fields(&self) -> &[Field] { |
| 37 | match *self { |
| 38 | VariantData::Struct(ref fields) | |
| 39 | VariantData::Tuple(ref fields) => fields, |
| 40 | VariantData::Unit => &[], |
| 41 | } |
| 42 | } |
Sean Griffin | 15085c4 | 2016-10-03 11:40:31 -0400 | [diff] [blame] | 43 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 44 | /// Mutable slice containing the fields stored in the variant. |
Sean Griffin | 15085c4 | 2016-10-03 11:40:31 -0400 | [diff] [blame] | 45 | pub fn fields_mut(&mut self) -> &mut [Field] { |
| 46 | match *self { |
| 47 | VariantData::Struct(ref mut fields) | |
| 48 | VariantData::Tuple(ref mut fields) => fields, |
| 49 | VariantData::Unit => &mut [], |
| 50 | } |
| 51 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 54 | ast_struct! { |
| 55 | /// A field of a struct or enum variant. |
| 56 | pub struct Field { |
| 57 | /// Name of the field, if any. |
| 58 | /// |
| 59 | /// Fields of tuple structs have no names. |
| 60 | pub ident: Option<Ident>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 61 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 62 | /// Visibility of the field. |
| 63 | pub vis: Visibility, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 64 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 65 | /// Attributes tagged on the field. |
| 66 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 67 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 68 | /// Type of the field. |
| 69 | pub ty: Ty, |
| 70 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 73 | ast_enum! { |
| 74 | /// Visibility level of an item. |
| 75 | pub enum Visibility { |
| 76 | /// Public, i.e. `pub`. |
| 77 | Public, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 78 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 79 | /// Crate-visible, i.e. `pub(crate)`. |
| 80 | Crate, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 81 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame^] | 82 | /// Restricted, e.g. `pub(self)` or `pub(super)` or `pub(in some::module)`. |
| 83 | Restricted(Box<Path>), |
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 | /// Inherited, i.e. private. |
| 86 | Inherited, |
| 87 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 88 | } |
| 89 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 90 | #[cfg(feature = "parsing")] |
| 91 | pub mod parsing { |
| 92 | use super::*; |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 93 | use WhereClause; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 94 | #[cfg(feature = "full")] |
| 95 | use ConstExpr; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 96 | use attr::parsing::outer_attr; |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 97 | #[cfg(feature = "full")] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 98 | use constant::parsing::const_expr; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 99 | #[cfg(feature = "full")] |
| 100 | use expr::parsing::expr; |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 101 | use generics::parsing::where_clause; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 102 | use ident::parsing::ident; |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 103 | use ty::parsing::{mod_style_path, ty}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 104 | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 105 | named!(pub struct_body -> (WhereClause, VariantData), alt!( |
| 106 | do_parse!( |
| 107 | wh: where_clause >> |
| 108 | body: struct_like_body >> |
| 109 | (wh, VariantData::Struct(body)) |
| 110 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 111 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 112 | do_parse!( |
| 113 | body: tuple_like_body >> |
| 114 | wh: where_clause >> |
| 115 | punct!(";") >> |
| 116 | (wh, VariantData::Tuple(body)) |
| 117 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 118 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 119 | do_parse!( |
| 120 | wh: where_clause >> |
| 121 | punct!(";") >> |
| 122 | (wh, VariantData::Unit) |
| 123 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 124 | )); |
| 125 | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 126 | named!(pub enum_body -> (WhereClause, Vec<Variant>), do_parse!( |
| 127 | wh: where_clause >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 128 | punct!("{") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 129 | variants: terminated_list!(punct!(","), variant) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 130 | punct!("}") >> |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 131 | (wh, variants) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 132 | )); |
| 133 | |
| 134 | named!(variant -> Variant, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 135 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 136 | id: ident >> |
| 137 | data: alt!( |
| 138 | struct_like_body => { VariantData::Struct } |
| 139 | | |
| 140 | tuple_like_body => { VariantData::Tuple } |
| 141 | | |
| 142 | epsilon!() => { |_| VariantData::Unit } |
| 143 | ) >> |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 144 | disr: option!(preceded!(punct!("="), discriminant)) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 145 | (Variant { |
| 146 | ident: id, |
| 147 | attrs: attrs, |
| 148 | data: data, |
| 149 | discriminant: disr, |
| 150 | }) |
| 151 | )); |
| 152 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 153 | #[cfg(not(feature = "full"))] |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 154 | use constant::parsing::const_expr as discriminant; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 155 | |
| 156 | #[cfg(feature = "full")] |
| 157 | named!(discriminant -> ConstExpr, alt!( |
| 158 | terminated!(const_expr, after_discriminant) |
| 159 | | |
| 160 | terminated!(expr, after_discriminant) => { ConstExpr::Other } |
| 161 | )); |
| 162 | |
| 163 | #[cfg(feature = "full")] |
| 164 | named!(after_discriminant -> &str, peek!(alt!(punct!(",") | punct!("}")))); |
| 165 | |
David Tolnay | 2f9fa63 | 2016-10-03 22:08:48 -0700 | [diff] [blame] | 166 | named!(pub struct_like_body -> Vec<Field>, do_parse!( |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 167 | punct!("{") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 168 | fields: terminated_list!(punct!(","), struct_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 169 | punct!("}") >> |
| 170 | (fields) |
| 171 | )); |
| 172 | |
| 173 | named!(tuple_like_body -> Vec<Field>, do_parse!( |
| 174 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 175 | fields: terminated_list!(punct!(","), tuple_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 176 | punct!(")") >> |
| 177 | (fields) |
| 178 | )); |
| 179 | |
| 180 | named!(struct_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 181 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 182 | vis: visibility >> |
| 183 | id: ident >> |
| 184 | punct!(":") >> |
| 185 | ty: ty >> |
| 186 | (Field { |
| 187 | ident: Some(id), |
| 188 | vis: vis, |
| 189 | attrs: attrs, |
| 190 | ty: ty, |
| 191 | }) |
| 192 | )); |
| 193 | |
| 194 | named!(tuple_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 195 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 196 | vis: visibility >> |
| 197 | ty: ty >> |
| 198 | (Field { |
| 199 | ident: None, |
| 200 | vis: vis, |
| 201 | attrs: attrs, |
| 202 | ty: ty, |
| 203 | }) |
| 204 | )); |
| 205 | |
| 206 | named!(pub visibility -> Visibility, alt!( |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 207 | do_parse!( |
| 208 | keyword!("pub") >> |
| 209 | punct!("(") >> |
| 210 | keyword!("crate") >> |
| 211 | punct!(")") >> |
| 212 | (Visibility::Crate) |
| 213 | ) |
| 214 | | |
| 215 | do_parse!( |
| 216 | keyword!("pub") >> |
| 217 | punct!("(") >> |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 218 | keyword!("self") >> |
| 219 | punct!(")") >> |
| 220 | (Visibility::Restricted(Box::new("self".into()))) |
| 221 | ) |
| 222 | | |
| 223 | do_parse!( |
| 224 | keyword!("pub") >> |
| 225 | punct!("(") >> |
| 226 | keyword!("super") >> |
| 227 | punct!(")") >> |
| 228 | (Visibility::Restricted(Box::new("super".into()))) |
| 229 | ) |
| 230 | | |
| 231 | do_parse!( |
| 232 | keyword!("pub") >> |
| 233 | punct!("(") >> |
| 234 | keyword!("in") >> |
| 235 | restricted: mod_style_path >> |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 236 | punct!(")") >> |
| 237 | (Visibility::Restricted(Box::new(restricted))) |
| 238 | ) |
| 239 | | |
David Tolnay | bd76e57 | 2016-10-02 13:43:16 -0700 | [diff] [blame] | 240 | keyword!("pub") => { |_| Visibility::Public } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 241 | | |
| 242 | epsilon!() => { |_| Visibility::Inherited } |
| 243 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | #[cfg(feature = "printing")] |
| 247 | mod printing { |
| 248 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 249 | use quote::{Tokens, ToTokens}; |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 250 | use ty::PathParameters; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 251 | |
| 252 | impl ToTokens for Variant { |
| 253 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 254 | for attr in &self.attrs { |
| 255 | attr.to_tokens(tokens); |
| 256 | } |
| 257 | self.ident.to_tokens(tokens); |
| 258 | self.data.to_tokens(tokens); |
| 259 | if let Some(ref disr) = self.discriminant { |
| 260 | tokens.append("="); |
| 261 | disr.to_tokens(tokens); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | impl ToTokens for VariantData { |
| 267 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 268 | match *self { |
| 269 | VariantData::Struct(ref fields) => { |
| 270 | tokens.append("{"); |
| 271 | tokens.append_separated(fields, ","); |
| 272 | tokens.append("}"); |
| 273 | } |
| 274 | VariantData::Tuple(ref fields) => { |
| 275 | tokens.append("("); |
| 276 | tokens.append_separated(fields, ","); |
| 277 | tokens.append(")"); |
| 278 | } |
| 279 | VariantData::Unit => {} |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | impl ToTokens for Field { |
| 285 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 286 | for attr in &self.attrs { |
| 287 | attr.to_tokens(tokens); |
| 288 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 289 | self.vis.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 290 | if let Some(ref ident) = self.ident { |
| 291 | ident.to_tokens(tokens); |
| 292 | tokens.append(":"); |
| 293 | } |
| 294 | self.ty.to_tokens(tokens); |
| 295 | } |
| 296 | } |
| 297 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 298 | impl ToTokens for Visibility { |
| 299 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 300 | match *self { |
| 301 | Visibility::Public => tokens.append("pub"), |
| 302 | Visibility::Crate => { |
| 303 | tokens.append("pub"); |
| 304 | tokens.append("("); |
| 305 | tokens.append("crate"); |
| 306 | tokens.append(")"); |
| 307 | } |
| 308 | Visibility::Restricted(ref path) => { |
| 309 | tokens.append("pub"); |
| 310 | tokens.append("("); |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 311 | |
| 312 | if !path.global && |
| 313 | path.segments.len() == 1 && |
| 314 | (path.segments[0].ident == "self" || path.segments[0].ident == "super") && |
| 315 | path.segments[0].parameters == PathParameters::none() { |
| 316 | |
| 317 | // Don't emit preceding `in` if path is `self` or `super` |
| 318 | } else { |
| 319 | tokens.append("in"); |
| 320 | } |
| 321 | |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 322 | path.to_tokens(tokens); |
| 323 | tokens.append(")"); |
| 324 | } |
| 325 | Visibility::Inherited => {} |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 329 | } |