blob: 5b722be0de51ec4852c78f019f98973adfd37cef [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 |
738 syn!(ItemStruct) => { Item::Struct }
739 |
740 syn!(ItemEnum) => { Item::Enum }
741 |
742 syn!(ItemUnion) => { Item::Union }
743 |
744 syn!(ItemTrait) => { Item::Trait }
745 |
David Tolnay4c614be2017-11-10 00:02:38 -0800746 syn!(ItemImpl) => { Item::Impl }
747 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800748 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800749 |
750 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800751 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700752
David Tolnaydecf28d2017-11-11 11:56:45 -0800753 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500754 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -0800755 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800756 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700757 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500758 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500759 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800760 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700761 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800762 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800763 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500764 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700765 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500766 delimiter: body.0,
767 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800768 },
David Tolnay57292da2017-12-27 21:03:33 -0500769 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800770 })
David Tolnayedf2b992016-09-23 20:43:45 -0700771 ));
772
David Tolnay500d8322017-12-18 00:32:51 -0800773 // TODO: figure out the actual grammar; is body required to be braced?
774 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500775 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800776 vis: syn!(Visibility) >>
777 macro_: keyword!(macro) >>
778 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500779 args: call!(tt::parenthesized) >>
780 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800781 (ItemMacro2 {
782 attrs: attrs,
783 vis: vis,
784 macro_token: macro_,
785 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500786 paren_token: args.0,
787 args: args.1,
788 brace_token: body.0,
789 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800790 })
791 ));
792
David Tolnay4c614be2017-11-10 00:02:38 -0800793 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500794 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700795 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800796 extern_: keyword!(extern) >>
797 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700798 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800799 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
800 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800801 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700802 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800803 vis: vis,
804 extern_token: extern_,
805 crate_token: crate_,
806 ident: ident,
807 rename: rename,
808 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800809 })
David Tolnayedf2b992016-09-23 20:43:45 -0700810 ));
811
David Tolnay4c614be2017-11-10 00:02:38 -0800812 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500813 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700814 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800815 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500816 leading_colon: option!(punct!(::)) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200817 tree: syn!(UseTree) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800818 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800819 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700820 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800821 vis: vis,
822 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500823 leading_colon: leading_colon,
David Tolnay5f332a92017-12-26 00:42:45 -0500824 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800825 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800826 })
David Tolnay4a057422016-10-08 00:02:31 -0700827 ));
828
David Tolnayd97a7d22018-03-31 19:17:01 +0200829 named!(use_element -> Ident, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500830 syn!(Ident)
831 |
832 keyword!(self) => { Into::into }
833 |
834 keyword!(super) => { Into::into }
835 |
836 keyword!(crate) => { Into::into }
David Tolnay0a4d4e92018-07-21 15:31:45 -0700837 |
838 keyword!(extern) => { Into::into }
David Tolnay5f332a92017-12-26 00:42:45 -0500839 ));
840
841 impl_synom!(UseTree "use tree" alt!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200842 syn!(UseRename) => { UseTree::Rename }
843 |
David Tolnay5f332a92017-12-26 00:42:45 -0500844 syn!(UsePath) => { UseTree::Path }
845 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200846 syn!(UseName) => { UseTree::Name }
847 |
David Tolnay5f332a92017-12-26 00:42:45 -0500848 syn!(UseGlob) => { UseTree::Glob }
849 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200850 syn!(UseGroup) => { UseTree::Group }
David Tolnay5f332a92017-12-26 00:42:45 -0500851 ));
852
853 impl_synom!(UsePath "use path" do_parse!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200854 ident: call!(use_element) >>
855 colon2_token: punct!(::) >>
856 tree: syn!(UseTree) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500857 (UsePath {
858 ident: ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200859 colon2_token: colon2_token,
860 tree: Box::new(tree),
861 })
862 ));
863
864 impl_synom!(UseName "use name" do_parse!(
865 ident: call!(use_element) >>
866 (UseName {
867 ident: ident,
868 })
869 ));
870
871 impl_synom!(UseRename "use rename" do_parse!(
872 ident: call!(use_element) >>
873 as_token: keyword!(as) >>
874 rename: syn!(Ident) >>
875 (UseRename {
876 ident: ident,
877 as_token: as_token,
David Tolnay5f332a92017-12-26 00:42:45 -0500878 rename: rename,
879 })
880 ));
David Tolnay4a057422016-10-08 00:02:31 -0700881
David Tolnay5f332a92017-12-26 00:42:45 -0500882 impl_synom!(UseGlob "use glob" do_parse!(
883 star: punct!(*) >>
884 (UseGlob {
885 star_token: star,
886 })
887 ));
David Tolnay4a057422016-10-08 00:02:31 -0700888
David Tolnayd97a7d22018-03-31 19:17:01 +0200889 impl_synom!(UseGroup "use group" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500890 list: braces!(Punctuated::parse_terminated) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200891 (UseGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500892 brace_token: list.0,
893 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500894 })
895 ));
David Tolnay4a057422016-10-08 00:02:31 -0700896
David Tolnay4c614be2017-11-10 00:02:38 -0800897 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500898 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700899 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800900 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500901 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700902 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800904 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800905 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700906 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800907 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800908 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700909 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800910 vis: vis,
911 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500912 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800913 ident: ident,
914 colon_token: colon,
915 ty: Box::new(ty),
916 eq_token: eq,
917 expr: Box::new(value),
918 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800919 })
David Tolnay47a877c2016-10-01 16:50:55 -0700920 ));
921
David Tolnay4c614be2017-11-10 00:02:38 -0800922 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500923 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700924 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800925 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700926 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800927 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800928 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800929 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700930 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800931 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800932 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700933 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800934 vis: vis,
935 const_token: const_,
936 ident: ident,
937 colon_token: colon,
938 ty: Box::new(ty),
939 eq_token: eq,
940 expr: Box::new(value),
941 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800942 })
David Tolnay47a877c2016-10-01 16:50:55 -0700943 ));
944
David Tolnay4c614be2017-11-10 00:02:38 -0800945 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500946 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700947 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500948 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500949 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700950 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800951 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700952 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700953 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500954 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800955 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500956 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700957 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500958 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -0700959 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -0400960 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800961 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700962 attrs: {
963 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500964 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700965 attrs
966 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800967 vis: vis,
968 constness: constness,
969 unsafety: unsafety,
970 abi: abi,
971 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800972 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500973 paren_token: inputs.0,
974 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800975 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500976 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800977 generics: Generics {
978 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -0700979 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -0800980 },
981 }),
982 ident: ident,
983 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500984 brace_token: inner_attrs_stmts.0,
985 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800986 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800987 })
David Tolnay42602292016-10-01 22:25:45 -0700988 ));
989
Yusuke Sasaki2dec3152018-07-31 20:41:50 +0900990 named!(unstable_async_fn -> ItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -0700991 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -0700992 many0!(Attribute::parse_outer) >>
993 syn!(Visibility) >>
994 option!(keyword!(const)) >>
995 option!(keyword!(unsafe)) >>
996 keyword!(async) >>
997 option!(syn!(Abi)) >>
998 keyword!(fn) >>
999 syn!(Ident) >>
1000 syn!(Generics) >>
1001 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1002 syn!(ReturnType) >>
1003 option!(syn!(WhereClause)) >>
1004 braces!(tuple!(
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001005 many0!(Attribute::parse_inner),
1006 call!(Block::parse_within),
1007 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001008 end: call!(verbatim::grab_cursor) >>
1009 (ItemVerbatim {
1010 tts: verbatim::token_range(begin..end),
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001011 })
1012 ));
1013
Alex Crichton954046c2017-05-30 21:49:42 -07001014 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -04001015 named!(parse -> Self, alt!(
1016 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001018 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001019 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 self_: keyword!(self) >>
1021 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001022 (ArgSelfRef {
1023 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -05001024 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001025 and_token: and,
1026 self_token: self_,
1027 }.into())
1028 )
1029 |
1030 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05001031 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 self_: keyword!(self) >>
1033 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001034 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -05001035 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001036 self_token: self_,
1037 }.into())
1038 )
1039 |
1040 do_parse!(
1041 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001042 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001043 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001044 (ArgCaptured {
1045 pat: pat,
1046 ty: ty,
1047 colon_token: colon,
1048 }.into())
1049 )
1050 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001051 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -04001052 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001053
1054 fn description() -> Option<&'static str> {
1055 Some("function argument")
1056 }
Alex Crichton954046c2017-05-30 21:49:42 -07001057 }
David Tolnay62f374c2016-10-02 13:37:00 -07001058
David Tolnay4c614be2017-11-10 00:02:38 -08001059 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001060 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001061 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001062 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -07001063 ident: syn!(Ident) >>
1064 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001065 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -07001066 Vec::new(),
1067 None,
1068 Some(semi),
1069 )}
David Tolnay37d10332016-10-13 20:51:04 -07001070 |
Alex Crichton954046c2017-05-30 21:49:42 -07001071 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -07001072 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001073 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001074 many0!(Item::parse),
Michael Layzell416724e2017-05-24 21:12:34 -04001075 )
David Tolnay8875fca2017-12-31 13:52:37 -05001076 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -07001077 inner_attrs,
1078 Some((brace, items)),
1079 None,
1080 )}
David Tolnay37d10332016-10-13 20:51:04 -07001081 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001082 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -07001083 attrs: {
1084 let mut attrs = outer_attrs;
1085 attrs.extend(content_semi.0);
1086 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -07001087 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001088 vis: vis,
1089 mod_token: mod_,
1090 ident: ident,
1091 content: content_semi.1,
1092 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -08001093 })
David Tolnay35902302016-10-06 01:11:08 -07001094 ));
1095
David Tolnay4c614be2017-11-10 00:02:38 -08001096 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay5c4613a2018-07-21 15:40:17 -07001097 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001098 abi: syn!(Abi) >>
David Tolnay5c4613a2018-07-21 15:40:17 -07001099 braced_content: braces!(tuple!(
1100 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001101 many0!(ForeignItem::parse),
David Tolnay5c4613a2018-07-21 15:40:17 -07001102 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001103 (ItemForeignMod {
David Tolnay5c4613a2018-07-21 15:40:17 -07001104 attrs: {
1105 let mut attrs = outer_attrs;
1106 attrs.extend((braced_content.1).0);
1107 attrs
1108 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001109 abi: abi,
David Tolnay5c4613a2018-07-21 15:40:17 -07001110 brace_token: braced_content.0,
1111 items: (braced_content.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001112 })
David Tolnay35902302016-10-06 01:11:08 -07001113 ));
1114
David Tolnay8894f602017-11-11 12:11:04 -08001115 impl_synom!(ForeignItem "foreign item" alt!(
1116 syn!(ForeignItemFn) => { ForeignItem::Fn }
1117 |
1118 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001119 |
1120 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -08001121 ));
David Tolnay35902302016-10-06 01:11:08 -07001122
David Tolnay8894f602017-11-11 12:11:04 -08001123 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001124 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001125 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001126 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001127 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001128 generics: syn!(Generics) >>
1129 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001130 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001131 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001132 (args, variadic)
1133 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001134 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001135 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001136 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001137 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001138 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -08001139 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -07001140 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001141 attrs: attrs,
1142 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001143 decl: Box::new(FnDecl {
1144 fn_token: fn_,
1145 paren_token: parens,
1146 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -05001147 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001148 output: ret,
1149 generics: Generics {
1150 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001151 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001152 },
1153 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001154 vis: vis,
1155 }
David Tolnay35902302016-10-06 01:11:08 -07001156 })
1157 ));
1158
David Tolnay8894f602017-11-11 12:11:04 -08001159 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001160 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001161 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001162 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001163 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -07001164 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001165 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001166 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001167 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -08001168 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001169 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001170 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001171 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001172 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001173 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001174 static_token: static_,
1175 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001176 vis: vis,
1177 })
1178 ));
1179
David Tolnay199bcbb2017-11-12 10:33:52 -08001180 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001181 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001182 vis: syn!(Visibility) >>
1183 type_: keyword!(type) >>
1184 ident: syn!(Ident) >>
1185 semi: punct!(;) >>
1186 (ForeignItemType {
1187 attrs: attrs,
1188 vis: vis,
1189 type_token: type_,
1190 ident: ident,
1191 semi_token: semi,
1192 })
1193 ));
1194
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001195 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001196 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001197 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001198 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001199 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001200 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001201 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001202 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001203 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001204 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001205 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001206 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001207 vis: vis,
1208 type_token: type_,
1209 ident: ident,
1210 generics: Generics {
1211 where_clause: where_clause,
1212 ..generics
1213 },
1214 eq_token: eq,
1215 ty: Box::new(ty),
1216 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001217 })
David Tolnay3cf52982016-10-01 17:11:37 -07001218 ));
1219
David Tolnay4c614be2017-11-10 00:02:38 -08001220 impl_synom!(ItemStruct "struct item" switch!(
1221 map!(syn!(DeriveInput), Into::into),
1222 Item::Struct(item) => value!(item)
1223 |
1224 _ => reject!()
1225 ));
David Tolnay42602292016-10-01 22:25:45 -07001226
David Tolnay4c614be2017-11-10 00:02:38 -08001227 impl_synom!(ItemEnum "enum item" switch!(
1228 map!(syn!(DeriveInput), Into::into),
1229 Item::Enum(item) => value!(item)
1230 |
1231 _ => reject!()
1232 ));
1233
1234 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001235 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001236 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001237 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001238 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001239 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001240 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001241 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001242 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001243 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001244 vis: vis,
1245 union_token: union_,
1246 ident: ident,
1247 generics: Generics {
1248 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001249 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001250 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001251 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001252 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001253 ));
1254
David Tolnay4c614be2017-11-10 00:02:38 -08001255 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001256 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001257 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001258 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001259 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001260 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001261 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001262 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001264 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001265 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001266 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001267 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001268 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001269 vis: vis,
1270 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001271 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001272 trait_token: trait_,
1273 ident: ident,
1274 generics: Generics {
1275 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001276 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001277 },
1278 colon_token: colon,
1279 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001280 brace_token: body.0,
1281 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001282 })
David Tolnay0aecb732016-10-03 23:03:50 -07001283 ));
1284
David Tolnayda705bd2017-11-10 21:58:05 -08001285 impl_synom!(TraitItem "trait item" alt!(
1286 syn!(TraitItemConst) => { TraitItem::Const }
1287 |
1288 syn!(TraitItemMethod) => { TraitItem::Method }
1289 |
1290 syn!(TraitItemType) => { TraitItem::Type }
1291 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001292 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001293 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001294
David Tolnayda705bd2017-11-10 21:58:05 -08001295 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001296 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001297 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001298 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001299 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001300 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001301 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1302 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001303 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001304 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001305 const_token: const_,
1306 ident: ident,
1307 colon_token: colon,
1308 ty: ty,
1309 default: default,
1310 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001311 })
1312 ));
1313
David Tolnayda705bd2017-11-10 21:58:05 -08001314 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001315 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001316 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001317 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001318 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001319 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001320 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001321 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001322 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001323 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001324 where_clause: option!(syn!(WhereClause)) >>
David Tolnay76178be2018-07-31 23:06:15 -07001325 body: option!(braces!(tuple!(
1326 many0!(Attribute::parse_inner),
1327 call!(Block::parse_within),
1328 ))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001329 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001330 ({
1331 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001332 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001333 None => (Vec::new(), None),
1334 };
David Tolnayda705bd2017-11-10 21:58:05 -08001335 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001336 attrs: {
1337 let mut attrs = outer_attrs;
1338 attrs.extend(inner_attrs);
1339 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001340 },
David Tolnayda705bd2017-11-10 21:58:05 -08001341 sig: MethodSig {
1342 constness: constness,
1343 unsafety: unsafety,
1344 abi: abi,
1345 ident: ident,
1346 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001347 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001348 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001349 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001350 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001351 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001352 generics: Generics {
1353 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001354 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001355 },
1356 },
David Tolnayda705bd2017-11-10 21:58:05 -08001357 },
1358 default: stmts.map(|stmts| {
1359 Block {
1360 stmts: stmts.0,
1361 brace_token: stmts.1,
1362 }
1363 }),
1364 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001365 }
David Tolnay0aecb732016-10-03 23:03:50 -07001366 })
1367 ));
1368
David Tolnayda705bd2017-11-10 21:58:05 -08001369 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001370 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001371 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001372 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001373 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001374 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001375 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001376 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001377 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001378 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001379 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001380 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001381 type_token: type_,
1382 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001383 generics: Generics {
1384 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001385 ..generics
Nika Layzell0183ca32017-12-05 15:24:01 -05001386 },
David Tolnayda705bd2017-11-10 21:58:05 -08001387 colon_token: colon,
1388 bounds: bounds.unwrap_or_default(),
1389 default: default,
1390 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001391 })
1392 ));
1393
David Tolnaydecf28d2017-11-11 11:56:45 -08001394 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001395 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001396 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001397 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001398 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001399 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001400 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001401 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001402 })
1403 ));
1404
David Tolnay4c614be2017-11-10 00:02:38 -08001405 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnaycf3697a2018-03-31 20:51:15 +02001406 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001407 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001408 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001409 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001410 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001411 polarity_path: alt!(
1412 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001413 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001414 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001415 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001416 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001417 )
1418 |
David Tolnay570695e2017-06-03 16:15:13 -07001419 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001420 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001421 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001422 where_clause: option!(syn!(WhereClause)) >>
David Tolnaycf3697a2018-03-31 20:51:15 +02001423 inner: braces!(tuple!(
1424 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001425 many0!(ImplItem::parse),
David Tolnaycf3697a2018-03-31 20:51:15 +02001426 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001427 (ItemImpl {
David Tolnaycf3697a2018-03-31 20:51:15 +02001428 attrs: {
1429 let mut attrs = outer_attrs;
1430 attrs.extend((inner.1).0);
1431 attrs
1432 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001433 defaultness: defaultness,
1434 unsafety: unsafety,
1435 impl_token: impl_,
1436 generics: Generics {
1437 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001438 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001439 },
1440 trait_: polarity_path,
1441 self_ty: Box::new(self_ty),
David Tolnaycf3697a2018-03-31 20:51:15 +02001442 brace_token: inner.0,
1443 items: (inner.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001444 })
David Tolnay4c9be372016-10-06 00:47:37 -07001445 ));
1446
David Tolnay857628c2017-11-11 12:25:31 -08001447 impl_synom!(ImplItem "item in impl block" alt!(
1448 syn!(ImplItemConst) => { ImplItem::Const }
1449 |
1450 syn!(ImplItemMethod) => { ImplItem::Method }
1451 |
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001452 call!(unstable_async_method) => { ImplItem::Verbatim }
1453 |
David Tolnay857628c2017-11-11 12:25:31 -08001454 syn!(ImplItemType) => { ImplItem::Type }
1455 |
1456 syn!(ImplItemMacro) => { ImplItem::Macro }
1457 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001458
David Tolnay857628c2017-11-11 12:25:31 -08001459 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001460 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001461 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001462 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001463 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001464 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001465 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001466 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001467 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001468 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001469 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001470 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001471 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001472 vis: vis,
1473 defaultness: defaultness,
1474 const_token: const_,
1475 ident: ident,
1476 colon_token: colon,
1477 ty: ty,
1478 eq_token: eq,
1479 expr: value,
1480 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001481 })
1482 ));
1483
David Tolnay857628c2017-11-11 12:25:31 -08001484 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001485 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001486 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001487 defaultness: option!(keyword!(default)) >>
1488 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001489 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001490 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001491 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001492 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001493 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001494 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001495 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001496 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001497 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001498 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001499 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -04001500 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001501 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001502 attrs: {
1503 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001504 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001505 attrs
1506 },
David Tolnay857628c2017-11-11 12:25:31 -08001507 vis: vis,
1508 defaultness: defaultness,
1509 sig: MethodSig {
1510 constness: constness,
1511 unsafety: unsafety,
1512 abi: abi,
1513 ident: ident,
1514 decl: FnDecl {
1515 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001516 paren_token: inputs.0,
1517 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001518 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001519 generics: Generics {
1520 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001521 ..generics
David Tolnay4c9be372016-10-06 00:47:37 -07001522 },
David Tolnayd2836e22017-12-27 23:13:00 -05001523 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001524 },
David Tolnay857628c2017-11-11 12:25:31 -08001525 },
1526 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001527 brace_token: inner_attrs_stmts.0,
1528 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001529 },
David Tolnay4c9be372016-10-06 00:47:37 -07001530 })
1531 ));
1532
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001533 named!(unstable_async_method -> ImplItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07001534 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07001535 many0!(Attribute::parse_outer) >>
1536 syn!(Visibility) >>
1537 option!(keyword!(default)) >>
1538 option!(keyword!(const)) >>
1539 option!(keyword!(unsafe)) >>
1540 keyword!(async) >>
1541 option!(syn!(Abi)) >>
1542 keyword!(fn) >>
1543 syn!(Ident) >>
1544 syn!(Generics) >>
1545 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1546 syn!(ReturnType) >>
1547 option!(syn!(WhereClause)) >>
1548 braces!(tuple!(
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001549 many0!(Attribute::parse_inner),
1550 call!(Block::parse_within),
1551 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001552 end: call!(verbatim::grab_cursor) >>
1553 (ImplItemVerbatim {
1554 tts: verbatim::token_range(begin..end),
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001555 })
1556 ));
1557
David Tolnay857628c2017-11-11 12:25:31 -08001558 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001559 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001560 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001561 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001562 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001563 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001564 generics: syn!(Generics) >>
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001565 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001566 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001567 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001568 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001569 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001570 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001571 vis: vis,
1572 defaultness: defaultness,
1573 type_token: type_,
1574 ident: ident,
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001575 generics: Generics {
1576 where_clause: where_clause,
1577 ..generics
1578 },
David Tolnay857628c2017-11-11 12:25:31 -08001579 eq_token: eq,
1580 ty: ty,
1581 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001582 })
1583 ));
1584
David Tolnay857628c2017-11-11 12:25:31 -08001585 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001586 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001587 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001588 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001589 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001590 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001591 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001592 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001593 })
1594 ));
1595
David Tolnayab919512017-12-30 23:31:51 -05001596 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1597 match *delimiter {
1598 MacroDelimiter::Brace(_) => true,
1599 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001600 }
1601 }
David Tolnayedf2b992016-09-23 20:43:45 -07001602}
David Tolnay4a51dc72016-10-01 00:40:31 -07001603
1604#[cfg(feature = "printing")]
1605mod printing {
1606 use super::*;
1607 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001608 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001609 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001610
David Tolnay1bfa7332017-11-11 12:41:20 -08001611 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001612 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001613 tokens.append_all(self.attrs.outer());
1614 self.vis.to_tokens(tokens);
1615 self.extern_token.to_tokens(tokens);
1616 self.crate_token.to_tokens(tokens);
1617 self.ident.to_tokens(tokens);
1618 if let Some((ref as_token, ref rename)) = self.rename {
1619 as_token.to_tokens(tokens);
1620 rename.to_tokens(tokens);
1621 }
1622 self.semi_token.to_tokens(tokens);
1623 }
1624 }
1625
1626 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001627 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001628 tokens.append_all(self.attrs.outer());
1629 self.vis.to_tokens(tokens);
1630 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001631 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001632 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001633 self.semi_token.to_tokens(tokens);
1634 }
1635 }
1636
1637 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001638 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001639 tokens.append_all(self.attrs.outer());
1640 self.vis.to_tokens(tokens);
1641 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001642 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001643 self.ident.to_tokens(tokens);
1644 self.colon_token.to_tokens(tokens);
1645 self.ty.to_tokens(tokens);
1646 self.eq_token.to_tokens(tokens);
1647 self.expr.to_tokens(tokens);
1648 self.semi_token.to_tokens(tokens);
1649 }
1650 }
1651
1652 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001653 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001654 tokens.append_all(self.attrs.outer());
1655 self.vis.to_tokens(tokens);
1656 self.const_token.to_tokens(tokens);
1657 self.ident.to_tokens(tokens);
1658 self.colon_token.to_tokens(tokens);
1659 self.ty.to_tokens(tokens);
1660 self.eq_token.to_tokens(tokens);
1661 self.expr.to_tokens(tokens);
1662 self.semi_token.to_tokens(tokens);
1663 }
1664 }
1665
1666 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001667 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001668 tokens.append_all(self.attrs.outer());
1669 self.vis.to_tokens(tokens);
1670 self.constness.to_tokens(tokens);
1671 self.unsafety.to_tokens(tokens);
1672 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001673 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001674 self.block.brace_token.surround(tokens, |tokens| {
1675 tokens.append_all(self.attrs.inner());
1676 tokens.append_all(&self.block.stmts);
1677 });
1678 }
1679 }
1680
1681 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001682 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001683 tokens.append_all(self.attrs.outer());
1684 self.vis.to_tokens(tokens);
1685 self.mod_token.to_tokens(tokens);
1686 self.ident.to_tokens(tokens);
1687 if let Some((ref brace, ref items)) = self.content {
1688 brace.surround(tokens, |tokens| {
1689 tokens.append_all(self.attrs.inner());
1690 tokens.append_all(items);
1691 });
1692 } else {
1693 TokensOrDefault(&self.semi).to_tokens(tokens);
1694 }
1695 }
1696 }
1697
1698 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001699 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001700 tokens.append_all(self.attrs.outer());
1701 self.abi.to_tokens(tokens);
1702 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001703 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001704 tokens.append_all(&self.items);
1705 });
1706 }
1707 }
1708
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001709 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001710 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001711 tokens.append_all(self.attrs.outer());
1712 self.vis.to_tokens(tokens);
1713 self.type_token.to_tokens(tokens);
1714 self.ident.to_tokens(tokens);
1715 self.generics.to_tokens(tokens);
1716 self.generics.where_clause.to_tokens(tokens);
1717 self.eq_token.to_tokens(tokens);
1718 self.ty.to_tokens(tokens);
1719 self.semi_token.to_tokens(tokens);
1720 }
1721 }
1722
1723 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001724 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001725 tokens.append_all(self.attrs.outer());
1726 self.vis.to_tokens(tokens);
1727 self.enum_token.to_tokens(tokens);
1728 self.ident.to_tokens(tokens);
1729 self.generics.to_tokens(tokens);
1730 self.generics.where_clause.to_tokens(tokens);
1731 self.brace_token.surround(tokens, |tokens| {
1732 self.variants.to_tokens(tokens);
1733 });
1734 }
1735 }
1736
1737 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001738 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001739 tokens.append_all(self.attrs.outer());
1740 self.vis.to_tokens(tokens);
1741 self.struct_token.to_tokens(tokens);
1742 self.ident.to_tokens(tokens);
1743 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001744 match self.fields {
1745 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001746 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001747 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001748 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001749 Fields::Unnamed(ref fields) => {
1750 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001751 self.generics.where_clause.to_tokens(tokens);
1752 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001753 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001754 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001755 self.generics.where_clause.to_tokens(tokens);
1756 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001757 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001758 }
1759 }
1760 }
1761
1762 impl ToTokens for ItemUnion {
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.union_token.to_tokens(tokens);
1767 self.ident.to_tokens(tokens);
1768 self.generics.to_tokens(tokens);
1769 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001770 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001771 }
1772 }
1773
1774 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001775 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001776 tokens.append_all(self.attrs.outer());
1777 self.vis.to_tokens(tokens);
1778 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001779 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001780 self.trait_token.to_tokens(tokens);
1781 self.ident.to_tokens(tokens);
1782 self.generics.to_tokens(tokens);
1783 if !self.supertraits.is_empty() {
1784 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1785 self.supertraits.to_tokens(tokens);
1786 }
1787 self.generics.where_clause.to_tokens(tokens);
1788 self.brace_token.surround(tokens, |tokens| {
1789 tokens.append_all(&self.items);
1790 });
1791 }
1792 }
1793
David Tolnay1bfa7332017-11-11 12:41:20 -08001794 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001795 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001796 tokens.append_all(self.attrs.outer());
1797 self.defaultness.to_tokens(tokens);
1798 self.unsafety.to_tokens(tokens);
1799 self.impl_token.to_tokens(tokens);
1800 self.generics.to_tokens(tokens);
1801 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1802 polarity.to_tokens(tokens);
1803 path.to_tokens(tokens);
1804 for_token.to_tokens(tokens);
1805 }
1806 self.self_ty.to_tokens(tokens);
1807 self.generics.where_clause.to_tokens(tokens);
1808 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02001809 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001810 tokens.append_all(&self.items);
1811 });
1812 }
1813 }
1814
1815 impl ToTokens for ItemMacro {
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.mac.path.to_tokens(tokens);
1819 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001820 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001821 match self.mac.delimiter {
1822 MacroDelimiter::Paren(ref paren) => {
1823 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1824 }
1825 MacroDelimiter::Brace(ref brace) => {
1826 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1827 }
1828 MacroDelimiter::Bracket(ref bracket) => {
1829 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1830 }
1831 }
David Tolnay57292da2017-12-27 21:03:33 -05001832 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001833 }
1834 }
David Tolnay42602292016-10-01 22:25:45 -07001835
David Tolnay500d8322017-12-18 00:32:51 -08001836 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001837 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08001838 tokens.append_all(self.attrs.outer());
1839 self.vis.to_tokens(tokens);
1840 self.macro_token.to_tokens(tokens);
1841 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001842 self.paren_token.surround(tokens, |tokens| {
1843 self.args.to_tokens(tokens);
1844 });
1845 self.brace_token.surround(tokens, |tokens| {
1846 self.body.to_tokens(tokens);
1847 });
David Tolnay500d8322017-12-18 00:32:51 -08001848 }
1849 }
1850
David Tolnay2ae520a2017-12-29 11:19:50 -05001851 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001852 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001853 self.tts.to_tokens(tokens);
1854 }
1855 }
1856
David Tolnay5f332a92017-12-26 00:42:45 -05001857 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001858 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05001859 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02001860 self.colon2_token.to_tokens(tokens);
1861 self.tree.to_tokens(tokens);
1862 }
1863 }
1864
1865 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001866 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001867 self.ident.to_tokens(tokens);
1868 }
1869 }
1870
1871 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001872 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001873 self.ident.to_tokens(tokens);
1874 self.as_token.to_tokens(tokens);
1875 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001876 }
1877 }
1878
David Tolnay5f332a92017-12-26 00:42:45 -05001879 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001880 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001881 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001882 }
1883 }
1884
David Tolnayd97a7d22018-03-31 19:17:01 +02001885 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001886 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001887 self.brace_token.surround(tokens, |tokens| {
1888 self.items.to_tokens(tokens);
1889 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001890 }
1891 }
1892
David Tolnay1bfa7332017-11-11 12:41:20 -08001893 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001894 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001895 tokens.append_all(self.attrs.outer());
1896 self.const_token.to_tokens(tokens);
1897 self.ident.to_tokens(tokens);
1898 self.colon_token.to_tokens(tokens);
1899 self.ty.to_tokens(tokens);
1900 if let Some((ref eq_token, ref default)) = self.default {
1901 eq_token.to_tokens(tokens);
1902 default.to_tokens(tokens);
1903 }
1904 self.semi_token.to_tokens(tokens);
1905 }
1906 }
1907
1908 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001909 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001910 tokens.append_all(self.attrs.outer());
1911 self.sig.to_tokens(tokens);
1912 match self.default {
1913 Some(ref block) => {
1914 block.brace_token.surround(tokens, |tokens| {
1915 tokens.append_all(self.attrs.inner());
1916 tokens.append_all(&block.stmts);
1917 });
David Tolnayca085422016-10-04 00:12:38 -07001918 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001919 None => {
1920 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001921 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001922 }
1923 }
1924 }
1925
1926 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001927 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001928 tokens.append_all(self.attrs.outer());
1929 self.type_token.to_tokens(tokens);
1930 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001931 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001932 if !self.bounds.is_empty() {
1933 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1934 self.bounds.to_tokens(tokens);
1935 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001936 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001937 if let Some((ref eq_token, ref default)) = self.default {
1938 eq_token.to_tokens(tokens);
1939 default.to_tokens(tokens);
1940 }
1941 self.semi_token.to_tokens(tokens);
1942 }
1943 }
1944
1945 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001946 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001947 tokens.append_all(self.attrs.outer());
1948 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001949 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001950 }
1951 }
1952
David Tolnay2ae520a2017-12-29 11:19:50 -05001953 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001954 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001955 self.tts.to_tokens(tokens);
1956 }
1957 }
1958
David Tolnay857628c2017-11-11 12:25:31 -08001959 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001960 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07001961 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001962 self.vis.to_tokens(tokens);
1963 self.defaultness.to_tokens(tokens);
1964 self.const_token.to_tokens(tokens);
1965 self.ident.to_tokens(tokens);
1966 self.colon_token.to_tokens(tokens);
1967 self.ty.to_tokens(tokens);
1968 self.eq_token.to_tokens(tokens);
1969 self.expr.to_tokens(tokens);
1970 self.semi_token.to_tokens(tokens);
1971 }
1972 }
1973
1974 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001975 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08001976 tokens.append_all(self.attrs.outer());
1977 self.vis.to_tokens(tokens);
1978 self.defaultness.to_tokens(tokens);
1979 self.sig.to_tokens(tokens);
1980 self.block.brace_token.surround(tokens, |tokens| {
1981 tokens.append_all(self.attrs.inner());
1982 tokens.append_all(&self.block.stmts);
1983 });
1984 }
1985 }
1986
1987 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001988 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08001989 tokens.append_all(self.attrs.outer());
1990 self.vis.to_tokens(tokens);
1991 self.defaultness.to_tokens(tokens);
1992 self.type_token.to_tokens(tokens);
1993 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001994 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001995 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001996 self.eq_token.to_tokens(tokens);
1997 self.ty.to_tokens(tokens);
1998 self.semi_token.to_tokens(tokens);
1999 }
2000 }
2001
2002 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002003 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002004 tokens.append_all(self.attrs.outer());
2005 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002006 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002007 }
2008 }
2009
David Tolnay2ae520a2017-12-29 11:19:50 -05002010 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002011 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002012 self.tts.to_tokens(tokens);
2013 }
2014 }
2015
David Tolnay8894f602017-11-11 12:11:04 -08002016 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002017 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002018 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002019 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002020 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002021 self.semi_token.to_tokens(tokens);
2022 }
2023 }
2024
2025 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002026 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002027 tokens.append_all(self.attrs.outer());
2028 self.vis.to_tokens(tokens);
2029 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002030 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002031 self.ident.to_tokens(tokens);
2032 self.colon_token.to_tokens(tokens);
2033 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002034 self.semi_token.to_tokens(tokens);
2035 }
2036 }
2037
David Tolnay199bcbb2017-11-12 10:33:52 -08002038 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002039 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002040 tokens.append_all(self.attrs.outer());
2041 self.vis.to_tokens(tokens);
2042 self.type_token.to_tokens(tokens);
2043 self.ident.to_tokens(tokens);
2044 self.semi_token.to_tokens(tokens);
2045 }
2046 }
2047
David Tolnay2ae520a2017-12-29 11:19:50 -05002048 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002049 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002050 self.tts.to_tokens(tokens);
2051 }
2052 }
2053
David Tolnay570695e2017-06-03 16:15:13 -07002054 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002055 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002056 self.constness.to_tokens(tokens);
2057 self.unsafety.to_tokens(tokens);
2058 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002059 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002060 }
2061 }
2062
Alex Crichtona74a1c82018-05-16 10:20:44 -07002063 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002064
2065 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002066 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002067 self.0.fn_token.to_tokens(tokens);
2068 self.1.to_tokens(tokens);
2069 self.0.generics.to_tokens(tokens);
2070 self.0.paren_token.surround(tokens, |tokens| {
2071 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002072 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2073 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002074 }
David Tolnayd2836e22017-12-27 23:13:00 -05002075 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002076 });
2077 self.0.output.to_tokens(tokens);
2078 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002079 }
2080 }
2081
Alex Crichton62a0a592017-05-22 13:58:53 -07002082 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002083 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002084 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002085 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002086 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002087 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002088 }
2089 }
2090
2091 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002092 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002093 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002094 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002095 }
2096 }
2097
2098 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002099 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002100 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002101 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002102 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002103 }
2104 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002105}