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 | )); |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame^] | 140 | |
| 141 | fn description() -> Option<&'static str> { |
| 142 | Some("named fields in a struct or struct variant") |
| 143 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | impl Synom for FieldsUnnamed { |
| 147 | named!(parse -> Self, map!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 148 | parens!(call!(Punctuated::parse_terminated_with, Field::parse_unnamed)), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 149 | |(paren, fields)| FieldsUnnamed { |
| 150 | paren_token: paren, |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 151 | unnamed: fields, |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 152 | } |
| 153 | )); |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame^] | 154 | |
| 155 | fn description() -> Option<&'static str> { |
| 156 | Some("unnamed fields in a tuple struct or tuple variant") |
| 157 | } |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 160 | impl Field { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 161 | named!(pub parse_named -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 162 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 163 | vis: syn!(Visibility) >> |
| 164 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 165 | colon: punct!(:) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 166 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 167 | (Field { |
| 168 | ident: Some(id), |
| 169 | vis: vis, |
| 170 | attrs: attrs, |
| 171 | ty: ty, |
| 172 | colon_token: Some(colon), |
| 173 | }) |
| 174 | )); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 175 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 176 | named!(pub parse_unnamed -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 177 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 178 | vis: syn!(Visibility) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 179 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 180 | (Field { |
| 181 | ident: None, |
| 182 | colon_token: None, |
| 183 | vis: vis, |
| 184 | attrs: attrs, |
| 185 | ty: ty, |
| 186 | }) |
| 187 | )); |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 190 | impl Synom for Visibility { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 191 | named!(parse -> Self, alt!( |
| 192 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 193 | pub_token: keyword!(pub) >> |
| 194 | other: parens!(keyword!(crate)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 195 | (Visibility::Crate(VisCrate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 196 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 197 | paren_token: other.0, |
| 198 | crate_token: other.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 199 | })) |
| 200 | ) |
| 201 | | |
| 202 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 203 | pub_token: keyword!(pub) >> |
| 204 | other: parens!(keyword!(self)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 205 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 206 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 207 | paren_token: other.0, |
| 208 | in_token: None, |
| 209 | path: Box::new(other.1.into()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 210 | })) |
| 211 | ) |
| 212 | | |
| 213 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 214 | pub_token: keyword!(pub) >> |
| 215 | other: parens!(keyword!(super)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 216 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 217 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 218 | paren_token: other.0, |
| 219 | in_token: None, |
| 220 | path: Box::new(other.1.into()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 221 | })) |
| 222 | ) |
| 223 | | |
| 224 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 225 | pub_token: keyword!(pub) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 226 | other: parens!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 227 | in_tok: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 228 | restricted: call!(Path::parse_mod_style) >> |
| 229 | (in_tok, restricted) |
| 230 | )) >> |
| 231 | (Visibility::Restricted(VisRestricted { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 232 | pub_token: pub_token, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 233 | paren_token: other.0, |
| 234 | in_token: Some((other.1).0), |
| 235 | path: Box::new((other.1).1), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 236 | })) |
| 237 | ) |
| 238 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 239 | keyword!(pub) => { |tok| { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 240 | Visibility::Public(VisPublic { |
| 241 | pub_token: tok, |
| 242 | }) |
| 243 | } } |
| 244 | | |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 245 | epsilon!() => { |_| Visibility::Inherited } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 246 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 247 | |
| 248 | fn description() -> Option<&'static str> { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 249 | Some("visibility qualifier such as `pub`") |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 250 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 251 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | #[cfg(feature = "printing")] |
| 255 | mod printing { |
| 256 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 257 | use quote::{ToTokens, Tokens}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 258 | |
| 259 | impl ToTokens for Variant { |
| 260 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 261 | tokens.append_all(&self.attrs); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 262 | self.ident.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 263 | self.fields.to_tokens(tokens); |
David Tolnay | e67902a | 2017-12-28 22:12:00 -0500 | [diff] [blame] | 264 | if let Some((ref eq_token, ref disc)) = self.discriminant { |
| 265 | eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 266 | disc.to_tokens(tokens); |
| 267 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 271 | impl ToTokens for FieldsNamed { |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 272 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 273 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 274 | self.named.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 275 | }); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | impl ToTokens for FieldsUnnamed { |
| 280 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 281 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | bdafb10 | 2018-01-01 19:39:10 -0800 | [diff] [blame] | 282 | self.unnamed.to_tokens(tokens); |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 283 | }); |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | impl ToTokens for Field { |
| 288 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 289 | tokens.append_all(&self.attrs); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 290 | self.vis.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 291 | if let Some(ref ident) = self.ident { |
| 292 | ident.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 293 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 294 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 295 | self.ty.to_tokens(tokens); |
| 296 | } |
| 297 | } |
| 298 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 299 | impl ToTokens for VisPublic { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 300 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 301 | self.pub_token.to_tokens(tokens) |
| 302 | } |
| 303 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 304 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 305 | impl ToTokens for VisCrate { |
| 306 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 307 | self.pub_token.to_tokens(tokens); |
| 308 | self.paren_token.surround(tokens, |tokens| { |
| 309 | self.crate_token.to_tokens(tokens); |
| 310 | }) |
| 311 | } |
| 312 | } |
Arnavion | d32b294 | 2017-04-29 17:18:02 -0700 | [diff] [blame] | 313 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 314 | impl ToTokens for VisRestricted { |
| 315 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 316 | self.pub_token.to_tokens(tokens); |
| 317 | self.paren_token.surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 318 | // XXX: If we have a path which is not "self" or "super", |
| 319 | // automatically add the "in" token. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 320 | self.in_token.to_tokens(tokens); |
| 321 | self.path.to_tokens(tokens); |
| 322 | }); |
| 323 | } |
| 324 | } |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 325 | } |