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