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