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 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 182 | // XXX: HACKY |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 183 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 184 | pub fn eof(input: &[synom::TokenTree]) -> synom::IResult<&[synom::TokenTree], &'static str> { |
| 185 | if input.is_empty() { |
| 186 | synom::IResult::Done(&[], "") |
| 187 | } else { |
| 188 | synom::IResult::Error |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #[cfg(feature = "full")] |
| 193 | named!(after_discriminant -> &str, peek!(alt!(punct!(",") | input_end!()))); |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 194 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 195 | 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] | 196 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 197 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 198 | struct_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 199 | punct!("}") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 200 | (fields, tokens::Brace::default()) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 201 | )); |
| 202 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 203 | named!(tuple_like_body -> (Delimited<Field, tokens::Comma>, tokens::Paren), do_parse!( |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 204 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 205 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 206 | tuple_field) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 207 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 208 | (fields, tokens::Paren::default()) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 209 | )); |
| 210 | |
| 211 | named!(struct_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 212 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 213 | vis: visibility >> |
| 214 | id: ident >> |
| 215 | punct!(":") >> |
| 216 | ty: ty >> |
| 217 | (Field { |
| 218 | ident: Some(id), |
| 219 | vis: vis, |
| 220 | attrs: attrs, |
| 221 | ty: ty, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 222 | colon_token: Some(tokens::Colon::default()), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 223 | }) |
| 224 | )); |
| 225 | |
| 226 | named!(tuple_field -> Field, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 227 | attrs: many0!(outer_attr) >> |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 228 | vis: visibility >> |
| 229 | ty: ty >> |
| 230 | (Field { |
| 231 | ident: None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 232 | colon_token: None, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 233 | vis: vis, |
| 234 | attrs: attrs, |
| 235 | ty: ty, |
| 236 | }) |
| 237 | )); |
| 238 | |
| 239 | named!(pub visibility -> Visibility, alt!( |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 240 | do_parse!( |
| 241 | keyword!("pub") >> |
| 242 | punct!("(") >> |
| 243 | keyword!("crate") >> |
| 244 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 245 | (Visibility::Crate(VisCrate { |
| 246 | crate_token: tokens::Crate::default(), |
| 247 | paren_token: tokens::Paren::default(), |
| 248 | pub_token: tokens::Pub::default(), |
| 249 | })) |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 250 | ) |
| 251 | | |
| 252 | do_parse!( |
| 253 | keyword!("pub") >> |
| 254 | punct!("(") >> |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 255 | keyword!("self") >> |
| 256 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 257 | (Visibility::Restricted(VisRestricted { |
| 258 | path: Box::new("self".into()), |
| 259 | in_token: None, |
| 260 | paren_token: tokens::Paren::default(), |
| 261 | pub_token: tokens::Pub::default(), |
| 262 | })) |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 263 | ) |
| 264 | | |
| 265 | do_parse!( |
| 266 | keyword!("pub") >> |
| 267 | punct!("(") >> |
| 268 | keyword!("super") >> |
| 269 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 270 | (Visibility::Restricted(VisRestricted { |
| 271 | path: Box::new("super".into()), |
| 272 | in_token: None, |
| 273 | paren_token: tokens::Paren::default(), |
| 274 | pub_token: tokens::Pub::default(), |
| 275 | })) |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 276 | ) |
| 277 | | |
| 278 | do_parse!( |
| 279 | keyword!("pub") >> |
| 280 | punct!("(") >> |
| 281 | keyword!("in") >> |
| 282 | restricted: mod_style_path >> |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 283 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 284 | (Visibility::Restricted(VisRestricted { |
| 285 | path: Box::new(restricted), |
| 286 | in_token: Some(tokens::In::default()), |
| 287 | paren_token: tokens::Paren::default(), |
| 288 | pub_token: tokens::Pub::default(), |
| 289 | })) |
David Tolnay | e07f9e0 | 2016-10-30 17:05:55 -0700 | [diff] [blame] | 290 | ) |
| 291 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 292 | keyword!("pub") => { |_| { |
| 293 | Visibility::Public(VisPublic { |
| 294 | pub_token: tokens::Pub::default(), |
| 295 | }) |
| 296 | } } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 297 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 298 | epsilon!() => { |_| Visibility::Inherited(VisInherited {}) } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 299 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | #[cfg(feature = "printing")] |
| 303 | mod printing { |
| 304 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 305 | use quote::{Tokens, ToTokens}; |
| 306 | |
| 307 | impl ToTokens for Variant { |
| 308 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 309 | tokens.append_all(&self.attrs); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 310 | self.ident.to_tokens(tokens); |
| 311 | self.data.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 312 | self.eq_token.to_tokens(tokens); |
| 313 | self.discriminant.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | impl ToTokens for VariantData { |
| 318 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 319 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 320 | VariantData::Struct(ref fields, ref brace) => { |
| 321 | brace.surround(tokens, |tokens| { |
| 322 | fields.to_tokens(tokens); |
| 323 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 324 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 325 | VariantData::Tuple(ref fields, ref paren) => { |
| 326 | paren.surround(tokens, |tokens| { |
| 327 | fields.to_tokens(tokens); |
| 328 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 329 | } |
| 330 | VariantData::Unit => {} |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | impl ToTokens for Field { |
| 336 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 337 | tokens.append_all(&self.attrs); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 338 | self.vis.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 339 | self.ident.to_tokens(tokens); |
| 340 | self.colon_token.to_tokens(tokens); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 341 | self.ty.to_tokens(tokens); |
| 342 | } |
| 343 | } |
| 344 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 345 | impl ToTokens for VisPublic { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 346 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 347 | self.pub_token.to_tokens(tokens) |
| 348 | } |
| 349 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 350 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 351 | impl ToTokens for VisCrate { |
| 352 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 353 | self.pub_token.to_tokens(tokens); |
| 354 | self.paren_token.surround(tokens, |tokens| { |
| 355 | self.crate_token.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 VisRestricted { |
| 361 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 362 | self.pub_token.to_tokens(tokens); |
| 363 | self.paren_token.surround(tokens, |tokens| { |
| 364 | self.in_token.to_tokens(tokens); |
| 365 | self.path.to_tokens(tokens); |
| 366 | }); |
| 367 | } |
| 368 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 369 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 370 | impl ToTokens for VisInherited { |
| 371 | fn to_tokens(&self, _tokens: &mut Tokens) { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 374 | } |