blob: 5df15a069c307ec2f2a0de55233e14dbd41a855d [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 Tolnayf2cfd722017-12-31 18:02:51 -050011use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080012use proc_macro2::TokenStream;
13use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050016use tt::TokenStreamHelper;
David Tolnay9c76bcb2017-12-26 23:14:59 -050017#[cfg(feature = "extra-traits")]
18use std::hash::{Hash, Hasher};
David Tolnayb79ee962016-09-04 09:39:20 -070019
Alex Crichton62a0a592017-05-22 13:58:53 -070020ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -080021 /// Things that can appear directly inside of a module.
22 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -070023 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070024 ///
25 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
26 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080027 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070028 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 pub extern_token: Token![extern],
30 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070031 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080032 pub rename: Option<(Token![as], Ident)>,
33 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
35 /// A use declaration (`use` or `pub use`) item.
36 ///
37 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
38 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080039 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070040 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050042 pub leading_colon: Option<Token![::]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050043 pub prefix: Punctuated<Ident, Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050044 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070046 }),
47 /// A static item (`static` or `pub static`).
48 ///
49 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
50 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080051 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070052 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050054 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070055 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080057 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080058 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070061 }),
62 /// A constant item (`const` or `pub const`).
63 ///
64 /// E.g. `const FOO: i32 = 42;`
65 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080066 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070067 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080068 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070069 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080070 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080071 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080072 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070073 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080074 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070075 }),
76 /// A function declaration (`fn` or `pub fn`).
77 ///
78 /// E.g. `fn foo(bar: usize) -> usize { .. }`
79 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080080 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070081 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050082 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050083 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070084 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070085 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050086 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070087 pub block: Box<Block>,
88 }),
89 /// A module declaration (`mod` or `pub mod`).
90 ///
91 /// E.g. `mod foo;` or `mod foo { .. }`
92 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080093 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070094 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080095 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070096 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050097 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 }),
100 /// An external module (`extern` or `pub extern`).
101 ///
102 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700103 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800104 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700105 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500106 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107 pub items: Vec<ForeignItem>,
108 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 /// A type alias (`type` or `pub type`).
110 ///
111 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800112 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800113 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700114 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800115 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700116 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700117 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800118 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800119 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800120 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500122 /// A struct definition (`struct` or `pub struct`).
123 ///
124 /// E.g. `struct Foo<A> { x: A }`
125 pub Struct(ItemStruct {
126 pub attrs: Vec<Attribute>,
127 pub vis: Visibility,
128 pub struct_token: Token![struct],
129 pub ident: Ident,
130 pub generics: Generics,
131 pub fields: Fields,
132 pub semi_token: Option<Token![;]>,
133 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 /// An enum definition (`enum` or `pub enum`).
135 ///
136 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
137 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800138 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700139 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800140 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700141 pub ident: Ident,
142 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500143 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500144 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700146 /// A union definition (`union` or `pub union`).
147 ///
148 /// E.g. `union Foo<A, B> { x: A, y: B }`
149 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800150 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700151 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700153 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700154 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500155 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700156 }),
157 /// A Trait declaration (`trait` or `pub trait`).
158 ///
159 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
160 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800161 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700162 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500163 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500164 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800165 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700166 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800168 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500169 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500170 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub items: Vec<TraitItem>,
172 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 /// An implementation.
174 ///
175 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
176 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800177 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500178 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500179 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800180 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700182 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500183 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700184 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800185 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500186 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 pub items: Vec<ImplItem>,
188 }),
189 /// A macro invocation (which includes macro definition).
190 ///
191 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800192 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800193 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800194 /// The `example` in `macro_rules! example { ... }`.
195 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800196 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500197 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800198 }),
David Tolnay9c76bcb2017-12-26 23:14:59 -0500199 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800200 pub attrs: Vec<Attribute>,
201 pub vis: Visibility,
202 pub macro_token: Token![macro],
203 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500204 pub paren_token: Paren,
205 pub args: TokenStream,
206 pub brace_token: Brace,
207 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800208 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500209 pub Verbatim(ItemVerbatim #manual_extra_traits {
210 pub tts: TokenStream,
211 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700212 }
David Tolnayb79ee962016-09-04 09:39:20 -0700213}
214
David Tolnay9c76bcb2017-12-26 23:14:59 -0500215#[cfg(feature = "extra-traits")]
216impl Eq for ItemMacro2 {}
217
218#[cfg(feature = "extra-traits")]
219impl PartialEq for ItemMacro2 {
220 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500221 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnayab919512017-12-30 23:31:51 -0500222 && self.ident == other.ident && self.paren_token == other.paren_token
223 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
224 && self.brace_token == other.brace_token
225 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500226 }
227}
228
229#[cfg(feature = "extra-traits")]
230impl Hash for ItemMacro2 {
231 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500232 where
233 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500234 {
235 self.attrs.hash(state);
236 self.vis.hash(state);
237 self.macro_token.hash(state);
238 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500239 self.paren_token.hash(state);
240 TokenStreamHelper(&self.args).hash(state);
241 self.brace_token.hash(state);
242 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500243 }
244}
245
David Tolnay2ae520a2017-12-29 11:19:50 -0500246#[cfg(feature = "extra-traits")]
247impl Eq for ItemVerbatim {}
248
249#[cfg(feature = "extra-traits")]
250impl PartialEq for ItemVerbatim {
251 fn eq(&self, other: &Self) -> bool {
252 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
253 }
254}
255
256#[cfg(feature = "extra-traits")]
257impl Hash for ItemVerbatim {
258 fn hash<H>(&self, state: &mut H)
259 where
260 H: Hasher,
261 {
262 TokenStreamHelper(&self.tts).hash(state);
263 }
264}
265
David Tolnay0e837402016-12-22 17:25:55 -0500266impl From<DeriveInput> for Item {
267 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500268 match input.data {
269 Data::Struct(data) => Item::Struct(ItemStruct {
270 attrs: input.attrs,
271 vis: input.vis,
272 struct_token: data.struct_token,
273 ident: input.ident,
274 generics: input.generics,
275 fields: data.fields,
276 semi_token: data.semi_token,
277 }),
278 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500279 attrs: input.attrs,
280 vis: input.vis,
281 enum_token: data.enum_token,
282 ident: input.ident,
283 generics: input.generics,
284 brace_token: data.brace_token,
285 variants: data.variants,
286 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500287 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500288 attrs: input.attrs,
289 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500290 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500291 ident: input.ident,
292 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500293 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500294 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700295 }
296 }
297}
298
Alex Crichton62a0a592017-05-22 13:58:53 -0700299ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500300 /// Things that can appear directly inside of a module.
301 pub enum UseTree {
302 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
303 pub Path(UsePath {
304 pub ident: Ident,
305 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500307 /// `use prefix::*`
308 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800309 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700310 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500311 /// `use prefix::{a, b, c}`
312 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500313 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500314 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700315 }),
316 }
317}
318
Alex Crichton62a0a592017-05-22 13:58:53 -0700319ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800320 /// An item within an `extern` block.
David Tolnay8894f602017-11-11 12:11:04 -0800321 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800322 /// A foreign function in an `extern` block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800324 pub attrs: Vec<Attribute>,
325 pub vis: Visibility,
326 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800328 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700329 }),
David Tolnayebb72722018-01-07 01:14:13 -0800330
331 /// A foreign static item in an `extern` block: `static ext: u8`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700332 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800333 pub attrs: Vec<Attribute>,
334 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800335 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500336 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800337 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800338 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800339 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800340 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
David Tolnayebb72722018-01-07 01:14:13 -0800342
343 /// A foreign type in an `extern` block: `type void`.
David Tolnay199bcbb2017-11-12 10:33:52 -0800344 pub Type(ForeignItemType {
345 pub attrs: Vec<Attribute>,
346 pub vis: Visibility,
347 pub type_token: Token![type],
348 pub ident: Ident,
349 pub semi_token: Token![;],
350 }),
David Tolnayebb72722018-01-07 01:14:13 -0800351
352 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500353 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
354 pub tts: TokenStream,
355 }),
356 }
357}
358
359#[cfg(feature = "extra-traits")]
360impl Eq for ForeignItemVerbatim {}
361
362#[cfg(feature = "extra-traits")]
363impl PartialEq for ForeignItemVerbatim {
364 fn eq(&self, other: &Self) -> bool {
365 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
366 }
367}
368
369#[cfg(feature = "extra-traits")]
370impl Hash for ForeignItemVerbatim {
371 fn hash<H>(&self, state: &mut H)
372 where
373 H: Hasher,
374 {
375 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700377}
378
David Tolnayda705bd2017-11-10 21:58:05 -0800379ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800380 /// An item declaration within the definition of a trait.
David Tolnayda705bd2017-11-10 21:58:05 -0800381 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800382 /// An associated constant within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800384 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700386 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800387 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800388 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800389 pub default: Option<(Token![=], Expr)>,
390 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 }),
David Tolnayebb72722018-01-07 01:14:13 -0800392
393 /// A trait method within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800395 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub sig: MethodSig,
397 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800398 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 }),
David Tolnayebb72722018-01-07 01:14:13 -0800400
401 /// An associated type within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800403 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800404 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700405 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500406 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800407 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500408 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800409 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800410 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 }),
David Tolnayebb72722018-01-07 01:14:13 -0800412
413 /// A macro invocation within the definition of a trait.
David Tolnaydecf28d2017-11-11 11:56:45 -0800414 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800415 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800416 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500417 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800418 }),
David Tolnayebb72722018-01-07 01:14:13 -0800419
420 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500421 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
422 pub tts: TokenStream,
423 }),
424 }
425}
426
427#[cfg(feature = "extra-traits")]
428impl Eq for TraitItemVerbatim {}
429
430#[cfg(feature = "extra-traits")]
431impl PartialEq for TraitItemVerbatim {
432 fn eq(&self, other: &Self) -> bool {
433 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
434 }
435}
436
437#[cfg(feature = "extra-traits")]
438impl Hash for TraitItemVerbatim {
439 fn hash<H>(&self, state: &mut H)
440 where
441 H: Hasher,
442 {
443 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700444 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700445}
446
Alex Crichton62a0a592017-05-22 13:58:53 -0700447ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800448 /// An item within an impl block.
David Tolnay857628c2017-11-11 12:25:31 -0800449 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800450 /// An associated constant within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700451 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800452 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700453 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500454 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800455 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700456 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800457 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800458 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800459 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700460 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800461 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700462 }),
David Tolnayebb72722018-01-07 01:14:13 -0800463
464 /// A method within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700465 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800466 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700467 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500468 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 pub sig: MethodSig,
470 pub block: Block,
471 }),
David Tolnayebb72722018-01-07 01:14:13 -0800472
473 /// An associated type within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700474 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800475 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700476 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500477 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800478 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700479 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500480 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800481 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800482 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800483 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 }),
David Tolnayebb72722018-01-07 01:14:13 -0800485
486 /// A macro invocation within an impl block.
David Tolnay857628c2017-11-11 12:25:31 -0800487 pub Macro(ImplItemMacro {
488 pub attrs: Vec<Attribute>,
489 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500490 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800491 }),
David Tolnayebb72722018-01-07 01:14:13 -0800492
493 /// Tokens within an impl block not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500494 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
495 pub tts: TokenStream,
496 }),
497 }
498}
499
500#[cfg(feature = "extra-traits")]
501impl Eq for ImplItemVerbatim {}
502
503#[cfg(feature = "extra-traits")]
504impl PartialEq for ImplItemVerbatim {
505 fn eq(&self, other: &Self) -> bool {
506 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
507 }
508}
509
510#[cfg(feature = "extra-traits")]
511impl Hash for ImplItemVerbatim {
512 fn hash<H>(&self, state: &mut H)
513 where
514 H: Hasher,
515 {
516 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700518}
519
520ast_struct! {
521 /// Represents a method's signature in a trait declaration,
522 /// or in an implementation.
523 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500524 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500525 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700526 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700527 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 }
530}
531
532ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800533 /// Header of a function declaration, without including the body.
Alex Crichton62a0a592017-05-22 13:58:53 -0700534 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800535 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500536 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500537 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500538 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500539 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500540 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700542}
543
Alex Crichton62a0a592017-05-22 13:58:53 -0700544ast_enum_of_structs! {
David Tolnay3f559052018-01-06 23:59:48 -0800545 /// An argument in a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 ///
547 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
548 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800549 /// Self captured by reference in a function signature: `&self` or `&mut
550 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700551 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800552 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700553 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500554 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500555 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700556 }),
David Tolnay3f559052018-01-06 23:59:48 -0800557 /// Self captured by value in a function signature: `self` or `mut
558 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500560 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800561 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700562 }),
David Tolnay3f559052018-01-06 23:59:48 -0800563 /// An explicitly typed pattern captured by a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700564 pub Captured(ArgCaptured {
565 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800566 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800567 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700568 }),
David Tolnay3f559052018-01-06 23:59:48 -0800569 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500570 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800571 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800572 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700573 }
David Tolnay62f374c2016-10-02 13:37:00 -0700574}
575
David Tolnayedf2b992016-09-23 20:43:45 -0700576#[cfg(feature = "parsing")]
577pub mod parsing {
578 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700579
David Tolnaydfc886b2018-01-06 08:03:09 -0800580 use buffer::Cursor;
581 use synom::{PResult, Synom};
David Tolnay84aa0752016-10-02 23:01:13 -0700582
David Tolnay4c614be2017-11-10 00:02:38 -0800583 impl_synom!(Item "item" alt!(
584 syn!(ItemExternCrate) => { Item::ExternCrate }
585 |
586 syn!(ItemUse) => { Item::Use }
587 |
588 syn!(ItemStatic) => { Item::Static }
589 |
590 syn!(ItemConst) => { Item::Const }
591 |
592 syn!(ItemFn) => { Item::Fn }
593 |
594 syn!(ItemMod) => { Item::Mod }
595 |
596 syn!(ItemForeignMod) => { Item::ForeignMod }
597 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800598 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800599 |
600 syn!(ItemStruct) => { Item::Struct }
601 |
602 syn!(ItemEnum) => { Item::Enum }
603 |
604 syn!(ItemUnion) => { Item::Union }
605 |
606 syn!(ItemTrait) => { Item::Trait }
607 |
David Tolnay03342952017-12-29 11:52:00 -0500608 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800609 |
610 syn!(ItemImpl) => { Item::Impl }
611 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800612 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800613 |
614 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800615 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700616
David Tolnaydecf28d2017-11-11 11:56:45 -0800617 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500618 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700619 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700621 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500622 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500623 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800624 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700625 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800626 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800627 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500628 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700629 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500630 delimiter: body.0,
631 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800632 },
David Tolnay57292da2017-12-27 21:03:33 -0500633 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800634 })
David Tolnayedf2b992016-09-23 20:43:45 -0700635 ));
636
David Tolnay500d8322017-12-18 00:32:51 -0800637 // TODO: figure out the actual grammar; is body required to be braced?
638 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500639 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800640 vis: syn!(Visibility) >>
641 macro_: keyword!(macro) >>
642 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500643 args: call!(tt::parenthesized) >>
644 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800645 (ItemMacro2 {
646 attrs: attrs,
647 vis: vis,
648 macro_token: macro_,
649 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500650 paren_token: args.0,
651 args: args.1,
652 brace_token: body.0,
653 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800654 })
655 ));
656
David Tolnay4c614be2017-11-10 00:02:38 -0800657 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500658 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700659 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800660 extern_: keyword!(extern) >>
661 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700662 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
664 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800665 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700666 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800667 vis: vis,
668 extern_token: extern_,
669 crate_token: crate_,
670 ident: ident,
671 rename: rename,
672 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800673 })
David Tolnayedf2b992016-09-23 20:43:45 -0700674 ));
675
David Tolnay4c614be2017-11-10 00:02:38 -0800676 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500677 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700678 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800679 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500680 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500681 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500682 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500683 true => syn!(UseTree)
684 |
David Tolnay8edcef12017-12-28 12:06:52 -0500685 false => alt!(
686 tuple!(keyword!(as), syn!(Ident)) => {
687 |rename| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800688 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500689 rename: Some(rename),
690 })
691 }
David Tolnay5f332a92017-12-26 00:42:45 -0500692 |
David Tolnay8edcef12017-12-28 12:06:52 -0500693 epsilon!() => {
694 |_| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800695 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500696 rename: None,
697 })
698 }
David Tolnay5f332a92017-12-26 00:42:45 -0500699 )
700 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800701 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800702 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700703 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800704 vis: vis,
705 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500706 leading_colon: leading_colon,
707 prefix: prefix,
708 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800709 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800710 })
David Tolnay4a057422016-10-08 00:02:31 -0700711 ));
712
David Tolnay5f332a92017-12-26 00:42:45 -0500713 named!(use_prefix -> Ident, alt!(
714 syn!(Ident)
715 |
716 keyword!(self) => { Into::into }
717 |
718 keyword!(super) => { Into::into }
719 |
720 keyword!(crate) => { Into::into }
721 ));
722
723 impl_synom!(UseTree "use tree" alt!(
724 syn!(UsePath) => { UseTree::Path }
725 |
726 syn!(UseGlob) => { UseTree::Glob }
727 |
728 syn!(UseList) => { UseTree::List }
729 ));
730
731 impl_synom!(UsePath "use path" do_parse!(
732 ident: alt!(
733 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400734 |
David Tolnay5f332a92017-12-26 00:42:45 -0500735 keyword!(self) => { Into::into }
736 ) >>
737 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
738 (UsePath {
739 ident: ident,
740 rename: rename,
741 })
742 ));
David Tolnay4a057422016-10-08 00:02:31 -0700743
David Tolnay5f332a92017-12-26 00:42:45 -0500744 impl_synom!(UseGlob "use glob" do_parse!(
745 star: punct!(*) >>
746 (UseGlob {
747 star_token: star,
748 })
749 ));
David Tolnay4a057422016-10-08 00:02:31 -0700750
David Tolnay5f332a92017-12-26 00:42:45 -0500751 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500752 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500753 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500754 brace_token: list.0,
755 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500756 })
757 ));
David Tolnay4a057422016-10-08 00:02:31 -0700758
David Tolnay4c614be2017-11-10 00:02:38 -0800759 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500760 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700761 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800762 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500763 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700764 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800765 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800766 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700768 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800769 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800770 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700771 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800772 vis: vis,
773 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500774 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800775 ident: ident,
776 colon_token: colon,
777 ty: Box::new(ty),
778 eq_token: eq,
779 expr: Box::new(value),
780 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800781 })
David Tolnay47a877c2016-10-01 16:50:55 -0700782 ));
783
David Tolnay4c614be2017-11-10 00:02:38 -0800784 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500785 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700786 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800787 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700788 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800789 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800790 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800791 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700792 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800793 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800794 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700795 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800796 vis: vis,
797 const_token: const_,
798 ident: ident,
799 colon_token: colon,
800 ty: Box::new(ty),
801 eq_token: eq,
802 expr: Box::new(value),
803 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800804 })
David Tolnay47a877c2016-10-01 16:50:55 -0700805 ));
806
David Tolnay4c614be2017-11-10 00:02:38 -0800807 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500808 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700809 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500810 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500811 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700812 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800813 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700814 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700815 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500816 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800817 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500818 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700819 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500820 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700821 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400822 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800823 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700824 attrs: {
825 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500826 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700827 attrs
828 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800829 vis: vis,
830 constness: constness,
831 unsafety: unsafety,
832 abi: abi,
833 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800834 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500835 paren_token: inputs.0,
836 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800837 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500838 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800839 generics: Generics {
840 where_clause: where_clause,
841 .. generics
842 },
843 }),
844 ident: ident,
845 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500846 brace_token: inner_attrs_stmts.0,
847 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800848 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800849 })
David Tolnay42602292016-10-01 22:25:45 -0700850 ));
851
Alex Crichton954046c2017-05-30 21:49:42 -0700852 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400853 named!(parse -> Self, alt!(
854 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800855 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400856 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500857 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800858 self_: keyword!(self) >>
859 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400860 (ArgSelfRef {
861 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500862 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400863 and_token: and,
864 self_token: self_,
865 }.into())
866 )
867 |
868 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500869 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800870 self_: keyword!(self) >>
871 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400872 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500873 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400874 self_token: self_,
875 }.into())
876 )
877 |
878 do_parse!(
879 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800880 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800881 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400882 (ArgCaptured {
883 pat: pat,
884 ty: ty,
885 colon_token: colon,
886 }.into())
887 )
888 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800889 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400890 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800891
892 fn description() -> Option<&'static str> {
893 Some("function argument")
894 }
Alex Crichton954046c2017-05-30 21:49:42 -0700895 }
David Tolnay62f374c2016-10-02 13:37:00 -0700896
David Tolnay4c614be2017-11-10 00:02:38 -0800897 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500898 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700899 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800900 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700901 ident: syn!(Ident) >>
902 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700904 Vec::new(),
905 None,
906 Some(semi),
907 )}
David Tolnay37d10332016-10-13 20:51:04 -0700908 |
Alex Crichton954046c2017-05-30 21:49:42 -0700909 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700910 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500911 many0!(Attribute::parse_inner),
912 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400913 )
David Tolnay8875fca2017-12-31 13:52:37 -0500914 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -0700915 inner_attrs,
916 Some((brace, items)),
917 None,
918 )}
David Tolnay37d10332016-10-13 20:51:04 -0700919 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800920 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700921 attrs: {
922 let mut attrs = outer_attrs;
923 attrs.extend(content_semi.0);
924 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700925 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800926 vis: vis,
927 mod_token: mod_,
928 ident: ident,
929 content: content_semi.1,
930 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800931 })
David Tolnay35902302016-10-06 01:11:08 -0700932 ));
933
David Tolnay4c614be2017-11-10 00:02:38 -0800934 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500935 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700936 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500937 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800938 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700939 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800940 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -0500941 brace_token: items.0,
942 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800943 })
David Tolnay35902302016-10-06 01:11:08 -0700944 ));
945
David Tolnay8894f602017-11-11 12:11:04 -0800946 impl_synom!(ForeignItem "foreign item" alt!(
947 syn!(ForeignItemFn) => { ForeignItem::Fn }
948 |
949 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800950 |
951 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800952 ));
David Tolnay35902302016-10-06 01:11:08 -0700953
David Tolnay8894f602017-11-11 12:11:04 -0800954 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500955 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700956 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700958 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700959 generics: syn!(Generics) >>
960 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500961 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500962 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700963 (args, variadic)
964 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800965 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500966 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800967 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700968 ({
David Tolnay8875fca2017-12-31 13:52:37 -0500969 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800970 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700971 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700972 attrs: attrs,
973 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800974 decl: Box::new(FnDecl {
975 fn_token: fn_,
976 paren_token: parens,
977 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500978 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800979 output: ret,
980 generics: Generics {
981 where_clause: where_clause,
982 .. generics
983 },
984 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700985 vis: vis,
986 }
David Tolnay35902302016-10-06 01:11:08 -0700987 })
988 ));
989
David Tolnay8894f602017-11-11 12:11:04 -0800990 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500991 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700992 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800993 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500994 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700995 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800996 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800997 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800998 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800999 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001000 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001001 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001002 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001003 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001004 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001005 static_token: static_,
1006 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001007 vis: vis,
1008 })
1009 ));
1010
David Tolnay199bcbb2017-11-12 10:33:52 -08001011 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001012 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001013 vis: syn!(Visibility) >>
1014 type_: keyword!(type) >>
1015 ident: syn!(Ident) >>
1016 semi: punct!(;) >>
1017 (ForeignItemType {
1018 attrs: attrs,
1019 vis: vis,
1020 type_token: type_,
1021 ident: ident,
1022 semi_token: semi,
1023 })
1024 ));
1025
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001026 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001027 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001028 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001029 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001030 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001031 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001032 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001034 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001035 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001036 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001037 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001038 vis: vis,
1039 type_token: type_,
1040 ident: ident,
1041 generics: Generics {
1042 where_clause: where_clause,
1043 ..generics
1044 },
1045 eq_token: eq,
1046 ty: Box::new(ty),
1047 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001048 })
David Tolnay3cf52982016-10-01 17:11:37 -07001049 ));
1050
David Tolnay4c614be2017-11-10 00:02:38 -08001051 impl_synom!(ItemStruct "struct item" switch!(
1052 map!(syn!(DeriveInput), Into::into),
1053 Item::Struct(item) => value!(item)
1054 |
1055 _ => reject!()
1056 ));
David Tolnay42602292016-10-01 22:25:45 -07001057
David Tolnay4c614be2017-11-10 00:02:38 -08001058 impl_synom!(ItemEnum "enum item" switch!(
1059 map!(syn!(DeriveInput), Into::into),
1060 Item::Enum(item) => value!(item)
1061 |
1062 _ => reject!()
1063 ));
1064
1065 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001066 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001067 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001068 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001069 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001070 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001071 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001072 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001073 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001074 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001075 vis: vis,
1076 union_token: union_,
1077 ident: ident,
1078 generics: Generics {
1079 where_clause: where_clause,
1080 .. generics
1081 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001082 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001083 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001084 ));
1085
David Tolnay4c614be2017-11-10 00:02:38 -08001086 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001087 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001088 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001089 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001090 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001091 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001092 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001093 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001094 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001095 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001096 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001097 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001098 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001099 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001100 vis: vis,
1101 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001102 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001103 trait_token: trait_,
1104 ident: ident,
1105 generics: Generics {
1106 where_clause: where_clause,
1107 .. generics
1108 },
1109 colon_token: colon,
1110 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001111 brace_token: body.0,
1112 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001113 })
David Tolnay0aecb732016-10-03 23:03:50 -07001114 ));
1115
David Tolnay03342952017-12-29 11:52:00 -05001116 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1117 Ok((cursor, cursor))
1118 }
1119
1120 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1121 begin: call!(grab_cursor) >>
1122 many0!(Attribute::parse_outer) >>
1123 option!(keyword!(unsafe)) >>
1124 keyword!(impl) >>
1125 syn!(Path) >>
1126 keyword!(for) >>
1127 punct!(..) >>
1128 braces!(epsilon!()) >>
1129 end: call!(grab_cursor) >>
1130 ({
David Tolnay61037c62018-01-05 16:21:03 -08001131 let tts = begin.token_stream().into_iter().collect::<Vec<_>>();
David Tolnay03342952017-12-29 11:52:00 -05001132 let len = tts.len() - end.token_stream().into_iter().count();
1133 ItemVerbatim {
1134 tts: tts.into_iter().take(len).collect(),
1135 }
David Tolnay4c614be2017-11-10 00:02:38 -08001136 })
David Tolnayf94e2362016-10-04 00:29:51 -07001137 ));
1138
David Tolnayda705bd2017-11-10 21:58:05 -08001139 impl_synom!(TraitItem "trait item" alt!(
1140 syn!(TraitItemConst) => { TraitItem::Const }
1141 |
1142 syn!(TraitItemMethod) => { TraitItem::Method }
1143 |
1144 syn!(TraitItemType) => { TraitItem::Type }
1145 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001146 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001147 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001148
David Tolnayda705bd2017-11-10 21:58:05 -08001149 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001150 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001151 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001152 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001153 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001154 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001155 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1156 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001157 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001158 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001159 const_token: const_,
1160 ident: ident,
1161 colon_token: colon,
1162 ty: ty,
1163 default: default,
1164 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001165 })
1166 ));
1167
David Tolnayda705bd2017-11-10 21:58:05 -08001168 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001169 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001170 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001171 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001172 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001173 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001174 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001175 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001176 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001177 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001178 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001179 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001180 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001181 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001182 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001184 ({
1185 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001186 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001187 None => (Vec::new(), None),
1188 };
David Tolnayda705bd2017-11-10 21:58:05 -08001189 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001190 attrs: {
1191 let mut attrs = outer_attrs;
1192 attrs.extend(inner_attrs);
1193 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001194 },
David Tolnayda705bd2017-11-10 21:58:05 -08001195 sig: MethodSig {
1196 constness: constness,
1197 unsafety: unsafety,
1198 abi: abi,
1199 ident: ident,
1200 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001201 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001202 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001203 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001204 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001205 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001206 generics: Generics {
1207 where_clause: where_clause,
1208 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001209 },
1210 },
David Tolnayda705bd2017-11-10 21:58:05 -08001211 },
1212 default: stmts.map(|stmts| {
1213 Block {
1214 stmts: stmts.0,
1215 brace_token: stmts.1,
1216 }
1217 }),
1218 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001219 }
David Tolnay0aecb732016-10-03 23:03:50 -07001220 })
1221 ));
1222
David Tolnayda705bd2017-11-10 21:58:05 -08001223 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001224 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001225 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001226 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001227 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001228 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001229 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001230 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001231 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001232 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001233 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001234 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001235 type_token: type_,
1236 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001237 generics: Generics {
1238 where_clause: where_clause,
1239 .. generics
1240 },
David Tolnayda705bd2017-11-10 21:58:05 -08001241 colon_token: colon,
1242 bounds: bounds.unwrap_or_default(),
1243 default: default,
1244 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001245 })
1246 ));
1247
David Tolnaydecf28d2017-11-11 11:56:45 -08001248 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001249 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001250 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001251 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001252 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001253 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001254 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001255 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001256 })
1257 ));
1258
David Tolnay4c614be2017-11-10 00:02:38 -08001259 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001260 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001261 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001262 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001264 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001265 polarity_path: alt!(
1266 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001267 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001268 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001270 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001271 )
1272 |
David Tolnay570695e2017-06-03 16:15:13 -07001273 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001274 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001275 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001276 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001277 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001278 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001279 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001280 defaultness: defaultness,
1281 unsafety: unsafety,
1282 impl_token: impl_,
1283 generics: Generics {
1284 where_clause: where_clause,
1285 .. generics
1286 },
1287 trait_: polarity_path,
1288 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001289 brace_token: body.0,
1290 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001291 })
David Tolnay4c9be372016-10-06 00:47:37 -07001292 ));
1293
David Tolnay857628c2017-11-11 12:25:31 -08001294 impl_synom!(ImplItem "item in impl block" alt!(
1295 syn!(ImplItemConst) => { ImplItem::Const }
1296 |
1297 syn!(ImplItemMethod) => { ImplItem::Method }
1298 |
1299 syn!(ImplItemType) => { ImplItem::Type }
1300 |
1301 syn!(ImplItemMacro) => { ImplItem::Macro }
1302 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001303
David Tolnay857628c2017-11-11 12:25:31 -08001304 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001305 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001306 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001307 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001309 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001310 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001311 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001312 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001313 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001314 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001315 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001316 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001317 vis: vis,
1318 defaultness: defaultness,
1319 const_token: const_,
1320 ident: ident,
1321 colon_token: colon,
1322 ty: ty,
1323 eq_token: eq,
1324 expr: value,
1325 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001326 })
1327 ));
1328
David Tolnay857628c2017-11-11 12:25:31 -08001329 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001330 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001331 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001332 defaultness: option!(keyword!(default)) >>
1333 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001334 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001335 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001336 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001337 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001338 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001339 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001340 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001341 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001342 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001343 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001344 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001345 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001346 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001347 attrs: {
1348 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001349 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001350 attrs
1351 },
David Tolnay857628c2017-11-11 12:25:31 -08001352 vis: vis,
1353 defaultness: defaultness,
1354 sig: MethodSig {
1355 constness: constness,
1356 unsafety: unsafety,
1357 abi: abi,
1358 ident: ident,
1359 decl: FnDecl {
1360 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001361 paren_token: inputs.0,
1362 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001363 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001364 generics: Generics {
1365 where_clause: where_clause,
1366 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001367 },
David Tolnayd2836e22017-12-27 23:13:00 -05001368 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001369 },
David Tolnay857628c2017-11-11 12:25:31 -08001370 },
1371 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001372 brace_token: inner_attrs_stmts.0,
1373 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001374 },
David Tolnay4c9be372016-10-06 00:47:37 -07001375 })
1376 ));
1377
David Tolnay857628c2017-11-11 12:25:31 -08001378 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001379 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001380 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001381 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001382 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001383 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001384 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001385 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001386 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001387 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001388 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001389 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001390 vis: vis,
1391 defaultness: defaultness,
1392 type_token: type_,
1393 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001394 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001395 eq_token: eq,
1396 ty: ty,
1397 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001398 })
1399 ));
1400
David Tolnay857628c2017-11-11 12:25:31 -08001401 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001402 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001403 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001404 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001405 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001406 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001407 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001408 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001409 })
1410 ));
1411
David Tolnayab919512017-12-30 23:31:51 -05001412 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1413 match *delimiter {
1414 MacroDelimiter::Brace(_) => true,
1415 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001416 }
1417 }
David Tolnayedf2b992016-09-23 20:43:45 -07001418}
David Tolnay4a51dc72016-10-01 00:40:31 -07001419
1420#[cfg(feature = "printing")]
1421mod printing {
1422 use super::*;
1423 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001424 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001425
David Tolnay1bfa7332017-11-11 12:41:20 -08001426 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001427 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001428 tokens.append_all(self.attrs.outer());
1429 self.vis.to_tokens(tokens);
1430 self.extern_token.to_tokens(tokens);
1431 self.crate_token.to_tokens(tokens);
1432 self.ident.to_tokens(tokens);
1433 if let Some((ref as_token, ref rename)) = self.rename {
1434 as_token.to_tokens(tokens);
1435 rename.to_tokens(tokens);
1436 }
1437 self.semi_token.to_tokens(tokens);
1438 }
1439 }
1440
1441 impl ToTokens for ItemUse {
1442 fn to_tokens(&self, tokens: &mut Tokens) {
1443 tokens.append_all(self.attrs.outer());
1444 self.vis.to_tokens(tokens);
1445 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001446 self.leading_colon.to_tokens(tokens);
1447 self.prefix.to_tokens(tokens);
1448 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001449 self.semi_token.to_tokens(tokens);
1450 }
1451 }
1452
1453 impl ToTokens for ItemStatic {
1454 fn to_tokens(&self, tokens: &mut Tokens) {
1455 tokens.append_all(self.attrs.outer());
1456 self.vis.to_tokens(tokens);
1457 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001458 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001459 self.ident.to_tokens(tokens);
1460 self.colon_token.to_tokens(tokens);
1461 self.ty.to_tokens(tokens);
1462 self.eq_token.to_tokens(tokens);
1463 self.expr.to_tokens(tokens);
1464 self.semi_token.to_tokens(tokens);
1465 }
1466 }
1467
1468 impl ToTokens for ItemConst {
1469 fn to_tokens(&self, tokens: &mut Tokens) {
1470 tokens.append_all(self.attrs.outer());
1471 self.vis.to_tokens(tokens);
1472 self.const_token.to_tokens(tokens);
1473 self.ident.to_tokens(tokens);
1474 self.colon_token.to_tokens(tokens);
1475 self.ty.to_tokens(tokens);
1476 self.eq_token.to_tokens(tokens);
1477 self.expr.to_tokens(tokens);
1478 self.semi_token.to_tokens(tokens);
1479 }
1480 }
1481
1482 impl ToTokens for ItemFn {
1483 fn to_tokens(&self, tokens: &mut Tokens) {
1484 tokens.append_all(self.attrs.outer());
1485 self.vis.to_tokens(tokens);
1486 self.constness.to_tokens(tokens);
1487 self.unsafety.to_tokens(tokens);
1488 self.abi.to_tokens(tokens);
1489 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1490 self.block.brace_token.surround(tokens, |tokens| {
1491 tokens.append_all(self.attrs.inner());
1492 tokens.append_all(&self.block.stmts);
1493 });
1494 }
1495 }
1496
1497 impl ToTokens for ItemMod {
1498 fn to_tokens(&self, tokens: &mut Tokens) {
1499 tokens.append_all(self.attrs.outer());
1500 self.vis.to_tokens(tokens);
1501 self.mod_token.to_tokens(tokens);
1502 self.ident.to_tokens(tokens);
1503 if let Some((ref brace, ref items)) = self.content {
1504 brace.surround(tokens, |tokens| {
1505 tokens.append_all(self.attrs.inner());
1506 tokens.append_all(items);
1507 });
1508 } else {
1509 TokensOrDefault(&self.semi).to_tokens(tokens);
1510 }
1511 }
1512 }
1513
1514 impl ToTokens for ItemForeignMod {
1515 fn to_tokens(&self, tokens: &mut Tokens) {
1516 tokens.append_all(self.attrs.outer());
1517 self.abi.to_tokens(tokens);
1518 self.brace_token.surround(tokens, |tokens| {
1519 tokens.append_all(&self.items);
1520 });
1521 }
1522 }
1523
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001524 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001525 fn to_tokens(&self, tokens: &mut Tokens) {
1526 tokens.append_all(self.attrs.outer());
1527 self.vis.to_tokens(tokens);
1528 self.type_token.to_tokens(tokens);
1529 self.ident.to_tokens(tokens);
1530 self.generics.to_tokens(tokens);
1531 self.generics.where_clause.to_tokens(tokens);
1532 self.eq_token.to_tokens(tokens);
1533 self.ty.to_tokens(tokens);
1534 self.semi_token.to_tokens(tokens);
1535 }
1536 }
1537
1538 impl ToTokens for ItemEnum {
1539 fn to_tokens(&self, tokens: &mut Tokens) {
1540 tokens.append_all(self.attrs.outer());
1541 self.vis.to_tokens(tokens);
1542 self.enum_token.to_tokens(tokens);
1543 self.ident.to_tokens(tokens);
1544 self.generics.to_tokens(tokens);
1545 self.generics.where_clause.to_tokens(tokens);
1546 self.brace_token.surround(tokens, |tokens| {
1547 self.variants.to_tokens(tokens);
1548 });
1549 }
1550 }
1551
1552 impl ToTokens for ItemStruct {
1553 fn to_tokens(&self, tokens: &mut Tokens) {
1554 tokens.append_all(self.attrs.outer());
1555 self.vis.to_tokens(tokens);
1556 self.struct_token.to_tokens(tokens);
1557 self.ident.to_tokens(tokens);
1558 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001559 match self.fields {
1560 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001561 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001562 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001563 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001564 Fields::Unnamed(ref fields) => {
1565 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001566 self.generics.where_clause.to_tokens(tokens);
1567 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001568 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001569 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001570 self.generics.where_clause.to_tokens(tokens);
1571 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001572 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001573 }
1574 }
1575 }
1576
1577 impl ToTokens for ItemUnion {
1578 fn to_tokens(&self, tokens: &mut Tokens) {
1579 tokens.append_all(self.attrs.outer());
1580 self.vis.to_tokens(tokens);
1581 self.union_token.to_tokens(tokens);
1582 self.ident.to_tokens(tokens);
1583 self.generics.to_tokens(tokens);
1584 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001585 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001586 }
1587 }
1588
1589 impl ToTokens for ItemTrait {
1590 fn to_tokens(&self, tokens: &mut Tokens) {
1591 tokens.append_all(self.attrs.outer());
1592 self.vis.to_tokens(tokens);
1593 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001594 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001595 self.trait_token.to_tokens(tokens);
1596 self.ident.to_tokens(tokens);
1597 self.generics.to_tokens(tokens);
1598 if !self.supertraits.is_empty() {
1599 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1600 self.supertraits.to_tokens(tokens);
1601 }
1602 self.generics.where_clause.to_tokens(tokens);
1603 self.brace_token.surround(tokens, |tokens| {
1604 tokens.append_all(&self.items);
1605 });
1606 }
1607 }
1608
David Tolnay1bfa7332017-11-11 12:41:20 -08001609 impl ToTokens for ItemImpl {
1610 fn to_tokens(&self, tokens: &mut Tokens) {
1611 tokens.append_all(self.attrs.outer());
1612 self.defaultness.to_tokens(tokens);
1613 self.unsafety.to_tokens(tokens);
1614 self.impl_token.to_tokens(tokens);
1615 self.generics.to_tokens(tokens);
1616 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1617 polarity.to_tokens(tokens);
1618 path.to_tokens(tokens);
1619 for_token.to_tokens(tokens);
1620 }
1621 self.self_ty.to_tokens(tokens);
1622 self.generics.where_clause.to_tokens(tokens);
1623 self.brace_token.surround(tokens, |tokens| {
1624 tokens.append_all(&self.items);
1625 });
1626 }
1627 }
1628
1629 impl ToTokens for ItemMacro {
1630 fn to_tokens(&self, tokens: &mut Tokens) {
1631 tokens.append_all(self.attrs.outer());
1632 self.mac.path.to_tokens(tokens);
1633 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001634 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001635 match self.mac.delimiter {
1636 MacroDelimiter::Paren(ref paren) => {
1637 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1638 }
1639 MacroDelimiter::Brace(ref brace) => {
1640 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1641 }
1642 MacroDelimiter::Bracket(ref bracket) => {
1643 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1644 }
1645 }
David Tolnay57292da2017-12-27 21:03:33 -05001646 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001647 }
1648 }
David Tolnay42602292016-10-01 22:25:45 -07001649
David Tolnay500d8322017-12-18 00:32:51 -08001650 impl ToTokens for ItemMacro2 {
1651 fn to_tokens(&self, tokens: &mut Tokens) {
1652 tokens.append_all(self.attrs.outer());
1653 self.vis.to_tokens(tokens);
1654 self.macro_token.to_tokens(tokens);
1655 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001656 self.paren_token.surround(tokens, |tokens| {
1657 self.args.to_tokens(tokens);
1658 });
1659 self.brace_token.surround(tokens, |tokens| {
1660 self.body.to_tokens(tokens);
1661 });
David Tolnay500d8322017-12-18 00:32:51 -08001662 }
1663 }
1664
David Tolnay2ae520a2017-12-29 11:19:50 -05001665 impl ToTokens for ItemVerbatim {
1666 fn to_tokens(&self, tokens: &mut Tokens) {
1667 self.tts.to_tokens(tokens);
1668 }
1669 }
1670
David Tolnay5f332a92017-12-26 00:42:45 -05001671 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001672 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001673 self.ident.to_tokens(tokens);
1674 if let Some((ref as_token, ref rename)) = self.rename {
1675 as_token.to_tokens(tokens);
1676 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001677 }
David Tolnay4a057422016-10-08 00:02:31 -07001678 }
1679 }
1680
David Tolnay5f332a92017-12-26 00:42:45 -05001681 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001682 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001683 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001684 }
1685 }
1686
David Tolnay5f332a92017-12-26 00:42:45 -05001687 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001688 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001689 self.brace_token.surround(tokens, |tokens| {
1690 self.items.to_tokens(tokens);
1691 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001692 }
1693 }
1694
David Tolnay1bfa7332017-11-11 12:41:20 -08001695 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001696 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001697 tokens.append_all(self.attrs.outer());
1698 self.const_token.to_tokens(tokens);
1699 self.ident.to_tokens(tokens);
1700 self.colon_token.to_tokens(tokens);
1701 self.ty.to_tokens(tokens);
1702 if let Some((ref eq_token, ref default)) = self.default {
1703 eq_token.to_tokens(tokens);
1704 default.to_tokens(tokens);
1705 }
1706 self.semi_token.to_tokens(tokens);
1707 }
1708 }
1709
1710 impl ToTokens for TraitItemMethod {
1711 fn to_tokens(&self, tokens: &mut Tokens) {
1712 tokens.append_all(self.attrs.outer());
1713 self.sig.to_tokens(tokens);
1714 match self.default {
1715 Some(ref block) => {
1716 block.brace_token.surround(tokens, |tokens| {
1717 tokens.append_all(self.attrs.inner());
1718 tokens.append_all(&block.stmts);
1719 });
David Tolnayca085422016-10-04 00:12:38 -07001720 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001721 None => {
1722 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001723 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001724 }
1725 }
1726 }
1727
1728 impl ToTokens for TraitItemType {
1729 fn to_tokens(&self, tokens: &mut Tokens) {
1730 tokens.append_all(self.attrs.outer());
1731 self.type_token.to_tokens(tokens);
1732 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001733 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001734 if !self.bounds.is_empty() {
1735 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1736 self.bounds.to_tokens(tokens);
1737 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001738 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001739 if let Some((ref eq_token, ref default)) = self.default {
1740 eq_token.to_tokens(tokens);
1741 default.to_tokens(tokens);
1742 }
1743 self.semi_token.to_tokens(tokens);
1744 }
1745 }
1746
1747 impl ToTokens for TraitItemMacro {
1748 fn to_tokens(&self, tokens: &mut Tokens) {
1749 tokens.append_all(self.attrs.outer());
1750 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001751 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001752 }
1753 }
1754
David Tolnay2ae520a2017-12-29 11:19:50 -05001755 impl ToTokens for TraitItemVerbatim {
1756 fn to_tokens(&self, tokens: &mut Tokens) {
1757 self.tts.to_tokens(tokens);
1758 }
1759 }
1760
David Tolnay857628c2017-11-11 12:25:31 -08001761 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001762 fn to_tokens(&self, tokens: &mut Tokens) {
1763 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001764 self.vis.to_tokens(tokens);
1765 self.defaultness.to_tokens(tokens);
1766 self.const_token.to_tokens(tokens);
1767 self.ident.to_tokens(tokens);
1768 self.colon_token.to_tokens(tokens);
1769 self.ty.to_tokens(tokens);
1770 self.eq_token.to_tokens(tokens);
1771 self.expr.to_tokens(tokens);
1772 self.semi_token.to_tokens(tokens);
1773 }
1774 }
1775
1776 impl ToTokens for ImplItemMethod {
1777 fn to_tokens(&self, tokens: &mut Tokens) {
1778 tokens.append_all(self.attrs.outer());
1779 self.vis.to_tokens(tokens);
1780 self.defaultness.to_tokens(tokens);
1781 self.sig.to_tokens(tokens);
1782 self.block.brace_token.surround(tokens, |tokens| {
1783 tokens.append_all(self.attrs.inner());
1784 tokens.append_all(&self.block.stmts);
1785 });
1786 }
1787 }
1788
1789 impl ToTokens for ImplItemType {
1790 fn to_tokens(&self, tokens: &mut Tokens) {
1791 tokens.append_all(self.attrs.outer());
1792 self.vis.to_tokens(tokens);
1793 self.defaultness.to_tokens(tokens);
1794 self.type_token.to_tokens(tokens);
1795 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001796 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001797 self.eq_token.to_tokens(tokens);
1798 self.ty.to_tokens(tokens);
1799 self.semi_token.to_tokens(tokens);
1800 }
1801 }
1802
1803 impl ToTokens for ImplItemMacro {
1804 fn to_tokens(&self, tokens: &mut Tokens) {
1805 tokens.append_all(self.attrs.outer());
1806 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001807 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001808 }
1809 }
1810
David Tolnay2ae520a2017-12-29 11:19:50 -05001811 impl ToTokens for ImplItemVerbatim {
1812 fn to_tokens(&self, tokens: &mut Tokens) {
1813 self.tts.to_tokens(tokens);
1814 }
1815 }
1816
David Tolnay8894f602017-11-11 12:11:04 -08001817 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001818 fn to_tokens(&self, tokens: &mut Tokens) {
1819 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001820 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001821 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1822 self.semi_token.to_tokens(tokens);
1823 }
1824 }
1825
1826 impl ToTokens for ForeignItemStatic {
1827 fn to_tokens(&self, tokens: &mut Tokens) {
1828 tokens.append_all(self.attrs.outer());
1829 self.vis.to_tokens(tokens);
1830 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001831 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001832 self.ident.to_tokens(tokens);
1833 self.colon_token.to_tokens(tokens);
1834 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001835 self.semi_token.to_tokens(tokens);
1836 }
1837 }
1838
David Tolnay199bcbb2017-11-12 10:33:52 -08001839 impl ToTokens for ForeignItemType {
1840 fn to_tokens(&self, tokens: &mut Tokens) {
1841 tokens.append_all(self.attrs.outer());
1842 self.vis.to_tokens(tokens);
1843 self.type_token.to_tokens(tokens);
1844 self.ident.to_tokens(tokens);
1845 self.semi_token.to_tokens(tokens);
1846 }
1847 }
1848
David Tolnay2ae520a2017-12-29 11:19:50 -05001849 impl ToTokens for ForeignItemVerbatim {
1850 fn to_tokens(&self, tokens: &mut Tokens) {
1851 self.tts.to_tokens(tokens);
1852 }
1853 }
1854
David Tolnay570695e2017-06-03 16:15:13 -07001855 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001856 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001857 self.constness.to_tokens(tokens);
1858 self.unsafety.to_tokens(tokens);
1859 self.abi.to_tokens(tokens);
1860 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001861 }
1862 }
1863
David Tolnay570695e2017-06-03 16:15:13 -07001864 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001865
1866 impl<'a> ToTokens for NamedDecl<'a> {
1867 fn to_tokens(&self, tokens: &mut Tokens) {
1868 self.0.fn_token.to_tokens(tokens);
1869 self.1.to_tokens(tokens);
1870 self.0.generics.to_tokens(tokens);
1871 self.0.paren_token.surround(tokens, |tokens| {
1872 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001873 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1874 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001875 }
David Tolnayd2836e22017-12-27 23:13:00 -05001876 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001877 });
1878 self.0.output.to_tokens(tokens);
1879 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001880 }
1881 }
1882
Alex Crichton62a0a592017-05-22 13:58:53 -07001883 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001884 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001885 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001886 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001887 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001888 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001889 }
1890 }
1891
1892 impl ToTokens for ArgSelf {
1893 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001894 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001895 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001896 }
1897 }
1898
1899 impl ToTokens for ArgCaptured {
1900 fn to_tokens(&self, tokens: &mut Tokens) {
1901 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001902 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001903 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001904 }
1905 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001906}