David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 3 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 4 | pub struct Variant { |
| 5 | pub ident: Ident, |
| 6 | pub attrs: Vec<Attribute>, |
| 7 | pub data: VariantData, |
| 8 | /// Explicit discriminant, e.g. `Foo = 1` |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 9 | pub discriminant: Option<ConstExpr>, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 10 | } |
| 11 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 12 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 13 | pub enum VariantData { |
| 14 | Struct(Vec<Field>), |
| 15 | Tuple(Vec<Field>), |
| 16 | Unit, |
| 17 | } |
| 18 | |
| 19 | impl VariantData { |
| 20 | pub fn fields(&self) -> &[Field] { |
| 21 | match *self { |
| 22 | VariantData::Struct(ref fields) | |
| 23 | VariantData::Tuple(ref fields) => fields, |
| 24 | VariantData::Unit => &[], |
| 25 | } |
| 26 | } |
Sean Griffin | 15085c4 | 2016-10-03 11:40:31 -0400 | [diff] [blame] | 27 | |
| 28 | pub fn fields_mut(&mut self) -> &mut [Field] { |
| 29 | match *self { |
| 30 | VariantData::Struct(ref mut fields) | |
| 31 | VariantData::Tuple(ref mut fields) => fields, |
| 32 | VariantData::Unit => &mut [], |
| 33 | } |
| 34 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 35 | } |
| 36 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 37 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 38 | pub struct Field { |
| 39 | pub ident: Option<Ident>, |
| 40 | pub vis: Visibility, |
| 41 | pub attrs: Vec<Attribute>, |
| 42 | pub ty: Ty, |
| 43 | } |
| 44 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 45 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 46 | pub enum Visibility { |
| 47 | Public, |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 48 | Crate, |
| 49 | Restricted(Box<Path>), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 50 | Inherited, |
| 51 | } |
| 52 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 53 | #[cfg(feature = "parsing")] |
| 54 | pub mod parsing { |
| 55 | use super::*; |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 56 | use WhereClause; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 57 | #[cfg(feature = "full")] |
| 58 | use ConstExpr; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 59 | use attr::parsing::outer_attr; |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 60 | #[cfg(feature = "full")] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 61 | use constant::parsing::const_expr; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 62 | #[cfg(feature = "full")] |
| 63 | use expr::parsing::expr; |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 64 | use generics::parsing::where_clause; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 65 | use ident::parsing::ident; |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 66 | use ty::parsing::{path, ty}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 67 | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 68 | named!(pub struct_body -> (WhereClause, VariantData), alt!( |
| 69 | do_parse!( |
| 70 | wh: where_clause >> |
| 71 | body: struct_like_body >> |
| 72 | (wh, VariantData::Struct(body)) |
| 73 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 74 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 75 | do_parse!( |
| 76 | body: tuple_like_body >> |
| 77 | wh: where_clause >> |
| 78 | punct!(";") >> |
| 79 | (wh, VariantData::Tuple(body)) |
| 80 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 81 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 82 | do_parse!( |
| 83 | wh: where_clause >> |
| 84 | punct!(";") >> |
| 85 | (wh, VariantData::Unit) |
| 86 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 87 | )); |
| 88 | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 89 | named!(pub enum_body -> (WhereClause, Vec<Variant>), do_parse!( |
| 90 | wh: where_clause >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 91 | punct!("{") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 92 | variants: terminated_list!(punct!(","), variant) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 93 | punct!("}") >> |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 94 | (wh, variants) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 95 | )); |
| 96 | |
| 97 | named!(variant -> Variant, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 98 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 99 | id: ident >> |
| 100 | data: alt!( |
| 101 | struct_like_body => { VariantData::Struct } |
| 102 | | |
| 103 | tuple_like_body => { VariantData::Tuple } |
| 104 | | |
| 105 | epsilon!() => { |_| VariantData::Unit } |
| 106 | ) >> |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 107 | disr: option!(preceded!(punct!("="), discriminant)) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 108 | (Variant { |
| 109 | ident: id, |
| 110 | attrs: attrs, |
| 111 | data: data, |
| 112 | discriminant: disr, |
| 113 | }) |
| 114 | )); |
| 115 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 116 | #[cfg(not(feature = "full"))] |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 117 | use constant::parsing::const_expr as discriminant; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 118 | |
| 119 | #[cfg(feature = "full")] |
| 120 | named!(discriminant -> ConstExpr, alt!( |
| 121 | terminated!(const_expr, after_discriminant) |
| 122 | | |
| 123 | terminated!(expr, after_discriminant) => { ConstExpr::Other } |
| 124 | )); |
| 125 | |
| 126 | #[cfg(feature = "full")] |
| 127 | named!(after_discriminant -> &str, peek!(alt!(punct!(",") | punct!("}")))); |
| 128 | |
David Tolnay | 2f9fa63 | 2016-10-03 22:08:48 -0700 | [diff] [blame] | 129 | named!(pub struct_like_body -> Vec<Field>, do_parse!( |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 130 | punct!("{") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 131 | fields: terminated_list!(punct!(","), struct_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 132 | punct!("}") >> |
| 133 | (fields) |
| 134 | )); |
| 135 | |
| 136 | named!(tuple_like_body -> Vec<Field>, do_parse!( |
| 137 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 138 | fields: terminated_list!(punct!(","), tuple_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 139 | punct!(")") >> |
| 140 | (fields) |
| 141 | )); |
| 142 | |
| 143 | named!(struct_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 144 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 145 | vis: visibility >> |
| 146 | id: ident >> |
| 147 | punct!(":") >> |
| 148 | ty: ty >> |
| 149 | (Field { |
| 150 | ident: Some(id), |
| 151 | vis: vis, |
| 152 | attrs: attrs, |
| 153 | ty: ty, |
| 154 | }) |
| 155 | )); |
| 156 | |
| 157 | named!(tuple_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 158 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 159 | vis: visibility >> |
| 160 | ty: ty >> |
| 161 | (Field { |
| 162 | ident: None, |
| 163 | vis: vis, |
| 164 | attrs: attrs, |
| 165 | ty: ty, |
| 166 | }) |
| 167 | )); |
| 168 | |
| 169 | named!(pub visibility -> Visibility, alt!( |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 170 | do_parse!( |
| 171 | keyword!("pub") >> |
| 172 | punct!("(") >> |
| 173 | keyword!("crate") >> |
| 174 | punct!(")") >> |
| 175 | (Visibility::Crate) |
| 176 | ) |
| 177 | | |
| 178 | do_parse!( |
| 179 | keyword!("pub") >> |
| 180 | punct!("(") >> |
| 181 | restricted: path >> |
| 182 | punct!(")") >> |
| 183 | (Visibility::Restricted(Box::new(restricted))) |
| 184 | ) |
| 185 | | |
David Tolnay | bd76e57 | 2016-10-02 13:43:16 -0700 | [diff] [blame] | 186 | keyword!("pub") => { |_| Visibility::Public } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 187 | | |
| 188 | epsilon!() => { |_| Visibility::Inherited } |
| 189 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | #[cfg(feature = "printing")] |
| 193 | mod printing { |
| 194 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 195 | use quote::{Tokens, ToTokens}; |
| 196 | |
| 197 | impl ToTokens for Variant { |
| 198 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 199 | for attr in &self.attrs { |
| 200 | attr.to_tokens(tokens); |
| 201 | } |
| 202 | self.ident.to_tokens(tokens); |
| 203 | self.data.to_tokens(tokens); |
| 204 | if let Some(ref disr) = self.discriminant { |
| 205 | tokens.append("="); |
| 206 | disr.to_tokens(tokens); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl ToTokens for VariantData { |
| 212 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 213 | match *self { |
| 214 | VariantData::Struct(ref fields) => { |
| 215 | tokens.append("{"); |
| 216 | tokens.append_separated(fields, ","); |
| 217 | tokens.append("}"); |
| 218 | } |
| 219 | VariantData::Tuple(ref fields) => { |
| 220 | tokens.append("("); |
| 221 | tokens.append_separated(fields, ","); |
| 222 | tokens.append(")"); |
| 223 | } |
| 224 | VariantData::Unit => {} |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | impl ToTokens for Field { |
| 230 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 231 | for attr in &self.attrs { |
| 232 | attr.to_tokens(tokens); |
| 233 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 234 | self.vis.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 235 | if let Some(ref ident) = self.ident { |
| 236 | ident.to_tokens(tokens); |
| 237 | tokens.append(":"); |
| 238 | } |
| 239 | self.ty.to_tokens(tokens); |
| 240 | } |
| 241 | } |
| 242 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 243 | impl ToTokens for Visibility { |
| 244 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 245 | match *self { |
| 246 | Visibility::Public => tokens.append("pub"), |
| 247 | Visibility::Crate => { |
| 248 | tokens.append("pub"); |
| 249 | tokens.append("("); |
| 250 | tokens.append("crate"); |
| 251 | tokens.append(")"); |
| 252 | } |
| 253 | Visibility::Restricted(ref path) => { |
| 254 | tokens.append("pub"); |
| 255 | tokens.append("("); |
| 256 | path.to_tokens(tokens); |
| 257 | tokens.append(")"); |
| 258 | } |
| 259 | Visibility::Inherited => {} |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 263 | } |