David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 11 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 12 | ast_struct! { |
| 13 | /// An enum variant. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 14 | /// |
| 15 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 16 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 17 | pub struct Variant { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | /// Attributes tagged on the variant. |
| 19 | pub attrs: Vec<Attribute>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 20 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 21 | /// Name of the variant. |
| 22 | pub ident: Ident, |
| 23 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 24 | /// Content stored in the variant. |
| 25 | pub fields: Fields, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 26 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 27 | /// Explicit discriminant: `Variant = 1` |
David Tolnay | e67902a | 2017-12-28 22:12:00 -0500 | [diff] [blame] | 28 | pub discriminant: Option<(Token![=], Expr)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 29 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 30 | } |
| 31 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 32 | ast_enum_of_structs! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 33 | /// Data stored within an enum variant or struct. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 34 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 35 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 36 | /// feature.* |
| 37 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 38 | /// # Syntax tree enum |
| 39 | /// |
| 40 | /// This type is a [syntax tree enum]. |
| 41 | /// |
| 42 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 43 | pub enum Fields { |
| 44 | /// Named fields of a struct or struct variant such as `Point { x: f64, |
| 45 | /// y: f64 }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 46 | /// |
| 47 | /// *This type is available if Syn is built with the `"derive"` or |
| 48 | /// `"full"` feature.* |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 49 | pub Named(FieldsNamed { |
| 50 | pub brace_token: token::Brace, |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 51 | pub named: Punctuated<Field, Token![,]>, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 52 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 53 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 54 | /// Unnamed fields of a tuple struct or tuple variant such as `Some(T)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 55 | /// |
| 56 | /// *This type is available if Syn is built with the `"derive"` or |
| 57 | /// `"full"` feature.* |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 58 | pub Unnamed(FieldsUnnamed { |
| 59 | pub paren_token: token::Paren, |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 60 | pub unnamed: Punctuated<Field, Token![,]>, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 61 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 62 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 63 | /// Unit struct or unit variant such as `None`. |
| 64 | pub Unit, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 65 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 68 | ast_struct! { |
| 69 | /// A field of a struct or enum variant. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 70 | /// |
| 71 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 72 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 73 | pub struct Field { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 74 | /// Attributes tagged on the field. |
| 75 | pub attrs: Vec<Attribute>, |
| 76 | |
| 77 | /// Visibility of the field. |
| 78 | pub vis: Visibility, |
| 79 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 80 | /// Name of the field, if any. |
| 81 | /// |
| 82 | /// Fields of tuple structs have no names. |
| 83 | pub ident: Option<Ident>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 84 | |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 85 | pub colon_token: Option<Token![:]>, |
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 | /// Type of the field. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 88 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 89 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 92 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 93 | /// The visibility level of an item: inherited or `pub` or |
| 94 | /// `pub(restricted)`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 95 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 96 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 97 | /// feature.* |
| 98 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 99 | /// # Syntax tree enum |
| 100 | /// |
| 101 | /// This type is a [syntax tree enum]. |
| 102 | /// |
| 103 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 104 | pub enum Visibility { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 105 | /// A public visibility level: `pub`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 106 | /// |
| 107 | /// *This type is available if Syn is built with the `"derive"` or |
| 108 | /// `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 109 | pub Public(VisPublic { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 110 | pub pub_token: Token![pub], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 112 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 113 | /// A crate-level visibility: `pub(crate)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 114 | /// |
| 115 | /// *This type is available if Syn is built with the `"derive"` or |
| 116 | /// `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 117 | pub Crate(VisCrate { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 118 | pub pub_token: Token![pub], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 119 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 120 | pub crate_token: Token![crate], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 122 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 123 | /// A visibility level restricted to some path: `pub(self)` or |
| 124 | /// `pub(super)` or `pub(in some::module)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 125 | /// |
| 126 | /// *This type is available if Syn is built with the `"derive"` or |
| 127 | /// `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 128 | pub Restricted(VisRestricted { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 129 | pub pub_token: Token![pub], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 130 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 131 | pub in_token: Option<Token![in]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 132 | pub path: Box<Path>, |
| 133 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 134 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 135 | /// An inherited visibility, which usually means private. |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 136 | pub Inherited, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 137 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 140 | #[cfg(feature = "parsing")] |
| 141 | pub mod parsing { |
| 142 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 143 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 144 | use synom::Synom; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 145 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 146 | impl Synom for Variant { |
| 147 | named!(parse -> Self, do_parse!( |
| 148 | attrs: many0!(Attribute::parse_outer) >> |
| 149 | id: syn!(Ident) >> |
| 150 | fields: alt!( |
| 151 | syn!(FieldsNamed) => { Fields::Named } |
| 152 | | |
| 153 | syn!(FieldsUnnamed) => { Fields::Unnamed } |
| 154 | | |
| 155 | epsilon!() => { |_| Fields::Unit } |
| 156 | ) >> |
| 157 | disr: option!(tuple!(punct!(=), syn!(Expr))) >> |
| 158 | (Variant { |
| 159 | ident: id, |
| 160 | attrs: attrs, |
| 161 | fields: fields, |
| 162 | discriminant: disr, |
| 163 | }) |
| 164 | )); |
| 165 | |
| 166 | fn description() -> Option<&'static str> { |
| 167 | Some("enum variant") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Synom for FieldsNamed { |
| 172 | named!(parse -> Self, map!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 173 | braces!(call!(Punctuated::parse_terminated_with, Field::parse_named)), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 174 | |(brace, fields)| FieldsNamed { |
| 175 | brace_token: brace, |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 176 | named: fields, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 177 | } |
| 178 | )); |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 179 | |
| 180 | fn description() -> Option<&'static str> { |
| 181 | Some("named fields in a struct or struct variant") |
| 182 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | impl Synom for FieldsUnnamed { |
| 186 | named!(parse -> Self, map!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 187 | parens!(call!(Punctuated::parse_terminated_with, Field::parse_unnamed)), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 188 | |(paren, fields)| FieldsUnnamed { |
| 189 | paren_token: paren, |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 190 | unnamed: fields, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 191 | } |
| 192 | )); |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 193 | |
| 194 | fn description() -> Option<&'static str> { |
| 195 | Some("unnamed fields in a tuple struct or tuple variant") |
| 196 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 199 | impl Field { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 200 | named!(pub parse_named -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 201 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 202 | vis: syn!(Visibility) >> |
| 203 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 204 | colon: punct!(:) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 205 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 206 | (Field { |
| 207 | ident: Some(id), |
| 208 | vis: vis, |
| 209 | attrs: attrs, |
| 210 | ty: ty, |
| 211 | colon_token: Some(colon), |
| 212 | }) |
| 213 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 214 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 215 | named!(pub parse_unnamed -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 216 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 217 | vis: syn!(Visibility) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 218 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 219 | (Field { |
| 220 | ident: None, |
| 221 | colon_token: None, |
| 222 | vis: vis, |
| 223 | attrs: attrs, |
| 224 | ty: ty, |
| 225 | }) |
| 226 | )); |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 229 | impl Synom for Visibility { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 230 | named!(parse -> Self, alt!( |
| 231 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 232 | pub_token: keyword!(pub) >> |
| 233 | other: parens!(keyword!(crate)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 234 | (Visibility::Crate(VisCrate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 235 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 236 | paren_token: other.0, |
| 237 | crate_token: other.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 238 | })) |
| 239 | ) |
| 240 | | |
| 241 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 242 | pub_token: keyword!(pub) >> |
| 243 | other: parens!(keyword!(self)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 244 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 245 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 246 | paren_token: other.0, |
| 247 | in_token: None, |
| 248 | path: Box::new(other.1.into()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 249 | })) |
| 250 | ) |
| 251 | | |
| 252 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 253 | pub_token: keyword!(pub) >> |
| 254 | other: parens!(keyword!(super)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 255 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 256 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 257 | paren_token: other.0, |
| 258 | in_token: None, |
| 259 | path: Box::new(other.1.into()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 260 | })) |
| 261 | ) |
| 262 | | |
| 263 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 264 | pub_token: keyword!(pub) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 265 | other: parens!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 266 | in_tok: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 267 | restricted: call!(Path::parse_mod_style) >> |
| 268 | (in_tok, restricted) |
| 269 | )) >> |
| 270 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 271 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 272 | paren_token: other.0, |
| 273 | in_token: Some((other.1).0), |
| 274 | path: Box::new((other.1).1), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 275 | })) |
| 276 | ) |
| 277 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 278 | keyword!(pub) => { |tok| { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 279 | Visibility::Public(VisPublic { |
| 280 | pub_token: tok, |
| 281 | }) |
| 282 | } } |
| 283 | | |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 284 | epsilon!() => { |_| Visibility::Inherited } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 285 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 286 | |
| 287 | fn description() -> Option<&'static str> { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 288 | Some("visibility qualifier such as `pub`") |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 289 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 290 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | #[cfg(feature = "printing")] |
| 294 | mod printing { |
| 295 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 296 | use quote::{ToTokens, Tokens}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 297 | |
| 298 | impl ToTokens for Variant { |
| 299 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 300 | tokens.append_all(&self.attrs); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 301 | self.ident.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 302 | self.fields.to_tokens(tokens); |
David Tolnay | e67902a | 2017-12-28 22:12:00 -0500 | [diff] [blame] | 303 | if let Some((ref eq_token, ref disc)) = self.discriminant { |
| 304 | eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 305 | disc.to_tokens(tokens); |
| 306 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 310 | impl ToTokens for FieldsNamed { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 311 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 312 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 313 | self.named.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 314 | }); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | impl ToTokens for FieldsUnnamed { |
| 319 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 320 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 321 | self.unnamed.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 322 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | impl ToTokens for Field { |
| 327 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 328 | tokens.append_all(&self.attrs); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 329 | self.vis.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 330 | if let Some(ref ident) = self.ident { |
| 331 | ident.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 332 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 333 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 334 | self.ty.to_tokens(tokens); |
| 335 | } |
| 336 | } |
| 337 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 338 | impl ToTokens for VisPublic { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 339 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 340 | self.pub_token.to_tokens(tokens) |
| 341 | } |
| 342 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 343 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 344 | impl ToTokens for VisCrate { |
| 345 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 346 | self.pub_token.to_tokens(tokens); |
| 347 | self.paren_token.surround(tokens, |tokens| { |
| 348 | self.crate_token.to_tokens(tokens); |
| 349 | }) |
| 350 | } |
| 351 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 352 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 353 | impl ToTokens for VisRestricted { |
| 354 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 355 | self.pub_token.to_tokens(tokens); |
| 356 | self.paren_token.surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 357 | // XXX: If we have a path which is not "self" or "super", |
| 358 | // automatically add the "in" token. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 359 | self.in_token.to_tokens(tokens); |
| 360 | self.path.to_tokens(tokens); |
| 361 | }); |
| 362 | } |
| 363 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 364 | } |