blob: 09469b54b1896e70376a9ae48800cca0f116fa96 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay3cfd1d32018-01-03 00:22:08 -080010use derive::{Data, DeriveInput};
David Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +090096 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
139 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500142 pub Struct(ItemStruct {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub struct_token: Token![struct],
146 pub ident: Ident,
147 pub generics: Generics,
148 pub fields: Fields,
149 pub semi_token: Option<Token![;]>,
150 }),
David Tolnay2b214082018-01-07 01:30:18 -0800151
152 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800153 ///
154 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800156 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700157 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700159 pub ident: Ident,
160 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500161 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500162 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 }),
David Tolnay2b214082018-01-07 01:30:18 -0800164
165 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800166 ///
167 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800169 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700170 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500174 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
David Tolnay2b214082018-01-07 01:30:18 -0800176
177 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800178 ///
179 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800181 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700182 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500183 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500184 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700186 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800188 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500189 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500190 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 pub items: Vec<TraitItem>,
192 }),
David Tolnay2b214082018-01-07 01:30:18 -0800193
194 /// An impl block providing trait or associated items: `impl<A> Trait
195 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800196 ///
197 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800199 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500200 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500201 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800202 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700204 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500205 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700206 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800207 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500208 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700209 pub items: Vec<ImplItem>,
210 }),
David Tolnay2b214082018-01-07 01:30:18 -0800211
212 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800213 ///
214 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800215 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800216 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800217 /// The `example` in `macro_rules! example { ... }`.
218 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800219 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500220 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800221 }),
David Tolnay2b214082018-01-07 01:30:18 -0800222
223 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500226 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800227 pub attrs: Vec<Attribute>,
228 pub vis: Visibility,
229 pub macro_token: Token![macro],
230 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500231 pub paren_token: Paren,
232 pub args: TokenStream,
233 pub brace_token: Brace,
234 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800235 }),
David Tolnay2b214082018-01-07 01:30:18 -0800236
237 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800238 ///
239 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500240 pub Verbatim(ItemVerbatim #manual_extra_traits {
241 pub tts: TokenStream,
242 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 }
David Tolnayb79ee962016-09-04 09:39:20 -0700244}
245
David Tolnay9c76bcb2017-12-26 23:14:59 -0500246#[cfg(feature = "extra-traits")]
247impl Eq for ItemMacro2 {}
248
249#[cfg(feature = "extra-traits")]
250impl PartialEq for ItemMacro2 {
251 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700252 self.attrs == other.attrs
253 && self.vis == other.vis
254 && self.macro_token == other.macro_token
255 && self.ident == other.ident
256 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500257 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
258 && self.brace_token == other.brace_token
259 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500260 }
261}
262
263#[cfg(feature = "extra-traits")]
264impl Hash for ItemMacro2 {
265 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500266 where
267 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500268 {
269 self.attrs.hash(state);
270 self.vis.hash(state);
271 self.macro_token.hash(state);
272 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500273 self.paren_token.hash(state);
274 TokenStreamHelper(&self.args).hash(state);
275 self.brace_token.hash(state);
276 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500277 }
278}
279
David Tolnay2ae520a2017-12-29 11:19:50 -0500280#[cfg(feature = "extra-traits")]
281impl Eq for ItemVerbatim {}
282
283#[cfg(feature = "extra-traits")]
284impl PartialEq for ItemVerbatim {
285 fn eq(&self, other: &Self) -> bool {
286 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
287 }
288}
289
290#[cfg(feature = "extra-traits")]
291impl Hash for ItemVerbatim {
292 fn hash<H>(&self, state: &mut H)
293 where
294 H: Hasher,
295 {
296 TokenStreamHelper(&self.tts).hash(state);
297 }
298}
299
David Tolnay0e837402016-12-22 17:25:55 -0500300impl From<DeriveInput> for Item {
301 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500302 match input.data {
303 Data::Struct(data) => Item::Struct(ItemStruct {
304 attrs: input.attrs,
305 vis: input.vis,
306 struct_token: data.struct_token,
307 ident: input.ident,
308 generics: input.generics,
309 fields: data.fields,
310 semi_token: data.semi_token,
311 }),
312 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500313 attrs: input.attrs,
314 vis: input.vis,
315 enum_token: data.enum_token,
316 ident: input.ident,
317 generics: input.generics,
318 brace_token: data.brace_token,
319 variants: data.variants,
320 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500321 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500322 attrs: input.attrs,
323 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500324 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500325 ident: input.ident,
326 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500327 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500328 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700329 }
330 }
331}
332
Alex Crichton62a0a592017-05-22 13:58:53 -0700333ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800334 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800335 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800336 /// *This type is available if Syn is built with the `"full"` feature.*
337 ///
David Tolnay614a0142018-01-07 10:25:43 -0800338 /// # Syntax tree enum
339 ///
340 /// This type is a [syntax tree enum].
341 ///
342 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500343 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200344 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800345 ///
346 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500347 pub Path(UsePath {
348 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200349 pub colon2_token: Token![::],
350 pub tree: Box<UseTree>,
351 }),
352
353 /// An identifier imported by a `use` item: `HashMap`.
354 ///
355 /// *This type is available if Syn is built with the `"full"` feature.*
356 pub Name(UseName {
357 pub ident: Ident,
358 }),
359
360 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
361 ///
362 /// *This type is available if Syn is built with the `"full"` feature.*
363 pub Rename(UseRename {
364 pub ident: Ident,
365 pub as_token: Token![as],
366 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700367 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800368
David Tolnay05658502018-01-07 09:56:37 -0800369 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800370 ///
371 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500372 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800373 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700374 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800375
David Tolnayd97a7d22018-03-31 19:17:01 +0200376 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800377 ///
378 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200379 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500380 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500381 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
383 }
384}
385
Alex Crichton62a0a592017-05-22 13:58:53 -0700386ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800387 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800388 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800389 /// *This type is available if Syn is built with the `"full"` feature.*
390 ///
David Tolnay614a0142018-01-07 10:25:43 -0800391 /// # Syntax tree enum
392 ///
393 /// This type is a [syntax tree enum].
394 ///
395 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800396 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800397 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800398 ///
399 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800401 pub attrs: Vec<Attribute>,
402 pub vis: Visibility,
403 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800405 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 }),
David Tolnayebb72722018-01-07 01:14:13 -0800407
408 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800409 ///
410 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800412 pub attrs: Vec<Attribute>,
413 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500415 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800416 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800418 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800419 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700420 }),
David Tolnayebb72722018-01-07 01:14:13 -0800421
422 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800423 ///
424 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800425 pub Type(ForeignItemType {
426 pub attrs: Vec<Attribute>,
427 pub vis: Visibility,
428 pub type_token: Token![type],
429 pub ident: Ident,
430 pub semi_token: Token![;],
431 }),
David Tolnayebb72722018-01-07 01:14:13 -0800432
David Tolnay435c1782018-08-24 16:15:44 -0400433 /// A macro invocation within an extern block.
434 ///
435 /// *This type is available if Syn is built with the `"full"` feature.*
436 pub Macro(ForeignItemMacro {
437 pub attrs: Vec<Attribute>,
438 pub mac: Macro,
439 pub semi_token: Option<Token![;]>,
440 }),
441
David Tolnayebb72722018-01-07 01:14:13 -0800442 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800443 ///
444 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500445 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
446 pub tts: TokenStream,
447 }),
448 }
449}
450
451#[cfg(feature = "extra-traits")]
452impl Eq for ForeignItemVerbatim {}
453
454#[cfg(feature = "extra-traits")]
455impl PartialEq for ForeignItemVerbatim {
456 fn eq(&self, other: &Self) -> bool {
457 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
458 }
459}
460
461#[cfg(feature = "extra-traits")]
462impl Hash for ForeignItemVerbatim {
463 fn hash<H>(&self, state: &mut H)
464 where
465 H: Hasher,
466 {
467 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700469}
470
David Tolnayda705bd2017-11-10 21:58:05 -0800471ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800472 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800473 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800474 /// *This type is available if Syn is built with the `"full"` feature.*
475 ///
David Tolnay614a0142018-01-07 10:25:43 -0800476 /// # Syntax tree enum
477 ///
478 /// This type is a [syntax tree enum].
479 ///
480 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800481 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800482 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800483 ///
484 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700485 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800486 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800487 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700488 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800489 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800490 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800491 pub default: Option<(Token![=], Expr)>,
492 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700493 }),
David Tolnayebb72722018-01-07 01:14:13 -0800494
495 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800496 ///
497 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700498 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800499 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 pub sig: MethodSig,
501 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700503 }),
David Tolnayebb72722018-01-07 01:14:13 -0800504
505 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800506 ///
507 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800509 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700511 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500512 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800513 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500514 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800515 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 }),
David Tolnayebb72722018-01-07 01:14:13 -0800518
519 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800520 ///
521 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800522 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800523 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800524 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500525 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800526 }),
David Tolnayebb72722018-01-07 01:14:13 -0800527
528 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800529 ///
530 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500531 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
532 pub tts: TokenStream,
533 }),
534 }
535}
536
537#[cfg(feature = "extra-traits")]
538impl Eq for TraitItemVerbatim {}
539
540#[cfg(feature = "extra-traits")]
541impl PartialEq for TraitItemVerbatim {
542 fn eq(&self, other: &Self) -> bool {
543 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
544 }
545}
546
547#[cfg(feature = "extra-traits")]
548impl Hash for TraitItemVerbatim {
549 fn hash<H>(&self, state: &mut H)
550 where
551 H: Hasher,
552 {
553 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700555}
556
Alex Crichton62a0a592017-05-22 13:58:53 -0700557ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800558 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800559 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800560 /// *This type is available if Syn is built with the `"full"` feature.*
561 ///
David Tolnay614a0142018-01-07 10:25:43 -0800562 /// # Syntax tree enum
563 ///
564 /// This type is a [syntax tree enum].
565 ///
566 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800567 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800568 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800569 ///
570 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700571 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800572 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700573 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500574 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800575 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700576 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800577 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800578 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800579 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700580 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800581 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700582 }),
David Tolnayebb72722018-01-07 01:14:13 -0800583
584 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800585 ///
586 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700587 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800588 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700589 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500590 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700591 pub sig: MethodSig,
592 pub block: Block,
593 }),
David Tolnayebb72722018-01-07 01:14:13 -0800594
595 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800596 ///
597 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800599 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700600 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500601 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700603 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500604 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800605 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800606 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800607 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700608 }),
David Tolnayebb72722018-01-07 01:14:13 -0800609
610 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800611 ///
612 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800613 pub Macro(ImplItemMacro {
614 pub attrs: Vec<Attribute>,
615 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500616 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800617 }),
David Tolnayebb72722018-01-07 01:14:13 -0800618
619 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800620 ///
621 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500622 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
623 pub tts: TokenStream,
624 }),
625 }
626}
627
628#[cfg(feature = "extra-traits")]
629impl Eq for ImplItemVerbatim {}
630
631#[cfg(feature = "extra-traits")]
632impl PartialEq for ImplItemVerbatim {
633 fn eq(&self, other: &Self) -> bool {
634 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
635 }
636}
637
638#[cfg(feature = "extra-traits")]
639impl Hash for ImplItemVerbatim {
640 fn hash<H>(&self, state: &mut H)
641 where
642 H: Hasher,
643 {
644 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700645 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700646}
647
648ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800649 /// A method's signature in a trait or implementation: `unsafe fn
650 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800651 ///
652 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500654 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500655 pub unsafety: Option<Token![unsafe]>,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900656 pub asyncness: Option<Token![async]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700658 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700659 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700660 }
661}
662
663ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800664 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800665 ///
666 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700667 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800668 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500669 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500670 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500671 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500672 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500673 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700675}
676
Alex Crichton62a0a592017-05-22 13:58:53 -0700677ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800678 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800679 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800680 /// *This type is available if Syn is built with the `"full"` feature.*
681 ///
David Tolnay614a0142018-01-07 10:25:43 -0800682 /// # Syntax tree enum
683 ///
684 /// This type is a [syntax tree enum].
685 ///
686 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800688 /// Self captured by reference in a function signature: `&self` or `&mut
689 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800690 ///
691 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800693 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500695 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500696 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700697 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800698
David Tolnay3f559052018-01-06 23:59:48 -0800699 /// Self captured by value in a function signature: `self` or `mut
700 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800701 ///
702 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500704 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800705 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700706 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800707
David Tolnay3f559052018-01-06 23:59:48 -0800708 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800709 ///
710 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 pub Captured(ArgCaptured {
712 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800713 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800714 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700715 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800716
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500718 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800719 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800720 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 }
David Tolnay62f374c2016-10-02 13:37:00 -0700722}
723
David Tolnayedf2b992016-09-23 20:43:45 -0700724#[cfg(feature = "parsing")]
725pub mod parsing {
726 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700727
David Tolnay9be32582018-07-31 22:37:26 -0700728 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700729
David Tolnay4c614be2017-11-10 00:02:38 -0800730 impl_synom!(Item "item" alt!(
731 syn!(ItemExternCrate) => { Item::ExternCrate }
732 |
733 syn!(ItemUse) => { Item::Use }
734 |
735 syn!(ItemStatic) => { Item::Static }
736 |
737 syn!(ItemConst) => { Item::Const }
738 |
739 syn!(ItemFn) => { Item::Fn }
740 |
741 syn!(ItemMod) => { Item::Mod }
742 |
743 syn!(ItemForeignMod) => { Item::ForeignMod }
744 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800745 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800746 |
David Tolnay758ee132018-08-21 21:29:40 -0400747 call!(unstable_existential_type) => { Item::Verbatim }
748 |
David Tolnay4c614be2017-11-10 00:02:38 -0800749 syn!(ItemStruct) => { Item::Struct }
750 |
751 syn!(ItemEnum) => { Item::Enum }
752 |
753 syn!(ItemUnion) => { Item::Union }
754 |
755 syn!(ItemTrait) => { Item::Trait }
756 |
David Tolnay4c614be2017-11-10 00:02:38 -0800757 syn!(ItemImpl) => { Item::Impl }
758 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800759 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800760 |
761 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800762 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700763
David Tolnaydecf28d2017-11-11 11:56:45 -0800764 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500765 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -0800766 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700768 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500769 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500770 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800771 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700772 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800773 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800774 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500775 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700776 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500777 delimiter: body.0,
778 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800779 },
David Tolnay57292da2017-12-27 21:03:33 -0500780 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800781 })
David Tolnayedf2b992016-09-23 20:43:45 -0700782 ));
783
David Tolnay500d8322017-12-18 00:32:51 -0800784 // TODO: figure out the actual grammar; is body required to be braced?
785 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500786 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800787 vis: syn!(Visibility) >>
788 macro_: keyword!(macro) >>
789 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500790 args: call!(tt::parenthesized) >>
791 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800792 (ItemMacro2 {
793 attrs: attrs,
794 vis: vis,
795 macro_token: macro_,
796 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500797 paren_token: args.0,
798 args: args.1,
799 brace_token: body.0,
800 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800801 })
802 ));
803
David Tolnay4c614be2017-11-10 00:02:38 -0800804 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500805 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700806 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800807 extern_: keyword!(extern) >>
808 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700809 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800810 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
811 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800812 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700813 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800814 vis: vis,
815 extern_token: extern_,
816 crate_token: crate_,
817 ident: ident,
818 rename: rename,
819 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800820 })
David Tolnayedf2b992016-09-23 20:43:45 -0700821 ));
822
David Tolnay4c614be2017-11-10 00:02:38 -0800823 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500824 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700825 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800826 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500827 leading_colon: option!(punct!(::)) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200828 tree: syn!(UseTree) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800829 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800830 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700831 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800832 vis: vis,
833 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500834 leading_colon: leading_colon,
David Tolnay5f332a92017-12-26 00:42:45 -0500835 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800836 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800837 })
David Tolnay4a057422016-10-08 00:02:31 -0700838 ));
839
David Tolnayd97a7d22018-03-31 19:17:01 +0200840 named!(use_element -> Ident, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500841 syn!(Ident)
842 |
843 keyword!(self) => { Into::into }
844 |
845 keyword!(super) => { Into::into }
846 |
847 keyword!(crate) => { Into::into }
David Tolnay0a4d4e92018-07-21 15:31:45 -0700848 |
849 keyword!(extern) => { Into::into }
David Tolnay5f332a92017-12-26 00:42:45 -0500850 ));
851
852 impl_synom!(UseTree "use tree" alt!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200853 syn!(UseRename) => { UseTree::Rename }
854 |
David Tolnay5f332a92017-12-26 00:42:45 -0500855 syn!(UsePath) => { UseTree::Path }
856 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200857 syn!(UseName) => { UseTree::Name }
858 |
David Tolnay5f332a92017-12-26 00:42:45 -0500859 syn!(UseGlob) => { UseTree::Glob }
860 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200861 syn!(UseGroup) => { UseTree::Group }
David Tolnay5f332a92017-12-26 00:42:45 -0500862 ));
863
864 impl_synom!(UsePath "use path" do_parse!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200865 ident: call!(use_element) >>
866 colon2_token: punct!(::) >>
867 tree: syn!(UseTree) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500868 (UsePath {
869 ident: ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200870 colon2_token: colon2_token,
871 tree: Box::new(tree),
872 })
873 ));
874
875 impl_synom!(UseName "use name" do_parse!(
876 ident: call!(use_element) >>
877 (UseName {
878 ident: ident,
879 })
880 ));
881
882 impl_synom!(UseRename "use rename" do_parse!(
883 ident: call!(use_element) >>
884 as_token: keyword!(as) >>
885 rename: syn!(Ident) >>
886 (UseRename {
887 ident: ident,
888 as_token: as_token,
David Tolnay5f332a92017-12-26 00:42:45 -0500889 rename: rename,
890 })
891 ));
David Tolnay4a057422016-10-08 00:02:31 -0700892
David Tolnay5f332a92017-12-26 00:42:45 -0500893 impl_synom!(UseGlob "use glob" do_parse!(
894 star: punct!(*) >>
895 (UseGlob {
896 star_token: star,
897 })
898 ));
David Tolnay4a057422016-10-08 00:02:31 -0700899
David Tolnayd97a7d22018-03-31 19:17:01 +0200900 impl_synom!(UseGroup "use group" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500901 list: braces!(Punctuated::parse_terminated) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200902 (UseGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500903 brace_token: list.0,
904 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500905 })
906 ));
David Tolnay4a057422016-10-08 00:02:31 -0700907
David Tolnay4c614be2017-11-10 00:02:38 -0800908 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500909 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700910 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800911 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500912 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700913 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800915 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800916 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700917 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800918 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800919 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700920 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800921 vis: vis,
922 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500923 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800924 ident: ident,
925 colon_token: colon,
926 ty: Box::new(ty),
927 eq_token: eq,
928 expr: Box::new(value),
929 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800930 })
David Tolnay47a877c2016-10-01 16:50:55 -0700931 ));
932
David Tolnay4c614be2017-11-10 00:02:38 -0800933 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500934 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700935 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800936 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700937 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800938 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800939 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800940 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700941 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800942 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800943 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700944 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800945 vis: vis,
946 const_token: const_,
947 ident: ident,
948 colon_token: colon,
949 ty: Box::new(ty),
950 eq_token: eq,
951 expr: Box::new(value),
952 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800953 })
David Tolnay47a877c2016-10-01 16:50:55 -0700954 ));
955
David Tolnay4c614be2017-11-10 00:02:38 -0800956 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500957 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700958 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500959 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500960 unsafety: option!(keyword!(unsafe)) >>
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900961 asyncness: option!(keyword!(async)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700962 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700964 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700965 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500966 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800967 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500968 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700969 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500970 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -0700971 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -0400972 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800973 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700974 attrs: {
975 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500976 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700977 attrs
978 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800979 vis: vis,
980 constness: constness,
981 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +0900982 asyncness: asyncness,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800983 abi: abi,
984 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800985 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500986 paren_token: inputs.0,
987 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800988 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500989 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800990 generics: Generics {
991 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -0700992 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -0800993 },
994 }),
995 ident: ident,
996 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500997 brace_token: inner_attrs_stmts.0,
998 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800999 }),
David Tolnay4c614be2017-11-10 00:02:38 -08001000 })
David Tolnay42602292016-10-01 22:25:45 -07001001 ));
1002
Alex Crichton954046c2017-05-30 21:49:42 -07001003 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -04001004 named!(parse -> Self, alt!(
1005 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001006 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001007 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001008 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001009 self_: keyword!(self) >>
1010 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001011 (ArgSelfRef {
1012 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -05001013 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001014 and_token: and,
1015 self_token: self_,
1016 }.into())
1017 )
1018 |
1019 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05001020 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 self_: keyword!(self) >>
1022 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001023 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -05001024 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001025 self_token: self_,
1026 }.into())
1027 )
1028 |
1029 do_parse!(
1030 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001032 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001033 (ArgCaptured {
1034 pat: pat,
1035 ty: ty,
1036 colon_token: colon,
1037 }.into())
1038 )
1039 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001040 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -04001041 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001042
1043 fn description() -> Option<&'static str> {
1044 Some("function argument")
1045 }
Alex Crichton954046c2017-05-30 21:49:42 -07001046 }
David Tolnay62f374c2016-10-02 13:37:00 -07001047
David Tolnay4c614be2017-11-10 00:02:38 -08001048 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001049 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001050 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001051 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -07001052 ident: syn!(Ident) >>
1053 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001054 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -07001055 Vec::new(),
1056 None,
1057 Some(semi),
1058 )}
David Tolnay37d10332016-10-13 20:51:04 -07001059 |
Alex Crichton954046c2017-05-30 21:49:42 -07001060 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -07001061 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001062 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001063 many0!(Item::parse),
Michael Layzell416724e2017-05-24 21:12:34 -04001064 )
David Tolnay8875fca2017-12-31 13:52:37 -05001065 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -07001066 inner_attrs,
1067 Some((brace, items)),
1068 None,
1069 )}
David Tolnay37d10332016-10-13 20:51:04 -07001070 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001071 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -07001072 attrs: {
1073 let mut attrs = outer_attrs;
1074 attrs.extend(content_semi.0);
1075 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -07001076 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001077 vis: vis,
1078 mod_token: mod_,
1079 ident: ident,
1080 content: content_semi.1,
1081 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -08001082 })
David Tolnay35902302016-10-06 01:11:08 -07001083 ));
1084
David Tolnay4c614be2017-11-10 00:02:38 -08001085 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay5c4613a2018-07-21 15:40:17 -07001086 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001087 abi: syn!(Abi) >>
David Tolnay5c4613a2018-07-21 15:40:17 -07001088 braced_content: braces!(tuple!(
1089 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001090 many0!(ForeignItem::parse),
David Tolnay5c4613a2018-07-21 15:40:17 -07001091 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001092 (ItemForeignMod {
David Tolnay5c4613a2018-07-21 15:40:17 -07001093 attrs: {
1094 let mut attrs = outer_attrs;
1095 attrs.extend((braced_content.1).0);
1096 attrs
1097 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001098 abi: abi,
David Tolnay5c4613a2018-07-21 15:40:17 -07001099 brace_token: braced_content.0,
1100 items: (braced_content.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001101 })
David Tolnay35902302016-10-06 01:11:08 -07001102 ));
1103
David Tolnay8894f602017-11-11 12:11:04 -08001104 impl_synom!(ForeignItem "foreign item" alt!(
1105 syn!(ForeignItemFn) => { ForeignItem::Fn }
1106 |
1107 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001108 |
1109 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay78572112018-08-01 00:36:18 -07001110 |
David Tolnay435c1782018-08-24 16:15:44 -04001111 syn!(ForeignItemMacro) => { ForeignItem::Macro }
David Tolnay8894f602017-11-11 12:11:04 -08001112 ));
David Tolnay35902302016-10-06 01:11:08 -07001113
David Tolnay8894f602017-11-11 12:11:04 -08001114 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001115 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001116 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001117 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001118 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001119 generics: syn!(Generics) >>
1120 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001121 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001122 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001123 (args, variadic)
1124 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001125 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001126 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001127 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001128 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001129 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -08001130 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -07001131 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001132 attrs: attrs,
1133 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001134 decl: Box::new(FnDecl {
1135 fn_token: fn_,
1136 paren_token: parens,
1137 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -05001138 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001139 output: ret,
1140 generics: Generics {
1141 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001142 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001143 },
1144 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001145 vis: vis,
1146 }
David Tolnay35902302016-10-06 01:11:08 -07001147 })
1148 ));
1149
David Tolnay8894f602017-11-11 12:11:04 -08001150 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001151 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001152 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001153 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001154 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -07001155 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001156 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001157 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001158 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -08001159 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001160 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001161 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001162 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001163 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001164 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001165 static_token: static_,
1166 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001167 vis: vis,
1168 })
1169 ));
1170
David Tolnay199bcbb2017-11-12 10:33:52 -08001171 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001172 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001173 vis: syn!(Visibility) >>
1174 type_: keyword!(type) >>
1175 ident: syn!(Ident) >>
1176 semi: punct!(;) >>
1177 (ForeignItemType {
1178 attrs: attrs,
1179 vis: vis,
1180 type_token: type_,
1181 ident: ident,
1182 semi_token: semi,
1183 })
1184 ));
1185
David Tolnay435c1782018-08-24 16:15:44 -04001186 impl_synom!(ForeignItemMacro "macro in extern block" do_parse!(
1187 attrs: many0!(Attribute::parse_outer) >>
David Tolnay78572112018-08-01 00:36:18 -07001188 mac: syn!(Macro) >>
David Tolnay435c1782018-08-24 16:15:44 -04001189 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1190 (ForeignItemMacro {
1191 attrs: attrs,
1192 mac: mac,
1193 semi_token: semi,
David Tolnay78572112018-08-01 00:36:18 -07001194 })
1195 ));
1196
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001197 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001198 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001199 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001200 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001201 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001202 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001203 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001204 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001205 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001206 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001207 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001208 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001209 vis: vis,
1210 type_token: type_,
1211 ident: ident,
1212 generics: Generics {
1213 where_clause: where_clause,
1214 ..generics
1215 },
1216 eq_token: eq,
1217 ty: Box::new(ty),
1218 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001219 })
David Tolnay3cf52982016-10-01 17:11:37 -07001220 ));
1221
David Tolnay758ee132018-08-21 21:29:40 -04001222 named!(existential_type_helper(vis: bool) -> TokenStream, do_parse!(
1223 begin: call!(verbatim::grab_cursor) >>
1224 many0!(Attribute::parse_outer) >>
1225 cond_reduce!(vis, syn!(Visibility)) >>
1226 custom_keyword!(existential) >>
1227 keyword!(type) >>
1228 syn!(Ident) >>
1229 syn!(Generics) >>
1230 option!(syn!(WhereClause)) >>
1231 colon: option!(punct!(:)) >>
1232 cond!(
1233 colon.is_some(),
1234 Punctuated::<TypeParamBound, Token![+]>::parse_separated_nonempty
1235 ) >>
1236 punct!(;) >>
1237 end: call!(verbatim::grab_cursor) >>
1238 (verbatim::token_range(begin..end))
1239 ));
1240
1241 named!(unstable_existential_type -> ItemVerbatim, map!(
1242 call!(existential_type_helper, true),
1243 |tts| ItemVerbatim { tts: tts }
1244 ));
1245
David Tolnay4c614be2017-11-10 00:02:38 -08001246 impl_synom!(ItemStruct "struct item" switch!(
1247 map!(syn!(DeriveInput), Into::into),
1248 Item::Struct(item) => value!(item)
1249 |
1250 _ => reject!()
1251 ));
David Tolnay42602292016-10-01 22:25:45 -07001252
David Tolnay4c614be2017-11-10 00:02:38 -08001253 impl_synom!(ItemEnum "enum item" switch!(
1254 map!(syn!(DeriveInput), Into::into),
1255 Item::Enum(item) => value!(item)
1256 |
1257 _ => reject!()
1258 ));
1259
1260 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001261 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001262 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001264 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001265 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001266 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001267 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001268 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001269 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001270 vis: vis,
1271 union_token: union_,
1272 ident: ident,
1273 generics: Generics {
1274 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001275 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001276 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001277 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001278 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001279 ));
1280
David Tolnay4c614be2017-11-10 00:02:38 -08001281 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001282 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001283 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001284 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001285 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001286 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001287 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001288 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001289 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001290 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001291 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001292 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001293 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001294 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001295 vis: vis,
1296 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001297 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001298 trait_token: trait_,
1299 ident: ident,
1300 generics: Generics {
1301 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001302 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001303 },
1304 colon_token: colon,
1305 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001306 brace_token: body.0,
1307 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001308 })
David Tolnay0aecb732016-10-03 23:03:50 -07001309 ));
1310
David Tolnayda705bd2017-11-10 21:58:05 -08001311 impl_synom!(TraitItem "trait item" alt!(
1312 syn!(TraitItemConst) => { TraitItem::Const }
1313 |
1314 syn!(TraitItemMethod) => { TraitItem::Method }
1315 |
1316 syn!(TraitItemType) => { TraitItem::Type }
1317 |
David Tolnay758ee132018-08-21 21:29:40 -04001318 call!(unstable_trait_existential_type) => { TraitItem::Verbatim }
1319 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001320 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001321 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001322
David Tolnayda705bd2017-11-10 21:58:05 -08001323 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001324 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001325 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001326 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001327 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001328 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001329 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1330 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001331 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001332 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001333 const_token: const_,
1334 ident: ident,
1335 colon_token: colon,
1336 ty: ty,
1337 default: default,
1338 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001339 })
1340 ));
1341
David Tolnayda705bd2017-11-10 21:58:05 -08001342 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001343 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001344 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001345 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001346 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001348 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001349 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001350 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001351 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001352 where_clause: option!(syn!(WhereClause)) >>
David Tolnay76178be2018-07-31 23:06:15 -07001353 body: option!(braces!(tuple!(
1354 many0!(Attribute::parse_inner),
1355 call!(Block::parse_within),
1356 ))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001357 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001358 ({
1359 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001360 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001361 None => (Vec::new(), None),
1362 };
David Tolnayda705bd2017-11-10 21:58:05 -08001363 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001364 attrs: {
1365 let mut attrs = outer_attrs;
1366 attrs.extend(inner_attrs);
1367 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001368 },
David Tolnayda705bd2017-11-10 21:58:05 -08001369 sig: MethodSig {
1370 constness: constness,
1371 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001372 asyncness: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001373 abi: abi,
1374 ident: ident,
1375 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001376 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001377 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001378 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001379 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001380 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001381 generics: Generics {
1382 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001383 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001384 },
1385 },
David Tolnayda705bd2017-11-10 21:58:05 -08001386 },
1387 default: stmts.map(|stmts| {
1388 Block {
1389 stmts: stmts.0,
1390 brace_token: stmts.1,
1391 }
1392 }),
1393 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001394 }
David Tolnay0aecb732016-10-03 23:03:50 -07001395 })
1396 ));
1397
David Tolnayda705bd2017-11-10 21:58:05 -08001398 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001399 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001400 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001401 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001402 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001403 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001404 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001405 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001406 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001407 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001408 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001409 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001410 type_token: type_,
1411 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001412 generics: Generics {
1413 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001414 ..generics
Nika Layzell0183ca32017-12-05 15:24:01 -05001415 },
David Tolnayda705bd2017-11-10 21:58:05 -08001416 colon_token: colon,
1417 bounds: bounds.unwrap_or_default(),
1418 default: default,
1419 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001420 })
1421 ));
1422
David Tolnay758ee132018-08-21 21:29:40 -04001423 named!(unstable_trait_existential_type -> TraitItemVerbatim, map!(
1424 call!(existential_type_helper, false),
1425 |tts| TraitItemVerbatim { tts: tts }
1426 ));
1427
David Tolnaydecf28d2017-11-11 11:56:45 -08001428 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001429 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001430 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001431 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001432 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001433 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001434 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001435 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001436 })
1437 ));
1438
David Tolnay4c614be2017-11-10 00:02:38 -08001439 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnaycf3697a2018-03-31 20:51:15 +02001440 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001441 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001442 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001443 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001444 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001445 polarity_path: alt!(
1446 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001447 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001448 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001449 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001450 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001451 )
1452 |
David Tolnay570695e2017-06-03 16:15:13 -07001453 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001454 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001455 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001456 where_clause: option!(syn!(WhereClause)) >>
David Tolnaycf3697a2018-03-31 20:51:15 +02001457 inner: braces!(tuple!(
1458 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001459 many0!(ImplItem::parse),
David Tolnaycf3697a2018-03-31 20:51:15 +02001460 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001461 (ItemImpl {
David Tolnaycf3697a2018-03-31 20:51:15 +02001462 attrs: {
1463 let mut attrs = outer_attrs;
1464 attrs.extend((inner.1).0);
1465 attrs
1466 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001467 defaultness: defaultness,
1468 unsafety: unsafety,
1469 impl_token: impl_,
1470 generics: Generics {
1471 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001472 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001473 },
1474 trait_: polarity_path,
1475 self_ty: Box::new(self_ty),
David Tolnaycf3697a2018-03-31 20:51:15 +02001476 brace_token: inner.0,
1477 items: (inner.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001478 })
David Tolnay4c9be372016-10-06 00:47:37 -07001479 ));
1480
David Tolnay857628c2017-11-11 12:25:31 -08001481 impl_synom!(ImplItem "item in impl block" alt!(
1482 syn!(ImplItemConst) => { ImplItem::Const }
1483 |
1484 syn!(ImplItemMethod) => { ImplItem::Method }
1485 |
1486 syn!(ImplItemType) => { ImplItem::Type }
1487 |
David Tolnay758ee132018-08-21 21:29:40 -04001488 call!(unstable_impl_existential_type) => { ImplItem::Verbatim }
1489 |
David Tolnay857628c2017-11-11 12:25:31 -08001490 syn!(ImplItemMacro) => { ImplItem::Macro }
1491 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001492
David Tolnay857628c2017-11-11 12:25:31 -08001493 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001494 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001495 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001496 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001497 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001498 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001499 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001500 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001501 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001502 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001503 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001504 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001505 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001506 vis: vis,
1507 defaultness: defaultness,
1508 const_token: const_,
1509 ident: ident,
1510 colon_token: colon,
1511 ty: ty,
1512 eq_token: eq,
1513 expr: value,
1514 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001515 })
1516 ));
1517
David Tolnay857628c2017-11-11 12:25:31 -08001518 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001519 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001520 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001521 defaultness: option!(keyword!(default)) >>
1522 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001523 unsafety: option!(keyword!(unsafe)) >>
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001524 asyncness: option!(keyword!(async)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001525 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001527 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001528 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001529 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001530 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001531 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001532 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001533 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001534 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -04001535 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001536 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001537 attrs: {
1538 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001539 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001540 attrs
1541 },
David Tolnay857628c2017-11-11 12:25:31 -08001542 vis: vis,
1543 defaultness: defaultness,
1544 sig: MethodSig {
1545 constness: constness,
1546 unsafety: unsafety,
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001547 asyncness: asyncness,
David Tolnay857628c2017-11-11 12:25:31 -08001548 abi: abi,
1549 ident: ident,
1550 decl: FnDecl {
1551 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001552 paren_token: inputs.0,
1553 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001554 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001555 generics: Generics {
1556 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001557 ..generics
David Tolnay4c9be372016-10-06 00:47:37 -07001558 },
David Tolnayd2836e22017-12-27 23:13:00 -05001559 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001560 },
David Tolnay857628c2017-11-11 12:25:31 -08001561 },
1562 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001563 brace_token: inner_attrs_stmts.0,
1564 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001565 },
David Tolnay4c9be372016-10-06 00:47:37 -07001566 })
1567 ));
1568
David Tolnay857628c2017-11-11 12:25:31 -08001569 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001570 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001571 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001572 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001573 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001574 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001575 generics: syn!(Generics) >>
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001576 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001577 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001578 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001579 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001580 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001581 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001582 vis: vis,
1583 defaultness: defaultness,
1584 type_token: type_,
1585 ident: ident,
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001586 generics: Generics {
1587 where_clause: where_clause,
1588 ..generics
1589 },
David Tolnay857628c2017-11-11 12:25:31 -08001590 eq_token: eq,
1591 ty: ty,
1592 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001593 })
1594 ));
1595
David Tolnay758ee132018-08-21 21:29:40 -04001596 named!(unstable_impl_existential_type -> ImplItemVerbatim, map!(
1597 call!(existential_type_helper, true),
1598 |tts| ImplItemVerbatim { tts: tts }
1599 ));
1600
David Tolnay857628c2017-11-11 12:25:31 -08001601 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001602 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001603 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001604 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001605 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001606 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001607 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001608 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001609 })
1610 ));
1611
David Tolnayab919512017-12-30 23:31:51 -05001612 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1613 match *delimiter {
1614 MacroDelimiter::Brace(_) => true,
1615 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001616 }
1617 }
David Tolnayedf2b992016-09-23 20:43:45 -07001618}
David Tolnay4a51dc72016-10-01 00:40:31 -07001619
1620#[cfg(feature = "printing")]
1621mod printing {
1622 use super::*;
1623 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001624 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001625 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001626
David Tolnay1bfa7332017-11-11 12:41:20 -08001627 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001628 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001629 tokens.append_all(self.attrs.outer());
1630 self.vis.to_tokens(tokens);
1631 self.extern_token.to_tokens(tokens);
1632 self.crate_token.to_tokens(tokens);
1633 self.ident.to_tokens(tokens);
1634 if let Some((ref as_token, ref rename)) = self.rename {
1635 as_token.to_tokens(tokens);
1636 rename.to_tokens(tokens);
1637 }
1638 self.semi_token.to_tokens(tokens);
1639 }
1640 }
1641
1642 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001643 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001644 tokens.append_all(self.attrs.outer());
1645 self.vis.to_tokens(tokens);
1646 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001647 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001648 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001649 self.semi_token.to_tokens(tokens);
1650 }
1651 }
1652
1653 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001654 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001655 tokens.append_all(self.attrs.outer());
1656 self.vis.to_tokens(tokens);
1657 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001658 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001659 self.ident.to_tokens(tokens);
1660 self.colon_token.to_tokens(tokens);
1661 self.ty.to_tokens(tokens);
1662 self.eq_token.to_tokens(tokens);
1663 self.expr.to_tokens(tokens);
1664 self.semi_token.to_tokens(tokens);
1665 }
1666 }
1667
1668 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001669 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001670 tokens.append_all(self.attrs.outer());
1671 self.vis.to_tokens(tokens);
1672 self.const_token.to_tokens(tokens);
1673 self.ident.to_tokens(tokens);
1674 self.colon_token.to_tokens(tokens);
1675 self.ty.to_tokens(tokens);
1676 self.eq_token.to_tokens(tokens);
1677 self.expr.to_tokens(tokens);
1678 self.semi_token.to_tokens(tokens);
1679 }
1680 }
1681
1682 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001683 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001684 tokens.append_all(self.attrs.outer());
1685 self.vis.to_tokens(tokens);
1686 self.constness.to_tokens(tokens);
1687 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09001688 self.asyncness.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001689 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001690 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001691 self.block.brace_token.surround(tokens, |tokens| {
1692 tokens.append_all(self.attrs.inner());
1693 tokens.append_all(&self.block.stmts);
1694 });
1695 }
1696 }
1697
1698 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001699 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001700 tokens.append_all(self.attrs.outer());
1701 self.vis.to_tokens(tokens);
1702 self.mod_token.to_tokens(tokens);
1703 self.ident.to_tokens(tokens);
1704 if let Some((ref brace, ref items)) = self.content {
1705 brace.surround(tokens, |tokens| {
1706 tokens.append_all(self.attrs.inner());
1707 tokens.append_all(items);
1708 });
1709 } else {
1710 TokensOrDefault(&self.semi).to_tokens(tokens);
1711 }
1712 }
1713 }
1714
1715 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001716 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001717 tokens.append_all(self.attrs.outer());
1718 self.abi.to_tokens(tokens);
1719 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001720 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001721 tokens.append_all(&self.items);
1722 });
1723 }
1724 }
1725
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001726 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001727 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001728 tokens.append_all(self.attrs.outer());
1729 self.vis.to_tokens(tokens);
1730 self.type_token.to_tokens(tokens);
1731 self.ident.to_tokens(tokens);
1732 self.generics.to_tokens(tokens);
1733 self.generics.where_clause.to_tokens(tokens);
1734 self.eq_token.to_tokens(tokens);
1735 self.ty.to_tokens(tokens);
1736 self.semi_token.to_tokens(tokens);
1737 }
1738 }
1739
1740 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001741 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001742 tokens.append_all(self.attrs.outer());
1743 self.vis.to_tokens(tokens);
1744 self.enum_token.to_tokens(tokens);
1745 self.ident.to_tokens(tokens);
1746 self.generics.to_tokens(tokens);
1747 self.generics.where_clause.to_tokens(tokens);
1748 self.brace_token.surround(tokens, |tokens| {
1749 self.variants.to_tokens(tokens);
1750 });
1751 }
1752 }
1753
1754 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001755 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001756 tokens.append_all(self.attrs.outer());
1757 self.vis.to_tokens(tokens);
1758 self.struct_token.to_tokens(tokens);
1759 self.ident.to_tokens(tokens);
1760 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001761 match self.fields {
1762 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001763 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001764 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001765 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001766 Fields::Unnamed(ref fields) => {
1767 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001768 self.generics.where_clause.to_tokens(tokens);
1769 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001770 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001771 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001772 self.generics.where_clause.to_tokens(tokens);
1773 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001774 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001775 }
1776 }
1777 }
1778
1779 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001780 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001781 tokens.append_all(self.attrs.outer());
1782 self.vis.to_tokens(tokens);
1783 self.union_token.to_tokens(tokens);
1784 self.ident.to_tokens(tokens);
1785 self.generics.to_tokens(tokens);
1786 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001787 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001788 }
1789 }
1790
1791 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001792 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001793 tokens.append_all(self.attrs.outer());
1794 self.vis.to_tokens(tokens);
1795 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001796 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001797 self.trait_token.to_tokens(tokens);
1798 self.ident.to_tokens(tokens);
1799 self.generics.to_tokens(tokens);
1800 if !self.supertraits.is_empty() {
1801 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1802 self.supertraits.to_tokens(tokens);
1803 }
1804 self.generics.where_clause.to_tokens(tokens);
1805 self.brace_token.surround(tokens, |tokens| {
1806 tokens.append_all(&self.items);
1807 });
1808 }
1809 }
1810
David Tolnay1bfa7332017-11-11 12:41:20 -08001811 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001812 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001813 tokens.append_all(self.attrs.outer());
1814 self.defaultness.to_tokens(tokens);
1815 self.unsafety.to_tokens(tokens);
1816 self.impl_token.to_tokens(tokens);
1817 self.generics.to_tokens(tokens);
1818 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1819 polarity.to_tokens(tokens);
1820 path.to_tokens(tokens);
1821 for_token.to_tokens(tokens);
1822 }
1823 self.self_ty.to_tokens(tokens);
1824 self.generics.where_clause.to_tokens(tokens);
1825 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02001826 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001827 tokens.append_all(&self.items);
1828 });
1829 }
1830 }
1831
1832 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001833 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001834 tokens.append_all(self.attrs.outer());
1835 self.mac.path.to_tokens(tokens);
1836 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001837 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001838 match self.mac.delimiter {
1839 MacroDelimiter::Paren(ref paren) => {
1840 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1841 }
1842 MacroDelimiter::Brace(ref brace) => {
1843 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1844 }
1845 MacroDelimiter::Bracket(ref bracket) => {
1846 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1847 }
1848 }
David Tolnay57292da2017-12-27 21:03:33 -05001849 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001850 }
1851 }
David Tolnay42602292016-10-01 22:25:45 -07001852
David Tolnay500d8322017-12-18 00:32:51 -08001853 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001854 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08001855 tokens.append_all(self.attrs.outer());
1856 self.vis.to_tokens(tokens);
1857 self.macro_token.to_tokens(tokens);
1858 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001859 self.paren_token.surround(tokens, |tokens| {
1860 self.args.to_tokens(tokens);
1861 });
1862 self.brace_token.surround(tokens, |tokens| {
1863 self.body.to_tokens(tokens);
1864 });
David Tolnay500d8322017-12-18 00:32:51 -08001865 }
1866 }
1867
David Tolnay2ae520a2017-12-29 11:19:50 -05001868 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001869 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001870 self.tts.to_tokens(tokens);
1871 }
1872 }
1873
David Tolnay5f332a92017-12-26 00:42:45 -05001874 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001875 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05001876 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02001877 self.colon2_token.to_tokens(tokens);
1878 self.tree.to_tokens(tokens);
1879 }
1880 }
1881
1882 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001883 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001884 self.ident.to_tokens(tokens);
1885 }
1886 }
1887
1888 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001889 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001890 self.ident.to_tokens(tokens);
1891 self.as_token.to_tokens(tokens);
1892 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001893 }
1894 }
1895
David Tolnay5f332a92017-12-26 00:42:45 -05001896 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001897 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001898 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001899 }
1900 }
1901
David Tolnayd97a7d22018-03-31 19:17:01 +02001902 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001903 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001904 self.brace_token.surround(tokens, |tokens| {
1905 self.items.to_tokens(tokens);
1906 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001907 }
1908 }
1909
David Tolnay1bfa7332017-11-11 12:41:20 -08001910 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001911 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001912 tokens.append_all(self.attrs.outer());
1913 self.const_token.to_tokens(tokens);
1914 self.ident.to_tokens(tokens);
1915 self.colon_token.to_tokens(tokens);
1916 self.ty.to_tokens(tokens);
1917 if let Some((ref eq_token, ref default)) = self.default {
1918 eq_token.to_tokens(tokens);
1919 default.to_tokens(tokens);
1920 }
1921 self.semi_token.to_tokens(tokens);
1922 }
1923 }
1924
1925 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001926 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001927 tokens.append_all(self.attrs.outer());
1928 self.sig.to_tokens(tokens);
1929 match self.default {
1930 Some(ref block) => {
1931 block.brace_token.surround(tokens, |tokens| {
1932 tokens.append_all(self.attrs.inner());
1933 tokens.append_all(&block.stmts);
1934 });
David Tolnayca085422016-10-04 00:12:38 -07001935 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001936 None => {
1937 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001938 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001939 }
1940 }
1941 }
1942
1943 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001944 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001945 tokens.append_all(self.attrs.outer());
1946 self.type_token.to_tokens(tokens);
1947 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001948 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001949 if !self.bounds.is_empty() {
1950 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1951 self.bounds.to_tokens(tokens);
1952 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001953 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001954 if let Some((ref eq_token, ref default)) = self.default {
1955 eq_token.to_tokens(tokens);
1956 default.to_tokens(tokens);
1957 }
1958 self.semi_token.to_tokens(tokens);
1959 }
1960 }
1961
1962 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001963 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001964 tokens.append_all(self.attrs.outer());
1965 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001966 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001967 }
1968 }
1969
David Tolnay2ae520a2017-12-29 11:19:50 -05001970 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001971 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001972 self.tts.to_tokens(tokens);
1973 }
1974 }
1975
David Tolnay857628c2017-11-11 12:25:31 -08001976 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001977 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07001978 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001979 self.vis.to_tokens(tokens);
1980 self.defaultness.to_tokens(tokens);
1981 self.const_token.to_tokens(tokens);
1982 self.ident.to_tokens(tokens);
1983 self.colon_token.to_tokens(tokens);
1984 self.ty.to_tokens(tokens);
1985 self.eq_token.to_tokens(tokens);
1986 self.expr.to_tokens(tokens);
1987 self.semi_token.to_tokens(tokens);
1988 }
1989 }
1990
1991 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001992 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08001993 tokens.append_all(self.attrs.outer());
1994 self.vis.to_tokens(tokens);
1995 self.defaultness.to_tokens(tokens);
1996 self.sig.to_tokens(tokens);
1997 self.block.brace_token.surround(tokens, |tokens| {
1998 tokens.append_all(self.attrs.inner());
1999 tokens.append_all(&self.block.stmts);
2000 });
2001 }
2002 }
2003
2004 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002005 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002006 tokens.append_all(self.attrs.outer());
2007 self.vis.to_tokens(tokens);
2008 self.defaultness.to_tokens(tokens);
2009 self.type_token.to_tokens(tokens);
2010 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002011 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002012 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002013 self.eq_token.to_tokens(tokens);
2014 self.ty.to_tokens(tokens);
2015 self.semi_token.to_tokens(tokens);
2016 }
2017 }
2018
2019 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002020 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002021 tokens.append_all(self.attrs.outer());
2022 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002023 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002024 }
2025 }
2026
David Tolnay2ae520a2017-12-29 11:19:50 -05002027 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002028 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002029 self.tts.to_tokens(tokens);
2030 }
2031 }
2032
David Tolnay8894f602017-11-11 12:11:04 -08002033 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002034 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002035 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002036 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002037 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002038 self.semi_token.to_tokens(tokens);
2039 }
2040 }
2041
2042 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002043 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002044 tokens.append_all(self.attrs.outer());
2045 self.vis.to_tokens(tokens);
2046 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002047 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002048 self.ident.to_tokens(tokens);
2049 self.colon_token.to_tokens(tokens);
2050 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002051 self.semi_token.to_tokens(tokens);
2052 }
2053 }
2054
David Tolnay199bcbb2017-11-12 10:33:52 -08002055 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002056 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002057 tokens.append_all(self.attrs.outer());
2058 self.vis.to_tokens(tokens);
2059 self.type_token.to_tokens(tokens);
2060 self.ident.to_tokens(tokens);
2061 self.semi_token.to_tokens(tokens);
2062 }
2063 }
2064
David Tolnay435c1782018-08-24 16:15:44 -04002065 impl ToTokens for ForeignItemMacro {
2066 fn to_tokens(&self, tokens: &mut TokenStream) {
2067 tokens.append_all(self.attrs.outer());
2068 self.mac.to_tokens(tokens);
2069 self.semi_token.to_tokens(tokens);
2070 }
2071 }
2072
David Tolnay2ae520a2017-12-29 11:19:50 -05002073 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002074 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002075 self.tts.to_tokens(tokens);
2076 }
2077 }
2078
David Tolnay570695e2017-06-03 16:15:13 -07002079 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002080 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002081 self.constness.to_tokens(tokens);
2082 self.unsafety.to_tokens(tokens);
Yusuke Sasakif00a3ef2018-07-20 22:08:42 +09002083 self.asyncness.to_tokens(tokens);
David Tolnay570695e2017-06-03 16:15:13 -07002084 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002085 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002086 }
2087 }
2088
Alex Crichtona74a1c82018-05-16 10:20:44 -07002089 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002090
2091 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002092 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002093 self.0.fn_token.to_tokens(tokens);
2094 self.1.to_tokens(tokens);
2095 self.0.generics.to_tokens(tokens);
2096 self.0.paren_token.surround(tokens, |tokens| {
2097 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002098 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2099 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002100 }
David Tolnayd2836e22017-12-27 23:13:00 -05002101 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002102 });
2103 self.0.output.to_tokens(tokens);
2104 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002105 }
2106 }
2107
Alex Crichton62a0a592017-05-22 13:58:53 -07002108 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002109 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002110 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002111 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002112 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002113 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002114 }
2115 }
2116
2117 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002118 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002119 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002120 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002121 }
2122 }
2123
2124 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002125 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002126 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002127 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002128 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002129 }
2130 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002131}