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` |
| 17 | pub discriminant: Option<ConstExpr>, |
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 | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 110 | use WhereClause; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 111 | #[cfg(feature = "full")] |
| 112 | use ConstExpr; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 113 | use attr::parsing::outer_attr; |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 114 | #[cfg(feature = "full")] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 115 | use constant::parsing::const_expr; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 116 | #[cfg(feature = "full")] |
| 117 | use expr::parsing::expr; |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 118 | use generics::parsing::where_clause; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 119 | use ident::parsing::ident; |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 120 | use ty::parsing::{mod_style_path, ty}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 121 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 122 | named!(pub struct_body -> (WhereClause, VariantData, Option<tokens::Semi>), alt!( |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 123 | do_parse!( |
| 124 | wh: where_clause >> |
| 125 | body: struct_like_body >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 126 | (wh, VariantData::Struct(body.0, body.1), None) |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 127 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 128 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 129 | do_parse!( |
| 130 | body: tuple_like_body >> |
| 131 | wh: where_clause >> |
| 132 | punct!(";") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 133 | (wh, VariantData::Tuple(body.0, body.1), Some(tokens::Semi::default())) |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 134 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 135 | | |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 136 | do_parse!( |
| 137 | wh: where_clause >> |
| 138 | punct!(";") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 139 | (wh, VariantData::Unit, Some(tokens::Semi::default())) |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 140 | ) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 141 | )); |
| 142 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 143 | named!(pub enum_body -> (WhereClause, Delimited<Variant, tokens::Comma>, tokens::Brace), do_parse!( |
David Tolnay | 28c1db6 | 2016-10-27 22:48:18 -0700 | [diff] [blame] | 144 | wh: where_clause >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 145 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 146 | variants: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 147 | variant) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 148 | punct!("}") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 149 | (wh, variants, tokens::Brace::default()) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 150 | )); |
| 151 | |
| 152 | named!(variant -> Variant, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 153 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 154 | id: ident >> |
| 155 | data: alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 156 | struct_like_body => { |(d, b)| VariantData::Struct(d, b) } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 157 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 158 | tuple_like_body => { |(d, b)| VariantData::Tuple(d, b) } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 159 | | |
| 160 | epsilon!() => { |_| VariantData::Unit } |
| 161 | ) >> |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 162 | disr: option!(preceded!(punct!("="), discriminant)) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 163 | (Variant { |
| 164 | ident: id, |
| 165 | attrs: attrs, |
| 166 | data: data, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 167 | eq_token: disr.as_ref().map(|_| tokens::Eq::default()), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 168 | discriminant: disr, |
| 169 | }) |
| 170 | )); |
| 171 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 172 | #[cfg(not(feature = "full"))] |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 173 | use constant::parsing::const_expr as discriminant; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 174 | |
| 175 | #[cfg(feature = "full")] |
| 176 | named!(discriminant -> ConstExpr, alt!( |
| 177 | terminated!(const_expr, after_discriminant) |
| 178 | | |
| 179 | terminated!(expr, after_discriminant) => { ConstExpr::Other } |
| 180 | )); |
| 181 | |
| 182 | #[cfg(feature = "full")] |
| 183 | named!(after_discriminant -> &str, peek!(alt!(punct!(",") | punct!("}")))); |
| 184 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 185 | named!(pub struct_like_body -> (Delimited<Field, tokens::Comma>, tokens::Brace), do_parse!( |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 186 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 187 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 188 | struct_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 189 | punct!("}") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 190 | (fields, tokens::Brace::default()) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 191 | )); |
| 192 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 193 | named!(tuple_like_body -> (Delimited<Field, tokens::Comma>, tokens::Paren), do_parse!( |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 194 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 195 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 196 | tuple_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 197 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 198 | (fields, tokens::Paren::default()) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 199 | )); |
| 200 | |
| 201 | named!(struct_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 202 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 203 | vis: visibility >> |
| 204 | id: ident >> |
| 205 | punct!(":") >> |
| 206 | ty: ty >> |
| 207 | (Field { |
| 208 | ident: Some(id), |
| 209 | vis: vis, |
| 210 | attrs: attrs, |
| 211 | ty: ty, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 212 | colon_token: Some(tokens::Colon::default()), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 213 | }) |
| 214 | )); |
| 215 | |
| 216 | named!(tuple_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 217 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 218 | vis: visibility >> |
| 219 | ty: ty >> |
| 220 | (Field { |
| 221 | ident: None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 222 | colon_token: None, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 223 | vis: vis, |
| 224 | attrs: attrs, |
| 225 | ty: ty, |
| 226 | }) |
| 227 | )); |
| 228 | |
| 229 | named!(pub visibility -> Visibility, alt!( |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 230 | do_parse!( |
| 231 | keyword!("pub") >> |
| 232 | punct!("(") >> |
| 233 | keyword!("crate") >> |
| 234 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 235 | (Visibility::Crate(VisCrate { |
| 236 | crate_token: tokens::Crate::default(), |
| 237 | paren_token: tokens::Paren::default(), |
| 238 | pub_token: tokens::Pub::default(), |
| 239 | })) |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 240 | ) |
| 241 | | |
| 242 | do_parse!( |
| 243 | keyword!("pub") >> |
| 244 | punct!("(") >> |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 245 | keyword!("self") >> |
| 246 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 247 | (Visibility::Restricted(VisRestricted { |
| 248 | path: Box::new("self".into()), |
| 249 | in_token: None, |
| 250 | paren_token: tokens::Paren::default(), |
| 251 | pub_token: tokens::Pub::default(), |
| 252 | })) |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 253 | ) |
| 254 | | |
| 255 | do_parse!( |
| 256 | keyword!("pub") >> |
| 257 | punct!("(") >> |
| 258 | keyword!("super") >> |
| 259 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 260 | (Visibility::Restricted(VisRestricted { |
| 261 | path: Box::new("super".into()), |
| 262 | in_token: None, |
| 263 | paren_token: tokens::Paren::default(), |
| 264 | pub_token: tokens::Pub::default(), |
| 265 | })) |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 266 | ) |
| 267 | | |
| 268 | do_parse!( |
| 269 | keyword!("pub") >> |
| 270 | punct!("(") >> |
| 271 | keyword!("in") >> |
| 272 | restricted: mod_style_path >> |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 273 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 274 | (Visibility::Restricted(VisRestricted { |
| 275 | path: Box::new(restricted), |
| 276 | in_token: Some(tokens::In::default()), |
| 277 | paren_token: tokens::Paren::default(), |
| 278 | pub_token: tokens::Pub::default(), |
| 279 | })) |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 280 | ) |
| 281 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 282 | keyword!("pub") => { |_| { |
| 283 | Visibility::Public(VisPublic { |
| 284 | pub_token: tokens::Pub::default(), |
| 285 | }) |
| 286 | } } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 287 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 288 | epsilon!() => { |_| Visibility::Inherited(VisInherited {}) } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 289 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | #[cfg(feature = "printing")] |
| 293 | mod printing { |
| 294 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 295 | use quote::{Tokens, ToTokens}; |
| 296 | |
| 297 | impl ToTokens for Variant { |
| 298 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 299 | tokens.append_all(&self.attrs); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 300 | self.ident.to_tokens(tokens); |
| 301 | self.data.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 302 | self.eq_token.to_tokens(tokens); |
| 303 | self.discriminant.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | impl ToTokens for VariantData { |
| 308 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 309 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 310 | VariantData::Struct(ref fields, ref brace) => { |
| 311 | brace.surround(tokens, |tokens| { |
| 312 | fields.to_tokens(tokens); |
| 313 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 314 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 315 | VariantData::Tuple(ref fields, ref paren) => { |
| 316 | paren.surround(tokens, |tokens| { |
| 317 | fields.to_tokens(tokens); |
| 318 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 319 | } |
| 320 | VariantData::Unit => {} |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | impl ToTokens for Field { |
| 326 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 327 | tokens.append_all(&self.attrs); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 328 | self.vis.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 329 | self.ident.to_tokens(tokens); |
| 330 | self.colon_token.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 331 | self.ty.to_tokens(tokens); |
| 332 | } |
| 333 | } |
| 334 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 335 | impl ToTokens for VisPublic { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 336 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 337 | self.pub_token.to_tokens(tokens) |
| 338 | } |
| 339 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 340 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 341 | impl ToTokens for VisCrate { |
| 342 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 343 | self.pub_token.to_tokens(tokens); |
| 344 | self.paren_token.surround(tokens, |tokens| { |
| 345 | self.crate_token.to_tokens(tokens); |
| 346 | }) |
| 347 | } |
| 348 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 349 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 350 | impl ToTokens for VisRestricted { |
| 351 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 352 | self.pub_token.to_tokens(tokens); |
| 353 | self.paren_token.surround(tokens, |tokens| { |
| 354 | self.in_token.to_tokens(tokens); |
| 355 | self.path.to_tokens(tokens); |
| 356 | }); |
| 357 | } |
| 358 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 359 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 360 | impl ToTokens for VisInherited { |
| 361 | fn to_tokens(&self, _tokens: &mut Tokens) { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 364 | } |