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