blob: 7e49a4a656767bbd8f9a099baa9ca5a394b766df [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]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070096 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070097 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050098 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub block: Box<Block>,
100 }),
David Tolnay2b214082018-01-07 01:30:18 -0800101
102 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800103 ///
104 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800106 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700107 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500110 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
David Tolnay2b214082018-01-07 01:30:18 -0800113
114 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800115 ///
116 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700117 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800118 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700119 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500120 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121 pub items: Vec<ForeignItem>,
122 }),
David Tolnay2b214082018-01-07 01:30:18 -0800123
124 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800125 ///
126 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800127 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800128 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700129 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800134 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
David Tolnay2b214082018-01-07 01:30:18 -0800137
138 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800139 ///
140 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500141 pub Struct(ItemStruct {
142 pub attrs: Vec<Attribute>,
143 pub vis: Visibility,
144 pub struct_token: Token![struct],
145 pub ident: Ident,
146 pub generics: Generics,
147 pub fields: Fields,
148 pub semi_token: Option<Token![;]>,
149 }),
David Tolnay2b214082018-01-07 01:30:18 -0800150
151 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800152 ///
153 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700154 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800155 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700156 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700158 pub ident: Ident,
159 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500160 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500161 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 }),
David Tolnay2b214082018-01-07 01:30:18 -0800163
164 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800165 ///
166 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800168 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700169 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800170 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700171 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500173 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
David Tolnay2b214082018-01-07 01:30:18 -0800175
176 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800177 ///
178 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800180 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700181 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500182 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500183 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800184 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500188 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500189 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 pub items: Vec<TraitItem>,
191 }),
David Tolnay2b214082018-01-07 01:30:18 -0800192
193 /// An impl block providing trait or associated items: `impl<A> Trait
194 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800195 ///
196 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800198 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500199 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500200 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700203 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500204 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700205 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500207 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700208 pub items: Vec<ImplItem>,
209 }),
David Tolnay2b214082018-01-07 01:30:18 -0800210
211 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800212 ///
213 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800214 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800215 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800216 /// The `example` in `macro_rules! example { ... }`.
217 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800218 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500219 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800220 }),
David Tolnay2b214082018-01-07 01:30:18 -0800221
222 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800223 ///
224 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500225 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800226 pub attrs: Vec<Attribute>,
227 pub vis: Visibility,
228 pub macro_token: Token![macro],
229 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500230 pub paren_token: Paren,
231 pub args: TokenStream,
232 pub brace_token: Brace,
233 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800234 }),
David Tolnay2b214082018-01-07 01:30:18 -0800235
236 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800237 ///
238 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500239 pub Verbatim(ItemVerbatim #manual_extra_traits {
240 pub tts: TokenStream,
241 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 }
David Tolnayb79ee962016-09-04 09:39:20 -0700243}
244
David Tolnay9c76bcb2017-12-26 23:14:59 -0500245#[cfg(feature = "extra-traits")]
246impl Eq for ItemMacro2 {}
247
248#[cfg(feature = "extra-traits")]
249impl PartialEq for ItemMacro2 {
250 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700251 self.attrs == other.attrs
252 && self.vis == other.vis
253 && self.macro_token == other.macro_token
254 && self.ident == other.ident
255 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500256 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
257 && self.brace_token == other.brace_token
258 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500259 }
260}
261
262#[cfg(feature = "extra-traits")]
263impl Hash for ItemMacro2 {
264 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500265 where
266 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500267 {
268 self.attrs.hash(state);
269 self.vis.hash(state);
270 self.macro_token.hash(state);
271 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500272 self.paren_token.hash(state);
273 TokenStreamHelper(&self.args).hash(state);
274 self.brace_token.hash(state);
275 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500276 }
277}
278
David Tolnay2ae520a2017-12-29 11:19:50 -0500279#[cfg(feature = "extra-traits")]
280impl Eq for ItemVerbatim {}
281
282#[cfg(feature = "extra-traits")]
283impl PartialEq for ItemVerbatim {
284 fn eq(&self, other: &Self) -> bool {
285 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
286 }
287}
288
289#[cfg(feature = "extra-traits")]
290impl Hash for ItemVerbatim {
291 fn hash<H>(&self, state: &mut H)
292 where
293 H: Hasher,
294 {
295 TokenStreamHelper(&self.tts).hash(state);
296 }
297}
298
David Tolnay0e837402016-12-22 17:25:55 -0500299impl From<DeriveInput> for Item {
300 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500301 match input.data {
302 Data::Struct(data) => Item::Struct(ItemStruct {
303 attrs: input.attrs,
304 vis: input.vis,
305 struct_token: data.struct_token,
306 ident: input.ident,
307 generics: input.generics,
308 fields: data.fields,
309 semi_token: data.semi_token,
310 }),
311 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500312 attrs: input.attrs,
313 vis: input.vis,
314 enum_token: data.enum_token,
315 ident: input.ident,
316 generics: input.generics,
317 brace_token: data.brace_token,
318 variants: data.variants,
319 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500320 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500321 attrs: input.attrs,
322 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500323 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500324 ident: input.ident,
325 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500326 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500327 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700328 }
329 }
330}
331
Alex Crichton62a0a592017-05-22 13:58:53 -0700332ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800333 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800334 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800335 /// *This type is available if Syn is built with the `"full"` feature.*
336 ///
David Tolnay614a0142018-01-07 10:25:43 -0800337 /// # Syntax tree enum
338 ///
339 /// This type is a [syntax tree enum].
340 ///
341 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500342 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200343 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800344 ///
345 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500346 pub Path(UsePath {
347 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200348 pub colon2_token: Token![::],
349 pub tree: Box<UseTree>,
350 }),
351
352 /// An identifier imported by a `use` item: `HashMap`.
353 ///
354 /// *This type is available if Syn is built with the `"full"` feature.*
355 pub Name(UseName {
356 pub ident: Ident,
357 }),
358
359 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
362 pub Rename(UseRename {
363 pub ident: Ident,
364 pub as_token: Token![as],
365 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800367
David Tolnay05658502018-01-07 09:56:37 -0800368 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500371 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800372 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800374
David Tolnayd97a7d22018-03-31 19:17:01 +0200375 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200378 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500379 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500380 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
382 }
383}
384
Alex Crichton62a0a592017-05-22 13:58:53 -0700385ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800386 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800387 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800388 /// *This type is available if Syn is built with the `"full"` feature.*
389 ///
David Tolnay614a0142018-01-07 10:25:43 -0800390 /// # Syntax tree enum
391 ///
392 /// This type is a [syntax tree enum].
393 ///
394 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800395 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800396 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800397 ///
398 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800400 pub attrs: Vec<Attribute>,
401 pub vis: Visibility,
402 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800404 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }),
David Tolnayebb72722018-01-07 01:14:13 -0800406
407 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800408 ///
409 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700410 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub attrs: Vec<Attribute>,
412 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800413 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500414 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800415 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800416 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800417 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 }),
David Tolnayebb72722018-01-07 01:14:13 -0800420
421 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800422 ///
423 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800424 pub Type(ForeignItemType {
425 pub attrs: Vec<Attribute>,
426 pub vis: Visibility,
427 pub type_token: Token![type],
428 pub ident: Ident,
429 pub semi_token: Token![;],
430 }),
David Tolnayebb72722018-01-07 01:14:13 -0800431
432 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800433 ///
434 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500435 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
436 pub tts: TokenStream,
437 }),
438 }
439}
440
441#[cfg(feature = "extra-traits")]
442impl Eq for ForeignItemVerbatim {}
443
444#[cfg(feature = "extra-traits")]
445impl PartialEq for ForeignItemVerbatim {
446 fn eq(&self, other: &Self) -> bool {
447 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
448 }
449}
450
451#[cfg(feature = "extra-traits")]
452impl Hash for ForeignItemVerbatim {
453 fn hash<H>(&self, state: &mut H)
454 where
455 H: Hasher,
456 {
457 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700458 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700459}
460
David Tolnayda705bd2017-11-10 21:58:05 -0800461ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800462 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800463 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800464 /// *This type is available if Syn is built with the `"full"` feature.*
465 ///
David Tolnay614a0142018-01-07 10:25:43 -0800466 /// # Syntax tree enum
467 ///
468 /// This type is a [syntax tree enum].
469 ///
470 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800471 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800472 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800473 ///
474 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700475 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800476 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800477 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700478 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800479 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800480 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800481 pub default: Option<(Token![=], Expr)>,
482 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 }),
David Tolnayebb72722018-01-07 01:14:13 -0800484
485 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800486 ///
487 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700488 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800489 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700490 pub sig: MethodSig,
491 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800492 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700493 }),
David Tolnayebb72722018-01-07 01:14:13 -0800494
495 /// An associated type 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 Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800499 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800500 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700501 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500502 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800503 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500504 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800505 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800506 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 }),
David Tolnayebb72722018-01-07 01:14:13 -0800508
509 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800510 ///
511 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800512 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800513 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800514 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500515 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800516 }),
David Tolnayebb72722018-01-07 01:14:13 -0800517
518 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800519 ///
520 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500521 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
522 pub tts: TokenStream,
523 }),
524 }
525}
526
527#[cfg(feature = "extra-traits")]
528impl Eq for TraitItemVerbatim {}
529
530#[cfg(feature = "extra-traits")]
531impl PartialEq for TraitItemVerbatim {
532 fn eq(&self, other: &Self) -> bool {
533 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
534 }
535}
536
537#[cfg(feature = "extra-traits")]
538impl Hash for TraitItemVerbatim {
539 fn hash<H>(&self, state: &mut H)
540 where
541 H: Hasher,
542 {
543 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700544 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700545}
546
Alex Crichton62a0a592017-05-22 13:58:53 -0700547ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800548 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800549 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800550 /// *This type is available if Syn is built with the `"full"` feature.*
551 ///
David Tolnay614a0142018-01-07 10:25:43 -0800552 /// # Syntax tree enum
553 ///
554 /// This type is a [syntax tree enum].
555 ///
556 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800557 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800558 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800559 ///
560 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700561 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800562 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700563 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500564 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800565 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700566 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800567 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800568 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800569 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700570 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800571 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700572 }),
David Tolnayebb72722018-01-07 01:14:13 -0800573
574 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800575 ///
576 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700577 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800578 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700579 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500580 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700581 pub sig: MethodSig,
582 pub block: Block,
583 }),
David Tolnayebb72722018-01-07 01:14:13 -0800584
585 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800586 ///
587 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800589 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700590 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500591 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800592 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700593 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500594 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800596 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800597 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 }),
David Tolnayebb72722018-01-07 01:14:13 -0800599
600 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800601 ///
602 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800603 pub Macro(ImplItemMacro {
604 pub attrs: Vec<Attribute>,
605 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500606 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800607 }),
David Tolnayebb72722018-01-07 01:14:13 -0800608
609 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800610 ///
611 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500612 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
613 pub tts: TokenStream,
614 }),
615 }
616}
617
618#[cfg(feature = "extra-traits")]
619impl Eq for ImplItemVerbatim {}
620
621#[cfg(feature = "extra-traits")]
622impl PartialEq for ImplItemVerbatim {
623 fn eq(&self, other: &Self) -> bool {
624 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
625 }
626}
627
628#[cfg(feature = "extra-traits")]
629impl Hash for ImplItemVerbatim {
630 fn hash<H>(&self, state: &mut H)
631 where
632 H: Hasher,
633 {
634 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700635 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700636}
637
638ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800639 /// A method's signature in a trait or implementation: `unsafe fn
640 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800641 ///
642 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700643 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500644 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500645 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700646 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700647 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700648 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700649 }
650}
651
652ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800653 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800654 ///
655 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700656 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800657 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500658 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500659 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500660 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500661 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500662 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700663 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700664}
665
Alex Crichton62a0a592017-05-22 13:58:53 -0700666ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800667 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800668 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800669 /// *This type is available if Syn is built with the `"full"` feature.*
670 ///
David Tolnay614a0142018-01-07 10:25:43 -0800671 /// # Syntax tree enum
672 ///
673 /// This type is a [syntax tree enum].
674 ///
675 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700676 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800677 /// Self captured by reference in a function signature: `&self` or `&mut
678 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800679 ///
680 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700681 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800682 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700683 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500684 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500685 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700686 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800687
David Tolnay3f559052018-01-06 23:59:48 -0800688 /// Self captured by value 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 SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500693 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800694 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800696
David Tolnay3f559052018-01-06 23:59:48 -0800697 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800698 ///
699 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub Captured(ArgCaptured {
701 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800702 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800703 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800705
David Tolnay3f559052018-01-06 23:59:48 -0800706 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500707 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800708 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800709 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 }
David Tolnay62f374c2016-10-02 13:37:00 -0700711}
712
David Tolnayedf2b992016-09-23 20:43:45 -0700713#[cfg(feature = "parsing")]
714pub mod parsing {
715 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700716
David Tolnay9be32582018-07-31 22:37:26 -0700717 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700718
David Tolnay4c614be2017-11-10 00:02:38 -0800719 impl_synom!(Item "item" alt!(
720 syn!(ItemExternCrate) => { Item::ExternCrate }
721 |
722 syn!(ItemUse) => { Item::Use }
723 |
724 syn!(ItemStatic) => { Item::Static }
725 |
726 syn!(ItemConst) => { Item::Const }
727 |
728 syn!(ItemFn) => { Item::Fn }
729 |
Yusuke Sasaki2dec3152018-07-31 20:41:50 +0900730 call!(unstable_async_fn) => { Item::Verbatim }
731 |
David Tolnay4c614be2017-11-10 00:02:38 -0800732 syn!(ItemMod) => { Item::Mod }
733 |
734 syn!(ItemForeignMod) => { Item::ForeignMod }
735 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800736 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800737 |
David Tolnay758ee132018-08-21 21:29:40 -0400738 call!(unstable_existential_type) => { Item::Verbatim }
739 |
David Tolnay4c614be2017-11-10 00:02:38 -0800740 syn!(ItemStruct) => { Item::Struct }
741 |
742 syn!(ItemEnum) => { Item::Enum }
743 |
744 syn!(ItemUnion) => { Item::Union }
745 |
746 syn!(ItemTrait) => { Item::Trait }
747 |
David Tolnay4c614be2017-11-10 00:02:38 -0800748 syn!(ItemImpl) => { Item::Impl }
749 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800750 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800751 |
752 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800753 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700754
David Tolnaydecf28d2017-11-11 11:56:45 -0800755 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500756 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -0800757 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800758 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700759 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500760 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500761 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800762 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700763 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800764 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800765 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500766 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700767 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500768 delimiter: body.0,
769 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800770 },
David Tolnay57292da2017-12-27 21:03:33 -0500771 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800772 })
David Tolnayedf2b992016-09-23 20:43:45 -0700773 ));
774
David Tolnay500d8322017-12-18 00:32:51 -0800775 // TODO: figure out the actual grammar; is body required to be braced?
776 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500777 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800778 vis: syn!(Visibility) >>
779 macro_: keyword!(macro) >>
780 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500781 args: call!(tt::parenthesized) >>
782 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800783 (ItemMacro2 {
784 attrs: attrs,
785 vis: vis,
786 macro_token: macro_,
787 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500788 paren_token: args.0,
789 args: args.1,
790 brace_token: body.0,
791 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800792 })
793 ));
794
David Tolnay4c614be2017-11-10 00:02:38 -0800795 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500796 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700797 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800798 extern_: keyword!(extern) >>
799 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700800 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800801 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
802 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800803 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700804 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800805 vis: vis,
806 extern_token: extern_,
807 crate_token: crate_,
808 ident: ident,
809 rename: rename,
810 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800811 })
David Tolnayedf2b992016-09-23 20:43:45 -0700812 ));
813
David Tolnay4c614be2017-11-10 00:02:38 -0800814 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500815 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700816 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800817 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500818 leading_colon: option!(punct!(::)) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200819 tree: syn!(UseTree) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800820 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800821 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700822 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800823 vis: vis,
824 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500825 leading_colon: leading_colon,
David Tolnay5f332a92017-12-26 00:42:45 -0500826 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800827 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800828 })
David Tolnay4a057422016-10-08 00:02:31 -0700829 ));
830
David Tolnayd97a7d22018-03-31 19:17:01 +0200831 named!(use_element -> Ident, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500832 syn!(Ident)
833 |
834 keyword!(self) => { Into::into }
835 |
836 keyword!(super) => { Into::into }
837 |
838 keyword!(crate) => { Into::into }
David Tolnay0a4d4e92018-07-21 15:31:45 -0700839 |
840 keyword!(extern) => { Into::into }
David Tolnay5f332a92017-12-26 00:42:45 -0500841 ));
842
843 impl_synom!(UseTree "use tree" alt!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200844 syn!(UseRename) => { UseTree::Rename }
845 |
David Tolnay5f332a92017-12-26 00:42:45 -0500846 syn!(UsePath) => { UseTree::Path }
847 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200848 syn!(UseName) => { UseTree::Name }
849 |
David Tolnay5f332a92017-12-26 00:42:45 -0500850 syn!(UseGlob) => { UseTree::Glob }
851 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200852 syn!(UseGroup) => { UseTree::Group }
David Tolnay5f332a92017-12-26 00:42:45 -0500853 ));
854
855 impl_synom!(UsePath "use path" do_parse!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200856 ident: call!(use_element) >>
857 colon2_token: punct!(::) >>
858 tree: syn!(UseTree) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500859 (UsePath {
860 ident: ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200861 colon2_token: colon2_token,
862 tree: Box::new(tree),
863 })
864 ));
865
866 impl_synom!(UseName "use name" do_parse!(
867 ident: call!(use_element) >>
868 (UseName {
869 ident: ident,
870 })
871 ));
872
873 impl_synom!(UseRename "use rename" do_parse!(
874 ident: call!(use_element) >>
875 as_token: keyword!(as) >>
876 rename: syn!(Ident) >>
877 (UseRename {
878 ident: ident,
879 as_token: as_token,
David Tolnay5f332a92017-12-26 00:42:45 -0500880 rename: rename,
881 })
882 ));
David Tolnay4a057422016-10-08 00:02:31 -0700883
David Tolnay5f332a92017-12-26 00:42:45 -0500884 impl_synom!(UseGlob "use glob" do_parse!(
885 star: punct!(*) >>
886 (UseGlob {
887 star_token: star,
888 })
889 ));
David Tolnay4a057422016-10-08 00:02:31 -0700890
David Tolnayd97a7d22018-03-31 19:17:01 +0200891 impl_synom!(UseGroup "use group" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500892 list: braces!(Punctuated::parse_terminated) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200893 (UseGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500894 brace_token: list.0,
895 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500896 })
897 ));
David Tolnay4a057422016-10-08 00:02:31 -0700898
David Tolnay4c614be2017-11-10 00:02:38 -0800899 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500900 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700901 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800902 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500903 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700904 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800905 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800906 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800907 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700908 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800909 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800910 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700911 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800912 vis: vis,
913 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500914 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800915 ident: ident,
916 colon_token: colon,
917 ty: Box::new(ty),
918 eq_token: eq,
919 expr: Box::new(value),
920 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800921 })
David Tolnay47a877c2016-10-01 16:50:55 -0700922 ));
923
David Tolnay4c614be2017-11-10 00:02:38 -0800924 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500925 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700926 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800927 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700928 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800929 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800930 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800931 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700932 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800934 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700935 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800936 vis: vis,
937 const_token: const_,
938 ident: ident,
939 colon_token: colon,
940 ty: Box::new(ty),
941 eq_token: eq,
942 expr: Box::new(value),
943 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800944 })
David Tolnay47a877c2016-10-01 16:50:55 -0700945 ));
946
David Tolnay4c614be2017-11-10 00:02:38 -0800947 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500948 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700949 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500950 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500951 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700952 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800953 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700954 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700955 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500956 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800957 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500958 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700959 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500960 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -0700961 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -0400962 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800963 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700964 attrs: {
965 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500966 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700967 attrs
968 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800969 vis: vis,
970 constness: constness,
971 unsafety: unsafety,
972 abi: abi,
973 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800974 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500975 paren_token: inputs.0,
976 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800977 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500978 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800979 generics: Generics {
980 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -0700981 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -0800982 },
983 }),
984 ident: ident,
985 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500986 brace_token: inner_attrs_stmts.0,
987 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800988 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800989 })
David Tolnay42602292016-10-01 22:25:45 -0700990 ));
991
Yusuke Sasaki2dec3152018-07-31 20:41:50 +0900992 named!(unstable_async_fn -> ItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -0700993 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -0700994 many0!(Attribute::parse_outer) >>
995 syn!(Visibility) >>
996 option!(keyword!(const)) >>
997 option!(keyword!(unsafe)) >>
998 keyword!(async) >>
999 option!(syn!(Abi)) >>
1000 keyword!(fn) >>
1001 syn!(Ident) >>
1002 syn!(Generics) >>
1003 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1004 syn!(ReturnType) >>
1005 option!(syn!(WhereClause)) >>
1006 braces!(tuple!(
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001007 many0!(Attribute::parse_inner),
1008 call!(Block::parse_within),
1009 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001010 end: call!(verbatim::grab_cursor) >>
1011 (ItemVerbatim {
1012 tts: verbatim::token_range(begin..end),
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001013 })
1014 ));
1015
Alex Crichton954046c2017-05-30 21:49:42 -07001016 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -04001017 named!(parse -> Self, alt!(
1018 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001019 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001020 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001021 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 self_: keyword!(self) >>
1023 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001024 (ArgSelfRef {
1025 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -05001026 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001027 and_token: and,
1028 self_token: self_,
1029 }.into())
1030 )
1031 |
1032 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05001033 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001034 self_: keyword!(self) >>
1035 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001036 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -05001037 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001038 self_token: self_,
1039 }.into())
1040 )
1041 |
1042 do_parse!(
1043 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001044 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001045 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001046 (ArgCaptured {
1047 pat: pat,
1048 ty: ty,
1049 colon_token: colon,
1050 }.into())
1051 )
1052 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001053 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -04001054 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001055
1056 fn description() -> Option<&'static str> {
1057 Some("function argument")
1058 }
Alex Crichton954046c2017-05-30 21:49:42 -07001059 }
David Tolnay62f374c2016-10-02 13:37:00 -07001060
David Tolnay4c614be2017-11-10 00:02:38 -08001061 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001062 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001063 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001064 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -07001065 ident: syn!(Ident) >>
1066 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001067 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -07001068 Vec::new(),
1069 None,
1070 Some(semi),
1071 )}
David Tolnay37d10332016-10-13 20:51:04 -07001072 |
Alex Crichton954046c2017-05-30 21:49:42 -07001073 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -07001074 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001075 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001076 many0!(Item::parse),
Michael Layzell416724e2017-05-24 21:12:34 -04001077 )
David Tolnay8875fca2017-12-31 13:52:37 -05001078 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -07001079 inner_attrs,
1080 Some((brace, items)),
1081 None,
1082 )}
David Tolnay37d10332016-10-13 20:51:04 -07001083 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001084 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -07001085 attrs: {
1086 let mut attrs = outer_attrs;
1087 attrs.extend(content_semi.0);
1088 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -07001089 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001090 vis: vis,
1091 mod_token: mod_,
1092 ident: ident,
1093 content: content_semi.1,
1094 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -08001095 })
David Tolnay35902302016-10-06 01:11:08 -07001096 ));
1097
David Tolnay4c614be2017-11-10 00:02:38 -08001098 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay5c4613a2018-07-21 15:40:17 -07001099 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001100 abi: syn!(Abi) >>
David Tolnay5c4613a2018-07-21 15:40:17 -07001101 braced_content: braces!(tuple!(
1102 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001103 many0!(ForeignItem::parse),
David Tolnay5c4613a2018-07-21 15:40:17 -07001104 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001105 (ItemForeignMod {
David Tolnay5c4613a2018-07-21 15:40:17 -07001106 attrs: {
1107 let mut attrs = outer_attrs;
1108 attrs.extend((braced_content.1).0);
1109 attrs
1110 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001111 abi: abi,
David Tolnay5c4613a2018-07-21 15:40:17 -07001112 brace_token: braced_content.0,
1113 items: (braced_content.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001114 })
David Tolnay35902302016-10-06 01:11:08 -07001115 ));
1116
David Tolnay8894f602017-11-11 12:11:04 -08001117 impl_synom!(ForeignItem "foreign item" alt!(
1118 syn!(ForeignItemFn) => { ForeignItem::Fn }
1119 |
1120 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001121 |
1122 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay78572112018-08-01 00:36:18 -07001123 |
1124 call!(foreign_item_macro) => { ForeignItem::Verbatim }
David Tolnay8894f602017-11-11 12:11:04 -08001125 ));
David Tolnay35902302016-10-06 01:11:08 -07001126
David Tolnay8894f602017-11-11 12:11:04 -08001127 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001128 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001129 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001130 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001131 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001132 generics: syn!(Generics) >>
1133 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001134 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001135 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001136 (args, variadic)
1137 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001138 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001139 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001140 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001141 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001142 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -08001143 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -07001144 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001145 attrs: attrs,
1146 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001147 decl: Box::new(FnDecl {
1148 fn_token: fn_,
1149 paren_token: parens,
1150 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -05001151 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001152 output: ret,
1153 generics: Generics {
1154 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001155 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001156 },
1157 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001158 vis: vis,
1159 }
David Tolnay35902302016-10-06 01:11:08 -07001160 })
1161 ));
1162
David Tolnay8894f602017-11-11 12:11:04 -08001163 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001164 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001165 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001166 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001167 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -07001168 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001169 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001170 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001171 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -08001172 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001173 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001174 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001175 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001176 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001177 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001178 static_token: static_,
1179 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001180 vis: vis,
1181 })
1182 ));
1183
David Tolnay199bcbb2017-11-12 10:33:52 -08001184 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001185 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001186 vis: syn!(Visibility) >>
1187 type_: keyword!(type) >>
1188 ident: syn!(Ident) >>
1189 semi: punct!(;) >>
1190 (ForeignItemType {
1191 attrs: attrs,
1192 vis: vis,
1193 type_token: type_,
1194 ident: ident,
1195 semi_token: semi,
1196 })
1197 ));
1198
David Tolnay78572112018-08-01 00:36:18 -07001199 named!(foreign_item_macro -> ForeignItemVerbatim, do_parse!(
1200 begin: call!(verbatim::grab_cursor) >>
1201 many0!(Attribute::parse_outer) >>
1202 mac: syn!(Macro) >>
1203 cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1204 end: call!(verbatim::grab_cursor) >>
1205 (ForeignItemVerbatim {
1206 tts: verbatim::token_range(begin..end),
1207 })
1208 ));
1209
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001210 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001211 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001212 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001213 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001214 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001215 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001216 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001217 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001218 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001219 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001220 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001221 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001222 vis: vis,
1223 type_token: type_,
1224 ident: ident,
1225 generics: Generics {
1226 where_clause: where_clause,
1227 ..generics
1228 },
1229 eq_token: eq,
1230 ty: Box::new(ty),
1231 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001232 })
David Tolnay3cf52982016-10-01 17:11:37 -07001233 ));
1234
David Tolnay758ee132018-08-21 21:29:40 -04001235 named!(existential_type_helper(vis: bool) -> TokenStream, do_parse!(
1236 begin: call!(verbatim::grab_cursor) >>
1237 many0!(Attribute::parse_outer) >>
1238 cond_reduce!(vis, syn!(Visibility)) >>
1239 custom_keyword!(existential) >>
1240 keyword!(type) >>
1241 syn!(Ident) >>
1242 syn!(Generics) >>
1243 option!(syn!(WhereClause)) >>
1244 colon: option!(punct!(:)) >>
1245 cond!(
1246 colon.is_some(),
1247 Punctuated::<TypeParamBound, Token![+]>::parse_separated_nonempty
1248 ) >>
1249 punct!(;) >>
1250 end: call!(verbatim::grab_cursor) >>
1251 (verbatim::token_range(begin..end))
1252 ));
1253
1254 named!(unstable_existential_type -> ItemVerbatim, map!(
1255 call!(existential_type_helper, true),
1256 |tts| ItemVerbatim { tts: tts }
1257 ));
1258
David Tolnay4c614be2017-11-10 00:02:38 -08001259 impl_synom!(ItemStruct "struct item" switch!(
1260 map!(syn!(DeriveInput), Into::into),
1261 Item::Struct(item) => value!(item)
1262 |
1263 _ => reject!()
1264 ));
David Tolnay42602292016-10-01 22:25:45 -07001265
David Tolnay4c614be2017-11-10 00:02:38 -08001266 impl_synom!(ItemEnum "enum item" switch!(
1267 map!(syn!(DeriveInput), Into::into),
1268 Item::Enum(item) => value!(item)
1269 |
1270 _ => reject!()
1271 ));
1272
1273 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001274 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001275 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001276 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001277 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001278 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001279 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001280 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001281 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001282 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001283 vis: vis,
1284 union_token: union_,
1285 ident: ident,
1286 generics: Generics {
1287 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001288 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001289 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001290 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001291 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001292 ));
1293
David Tolnay4c614be2017-11-10 00:02:38 -08001294 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001295 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001297 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001298 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001299 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001300 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001301 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001302 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001303 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001304 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001305 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001306 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001307 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001308 vis: vis,
1309 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001310 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001311 trait_token: trait_,
1312 ident: ident,
1313 generics: Generics {
1314 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001315 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001316 },
1317 colon_token: colon,
1318 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001319 brace_token: body.0,
1320 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001321 })
David Tolnay0aecb732016-10-03 23:03:50 -07001322 ));
1323
David Tolnayda705bd2017-11-10 21:58:05 -08001324 impl_synom!(TraitItem "trait item" alt!(
1325 syn!(TraitItemConst) => { TraitItem::Const }
1326 |
1327 syn!(TraitItemMethod) => { TraitItem::Method }
1328 |
1329 syn!(TraitItemType) => { TraitItem::Type }
1330 |
David Tolnay758ee132018-08-21 21:29:40 -04001331 call!(unstable_trait_existential_type) => { TraitItem::Verbatim }
1332 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001333 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001334 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001335
David Tolnayda705bd2017-11-10 21:58:05 -08001336 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001337 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001338 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001339 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001340 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001341 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001342 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1343 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001344 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001345 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001346 const_token: const_,
1347 ident: ident,
1348 colon_token: colon,
1349 ty: ty,
1350 default: default,
1351 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001352 })
1353 ));
1354
David Tolnayda705bd2017-11-10 21:58:05 -08001355 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001356 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001357 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001358 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001359 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001360 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001361 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001362 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001363 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001364 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001365 where_clause: option!(syn!(WhereClause)) >>
David Tolnay76178be2018-07-31 23:06:15 -07001366 body: option!(braces!(tuple!(
1367 many0!(Attribute::parse_inner),
1368 call!(Block::parse_within),
1369 ))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001370 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001371 ({
1372 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001373 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001374 None => (Vec::new(), None),
1375 };
David Tolnayda705bd2017-11-10 21:58:05 -08001376 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001377 attrs: {
1378 let mut attrs = outer_attrs;
1379 attrs.extend(inner_attrs);
1380 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001381 },
David Tolnayda705bd2017-11-10 21:58:05 -08001382 sig: MethodSig {
1383 constness: constness,
1384 unsafety: unsafety,
1385 abi: abi,
1386 ident: ident,
1387 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001388 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001389 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001390 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001391 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001392 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001393 generics: Generics {
1394 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001395 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001396 },
1397 },
David Tolnayda705bd2017-11-10 21:58:05 -08001398 },
1399 default: stmts.map(|stmts| {
1400 Block {
1401 stmts: stmts.0,
1402 brace_token: stmts.1,
1403 }
1404 }),
1405 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001406 }
David Tolnay0aecb732016-10-03 23:03:50 -07001407 })
1408 ));
1409
David Tolnayda705bd2017-11-10 21:58:05 -08001410 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001411 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001412 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001413 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001414 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001415 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001416 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001417 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001418 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001419 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001420 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001421 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001422 type_token: type_,
1423 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001424 generics: Generics {
1425 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001426 ..generics
Nika Layzell0183ca32017-12-05 15:24:01 -05001427 },
David Tolnayda705bd2017-11-10 21:58:05 -08001428 colon_token: colon,
1429 bounds: bounds.unwrap_or_default(),
1430 default: default,
1431 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001432 })
1433 ));
1434
David Tolnay758ee132018-08-21 21:29:40 -04001435 named!(unstable_trait_existential_type -> TraitItemVerbatim, map!(
1436 call!(existential_type_helper, false),
1437 |tts| TraitItemVerbatim { tts: tts }
1438 ));
1439
David Tolnaydecf28d2017-11-11 11:56:45 -08001440 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001441 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001442 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001443 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001444 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001445 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001446 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001447 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001448 })
1449 ));
1450
David Tolnay4c614be2017-11-10 00:02:38 -08001451 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnaycf3697a2018-03-31 20:51:15 +02001452 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001453 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001454 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001455 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001456 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001457 polarity_path: alt!(
1458 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001459 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001460 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001461 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001462 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001463 )
1464 |
David Tolnay570695e2017-06-03 16:15:13 -07001465 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001466 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001467 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001468 where_clause: option!(syn!(WhereClause)) >>
David Tolnaycf3697a2018-03-31 20:51:15 +02001469 inner: braces!(tuple!(
1470 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001471 many0!(ImplItem::parse),
David Tolnaycf3697a2018-03-31 20:51:15 +02001472 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001473 (ItemImpl {
David Tolnaycf3697a2018-03-31 20:51:15 +02001474 attrs: {
1475 let mut attrs = outer_attrs;
1476 attrs.extend((inner.1).0);
1477 attrs
1478 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001479 defaultness: defaultness,
1480 unsafety: unsafety,
1481 impl_token: impl_,
1482 generics: Generics {
1483 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001484 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001485 },
1486 trait_: polarity_path,
1487 self_ty: Box::new(self_ty),
David Tolnaycf3697a2018-03-31 20:51:15 +02001488 brace_token: inner.0,
1489 items: (inner.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001490 })
David Tolnay4c9be372016-10-06 00:47:37 -07001491 ));
1492
David Tolnay857628c2017-11-11 12:25:31 -08001493 impl_synom!(ImplItem "item in impl block" alt!(
1494 syn!(ImplItemConst) => { ImplItem::Const }
1495 |
1496 syn!(ImplItemMethod) => { ImplItem::Method }
1497 |
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001498 call!(unstable_async_method) => { ImplItem::Verbatim }
1499 |
David Tolnay857628c2017-11-11 12:25:31 -08001500 syn!(ImplItemType) => { ImplItem::Type }
1501 |
David Tolnay758ee132018-08-21 21:29:40 -04001502 call!(unstable_impl_existential_type) => { ImplItem::Verbatim }
1503 |
David Tolnay857628c2017-11-11 12:25:31 -08001504 syn!(ImplItemMacro) => { ImplItem::Macro }
1505 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001506
David Tolnay857628c2017-11-11 12:25:31 -08001507 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001508 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001509 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001510 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001511 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001512 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001513 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001514 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001515 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001516 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001517 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001518 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001519 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001520 vis: vis,
1521 defaultness: defaultness,
1522 const_token: const_,
1523 ident: ident,
1524 colon_token: colon,
1525 ty: ty,
1526 eq_token: eq,
1527 expr: value,
1528 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001529 })
1530 ));
1531
David Tolnay857628c2017-11-11 12:25:31 -08001532 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001533 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001534 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001535 defaultness: option!(keyword!(default)) >>
1536 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001537 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001538 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001539 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001540 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001541 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001542 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001543 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001544 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001545 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001546 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001547 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -04001548 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001549 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001550 attrs: {
1551 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001552 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001553 attrs
1554 },
David Tolnay857628c2017-11-11 12:25:31 -08001555 vis: vis,
1556 defaultness: defaultness,
1557 sig: MethodSig {
1558 constness: constness,
1559 unsafety: unsafety,
1560 abi: abi,
1561 ident: ident,
1562 decl: FnDecl {
1563 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001564 paren_token: inputs.0,
1565 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001566 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001567 generics: Generics {
1568 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001569 ..generics
David Tolnay4c9be372016-10-06 00:47:37 -07001570 },
David Tolnayd2836e22017-12-27 23:13:00 -05001571 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001572 },
David Tolnay857628c2017-11-11 12:25:31 -08001573 },
1574 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001575 brace_token: inner_attrs_stmts.0,
1576 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001577 },
David Tolnay4c9be372016-10-06 00:47:37 -07001578 })
1579 ));
1580
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001581 named!(unstable_async_method -> ImplItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07001582 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07001583 many0!(Attribute::parse_outer) >>
1584 syn!(Visibility) >>
1585 option!(keyword!(default)) >>
1586 option!(keyword!(const)) >>
1587 option!(keyword!(unsafe)) >>
1588 keyword!(async) >>
1589 option!(syn!(Abi)) >>
1590 keyword!(fn) >>
1591 syn!(Ident) >>
1592 syn!(Generics) >>
1593 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1594 syn!(ReturnType) >>
1595 option!(syn!(WhereClause)) >>
1596 braces!(tuple!(
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001597 many0!(Attribute::parse_inner),
1598 call!(Block::parse_within),
1599 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001600 end: call!(verbatim::grab_cursor) >>
1601 (ImplItemVerbatim {
1602 tts: verbatim::token_range(begin..end),
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001603 })
1604 ));
1605
David Tolnay857628c2017-11-11 12:25:31 -08001606 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001607 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001608 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001609 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001610 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001611 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001612 generics: syn!(Generics) >>
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001613 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001614 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001615 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001616 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001617 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001618 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001619 vis: vis,
1620 defaultness: defaultness,
1621 type_token: type_,
1622 ident: ident,
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001623 generics: Generics {
1624 where_clause: where_clause,
1625 ..generics
1626 },
David Tolnay857628c2017-11-11 12:25:31 -08001627 eq_token: eq,
1628 ty: ty,
1629 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001630 })
1631 ));
1632
David Tolnay758ee132018-08-21 21:29:40 -04001633 named!(unstable_impl_existential_type -> ImplItemVerbatim, map!(
1634 call!(existential_type_helper, true),
1635 |tts| ImplItemVerbatim { tts: tts }
1636 ));
1637
David Tolnay857628c2017-11-11 12:25:31 -08001638 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001639 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001640 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001641 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001642 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001643 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001644 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001645 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001646 })
1647 ));
1648
David Tolnayab919512017-12-30 23:31:51 -05001649 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1650 match *delimiter {
1651 MacroDelimiter::Brace(_) => true,
1652 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001653 }
1654 }
David Tolnayedf2b992016-09-23 20:43:45 -07001655}
David Tolnay4a51dc72016-10-01 00:40:31 -07001656
1657#[cfg(feature = "printing")]
1658mod printing {
1659 use super::*;
1660 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001661 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001662 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001663
David Tolnay1bfa7332017-11-11 12:41:20 -08001664 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001665 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001666 tokens.append_all(self.attrs.outer());
1667 self.vis.to_tokens(tokens);
1668 self.extern_token.to_tokens(tokens);
1669 self.crate_token.to_tokens(tokens);
1670 self.ident.to_tokens(tokens);
1671 if let Some((ref as_token, ref rename)) = self.rename {
1672 as_token.to_tokens(tokens);
1673 rename.to_tokens(tokens);
1674 }
1675 self.semi_token.to_tokens(tokens);
1676 }
1677 }
1678
1679 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001680 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001681 tokens.append_all(self.attrs.outer());
1682 self.vis.to_tokens(tokens);
1683 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001684 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001685 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001686 self.semi_token.to_tokens(tokens);
1687 }
1688 }
1689
1690 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001691 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001692 tokens.append_all(self.attrs.outer());
1693 self.vis.to_tokens(tokens);
1694 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001695 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001696 self.ident.to_tokens(tokens);
1697 self.colon_token.to_tokens(tokens);
1698 self.ty.to_tokens(tokens);
1699 self.eq_token.to_tokens(tokens);
1700 self.expr.to_tokens(tokens);
1701 self.semi_token.to_tokens(tokens);
1702 }
1703 }
1704
1705 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001706 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001707 tokens.append_all(self.attrs.outer());
1708 self.vis.to_tokens(tokens);
1709 self.const_token.to_tokens(tokens);
1710 self.ident.to_tokens(tokens);
1711 self.colon_token.to_tokens(tokens);
1712 self.ty.to_tokens(tokens);
1713 self.eq_token.to_tokens(tokens);
1714 self.expr.to_tokens(tokens);
1715 self.semi_token.to_tokens(tokens);
1716 }
1717 }
1718
1719 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001720 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001721 tokens.append_all(self.attrs.outer());
1722 self.vis.to_tokens(tokens);
1723 self.constness.to_tokens(tokens);
1724 self.unsafety.to_tokens(tokens);
1725 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001726 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001727 self.block.brace_token.surround(tokens, |tokens| {
1728 tokens.append_all(self.attrs.inner());
1729 tokens.append_all(&self.block.stmts);
1730 });
1731 }
1732 }
1733
1734 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001735 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001736 tokens.append_all(self.attrs.outer());
1737 self.vis.to_tokens(tokens);
1738 self.mod_token.to_tokens(tokens);
1739 self.ident.to_tokens(tokens);
1740 if let Some((ref brace, ref items)) = self.content {
1741 brace.surround(tokens, |tokens| {
1742 tokens.append_all(self.attrs.inner());
1743 tokens.append_all(items);
1744 });
1745 } else {
1746 TokensOrDefault(&self.semi).to_tokens(tokens);
1747 }
1748 }
1749 }
1750
1751 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001752 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001753 tokens.append_all(self.attrs.outer());
1754 self.abi.to_tokens(tokens);
1755 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001756 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001757 tokens.append_all(&self.items);
1758 });
1759 }
1760 }
1761
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001762 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001763 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001764 tokens.append_all(self.attrs.outer());
1765 self.vis.to_tokens(tokens);
1766 self.type_token.to_tokens(tokens);
1767 self.ident.to_tokens(tokens);
1768 self.generics.to_tokens(tokens);
1769 self.generics.where_clause.to_tokens(tokens);
1770 self.eq_token.to_tokens(tokens);
1771 self.ty.to_tokens(tokens);
1772 self.semi_token.to_tokens(tokens);
1773 }
1774 }
1775
1776 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001777 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001778 tokens.append_all(self.attrs.outer());
1779 self.vis.to_tokens(tokens);
1780 self.enum_token.to_tokens(tokens);
1781 self.ident.to_tokens(tokens);
1782 self.generics.to_tokens(tokens);
1783 self.generics.where_clause.to_tokens(tokens);
1784 self.brace_token.surround(tokens, |tokens| {
1785 self.variants.to_tokens(tokens);
1786 });
1787 }
1788 }
1789
1790 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001791 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001792 tokens.append_all(self.attrs.outer());
1793 self.vis.to_tokens(tokens);
1794 self.struct_token.to_tokens(tokens);
1795 self.ident.to_tokens(tokens);
1796 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001797 match self.fields {
1798 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001799 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001800 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001801 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001802 Fields::Unnamed(ref fields) => {
1803 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001804 self.generics.where_clause.to_tokens(tokens);
1805 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001806 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001807 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001808 self.generics.where_clause.to_tokens(tokens);
1809 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001810 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001811 }
1812 }
1813 }
1814
1815 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001816 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001817 tokens.append_all(self.attrs.outer());
1818 self.vis.to_tokens(tokens);
1819 self.union_token.to_tokens(tokens);
1820 self.ident.to_tokens(tokens);
1821 self.generics.to_tokens(tokens);
1822 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001823 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001824 }
1825 }
1826
1827 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001828 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001829 tokens.append_all(self.attrs.outer());
1830 self.vis.to_tokens(tokens);
1831 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001832 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001833 self.trait_token.to_tokens(tokens);
1834 self.ident.to_tokens(tokens);
1835 self.generics.to_tokens(tokens);
1836 if !self.supertraits.is_empty() {
1837 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1838 self.supertraits.to_tokens(tokens);
1839 }
1840 self.generics.where_clause.to_tokens(tokens);
1841 self.brace_token.surround(tokens, |tokens| {
1842 tokens.append_all(&self.items);
1843 });
1844 }
1845 }
1846
David Tolnay1bfa7332017-11-11 12:41:20 -08001847 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001848 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001849 tokens.append_all(self.attrs.outer());
1850 self.defaultness.to_tokens(tokens);
1851 self.unsafety.to_tokens(tokens);
1852 self.impl_token.to_tokens(tokens);
1853 self.generics.to_tokens(tokens);
1854 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1855 polarity.to_tokens(tokens);
1856 path.to_tokens(tokens);
1857 for_token.to_tokens(tokens);
1858 }
1859 self.self_ty.to_tokens(tokens);
1860 self.generics.where_clause.to_tokens(tokens);
1861 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02001862 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001863 tokens.append_all(&self.items);
1864 });
1865 }
1866 }
1867
1868 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001869 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001870 tokens.append_all(self.attrs.outer());
1871 self.mac.path.to_tokens(tokens);
1872 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001873 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001874 match self.mac.delimiter {
1875 MacroDelimiter::Paren(ref paren) => {
1876 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1877 }
1878 MacroDelimiter::Brace(ref brace) => {
1879 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1880 }
1881 MacroDelimiter::Bracket(ref bracket) => {
1882 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1883 }
1884 }
David Tolnay57292da2017-12-27 21:03:33 -05001885 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001886 }
1887 }
David Tolnay42602292016-10-01 22:25:45 -07001888
David Tolnay500d8322017-12-18 00:32:51 -08001889 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001890 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08001891 tokens.append_all(self.attrs.outer());
1892 self.vis.to_tokens(tokens);
1893 self.macro_token.to_tokens(tokens);
1894 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001895 self.paren_token.surround(tokens, |tokens| {
1896 self.args.to_tokens(tokens);
1897 });
1898 self.brace_token.surround(tokens, |tokens| {
1899 self.body.to_tokens(tokens);
1900 });
David Tolnay500d8322017-12-18 00:32:51 -08001901 }
1902 }
1903
David Tolnay2ae520a2017-12-29 11:19:50 -05001904 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001905 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001906 self.tts.to_tokens(tokens);
1907 }
1908 }
1909
David Tolnay5f332a92017-12-26 00:42:45 -05001910 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001911 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05001912 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02001913 self.colon2_token.to_tokens(tokens);
1914 self.tree.to_tokens(tokens);
1915 }
1916 }
1917
1918 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001919 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001920 self.ident.to_tokens(tokens);
1921 }
1922 }
1923
1924 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001925 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001926 self.ident.to_tokens(tokens);
1927 self.as_token.to_tokens(tokens);
1928 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001929 }
1930 }
1931
David Tolnay5f332a92017-12-26 00:42:45 -05001932 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001933 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001934 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001935 }
1936 }
1937
David Tolnayd97a7d22018-03-31 19:17:01 +02001938 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001939 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001940 self.brace_token.surround(tokens, |tokens| {
1941 self.items.to_tokens(tokens);
1942 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001943 }
1944 }
1945
David Tolnay1bfa7332017-11-11 12:41:20 -08001946 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001947 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001948 tokens.append_all(self.attrs.outer());
1949 self.const_token.to_tokens(tokens);
1950 self.ident.to_tokens(tokens);
1951 self.colon_token.to_tokens(tokens);
1952 self.ty.to_tokens(tokens);
1953 if let Some((ref eq_token, ref default)) = self.default {
1954 eq_token.to_tokens(tokens);
1955 default.to_tokens(tokens);
1956 }
1957 self.semi_token.to_tokens(tokens);
1958 }
1959 }
1960
1961 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001962 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001963 tokens.append_all(self.attrs.outer());
1964 self.sig.to_tokens(tokens);
1965 match self.default {
1966 Some(ref block) => {
1967 block.brace_token.surround(tokens, |tokens| {
1968 tokens.append_all(self.attrs.inner());
1969 tokens.append_all(&block.stmts);
1970 });
David Tolnayca085422016-10-04 00:12:38 -07001971 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001972 None => {
1973 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001974 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001975 }
1976 }
1977 }
1978
1979 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001980 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001981 tokens.append_all(self.attrs.outer());
1982 self.type_token.to_tokens(tokens);
1983 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001984 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001985 if !self.bounds.is_empty() {
1986 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1987 self.bounds.to_tokens(tokens);
1988 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001989 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001990 if let Some((ref eq_token, ref default)) = self.default {
1991 eq_token.to_tokens(tokens);
1992 default.to_tokens(tokens);
1993 }
1994 self.semi_token.to_tokens(tokens);
1995 }
1996 }
1997
1998 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001999 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002000 tokens.append_all(self.attrs.outer());
2001 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002002 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002003 }
2004 }
2005
David Tolnay2ae520a2017-12-29 11:19:50 -05002006 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002007 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002008 self.tts.to_tokens(tokens);
2009 }
2010 }
2011
David Tolnay857628c2017-11-11 12:25:31 -08002012 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002013 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002014 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002015 self.vis.to_tokens(tokens);
2016 self.defaultness.to_tokens(tokens);
2017 self.const_token.to_tokens(tokens);
2018 self.ident.to_tokens(tokens);
2019 self.colon_token.to_tokens(tokens);
2020 self.ty.to_tokens(tokens);
2021 self.eq_token.to_tokens(tokens);
2022 self.expr.to_tokens(tokens);
2023 self.semi_token.to_tokens(tokens);
2024 }
2025 }
2026
2027 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002028 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002029 tokens.append_all(self.attrs.outer());
2030 self.vis.to_tokens(tokens);
2031 self.defaultness.to_tokens(tokens);
2032 self.sig.to_tokens(tokens);
2033 self.block.brace_token.surround(tokens, |tokens| {
2034 tokens.append_all(self.attrs.inner());
2035 tokens.append_all(&self.block.stmts);
2036 });
2037 }
2038 }
2039
2040 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002041 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002042 tokens.append_all(self.attrs.outer());
2043 self.vis.to_tokens(tokens);
2044 self.defaultness.to_tokens(tokens);
2045 self.type_token.to_tokens(tokens);
2046 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002047 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002048 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002049 self.eq_token.to_tokens(tokens);
2050 self.ty.to_tokens(tokens);
2051 self.semi_token.to_tokens(tokens);
2052 }
2053 }
2054
2055 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002056 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002057 tokens.append_all(self.attrs.outer());
2058 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002059 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002060 }
2061 }
2062
David Tolnay2ae520a2017-12-29 11:19:50 -05002063 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002064 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002065 self.tts.to_tokens(tokens);
2066 }
2067 }
2068
David Tolnay8894f602017-11-11 12:11:04 -08002069 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002070 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002071 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002072 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002073 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002074 self.semi_token.to_tokens(tokens);
2075 }
2076 }
2077
2078 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002079 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002080 tokens.append_all(self.attrs.outer());
2081 self.vis.to_tokens(tokens);
2082 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002083 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002084 self.ident.to_tokens(tokens);
2085 self.colon_token.to_tokens(tokens);
2086 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002087 self.semi_token.to_tokens(tokens);
2088 }
2089 }
2090
David Tolnay199bcbb2017-11-12 10:33:52 -08002091 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002092 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002093 tokens.append_all(self.attrs.outer());
2094 self.vis.to_tokens(tokens);
2095 self.type_token.to_tokens(tokens);
2096 self.ident.to_tokens(tokens);
2097 self.semi_token.to_tokens(tokens);
2098 }
2099 }
2100
David Tolnay2ae520a2017-12-29 11:19:50 -05002101 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002102 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002103 self.tts.to_tokens(tokens);
2104 }
2105 }
2106
David Tolnay570695e2017-06-03 16:15:13 -07002107 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002108 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002109 self.constness.to_tokens(tokens);
2110 self.unsafety.to_tokens(tokens);
2111 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002112 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002113 }
2114 }
2115
Alex Crichtona74a1c82018-05-16 10:20:44 -07002116 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002117
2118 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002119 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002120 self.0.fn_token.to_tokens(tokens);
2121 self.1.to_tokens(tokens);
2122 self.0.generics.to_tokens(tokens);
2123 self.0.paren_token.surround(tokens, |tokens| {
2124 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002125 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2126 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002127 }
David Tolnayd2836e22017-12-27 23:13:00 -05002128 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002129 });
2130 self.0.output.to_tokens(tokens);
2131 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002132 }
2133 }
2134
Alex Crichton62a0a592017-05-22 13:58:53 -07002135 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002136 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002137 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002138 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002139 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002140 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002141 }
2142 }
2143
2144 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002145 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002146 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002147 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002148 }
2149 }
2150
2151 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002152 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002153 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002154 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002155 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002156 }
2157 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002158}