blob: f5091d29fc03802cbdf8da6ea0d4dde08c16b9ce [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! {
320 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800321 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 /// A foreign function
323 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 }),
330 /// A foreign static item (`static ext: u8`)
331 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800332 pub attrs: Vec<Attribute>,
333 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800334 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500335 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800336 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800337 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800338 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800339 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800341 /// A foreign type
342 pub Type(ForeignItemType {
343 pub attrs: Vec<Attribute>,
344 pub vis: Visibility,
345 pub type_token: Token![type],
346 pub ident: Ident,
347 pub semi_token: Token![;],
348 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500349 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
350 pub tts: TokenStream,
351 }),
352 }
353}
354
355#[cfg(feature = "extra-traits")]
356impl Eq for ForeignItemVerbatim {}
357
358#[cfg(feature = "extra-traits")]
359impl PartialEq for ForeignItemVerbatim {
360 fn eq(&self, other: &Self) -> bool {
361 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
362 }
363}
364
365#[cfg(feature = "extra-traits")]
366impl Hash for ForeignItemVerbatim {
367 fn hash<H>(&self, state: &mut H)
368 where
369 H: Hasher,
370 {
371 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700373}
374
David Tolnayda705bd2017-11-10 21:58:05 -0800375ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 /// Represents an item declaration within a trait declaration,
377 /// possibly including a default implementation. A trait item is
378 /// either required (meaning it doesn't have an implementation, just a
379 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800380 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800382 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800383 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700384 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800386 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800387 pub default: Option<(Token![=], Expr)>,
388 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
390 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800391 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub sig: MethodSig,
393 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 }),
396 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800397 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800398 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700399 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500400 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800401 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500402 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800403 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800404 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800406 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800407 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800408 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500409 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800410 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500411 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
412 pub tts: TokenStream,
413 }),
414 }
415}
416
417#[cfg(feature = "extra-traits")]
418impl Eq for TraitItemVerbatim {}
419
420#[cfg(feature = "extra-traits")]
421impl PartialEq for TraitItemVerbatim {
422 fn eq(&self, other: &Self) -> bool {
423 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
424 }
425}
426
427#[cfg(feature = "extra-traits")]
428impl Hash for TraitItemVerbatim {
429 fn hash<H>(&self, state: &mut H)
430 where
431 H: Hasher,
432 {
433 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700435}
436
Alex Crichton62a0a592017-05-22 13:58:53 -0700437ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800438 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800440 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700441 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500442 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700444 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800445 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800446 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800447 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800449 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 }),
451 pub Method(ImplItemMethod {
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]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700455 pub sig: MethodSig,
456 pub block: Block,
457 }),
458 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800459 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700460 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500461 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800462 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700463 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500464 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800465 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800466 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800467 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 }),
David Tolnay857628c2017-11-11 12:25:31 -0800469 pub Macro(ImplItemMacro {
470 pub attrs: Vec<Attribute>,
471 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500472 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800473 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500474 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
475 pub tts: TokenStream,
476 }),
477 }
478}
479
480#[cfg(feature = "extra-traits")]
481impl Eq for ImplItemVerbatim {}
482
483#[cfg(feature = "extra-traits")]
484impl PartialEq for ImplItemVerbatim {
485 fn eq(&self, other: &Self) -> bool {
486 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
487 }
488}
489
490#[cfg(feature = "extra-traits")]
491impl Hash for ImplItemVerbatim {
492 fn hash<H>(&self, state: &mut H)
493 where
494 H: Hasher,
495 {
496 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700498}
499
500ast_struct! {
501 /// Represents a method's signature in a trait declaration,
502 /// or in an implementation.
503 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500504 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500505 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700507 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 }
510}
511
512ast_struct! {
513 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700514 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 /// E.g. `fn foo(bar: baz)`
516 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500518 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500519 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500520 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500521 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500522 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700524}
525
Alex Crichton62a0a592017-05-22 13:58:53 -0700526ast_enum_of_structs! {
David Tolnay3f559052018-01-06 23:59:48 -0800527 /// An argument in a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 ///
529 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
530 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800531 /// Self captured by reference in a function signature: `&self` or `&mut
532 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700533 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800534 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700535 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500536 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500537 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 }),
David Tolnay3f559052018-01-06 23:59:48 -0800539 /// Self captured by value in a function signature: `self` or `mut
540 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500542 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800543 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700544 }),
David Tolnay3f559052018-01-06 23:59:48 -0800545 /// An explicitly typed pattern captured by a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 pub Captured(ArgCaptured {
547 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800548 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800549 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700550 }),
David Tolnay3f559052018-01-06 23:59:48 -0800551 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500552 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800553 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800554 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700555 }
David Tolnay62f374c2016-10-02 13:37:00 -0700556}
557
David Tolnayedf2b992016-09-23 20:43:45 -0700558#[cfg(feature = "parsing")]
559pub mod parsing {
560 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700561
David Tolnaydfc886b2018-01-06 08:03:09 -0800562 use buffer::Cursor;
563 use synom::{PResult, Synom};
David Tolnay84aa0752016-10-02 23:01:13 -0700564
David Tolnay4c614be2017-11-10 00:02:38 -0800565 impl_synom!(Item "item" alt!(
566 syn!(ItemExternCrate) => { Item::ExternCrate }
567 |
568 syn!(ItemUse) => { Item::Use }
569 |
570 syn!(ItemStatic) => { Item::Static }
571 |
572 syn!(ItemConst) => { Item::Const }
573 |
574 syn!(ItemFn) => { Item::Fn }
575 |
576 syn!(ItemMod) => { Item::Mod }
577 |
578 syn!(ItemForeignMod) => { Item::ForeignMod }
579 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800580 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800581 |
582 syn!(ItemStruct) => { Item::Struct }
583 |
584 syn!(ItemEnum) => { Item::Enum }
585 |
586 syn!(ItemUnion) => { Item::Union }
587 |
588 syn!(ItemTrait) => { Item::Trait }
589 |
David Tolnay03342952017-12-29 11:52:00 -0500590 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800591 |
592 syn!(ItemImpl) => { Item::Impl }
593 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800594 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800595 |
596 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800597 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700598
David Tolnaydecf28d2017-11-11 11:56:45 -0800599 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500600 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700601 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700603 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500604 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500605 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800606 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700607 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800608 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800609 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500610 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700611 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500612 delimiter: body.0,
613 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800614 },
David Tolnay57292da2017-12-27 21:03:33 -0500615 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800616 })
David Tolnayedf2b992016-09-23 20:43:45 -0700617 ));
618
David Tolnay500d8322017-12-18 00:32:51 -0800619 // TODO: figure out the actual grammar; is body required to be braced?
620 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500621 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800622 vis: syn!(Visibility) >>
623 macro_: keyword!(macro) >>
624 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500625 args: call!(tt::parenthesized) >>
626 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800627 (ItemMacro2 {
628 attrs: attrs,
629 vis: vis,
630 macro_token: macro_,
631 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500632 paren_token: args.0,
633 args: args.1,
634 brace_token: body.0,
635 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800636 })
637 ));
638
David Tolnay4c614be2017-11-10 00:02:38 -0800639 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500640 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700641 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800642 extern_: keyword!(extern) >>
643 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700644 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800645 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
646 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800647 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700648 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800649 vis: vis,
650 extern_token: extern_,
651 crate_token: crate_,
652 ident: ident,
653 rename: rename,
654 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800655 })
David Tolnayedf2b992016-09-23 20:43:45 -0700656 ));
657
David Tolnay4c614be2017-11-10 00:02:38 -0800658 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500659 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700660 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800661 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500662 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500663 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500664 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500665 true => syn!(UseTree)
666 |
David Tolnay8edcef12017-12-28 12:06:52 -0500667 false => alt!(
668 tuple!(keyword!(as), syn!(Ident)) => {
669 |rename| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800670 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500671 rename: Some(rename),
672 })
673 }
David Tolnay5f332a92017-12-26 00:42:45 -0500674 |
David Tolnay8edcef12017-12-28 12:06:52 -0500675 epsilon!() => {
676 |_| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800677 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500678 rename: None,
679 })
680 }
David Tolnay5f332a92017-12-26 00:42:45 -0500681 )
682 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800683 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800684 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700685 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800686 vis: vis,
687 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500688 leading_colon: leading_colon,
689 prefix: prefix,
690 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800691 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800692 })
David Tolnay4a057422016-10-08 00:02:31 -0700693 ));
694
David Tolnay5f332a92017-12-26 00:42:45 -0500695 named!(use_prefix -> Ident, alt!(
696 syn!(Ident)
697 |
698 keyword!(self) => { Into::into }
699 |
700 keyword!(super) => { Into::into }
701 |
702 keyword!(crate) => { Into::into }
703 ));
704
705 impl_synom!(UseTree "use tree" alt!(
706 syn!(UsePath) => { UseTree::Path }
707 |
708 syn!(UseGlob) => { UseTree::Glob }
709 |
710 syn!(UseList) => { UseTree::List }
711 ));
712
713 impl_synom!(UsePath "use path" do_parse!(
714 ident: alt!(
715 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400716 |
David Tolnay5f332a92017-12-26 00:42:45 -0500717 keyword!(self) => { Into::into }
718 ) >>
719 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
720 (UsePath {
721 ident: ident,
722 rename: rename,
723 })
724 ));
David Tolnay4a057422016-10-08 00:02:31 -0700725
David Tolnay5f332a92017-12-26 00:42:45 -0500726 impl_synom!(UseGlob "use glob" do_parse!(
727 star: punct!(*) >>
728 (UseGlob {
729 star_token: star,
730 })
731 ));
David Tolnay4a057422016-10-08 00:02:31 -0700732
David Tolnay5f332a92017-12-26 00:42:45 -0500733 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500734 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500735 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500736 brace_token: list.0,
737 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500738 })
739 ));
David Tolnay4a057422016-10-08 00:02:31 -0700740
David Tolnay4c614be2017-11-10 00:02:38 -0800741 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500742 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700743 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800744 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500745 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700746 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800747 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800748 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800749 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700750 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800751 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800752 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700753 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800754 vis: vis,
755 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500756 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800757 ident: ident,
758 colon_token: colon,
759 ty: Box::new(ty),
760 eq_token: eq,
761 expr: Box::new(value),
762 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800763 })
David Tolnay47a877c2016-10-01 16:50:55 -0700764 ));
765
David Tolnay4c614be2017-11-10 00:02:38 -0800766 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500767 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700768 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800769 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700770 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800771 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800772 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800773 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700774 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800775 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800776 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700777 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800778 vis: vis,
779 const_token: const_,
780 ident: ident,
781 colon_token: colon,
782 ty: Box::new(ty),
783 eq_token: eq,
784 expr: Box::new(value),
785 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800786 })
David Tolnay47a877c2016-10-01 16:50:55 -0700787 ));
788
David Tolnay4c614be2017-11-10 00:02:38 -0800789 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500790 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700791 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500792 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500793 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700794 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800795 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700796 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700797 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500798 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800799 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500800 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700801 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500802 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700803 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400804 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800805 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700806 attrs: {
807 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500808 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700809 attrs
810 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800811 vis: vis,
812 constness: constness,
813 unsafety: unsafety,
814 abi: abi,
815 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800816 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500817 paren_token: inputs.0,
818 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800819 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500820 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800821 generics: Generics {
822 where_clause: where_clause,
823 .. generics
824 },
825 }),
826 ident: ident,
827 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500828 brace_token: inner_attrs_stmts.0,
829 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800830 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800831 })
David Tolnay42602292016-10-01 22:25:45 -0700832 ));
833
Alex Crichton954046c2017-05-30 21:49:42 -0700834 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400835 named!(parse -> Self, alt!(
836 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800837 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400838 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500839 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800840 self_: keyword!(self) >>
841 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400842 (ArgSelfRef {
843 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500844 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400845 and_token: and,
846 self_token: self_,
847 }.into())
848 )
849 |
850 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500851 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800852 self_: keyword!(self) >>
853 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400854 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500855 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400856 self_token: self_,
857 }.into())
858 )
859 |
860 do_parse!(
861 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800862 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800863 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400864 (ArgCaptured {
865 pat: pat,
866 ty: ty,
867 colon_token: colon,
868 }.into())
869 )
870 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800871 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400872 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800873
874 fn description() -> Option<&'static str> {
875 Some("function argument")
876 }
Alex Crichton954046c2017-05-30 21:49:42 -0700877 }
David Tolnay62f374c2016-10-02 13:37:00 -0700878
David Tolnay4c614be2017-11-10 00:02:38 -0800879 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500880 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700881 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800882 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700883 ident: syn!(Ident) >>
884 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800885 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700886 Vec::new(),
887 None,
888 Some(semi),
889 )}
David Tolnay37d10332016-10-13 20:51:04 -0700890 |
Alex Crichton954046c2017-05-30 21:49:42 -0700891 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700892 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500893 many0!(Attribute::parse_inner),
894 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400895 )
David Tolnay8875fca2017-12-31 13:52:37 -0500896 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -0700897 inner_attrs,
898 Some((brace, items)),
899 None,
900 )}
David Tolnay37d10332016-10-13 20:51:04 -0700901 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800902 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700903 attrs: {
904 let mut attrs = outer_attrs;
905 attrs.extend(content_semi.0);
906 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700907 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800908 vis: vis,
909 mod_token: mod_,
910 ident: ident,
911 content: content_semi.1,
912 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800913 })
David Tolnay35902302016-10-06 01:11:08 -0700914 ));
915
David Tolnay4c614be2017-11-10 00:02:38 -0800916 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500917 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700918 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500919 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800920 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700921 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800922 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -0500923 brace_token: items.0,
924 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800925 })
David Tolnay35902302016-10-06 01:11:08 -0700926 ));
927
David Tolnay8894f602017-11-11 12:11:04 -0800928 impl_synom!(ForeignItem "foreign item" alt!(
929 syn!(ForeignItemFn) => { ForeignItem::Fn }
930 |
931 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800932 |
933 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800934 ));
David Tolnay35902302016-10-06 01:11:08 -0700935
David Tolnay8894f602017-11-11 12:11:04 -0800936 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500937 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700938 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800939 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700940 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700941 generics: syn!(Generics) >>
942 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500943 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500944 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700945 (args, variadic)
946 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800947 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500948 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700950 ({
David Tolnay8875fca2017-12-31 13:52:37 -0500951 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800952 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700953 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700954 attrs: attrs,
955 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800956 decl: Box::new(FnDecl {
957 fn_token: fn_,
958 paren_token: parens,
959 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500960 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800961 output: ret,
962 generics: Generics {
963 where_clause: where_clause,
964 .. generics
965 },
966 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700967 vis: vis,
968 }
David Tolnay35902302016-10-06 01:11:08 -0700969 })
970 ));
971
David Tolnay8894f602017-11-11 12:11:04 -0800972 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500973 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700974 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500976 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700977 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800978 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800979 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800980 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800981 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700982 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700983 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700984 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800985 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500986 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800987 static_token: static_,
988 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700989 vis: vis,
990 })
991 ));
992
David Tolnay199bcbb2017-11-12 10:33:52 -0800993 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500994 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800995 vis: syn!(Visibility) >>
996 type_: keyword!(type) >>
997 ident: syn!(Ident) >>
998 semi: punct!(;) >>
999 (ForeignItemType {
1000 attrs: attrs,
1001 vis: vis,
1002 type_token: type_,
1003 ident: ident,
1004 semi_token: semi,
1005 })
1006 ));
1007
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001008 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001009 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001010 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001011 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001012 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001013 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001014 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001016 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001018 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001019 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001020 vis: vis,
1021 type_token: type_,
1022 ident: ident,
1023 generics: Generics {
1024 where_clause: where_clause,
1025 ..generics
1026 },
1027 eq_token: eq,
1028 ty: Box::new(ty),
1029 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001030 })
David Tolnay3cf52982016-10-01 17:11:37 -07001031 ));
1032
David Tolnay4c614be2017-11-10 00:02:38 -08001033 impl_synom!(ItemStruct "struct item" switch!(
1034 map!(syn!(DeriveInput), Into::into),
1035 Item::Struct(item) => value!(item)
1036 |
1037 _ => reject!()
1038 ));
David Tolnay42602292016-10-01 22:25:45 -07001039
David Tolnay4c614be2017-11-10 00:02:38 -08001040 impl_synom!(ItemEnum "enum item" switch!(
1041 map!(syn!(DeriveInput), Into::into),
1042 Item::Enum(item) => value!(item)
1043 |
1044 _ => reject!()
1045 ));
1046
1047 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001048 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001049 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001051 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001052 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001053 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001054 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001055 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001056 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001057 vis: vis,
1058 union_token: union_,
1059 ident: ident,
1060 generics: Generics {
1061 where_clause: where_clause,
1062 .. generics
1063 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001064 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001065 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001066 ));
1067
David Tolnay4c614be2017-11-10 00:02:38 -08001068 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001069 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001070 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001071 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001072 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001073 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001074 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001075 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001076 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001077 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001078 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001079 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001080 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001081 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001082 vis: vis,
1083 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001084 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001085 trait_token: trait_,
1086 ident: ident,
1087 generics: Generics {
1088 where_clause: where_clause,
1089 .. generics
1090 },
1091 colon_token: colon,
1092 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001093 brace_token: body.0,
1094 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001095 })
David Tolnay0aecb732016-10-03 23:03:50 -07001096 ));
1097
David Tolnay03342952017-12-29 11:52:00 -05001098 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1099 Ok((cursor, cursor))
1100 }
1101
1102 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1103 begin: call!(grab_cursor) >>
1104 many0!(Attribute::parse_outer) >>
1105 option!(keyword!(unsafe)) >>
1106 keyword!(impl) >>
1107 syn!(Path) >>
1108 keyword!(for) >>
1109 punct!(..) >>
1110 braces!(epsilon!()) >>
1111 end: call!(grab_cursor) >>
1112 ({
David Tolnay61037c62018-01-05 16:21:03 -08001113 let tts = begin.token_stream().into_iter().collect::<Vec<_>>();
David Tolnay03342952017-12-29 11:52:00 -05001114 let len = tts.len() - end.token_stream().into_iter().count();
1115 ItemVerbatim {
1116 tts: tts.into_iter().take(len).collect(),
1117 }
David Tolnay4c614be2017-11-10 00:02:38 -08001118 })
David Tolnayf94e2362016-10-04 00:29:51 -07001119 ));
1120
David Tolnayda705bd2017-11-10 21:58:05 -08001121 impl_synom!(TraitItem "trait item" alt!(
1122 syn!(TraitItemConst) => { TraitItem::Const }
1123 |
1124 syn!(TraitItemMethod) => { TraitItem::Method }
1125 |
1126 syn!(TraitItemType) => { TraitItem::Type }
1127 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001128 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001129 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001130
David Tolnayda705bd2017-11-10 21:58:05 -08001131 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001132 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001133 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001134 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001135 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001136 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001137 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1138 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001139 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001140 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001141 const_token: const_,
1142 ident: ident,
1143 colon_token: colon,
1144 ty: ty,
1145 default: default,
1146 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001147 })
1148 ));
1149
David Tolnayda705bd2017-11-10 21:58:05 -08001150 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001151 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001152 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001153 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001154 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001155 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001156 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001157 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001158 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001159 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001160 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001161 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001162 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001163 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001164 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001165 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001166 ({
1167 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001168 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001169 None => (Vec::new(), None),
1170 };
David Tolnayda705bd2017-11-10 21:58:05 -08001171 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001172 attrs: {
1173 let mut attrs = outer_attrs;
1174 attrs.extend(inner_attrs);
1175 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001176 },
David Tolnayda705bd2017-11-10 21:58:05 -08001177 sig: MethodSig {
1178 constness: constness,
1179 unsafety: unsafety,
1180 abi: abi,
1181 ident: ident,
1182 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001183 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001184 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001185 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001186 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001187 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001188 generics: Generics {
1189 where_clause: where_clause,
1190 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001191 },
1192 },
David Tolnayda705bd2017-11-10 21:58:05 -08001193 },
1194 default: stmts.map(|stmts| {
1195 Block {
1196 stmts: stmts.0,
1197 brace_token: stmts.1,
1198 }
1199 }),
1200 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001201 }
David Tolnay0aecb732016-10-03 23:03:50 -07001202 })
1203 ));
1204
David Tolnayda705bd2017-11-10 21:58:05 -08001205 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001206 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001207 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001208 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001209 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001211 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001212 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001213 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001214 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001215 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001216 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001217 type_token: type_,
1218 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001219 generics: Generics {
1220 where_clause: where_clause,
1221 .. generics
1222 },
David Tolnayda705bd2017-11-10 21:58:05 -08001223 colon_token: colon,
1224 bounds: bounds.unwrap_or_default(),
1225 default: default,
1226 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001227 })
1228 ));
1229
David Tolnaydecf28d2017-11-11 11:56:45 -08001230 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001231 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001232 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001233 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001234 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001235 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001236 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001237 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001238 })
1239 ));
1240
David Tolnay4c614be2017-11-10 00:02:38 -08001241 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001242 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001243 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001244 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001245 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001246 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001247 polarity_path: alt!(
1248 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001249 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001250 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001251 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001252 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001253 )
1254 |
David Tolnay570695e2017-06-03 16:15:13 -07001255 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001256 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001257 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001258 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001259 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001260 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001261 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001262 defaultness: defaultness,
1263 unsafety: unsafety,
1264 impl_token: impl_,
1265 generics: Generics {
1266 where_clause: where_clause,
1267 .. generics
1268 },
1269 trait_: polarity_path,
1270 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001271 brace_token: body.0,
1272 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001273 })
David Tolnay4c9be372016-10-06 00:47:37 -07001274 ));
1275
David Tolnay857628c2017-11-11 12:25:31 -08001276 impl_synom!(ImplItem "item in impl block" alt!(
1277 syn!(ImplItemConst) => { ImplItem::Const }
1278 |
1279 syn!(ImplItemMethod) => { ImplItem::Method }
1280 |
1281 syn!(ImplItemType) => { ImplItem::Type }
1282 |
1283 syn!(ImplItemMacro) => { ImplItem::Macro }
1284 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001285
David Tolnay857628c2017-11-11 12:25:31 -08001286 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001287 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001288 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001289 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001290 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001291 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001292 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001293 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001295 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001297 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001298 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001299 vis: vis,
1300 defaultness: defaultness,
1301 const_token: const_,
1302 ident: ident,
1303 colon_token: colon,
1304 ty: ty,
1305 eq_token: eq,
1306 expr: value,
1307 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001308 })
1309 ));
1310
David Tolnay857628c2017-11-11 12:25:31 -08001311 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001312 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001313 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001314 defaultness: option!(keyword!(default)) >>
1315 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001316 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001317 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001318 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001319 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001320 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001321 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001322 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001323 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001324 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001325 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001326 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001327 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001328 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001329 attrs: {
1330 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001331 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001332 attrs
1333 },
David Tolnay857628c2017-11-11 12:25:31 -08001334 vis: vis,
1335 defaultness: defaultness,
1336 sig: MethodSig {
1337 constness: constness,
1338 unsafety: unsafety,
1339 abi: abi,
1340 ident: ident,
1341 decl: FnDecl {
1342 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001343 paren_token: inputs.0,
1344 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001345 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001346 generics: Generics {
1347 where_clause: where_clause,
1348 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001349 },
David Tolnayd2836e22017-12-27 23:13:00 -05001350 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001351 },
David Tolnay857628c2017-11-11 12:25:31 -08001352 },
1353 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001354 brace_token: inner_attrs_stmts.0,
1355 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001356 },
David Tolnay4c9be372016-10-06 00:47:37 -07001357 })
1358 ));
1359
David Tolnay857628c2017-11-11 12:25:31 -08001360 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001361 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001362 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001363 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001364 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001365 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001366 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001367 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001368 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001369 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001370 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001371 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001372 vis: vis,
1373 defaultness: defaultness,
1374 type_token: type_,
1375 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001376 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001377 eq_token: eq,
1378 ty: ty,
1379 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001380 })
1381 ));
1382
David Tolnay857628c2017-11-11 12:25:31 -08001383 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001384 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001385 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001386 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001387 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001388 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001389 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001390 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001391 })
1392 ));
1393
David Tolnayab919512017-12-30 23:31:51 -05001394 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1395 match *delimiter {
1396 MacroDelimiter::Brace(_) => true,
1397 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001398 }
1399 }
David Tolnayedf2b992016-09-23 20:43:45 -07001400}
David Tolnay4a51dc72016-10-01 00:40:31 -07001401
1402#[cfg(feature = "printing")]
1403mod printing {
1404 use super::*;
1405 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001406 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001407
David Tolnay1bfa7332017-11-11 12:41:20 -08001408 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001409 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001410 tokens.append_all(self.attrs.outer());
1411 self.vis.to_tokens(tokens);
1412 self.extern_token.to_tokens(tokens);
1413 self.crate_token.to_tokens(tokens);
1414 self.ident.to_tokens(tokens);
1415 if let Some((ref as_token, ref rename)) = self.rename {
1416 as_token.to_tokens(tokens);
1417 rename.to_tokens(tokens);
1418 }
1419 self.semi_token.to_tokens(tokens);
1420 }
1421 }
1422
1423 impl ToTokens for ItemUse {
1424 fn to_tokens(&self, tokens: &mut Tokens) {
1425 tokens.append_all(self.attrs.outer());
1426 self.vis.to_tokens(tokens);
1427 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001428 self.leading_colon.to_tokens(tokens);
1429 self.prefix.to_tokens(tokens);
1430 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001431 self.semi_token.to_tokens(tokens);
1432 }
1433 }
1434
1435 impl ToTokens for ItemStatic {
1436 fn to_tokens(&self, tokens: &mut Tokens) {
1437 tokens.append_all(self.attrs.outer());
1438 self.vis.to_tokens(tokens);
1439 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001440 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001441 self.ident.to_tokens(tokens);
1442 self.colon_token.to_tokens(tokens);
1443 self.ty.to_tokens(tokens);
1444 self.eq_token.to_tokens(tokens);
1445 self.expr.to_tokens(tokens);
1446 self.semi_token.to_tokens(tokens);
1447 }
1448 }
1449
1450 impl ToTokens for ItemConst {
1451 fn to_tokens(&self, tokens: &mut Tokens) {
1452 tokens.append_all(self.attrs.outer());
1453 self.vis.to_tokens(tokens);
1454 self.const_token.to_tokens(tokens);
1455 self.ident.to_tokens(tokens);
1456 self.colon_token.to_tokens(tokens);
1457 self.ty.to_tokens(tokens);
1458 self.eq_token.to_tokens(tokens);
1459 self.expr.to_tokens(tokens);
1460 self.semi_token.to_tokens(tokens);
1461 }
1462 }
1463
1464 impl ToTokens for ItemFn {
1465 fn to_tokens(&self, tokens: &mut Tokens) {
1466 tokens.append_all(self.attrs.outer());
1467 self.vis.to_tokens(tokens);
1468 self.constness.to_tokens(tokens);
1469 self.unsafety.to_tokens(tokens);
1470 self.abi.to_tokens(tokens);
1471 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1472 self.block.brace_token.surround(tokens, |tokens| {
1473 tokens.append_all(self.attrs.inner());
1474 tokens.append_all(&self.block.stmts);
1475 });
1476 }
1477 }
1478
1479 impl ToTokens for ItemMod {
1480 fn to_tokens(&self, tokens: &mut Tokens) {
1481 tokens.append_all(self.attrs.outer());
1482 self.vis.to_tokens(tokens);
1483 self.mod_token.to_tokens(tokens);
1484 self.ident.to_tokens(tokens);
1485 if let Some((ref brace, ref items)) = self.content {
1486 brace.surround(tokens, |tokens| {
1487 tokens.append_all(self.attrs.inner());
1488 tokens.append_all(items);
1489 });
1490 } else {
1491 TokensOrDefault(&self.semi).to_tokens(tokens);
1492 }
1493 }
1494 }
1495
1496 impl ToTokens for ItemForeignMod {
1497 fn to_tokens(&self, tokens: &mut Tokens) {
1498 tokens.append_all(self.attrs.outer());
1499 self.abi.to_tokens(tokens);
1500 self.brace_token.surround(tokens, |tokens| {
1501 tokens.append_all(&self.items);
1502 });
1503 }
1504 }
1505
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001506 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001507 fn to_tokens(&self, tokens: &mut Tokens) {
1508 tokens.append_all(self.attrs.outer());
1509 self.vis.to_tokens(tokens);
1510 self.type_token.to_tokens(tokens);
1511 self.ident.to_tokens(tokens);
1512 self.generics.to_tokens(tokens);
1513 self.generics.where_clause.to_tokens(tokens);
1514 self.eq_token.to_tokens(tokens);
1515 self.ty.to_tokens(tokens);
1516 self.semi_token.to_tokens(tokens);
1517 }
1518 }
1519
1520 impl ToTokens for ItemEnum {
1521 fn to_tokens(&self, tokens: &mut Tokens) {
1522 tokens.append_all(self.attrs.outer());
1523 self.vis.to_tokens(tokens);
1524 self.enum_token.to_tokens(tokens);
1525 self.ident.to_tokens(tokens);
1526 self.generics.to_tokens(tokens);
1527 self.generics.where_clause.to_tokens(tokens);
1528 self.brace_token.surround(tokens, |tokens| {
1529 self.variants.to_tokens(tokens);
1530 });
1531 }
1532 }
1533
1534 impl ToTokens for ItemStruct {
1535 fn to_tokens(&self, tokens: &mut Tokens) {
1536 tokens.append_all(self.attrs.outer());
1537 self.vis.to_tokens(tokens);
1538 self.struct_token.to_tokens(tokens);
1539 self.ident.to_tokens(tokens);
1540 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001541 match self.fields {
1542 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001543 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001544 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001545 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001546 Fields::Unnamed(ref fields) => {
1547 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001548 self.generics.where_clause.to_tokens(tokens);
1549 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001550 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001551 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001552 self.generics.where_clause.to_tokens(tokens);
1553 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001554 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001555 }
1556 }
1557 }
1558
1559 impl ToTokens for ItemUnion {
1560 fn to_tokens(&self, tokens: &mut Tokens) {
1561 tokens.append_all(self.attrs.outer());
1562 self.vis.to_tokens(tokens);
1563 self.union_token.to_tokens(tokens);
1564 self.ident.to_tokens(tokens);
1565 self.generics.to_tokens(tokens);
1566 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001567 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001568 }
1569 }
1570
1571 impl ToTokens for ItemTrait {
1572 fn to_tokens(&self, tokens: &mut Tokens) {
1573 tokens.append_all(self.attrs.outer());
1574 self.vis.to_tokens(tokens);
1575 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001576 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001577 self.trait_token.to_tokens(tokens);
1578 self.ident.to_tokens(tokens);
1579 self.generics.to_tokens(tokens);
1580 if !self.supertraits.is_empty() {
1581 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1582 self.supertraits.to_tokens(tokens);
1583 }
1584 self.generics.where_clause.to_tokens(tokens);
1585 self.brace_token.surround(tokens, |tokens| {
1586 tokens.append_all(&self.items);
1587 });
1588 }
1589 }
1590
David Tolnay1bfa7332017-11-11 12:41:20 -08001591 impl ToTokens for ItemImpl {
1592 fn to_tokens(&self, tokens: &mut Tokens) {
1593 tokens.append_all(self.attrs.outer());
1594 self.defaultness.to_tokens(tokens);
1595 self.unsafety.to_tokens(tokens);
1596 self.impl_token.to_tokens(tokens);
1597 self.generics.to_tokens(tokens);
1598 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1599 polarity.to_tokens(tokens);
1600 path.to_tokens(tokens);
1601 for_token.to_tokens(tokens);
1602 }
1603 self.self_ty.to_tokens(tokens);
1604 self.generics.where_clause.to_tokens(tokens);
1605 self.brace_token.surround(tokens, |tokens| {
1606 tokens.append_all(&self.items);
1607 });
1608 }
1609 }
1610
1611 impl ToTokens for ItemMacro {
1612 fn to_tokens(&self, tokens: &mut Tokens) {
1613 tokens.append_all(self.attrs.outer());
1614 self.mac.path.to_tokens(tokens);
1615 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001616 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001617 match self.mac.delimiter {
1618 MacroDelimiter::Paren(ref paren) => {
1619 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1620 }
1621 MacroDelimiter::Brace(ref brace) => {
1622 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1623 }
1624 MacroDelimiter::Bracket(ref bracket) => {
1625 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1626 }
1627 }
David Tolnay57292da2017-12-27 21:03:33 -05001628 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001629 }
1630 }
David Tolnay42602292016-10-01 22:25:45 -07001631
David Tolnay500d8322017-12-18 00:32:51 -08001632 impl ToTokens for ItemMacro2 {
1633 fn to_tokens(&self, tokens: &mut Tokens) {
1634 tokens.append_all(self.attrs.outer());
1635 self.vis.to_tokens(tokens);
1636 self.macro_token.to_tokens(tokens);
1637 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001638 self.paren_token.surround(tokens, |tokens| {
1639 self.args.to_tokens(tokens);
1640 });
1641 self.brace_token.surround(tokens, |tokens| {
1642 self.body.to_tokens(tokens);
1643 });
David Tolnay500d8322017-12-18 00:32:51 -08001644 }
1645 }
1646
David Tolnay2ae520a2017-12-29 11:19:50 -05001647 impl ToTokens for ItemVerbatim {
1648 fn to_tokens(&self, tokens: &mut Tokens) {
1649 self.tts.to_tokens(tokens);
1650 }
1651 }
1652
David Tolnay5f332a92017-12-26 00:42:45 -05001653 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001654 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001655 self.ident.to_tokens(tokens);
1656 if let Some((ref as_token, ref rename)) = self.rename {
1657 as_token.to_tokens(tokens);
1658 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001659 }
David Tolnay4a057422016-10-08 00:02:31 -07001660 }
1661 }
1662
David Tolnay5f332a92017-12-26 00:42:45 -05001663 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001664 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001665 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001666 }
1667 }
1668
David Tolnay5f332a92017-12-26 00:42:45 -05001669 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001670 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001671 self.brace_token.surround(tokens, |tokens| {
1672 self.items.to_tokens(tokens);
1673 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001674 }
1675 }
1676
David Tolnay1bfa7332017-11-11 12:41:20 -08001677 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001678 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001679 tokens.append_all(self.attrs.outer());
1680 self.const_token.to_tokens(tokens);
1681 self.ident.to_tokens(tokens);
1682 self.colon_token.to_tokens(tokens);
1683 self.ty.to_tokens(tokens);
1684 if let Some((ref eq_token, ref default)) = self.default {
1685 eq_token.to_tokens(tokens);
1686 default.to_tokens(tokens);
1687 }
1688 self.semi_token.to_tokens(tokens);
1689 }
1690 }
1691
1692 impl ToTokens for TraitItemMethod {
1693 fn to_tokens(&self, tokens: &mut Tokens) {
1694 tokens.append_all(self.attrs.outer());
1695 self.sig.to_tokens(tokens);
1696 match self.default {
1697 Some(ref block) => {
1698 block.brace_token.surround(tokens, |tokens| {
1699 tokens.append_all(self.attrs.inner());
1700 tokens.append_all(&block.stmts);
1701 });
David Tolnayca085422016-10-04 00:12:38 -07001702 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001703 None => {
1704 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001705 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001706 }
1707 }
1708 }
1709
1710 impl ToTokens for TraitItemType {
1711 fn to_tokens(&self, tokens: &mut Tokens) {
1712 tokens.append_all(self.attrs.outer());
1713 self.type_token.to_tokens(tokens);
1714 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001715 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001716 if !self.bounds.is_empty() {
1717 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1718 self.bounds.to_tokens(tokens);
1719 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001720 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001721 if let Some((ref eq_token, ref default)) = self.default {
1722 eq_token.to_tokens(tokens);
1723 default.to_tokens(tokens);
1724 }
1725 self.semi_token.to_tokens(tokens);
1726 }
1727 }
1728
1729 impl ToTokens for TraitItemMacro {
1730 fn to_tokens(&self, tokens: &mut Tokens) {
1731 tokens.append_all(self.attrs.outer());
1732 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001733 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001734 }
1735 }
1736
David Tolnay2ae520a2017-12-29 11:19:50 -05001737 impl ToTokens for TraitItemVerbatim {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
1739 self.tts.to_tokens(tokens);
1740 }
1741 }
1742
David Tolnay857628c2017-11-11 12:25:31 -08001743 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001744 fn to_tokens(&self, tokens: &mut Tokens) {
1745 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001746 self.vis.to_tokens(tokens);
1747 self.defaultness.to_tokens(tokens);
1748 self.const_token.to_tokens(tokens);
1749 self.ident.to_tokens(tokens);
1750 self.colon_token.to_tokens(tokens);
1751 self.ty.to_tokens(tokens);
1752 self.eq_token.to_tokens(tokens);
1753 self.expr.to_tokens(tokens);
1754 self.semi_token.to_tokens(tokens);
1755 }
1756 }
1757
1758 impl ToTokens for ImplItemMethod {
1759 fn to_tokens(&self, tokens: &mut Tokens) {
1760 tokens.append_all(self.attrs.outer());
1761 self.vis.to_tokens(tokens);
1762 self.defaultness.to_tokens(tokens);
1763 self.sig.to_tokens(tokens);
1764 self.block.brace_token.surround(tokens, |tokens| {
1765 tokens.append_all(self.attrs.inner());
1766 tokens.append_all(&self.block.stmts);
1767 });
1768 }
1769 }
1770
1771 impl ToTokens for ImplItemType {
1772 fn to_tokens(&self, tokens: &mut Tokens) {
1773 tokens.append_all(self.attrs.outer());
1774 self.vis.to_tokens(tokens);
1775 self.defaultness.to_tokens(tokens);
1776 self.type_token.to_tokens(tokens);
1777 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001778 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001779 self.eq_token.to_tokens(tokens);
1780 self.ty.to_tokens(tokens);
1781 self.semi_token.to_tokens(tokens);
1782 }
1783 }
1784
1785 impl ToTokens for ImplItemMacro {
1786 fn to_tokens(&self, tokens: &mut Tokens) {
1787 tokens.append_all(self.attrs.outer());
1788 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001789 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001790 }
1791 }
1792
David Tolnay2ae520a2017-12-29 11:19:50 -05001793 impl ToTokens for ImplItemVerbatim {
1794 fn to_tokens(&self, tokens: &mut Tokens) {
1795 self.tts.to_tokens(tokens);
1796 }
1797 }
1798
David Tolnay8894f602017-11-11 12:11:04 -08001799 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001800 fn to_tokens(&self, tokens: &mut Tokens) {
1801 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001803 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1804 self.semi_token.to_tokens(tokens);
1805 }
1806 }
1807
1808 impl ToTokens for ForeignItemStatic {
1809 fn to_tokens(&self, tokens: &mut Tokens) {
1810 tokens.append_all(self.attrs.outer());
1811 self.vis.to_tokens(tokens);
1812 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001813 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001814 self.ident.to_tokens(tokens);
1815 self.colon_token.to_tokens(tokens);
1816 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 self.semi_token.to_tokens(tokens);
1818 }
1819 }
1820
David Tolnay199bcbb2017-11-12 10:33:52 -08001821 impl ToTokens for ForeignItemType {
1822 fn to_tokens(&self, tokens: &mut Tokens) {
1823 tokens.append_all(self.attrs.outer());
1824 self.vis.to_tokens(tokens);
1825 self.type_token.to_tokens(tokens);
1826 self.ident.to_tokens(tokens);
1827 self.semi_token.to_tokens(tokens);
1828 }
1829 }
1830
David Tolnay2ae520a2017-12-29 11:19:50 -05001831 impl ToTokens for ForeignItemVerbatim {
1832 fn to_tokens(&self, tokens: &mut Tokens) {
1833 self.tts.to_tokens(tokens);
1834 }
1835 }
1836
David Tolnay570695e2017-06-03 16:15:13 -07001837 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001838 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001839 self.constness.to_tokens(tokens);
1840 self.unsafety.to_tokens(tokens);
1841 self.abi.to_tokens(tokens);
1842 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001843 }
1844 }
1845
David Tolnay570695e2017-06-03 16:15:13 -07001846 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001847
1848 impl<'a> ToTokens for NamedDecl<'a> {
1849 fn to_tokens(&self, tokens: &mut Tokens) {
1850 self.0.fn_token.to_tokens(tokens);
1851 self.1.to_tokens(tokens);
1852 self.0.generics.to_tokens(tokens);
1853 self.0.paren_token.surround(tokens, |tokens| {
1854 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001855 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1856 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001857 }
David Tolnayd2836e22017-12-27 23:13:00 -05001858 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001859 });
1860 self.0.output.to_tokens(tokens);
1861 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001862 }
1863 }
1864
Alex Crichton62a0a592017-05-22 13:58:53 -07001865 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001866 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001867 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001868 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001869 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001870 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001871 }
1872 }
1873
1874 impl ToTokens for ArgSelf {
1875 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001876 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001877 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001878 }
1879 }
1880
1881 impl ToTokens for ArgCaptured {
1882 fn to_tokens(&self, tokens: &mut Tokens) {
1883 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001884 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001885 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001886 }
1887 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001888}