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