blob: 0cda50c164e6e5467dd1b6d9b287ce313723881e [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 Tolnay2b214082018-01-07 01:30:18 -080021 /// Things that can appear directly inside of a module or scope.
David Tolnayc6b55bc2017-11-09 22:48:38 -080022 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080023 /// An `extern crate` item: `extern crate serde`.
Alex Crichton62a0a592017-05-22 13:58:53 -070024 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080025 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070026 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 pub extern_token: Token![extern],
28 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070029 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080030 pub rename: Option<(Token![as], Ident)>,
31 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070032 }),
David Tolnay2b214082018-01-07 01:30:18 -080033
34 /// A use declaration: `use std::collections::HashMap`.
Alex Crichton62a0a592017-05-22 13:58:53 -070035 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080036 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080038 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050039 pub leading_colon: Option<Token![::]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050040 pub prefix: Punctuated<Ident, Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050041 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
David Tolnay2b214082018-01-07 01:30:18 -080044
45 /// A static item: `static BIKE: Shed = Shed(42)`.
Alex Crichton62a0a592017-05-22 13:58:53 -070046 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080047 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070048 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050050 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
David Tolnay2b214082018-01-07 01:30:18 -080058
59 /// A constant item: `const MAX: u16 = 65535`.
Alex Crichton62a0a592017-05-22 13:58:53 -070060 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080061 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070062 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080063 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A free-standing function: `fn process(n: usize) -> Result<()> { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -070073 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080074 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070075 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050076 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050077 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070078 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050080 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070081 pub block: Box<Block>,
82 }),
David Tolnay2b214082018-01-07 01:30:18 -080083
84 /// A module or module declaration: `mod m` or `mod m { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -070085 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080086 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070087 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080088 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070089 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050090 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070092 }),
David Tolnay2b214082018-01-07 01:30:18 -080093
94 /// A block of foreign items: `extern "C" { ... }`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080096 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050098 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070099 pub items: Vec<ForeignItem>,
100 }),
David Tolnay2b214082018-01-07 01:30:18 -0800101
102 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800103 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800104 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700105 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800106 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700107 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700108 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800110 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
David Tolnay2b214082018-01-07 01:30:18 -0800113
114 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnaye3d41b72017-12-31 15:24:00 -0500115 pub Struct(ItemStruct {
116 pub attrs: Vec<Attribute>,
117 pub vis: Visibility,
118 pub struct_token: Token![struct],
119 pub ident: Ident,
120 pub generics: Generics,
121 pub fields: Fields,
122 pub semi_token: Option<Token![;]>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800127 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700128 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800129 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub ident: Ident,
131 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500132 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500133 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 }),
David Tolnay2b214082018-01-07 01:30:18 -0800135
136 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 pub Union(ItemUnion {
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 union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700141 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500143 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
David Tolnay2b214082018-01-07 01:30:18 -0800145
146 /// A trait definition: `pub trait Iterator { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800148 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700149 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500150 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500151 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700153 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700154 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500156 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500157 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700158 pub items: Vec<TraitItem>,
159 }),
David Tolnay2b214082018-01-07 01:30:18 -0800160
161 /// An impl block providing trait or associated items: `impl<A> Trait
162 /// for Data<A> { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800164 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500165 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500166 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800167 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700169 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500170 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700171 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800172 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500173 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub items: Vec<ImplItem>,
175 }),
David Tolnay2b214082018-01-07 01:30:18 -0800176
177 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnaydecf28d2017-11-11 11:56:45 -0800178 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800179 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800180 /// The `example` in `macro_rules! example { ... }`.
181 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800182 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500183 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800184 }),
David Tolnay2b214082018-01-07 01:30:18 -0800185
186 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay9c76bcb2017-12-26 23:14:59 -0500187 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800188 pub attrs: Vec<Attribute>,
189 pub vis: Visibility,
190 pub macro_token: Token![macro],
191 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500192 pub paren_token: Paren,
193 pub args: TokenStream,
194 pub brace_token: Brace,
195 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800196 }),
David Tolnay2b214082018-01-07 01:30:18 -0800197
198 /// Tokens forming an item not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500199 pub Verbatim(ItemVerbatim #manual_extra_traits {
200 pub tts: TokenStream,
201 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 }
David Tolnayb79ee962016-09-04 09:39:20 -0700203}
204
David Tolnay9c76bcb2017-12-26 23:14:59 -0500205#[cfg(feature = "extra-traits")]
206impl Eq for ItemMacro2 {}
207
208#[cfg(feature = "extra-traits")]
209impl PartialEq for ItemMacro2 {
210 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500211 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnayab919512017-12-30 23:31:51 -0500212 && self.ident == other.ident && self.paren_token == other.paren_token
213 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
214 && self.brace_token == other.brace_token
215 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500216 }
217}
218
219#[cfg(feature = "extra-traits")]
220impl Hash for ItemMacro2 {
221 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500222 where
223 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500224 {
225 self.attrs.hash(state);
226 self.vis.hash(state);
227 self.macro_token.hash(state);
228 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500229 self.paren_token.hash(state);
230 TokenStreamHelper(&self.args).hash(state);
231 self.brace_token.hash(state);
232 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500233 }
234}
235
David Tolnay2ae520a2017-12-29 11:19:50 -0500236#[cfg(feature = "extra-traits")]
237impl Eq for ItemVerbatim {}
238
239#[cfg(feature = "extra-traits")]
240impl PartialEq for ItemVerbatim {
241 fn eq(&self, other: &Self) -> bool {
242 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
243 }
244}
245
246#[cfg(feature = "extra-traits")]
247impl Hash for ItemVerbatim {
248 fn hash<H>(&self, state: &mut H)
249 where
250 H: Hasher,
251 {
252 TokenStreamHelper(&self.tts).hash(state);
253 }
254}
255
David Tolnay0e837402016-12-22 17:25:55 -0500256impl From<DeriveInput> for Item {
257 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500258 match input.data {
259 Data::Struct(data) => Item::Struct(ItemStruct {
260 attrs: input.attrs,
261 vis: input.vis,
262 struct_token: data.struct_token,
263 ident: input.ident,
264 generics: input.generics,
265 fields: data.fields,
266 semi_token: data.semi_token,
267 }),
268 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500269 attrs: input.attrs,
270 vis: input.vis,
271 enum_token: data.enum_token,
272 ident: input.ident,
273 generics: input.generics,
274 brace_token: data.brace_token,
275 variants: data.variants,
276 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500277 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500278 attrs: input.attrs,
279 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500280 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500281 ident: input.ident,
282 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500283 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500284 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700285 }
286 }
287}
288
Alex Crichton62a0a592017-05-22 13:58:53 -0700289ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800290 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay5f332a92017-12-26 00:42:45 -0500291 pub enum UseTree {
David Tolnay05658502018-01-07 09:56:37 -0800292 /// An identifier imported by a `use` item: `Type` or `Type as Renamed`.
David Tolnay5f332a92017-12-26 00:42:45 -0500293 pub Path(UsePath {
294 pub ident: Ident,
295 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
David Tolnay05658502018-01-07 09:56:37 -0800297 /// A glob import in a `use` item: `*`.
David Tolnay5f332a92017-12-26 00:42:45 -0500298 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800299 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 }),
David Tolnay05658502018-01-07 09:56:37 -0800301 /// A braced list of imports in a `use` item: `{A, B, C}`.
David Tolnay5f332a92017-12-26 00:42:45 -0500302 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500303 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500304 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 }),
306 }
307}
308
Alex Crichton62a0a592017-05-22 13:58:53 -0700309ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800310 /// An item within an `extern` block.
David Tolnay8894f602017-11-11 12:11:04 -0800311 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800312 /// A foreign function in an `extern` block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800314 pub attrs: Vec<Attribute>,
315 pub vis: Visibility,
316 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800318 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 }),
David Tolnayebb72722018-01-07 01:14:13 -0800320
321 /// A foreign static item in an `extern` block: `static ext: u8`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800323 pub attrs: Vec<Attribute>,
324 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800325 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500326 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800327 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800328 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800329 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800330 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 }),
David Tolnayebb72722018-01-07 01:14:13 -0800332
333 /// A foreign type in an `extern` block: `type void`.
David Tolnay199bcbb2017-11-12 10:33:52 -0800334 pub Type(ForeignItemType {
335 pub attrs: Vec<Attribute>,
336 pub vis: Visibility,
337 pub type_token: Token![type],
338 pub ident: Ident,
339 pub semi_token: Token![;],
340 }),
David Tolnayebb72722018-01-07 01:14:13 -0800341
342 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500343 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
344 pub tts: TokenStream,
345 }),
346 }
347}
348
349#[cfg(feature = "extra-traits")]
350impl Eq for ForeignItemVerbatim {}
351
352#[cfg(feature = "extra-traits")]
353impl PartialEq for ForeignItemVerbatim {
354 fn eq(&self, other: &Self) -> bool {
355 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
356 }
357}
358
359#[cfg(feature = "extra-traits")]
360impl Hash for ForeignItemVerbatim {
361 fn hash<H>(&self, state: &mut H)
362 where
363 H: Hasher,
364 {
365 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700367}
368
David Tolnayda705bd2017-11-10 21:58:05 -0800369ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800370 /// An item declaration within the definition of a trait.
David Tolnayda705bd2017-11-10 21:58:05 -0800371 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800372 /// An associated constant within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800374 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700376 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800378 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800379 pub default: Option<(Token![=], Expr)>,
380 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
David Tolnayebb72722018-01-07 01:14:13 -0800382
383 /// A trait method within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800385 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 pub sig: MethodSig,
387 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnayebb72722018-01-07 01:14:13 -0800390
391 /// An associated type within the definition of a trait.
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800393 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700395 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500396 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800397 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500398 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800399 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800400 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 }),
David Tolnayebb72722018-01-07 01:14:13 -0800402
403 /// A macro invocation within the definition of a trait.
David Tolnaydecf28d2017-11-11 11:56:45 -0800404 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800405 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800406 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500407 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800408 }),
David Tolnayebb72722018-01-07 01:14:13 -0800409
410 /// Tokens within the definition of a trait not interpreted by Syn.
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 Tolnayebb72722018-01-07 01:14:13 -0800438 /// An item within an impl block.
David Tolnay857628c2017-11-11 12:25:31 -0800439 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800440 /// An associated constant within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700441 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800442 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700443 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500444 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800445 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700446 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800447 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800448 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800449 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800451 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700452 }),
David Tolnayebb72722018-01-07 01:14:13 -0800453
454 /// A method within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700455 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800456 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700457 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500458 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700459 pub sig: MethodSig,
460 pub block: Block,
461 }),
David Tolnayebb72722018-01-07 01:14:13 -0800462
463 /// An associated type within an impl block.
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800465 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700466 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500467 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800468 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700469 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500470 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800471 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800472 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800473 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700474 }),
David Tolnayebb72722018-01-07 01:14:13 -0800475
476 /// A macro invocation within an impl block.
David Tolnay857628c2017-11-11 12:25:31 -0800477 pub Macro(ImplItemMacro {
478 pub attrs: Vec<Attribute>,
479 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500480 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800481 }),
David Tolnayebb72722018-01-07 01:14:13 -0800482
483 /// Tokens within an impl block not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500484 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
485 pub tts: TokenStream,
486 }),
487 }
488}
489
490#[cfg(feature = "extra-traits")]
491impl Eq for ImplItemVerbatim {}
492
493#[cfg(feature = "extra-traits")]
494impl PartialEq for ImplItemVerbatim {
495 fn eq(&self, other: &Self) -> bool {
496 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
497 }
498}
499
500#[cfg(feature = "extra-traits")]
501impl Hash for ImplItemVerbatim {
502 fn hash<H>(&self, state: &mut H)
503 where
504 H: Hasher,
505 {
506 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700508}
509
510ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800511 /// A method's signature in a trait or implementation: `unsafe fn
512 /// initialize(&self)`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500514 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500515 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700517 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700519 }
520}
521
522ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800523 /// Header of a function declaration, without including the body.
Alex Crichton62a0a592017-05-22 13:58:53 -0700524 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500526 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500527 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500528 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500529 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500530 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700531 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700532}
533
Alex Crichton62a0a592017-05-22 13:58:53 -0700534ast_enum_of_structs! {
David Tolnay3f559052018-01-06 23:59:48 -0800535 /// An argument in a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700536 ///
537 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
538 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800539 /// Self captured by reference in a function signature: `&self` or `&mut
540 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700543 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500544 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500545 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 }),
David Tolnay3f559052018-01-06 23:59:48 -0800547 /// Self captured by value in a function signature: `self` or `mut
548 /// self`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700549 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500550 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700552 }),
David Tolnay3f559052018-01-06 23:59:48 -0800553 /// An explicitly typed pattern captured by a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 pub Captured(ArgCaptured {
555 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800556 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800557 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700558 }),
David Tolnay3f559052018-01-06 23:59:48 -0800559 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500560 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800561 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800562 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700563 }
David Tolnay62f374c2016-10-02 13:37:00 -0700564}
565
David Tolnayedf2b992016-09-23 20:43:45 -0700566#[cfg(feature = "parsing")]
567pub mod parsing {
568 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700569
David Tolnaydfc886b2018-01-06 08:03:09 -0800570 use buffer::Cursor;
571 use synom::{PResult, Synom};
David Tolnay84aa0752016-10-02 23:01:13 -0700572
David Tolnay4c614be2017-11-10 00:02:38 -0800573 impl_synom!(Item "item" alt!(
574 syn!(ItemExternCrate) => { Item::ExternCrate }
575 |
576 syn!(ItemUse) => { Item::Use }
577 |
578 syn!(ItemStatic) => { Item::Static }
579 |
580 syn!(ItemConst) => { Item::Const }
581 |
582 syn!(ItemFn) => { Item::Fn }
583 |
584 syn!(ItemMod) => { Item::Mod }
585 |
586 syn!(ItemForeignMod) => { Item::ForeignMod }
587 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800588 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800589 |
590 syn!(ItemStruct) => { Item::Struct }
591 |
592 syn!(ItemEnum) => { Item::Enum }
593 |
594 syn!(ItemUnion) => { Item::Union }
595 |
596 syn!(ItemTrait) => { Item::Trait }
597 |
David Tolnay03342952017-12-29 11:52:00 -0500598 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800599 |
600 syn!(ItemImpl) => { Item::Impl }
601 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800602 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800603 |
604 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800605 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700606
David Tolnaydecf28d2017-11-11 11:56:45 -0800607 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500608 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700609 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800610 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700611 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500612 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500613 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800614 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700615 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800616 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800617 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500618 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700619 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500620 delimiter: body.0,
621 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800622 },
David Tolnay57292da2017-12-27 21:03:33 -0500623 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800624 })
David Tolnayedf2b992016-09-23 20:43:45 -0700625 ));
626
David Tolnay500d8322017-12-18 00:32:51 -0800627 // TODO: figure out the actual grammar; is body required to be braced?
628 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500629 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800630 vis: syn!(Visibility) >>
631 macro_: keyword!(macro) >>
632 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500633 args: call!(tt::parenthesized) >>
634 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800635 (ItemMacro2 {
636 attrs: attrs,
637 vis: vis,
638 macro_token: macro_,
639 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500640 paren_token: args.0,
641 args: args.1,
642 brace_token: body.0,
643 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800644 })
645 ));
646
David Tolnay4c614be2017-11-10 00:02:38 -0800647 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500648 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700649 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800650 extern_: keyword!(extern) >>
651 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700652 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800653 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
654 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800655 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700656 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800657 vis: vis,
658 extern_token: extern_,
659 crate_token: crate_,
660 ident: ident,
661 rename: rename,
662 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800663 })
David Tolnayedf2b992016-09-23 20:43:45 -0700664 ));
665
David Tolnay4c614be2017-11-10 00:02:38 -0800666 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500667 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700668 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800669 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500670 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500671 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500672 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500673 true => syn!(UseTree)
674 |
David Tolnay8edcef12017-12-28 12:06:52 -0500675 false => alt!(
676 tuple!(keyword!(as), syn!(Ident)) => {
677 |rename| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800678 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500679 rename: Some(rename),
680 })
681 }
David Tolnay5f332a92017-12-26 00:42:45 -0500682 |
David Tolnay8edcef12017-12-28 12:06:52 -0500683 epsilon!() => {
684 |_| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800685 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500686 rename: None,
687 })
688 }
David Tolnay5f332a92017-12-26 00:42:45 -0500689 )
690 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800691 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800692 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700693 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800694 vis: vis,
695 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500696 leading_colon: leading_colon,
697 prefix: prefix,
698 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800699 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800700 })
David Tolnay4a057422016-10-08 00:02:31 -0700701 ));
702
David Tolnay5f332a92017-12-26 00:42:45 -0500703 named!(use_prefix -> Ident, alt!(
704 syn!(Ident)
705 |
706 keyword!(self) => { Into::into }
707 |
708 keyword!(super) => { Into::into }
709 |
710 keyword!(crate) => { Into::into }
711 ));
712
713 impl_synom!(UseTree "use tree" alt!(
714 syn!(UsePath) => { UseTree::Path }
715 |
716 syn!(UseGlob) => { UseTree::Glob }
717 |
718 syn!(UseList) => { UseTree::List }
719 ));
720
721 impl_synom!(UsePath "use path" do_parse!(
722 ident: alt!(
723 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400724 |
David Tolnay5f332a92017-12-26 00:42:45 -0500725 keyword!(self) => { Into::into }
726 ) >>
727 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
728 (UsePath {
729 ident: ident,
730 rename: rename,
731 })
732 ));
David Tolnay4a057422016-10-08 00:02:31 -0700733
David Tolnay5f332a92017-12-26 00:42:45 -0500734 impl_synom!(UseGlob "use glob" do_parse!(
735 star: punct!(*) >>
736 (UseGlob {
737 star_token: star,
738 })
739 ));
David Tolnay4a057422016-10-08 00:02:31 -0700740
David Tolnay5f332a92017-12-26 00:42:45 -0500741 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500742 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500743 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500744 brace_token: list.0,
745 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500746 })
747 ));
David Tolnay4a057422016-10-08 00:02:31 -0700748
David Tolnay4c614be2017-11-10 00:02:38 -0800749 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500750 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700751 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800752 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500753 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700754 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800755 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800756 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800757 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700758 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800759 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800760 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700761 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800762 vis: vis,
763 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500764 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800765 ident: ident,
766 colon_token: colon,
767 ty: Box::new(ty),
768 eq_token: eq,
769 expr: Box::new(value),
770 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800771 })
David Tolnay47a877c2016-10-01 16:50:55 -0700772 ));
773
David Tolnay4c614be2017-11-10 00:02:38 -0800774 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500775 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700776 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800777 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700778 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800779 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800780 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800781 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700782 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800783 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800784 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700785 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800786 vis: vis,
787 const_token: const_,
788 ident: ident,
789 colon_token: colon,
790 ty: Box::new(ty),
791 eq_token: eq,
792 expr: Box::new(value),
793 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800794 })
David Tolnay47a877c2016-10-01 16:50:55 -0700795 ));
796
David Tolnay4c614be2017-11-10 00:02:38 -0800797 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500798 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700799 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500800 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500801 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700802 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800803 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700804 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700805 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500806 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800807 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500808 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700809 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500810 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700811 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400812 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800813 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700814 attrs: {
815 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500816 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700817 attrs
818 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800819 vis: vis,
820 constness: constness,
821 unsafety: unsafety,
822 abi: abi,
823 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800824 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500825 paren_token: inputs.0,
826 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800827 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500828 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800829 generics: Generics {
830 where_clause: where_clause,
831 .. generics
832 },
833 }),
834 ident: ident,
835 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500836 brace_token: inner_attrs_stmts.0,
837 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800838 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800839 })
David Tolnay42602292016-10-01 22:25:45 -0700840 ));
841
Alex Crichton954046c2017-05-30 21:49:42 -0700842 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400843 named!(parse -> Self, alt!(
844 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800845 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400846 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500847 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800848 self_: keyword!(self) >>
849 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400850 (ArgSelfRef {
851 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500852 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400853 and_token: and,
854 self_token: self_,
855 }.into())
856 )
857 |
858 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500859 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800860 self_: keyword!(self) >>
861 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400862 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500863 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400864 self_token: self_,
865 }.into())
866 )
867 |
868 do_parse!(
869 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800870 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800871 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400872 (ArgCaptured {
873 pat: pat,
874 ty: ty,
875 colon_token: colon,
876 }.into())
877 )
878 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800879 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400880 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800881
882 fn description() -> Option<&'static str> {
883 Some("function argument")
884 }
Alex Crichton954046c2017-05-30 21:49:42 -0700885 }
David Tolnay62f374c2016-10-02 13:37:00 -0700886
David Tolnay4c614be2017-11-10 00:02:38 -0800887 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500888 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700889 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800890 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700891 ident: syn!(Ident) >>
892 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800893 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700894 Vec::new(),
895 None,
896 Some(semi),
897 )}
David Tolnay37d10332016-10-13 20:51:04 -0700898 |
Alex Crichton954046c2017-05-30 21:49:42 -0700899 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700900 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500901 many0!(Attribute::parse_inner),
902 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400903 )
David Tolnay8875fca2017-12-31 13:52:37 -0500904 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -0700905 inner_attrs,
906 Some((brace, items)),
907 None,
908 )}
David Tolnay37d10332016-10-13 20:51:04 -0700909 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800910 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700911 attrs: {
912 let mut attrs = outer_attrs;
913 attrs.extend(content_semi.0);
914 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700915 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800916 vis: vis,
917 mod_token: mod_,
918 ident: ident,
919 content: content_semi.1,
920 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800921 })
David Tolnay35902302016-10-06 01:11:08 -0700922 ));
923
David Tolnay4c614be2017-11-10 00:02:38 -0800924 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500925 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700926 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500927 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800928 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700929 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800930 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -0500931 brace_token: items.0,
932 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -0800933 })
David Tolnay35902302016-10-06 01:11:08 -0700934 ));
935
David Tolnay8894f602017-11-11 12:11:04 -0800936 impl_synom!(ForeignItem "foreign item" alt!(
937 syn!(ForeignItemFn) => { ForeignItem::Fn }
938 |
939 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800940 |
941 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800942 ));
David Tolnay35902302016-10-06 01:11:08 -0700943
David Tolnay8894f602017-11-11 12:11:04 -0800944 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500945 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700946 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800947 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700948 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700949 generics: syn!(Generics) >>
950 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500951 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500952 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700953 (args, variadic)
954 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800955 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500956 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700958 ({
David Tolnay8875fca2017-12-31 13:52:37 -0500959 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -0800960 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700961 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700962 attrs: attrs,
963 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800964 decl: Box::new(FnDecl {
965 fn_token: fn_,
966 paren_token: parens,
967 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500968 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800969 output: ret,
970 generics: Generics {
971 where_clause: where_clause,
972 .. generics
973 },
974 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700975 vis: vis,
976 }
David Tolnay35902302016-10-06 01:11:08 -0700977 })
978 ));
979
David Tolnay8894f602017-11-11 12:11:04 -0800980 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500981 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700982 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800983 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500984 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700985 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800987 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800988 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800989 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700990 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700991 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700992 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800993 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500994 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800995 static_token: static_,
996 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700997 vis: vis,
998 })
999 ));
1000
David Tolnay199bcbb2017-11-12 10:33:52 -08001001 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001002 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001003 vis: syn!(Visibility) >>
1004 type_: keyword!(type) >>
1005 ident: syn!(Ident) >>
1006 semi: punct!(;) >>
1007 (ForeignItemType {
1008 attrs: attrs,
1009 vis: vis,
1010 type_token: type_,
1011 ident: ident,
1012 semi_token: semi,
1013 })
1014 ));
1015
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001016 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001017 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001018 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001019 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001020 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001021 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001022 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001024 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001026 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001027 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001028 vis: vis,
1029 type_token: type_,
1030 ident: ident,
1031 generics: Generics {
1032 where_clause: where_clause,
1033 ..generics
1034 },
1035 eq_token: eq,
1036 ty: Box::new(ty),
1037 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001038 })
David Tolnay3cf52982016-10-01 17:11:37 -07001039 ));
1040
David Tolnay4c614be2017-11-10 00:02:38 -08001041 impl_synom!(ItemStruct "struct item" switch!(
1042 map!(syn!(DeriveInput), Into::into),
1043 Item::Struct(item) => value!(item)
1044 |
1045 _ => reject!()
1046 ));
David Tolnay42602292016-10-01 22:25:45 -07001047
David Tolnay4c614be2017-11-10 00:02:38 -08001048 impl_synom!(ItemEnum "enum item" switch!(
1049 map!(syn!(DeriveInput), Into::into),
1050 Item::Enum(item) => value!(item)
1051 |
1052 _ => reject!()
1053 ));
1054
1055 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001056 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001057 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001058 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001059 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001060 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001061 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001062 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001063 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001064 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001065 vis: vis,
1066 union_token: union_,
1067 ident: ident,
1068 generics: Generics {
1069 where_clause: where_clause,
1070 .. generics
1071 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001072 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001073 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001074 ));
1075
David Tolnay4c614be2017-11-10 00:02:38 -08001076 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001077 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001078 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001079 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001080 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001081 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001082 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001083 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001084 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001085 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001086 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001087 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001088 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001089 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001090 vis: vis,
1091 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001092 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001093 trait_token: trait_,
1094 ident: ident,
1095 generics: Generics {
1096 where_clause: where_clause,
1097 .. generics
1098 },
1099 colon_token: colon,
1100 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001101 brace_token: body.0,
1102 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001103 })
David Tolnay0aecb732016-10-03 23:03:50 -07001104 ));
1105
David Tolnay03342952017-12-29 11:52:00 -05001106 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1107 Ok((cursor, cursor))
1108 }
1109
1110 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1111 begin: call!(grab_cursor) >>
1112 many0!(Attribute::parse_outer) >>
1113 option!(keyword!(unsafe)) >>
1114 keyword!(impl) >>
1115 syn!(Path) >>
1116 keyword!(for) >>
1117 punct!(..) >>
1118 braces!(epsilon!()) >>
1119 end: call!(grab_cursor) >>
1120 ({
David Tolnay61037c62018-01-05 16:21:03 -08001121 let tts = begin.token_stream().into_iter().collect::<Vec<_>>();
David Tolnay03342952017-12-29 11:52:00 -05001122 let len = tts.len() - end.token_stream().into_iter().count();
1123 ItemVerbatim {
1124 tts: tts.into_iter().take(len).collect(),
1125 }
David Tolnay4c614be2017-11-10 00:02:38 -08001126 })
David Tolnayf94e2362016-10-04 00:29:51 -07001127 ));
1128
David Tolnayda705bd2017-11-10 21:58:05 -08001129 impl_synom!(TraitItem "trait item" alt!(
1130 syn!(TraitItemConst) => { TraitItem::Const }
1131 |
1132 syn!(TraitItemMethod) => { TraitItem::Method }
1133 |
1134 syn!(TraitItemType) => { TraitItem::Type }
1135 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001136 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001137 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001138
David Tolnayda705bd2017-11-10 21:58:05 -08001139 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001140 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001141 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001142 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001143 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001144 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001145 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1146 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001147 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001148 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001149 const_token: const_,
1150 ident: ident,
1151 colon_token: colon,
1152 ty: ty,
1153 default: default,
1154 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001155 })
1156 ));
1157
David Tolnayda705bd2017-11-10 21:58:05 -08001158 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001159 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001160 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001161 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001162 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001163 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001164 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001165 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001166 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001167 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001168 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001169 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001170 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001171 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001172 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001173 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001174 ({
1175 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001176 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001177 None => (Vec::new(), None),
1178 };
David Tolnayda705bd2017-11-10 21:58:05 -08001179 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001180 attrs: {
1181 let mut attrs = outer_attrs;
1182 attrs.extend(inner_attrs);
1183 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001184 },
David Tolnayda705bd2017-11-10 21:58:05 -08001185 sig: MethodSig {
1186 constness: constness,
1187 unsafety: unsafety,
1188 abi: abi,
1189 ident: ident,
1190 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001191 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001192 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001193 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001194 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001195 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001196 generics: Generics {
1197 where_clause: where_clause,
1198 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001199 },
1200 },
David Tolnayda705bd2017-11-10 21:58:05 -08001201 },
1202 default: stmts.map(|stmts| {
1203 Block {
1204 stmts: stmts.0,
1205 brace_token: stmts.1,
1206 }
1207 }),
1208 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001209 }
David Tolnay0aecb732016-10-03 23:03:50 -07001210 })
1211 ));
1212
David Tolnayda705bd2017-11-10 21:58:05 -08001213 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001214 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001215 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001216 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001217 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001218 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001219 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001220 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001221 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001222 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001223 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001224 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001225 type_token: type_,
1226 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001227 generics: Generics {
1228 where_clause: where_clause,
1229 .. generics
1230 },
David Tolnayda705bd2017-11-10 21:58:05 -08001231 colon_token: colon,
1232 bounds: bounds.unwrap_or_default(),
1233 default: default,
1234 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001235 })
1236 ));
1237
David Tolnaydecf28d2017-11-11 11:56:45 -08001238 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001239 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001240 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001241 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001242 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001243 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001244 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001245 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001246 })
1247 ));
1248
David Tolnay4c614be2017-11-10 00:02:38 -08001249 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001250 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001251 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001252 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001253 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001254 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001255 polarity_path: alt!(
1256 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001257 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001258 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001259 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001260 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001261 )
1262 |
David Tolnay570695e2017-06-03 16:15:13 -07001263 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001264 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001265 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001266 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001267 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001268 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001269 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001270 defaultness: defaultness,
1271 unsafety: unsafety,
1272 impl_token: impl_,
1273 generics: Generics {
1274 where_clause: where_clause,
1275 .. generics
1276 },
1277 trait_: polarity_path,
1278 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001279 brace_token: body.0,
1280 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001281 })
David Tolnay4c9be372016-10-06 00:47:37 -07001282 ));
1283
David Tolnay857628c2017-11-11 12:25:31 -08001284 impl_synom!(ImplItem "item in impl block" alt!(
1285 syn!(ImplItemConst) => { ImplItem::Const }
1286 |
1287 syn!(ImplItemMethod) => { ImplItem::Method }
1288 |
1289 syn!(ImplItemType) => { ImplItem::Type }
1290 |
1291 syn!(ImplItemMacro) => { ImplItem::Macro }
1292 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001293
David Tolnay857628c2017-11-11 12:25:31 -08001294 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001295 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001296 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001297 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001298 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001299 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001300 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001301 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001302 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001303 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001304 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001305 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001306 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001307 vis: vis,
1308 defaultness: defaultness,
1309 const_token: const_,
1310 ident: ident,
1311 colon_token: colon,
1312 ty: ty,
1313 eq_token: eq,
1314 expr: value,
1315 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001316 })
1317 ));
1318
David Tolnay857628c2017-11-11 12:25:31 -08001319 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001320 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001321 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001322 defaultness: option!(keyword!(default)) >>
1323 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001324 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001325 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001326 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001327 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001328 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001329 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001330 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001331 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001332 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001333 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001334 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001335 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001336 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001337 attrs: {
1338 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001339 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001340 attrs
1341 },
David Tolnay857628c2017-11-11 12:25:31 -08001342 vis: vis,
1343 defaultness: defaultness,
1344 sig: MethodSig {
1345 constness: constness,
1346 unsafety: unsafety,
1347 abi: abi,
1348 ident: ident,
1349 decl: FnDecl {
1350 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001351 paren_token: inputs.0,
1352 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001353 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001354 generics: Generics {
1355 where_clause: where_clause,
1356 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001357 },
David Tolnayd2836e22017-12-27 23:13:00 -05001358 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001359 },
David Tolnay857628c2017-11-11 12:25:31 -08001360 },
1361 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001362 brace_token: inner_attrs_stmts.0,
1363 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001364 },
David Tolnay4c9be372016-10-06 00:47:37 -07001365 })
1366 ));
1367
David Tolnay857628c2017-11-11 12:25:31 -08001368 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001369 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001370 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001371 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001372 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001373 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001374 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001375 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001376 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001377 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001378 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001379 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001380 vis: vis,
1381 defaultness: defaultness,
1382 type_token: type_,
1383 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001384 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001385 eq_token: eq,
1386 ty: ty,
1387 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001388 })
1389 ));
1390
David Tolnay857628c2017-11-11 12:25:31 -08001391 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001392 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001393 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001394 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001395 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001396 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001397 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001398 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001399 })
1400 ));
1401
David Tolnayab919512017-12-30 23:31:51 -05001402 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1403 match *delimiter {
1404 MacroDelimiter::Brace(_) => true,
1405 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001406 }
1407 }
David Tolnayedf2b992016-09-23 20:43:45 -07001408}
David Tolnay4a51dc72016-10-01 00:40:31 -07001409
1410#[cfg(feature = "printing")]
1411mod printing {
1412 use super::*;
1413 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001414 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001415
David Tolnay1bfa7332017-11-11 12:41:20 -08001416 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001417 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001418 tokens.append_all(self.attrs.outer());
1419 self.vis.to_tokens(tokens);
1420 self.extern_token.to_tokens(tokens);
1421 self.crate_token.to_tokens(tokens);
1422 self.ident.to_tokens(tokens);
1423 if let Some((ref as_token, ref rename)) = self.rename {
1424 as_token.to_tokens(tokens);
1425 rename.to_tokens(tokens);
1426 }
1427 self.semi_token.to_tokens(tokens);
1428 }
1429 }
1430
1431 impl ToTokens for ItemUse {
1432 fn to_tokens(&self, tokens: &mut Tokens) {
1433 tokens.append_all(self.attrs.outer());
1434 self.vis.to_tokens(tokens);
1435 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001436 self.leading_colon.to_tokens(tokens);
1437 self.prefix.to_tokens(tokens);
1438 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001439 self.semi_token.to_tokens(tokens);
1440 }
1441 }
1442
1443 impl ToTokens for ItemStatic {
1444 fn to_tokens(&self, tokens: &mut Tokens) {
1445 tokens.append_all(self.attrs.outer());
1446 self.vis.to_tokens(tokens);
1447 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001448 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001449 self.ident.to_tokens(tokens);
1450 self.colon_token.to_tokens(tokens);
1451 self.ty.to_tokens(tokens);
1452 self.eq_token.to_tokens(tokens);
1453 self.expr.to_tokens(tokens);
1454 self.semi_token.to_tokens(tokens);
1455 }
1456 }
1457
1458 impl ToTokens for ItemConst {
1459 fn to_tokens(&self, tokens: &mut Tokens) {
1460 tokens.append_all(self.attrs.outer());
1461 self.vis.to_tokens(tokens);
1462 self.const_token.to_tokens(tokens);
1463 self.ident.to_tokens(tokens);
1464 self.colon_token.to_tokens(tokens);
1465 self.ty.to_tokens(tokens);
1466 self.eq_token.to_tokens(tokens);
1467 self.expr.to_tokens(tokens);
1468 self.semi_token.to_tokens(tokens);
1469 }
1470 }
1471
1472 impl ToTokens for ItemFn {
1473 fn to_tokens(&self, tokens: &mut Tokens) {
1474 tokens.append_all(self.attrs.outer());
1475 self.vis.to_tokens(tokens);
1476 self.constness.to_tokens(tokens);
1477 self.unsafety.to_tokens(tokens);
1478 self.abi.to_tokens(tokens);
1479 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1480 self.block.brace_token.surround(tokens, |tokens| {
1481 tokens.append_all(self.attrs.inner());
1482 tokens.append_all(&self.block.stmts);
1483 });
1484 }
1485 }
1486
1487 impl ToTokens for ItemMod {
1488 fn to_tokens(&self, tokens: &mut Tokens) {
1489 tokens.append_all(self.attrs.outer());
1490 self.vis.to_tokens(tokens);
1491 self.mod_token.to_tokens(tokens);
1492 self.ident.to_tokens(tokens);
1493 if let Some((ref brace, ref items)) = self.content {
1494 brace.surround(tokens, |tokens| {
1495 tokens.append_all(self.attrs.inner());
1496 tokens.append_all(items);
1497 });
1498 } else {
1499 TokensOrDefault(&self.semi).to_tokens(tokens);
1500 }
1501 }
1502 }
1503
1504 impl ToTokens for ItemForeignMod {
1505 fn to_tokens(&self, tokens: &mut Tokens) {
1506 tokens.append_all(self.attrs.outer());
1507 self.abi.to_tokens(tokens);
1508 self.brace_token.surround(tokens, |tokens| {
1509 tokens.append_all(&self.items);
1510 });
1511 }
1512 }
1513
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001514 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001515 fn to_tokens(&self, tokens: &mut Tokens) {
1516 tokens.append_all(self.attrs.outer());
1517 self.vis.to_tokens(tokens);
1518 self.type_token.to_tokens(tokens);
1519 self.ident.to_tokens(tokens);
1520 self.generics.to_tokens(tokens);
1521 self.generics.where_clause.to_tokens(tokens);
1522 self.eq_token.to_tokens(tokens);
1523 self.ty.to_tokens(tokens);
1524 self.semi_token.to_tokens(tokens);
1525 }
1526 }
1527
1528 impl ToTokens for ItemEnum {
1529 fn to_tokens(&self, tokens: &mut Tokens) {
1530 tokens.append_all(self.attrs.outer());
1531 self.vis.to_tokens(tokens);
1532 self.enum_token.to_tokens(tokens);
1533 self.ident.to_tokens(tokens);
1534 self.generics.to_tokens(tokens);
1535 self.generics.where_clause.to_tokens(tokens);
1536 self.brace_token.surround(tokens, |tokens| {
1537 self.variants.to_tokens(tokens);
1538 });
1539 }
1540 }
1541
1542 impl ToTokens for ItemStruct {
1543 fn to_tokens(&self, tokens: &mut Tokens) {
1544 tokens.append_all(self.attrs.outer());
1545 self.vis.to_tokens(tokens);
1546 self.struct_token.to_tokens(tokens);
1547 self.ident.to_tokens(tokens);
1548 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001549 match self.fields {
1550 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001551 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001552 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001553 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001554 Fields::Unnamed(ref fields) => {
1555 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001556 self.generics.where_clause.to_tokens(tokens);
1557 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001558 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001559 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001560 self.generics.where_clause.to_tokens(tokens);
1561 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001562 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001563 }
1564 }
1565 }
1566
1567 impl ToTokens for ItemUnion {
1568 fn to_tokens(&self, tokens: &mut Tokens) {
1569 tokens.append_all(self.attrs.outer());
1570 self.vis.to_tokens(tokens);
1571 self.union_token.to_tokens(tokens);
1572 self.ident.to_tokens(tokens);
1573 self.generics.to_tokens(tokens);
1574 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001575 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001576 }
1577 }
1578
1579 impl ToTokens for ItemTrait {
1580 fn to_tokens(&self, tokens: &mut Tokens) {
1581 tokens.append_all(self.attrs.outer());
1582 self.vis.to_tokens(tokens);
1583 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001584 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001585 self.trait_token.to_tokens(tokens);
1586 self.ident.to_tokens(tokens);
1587 self.generics.to_tokens(tokens);
1588 if !self.supertraits.is_empty() {
1589 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1590 self.supertraits.to_tokens(tokens);
1591 }
1592 self.generics.where_clause.to_tokens(tokens);
1593 self.brace_token.surround(tokens, |tokens| {
1594 tokens.append_all(&self.items);
1595 });
1596 }
1597 }
1598
David Tolnay1bfa7332017-11-11 12:41:20 -08001599 impl ToTokens for ItemImpl {
1600 fn to_tokens(&self, tokens: &mut Tokens) {
1601 tokens.append_all(self.attrs.outer());
1602 self.defaultness.to_tokens(tokens);
1603 self.unsafety.to_tokens(tokens);
1604 self.impl_token.to_tokens(tokens);
1605 self.generics.to_tokens(tokens);
1606 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1607 polarity.to_tokens(tokens);
1608 path.to_tokens(tokens);
1609 for_token.to_tokens(tokens);
1610 }
1611 self.self_ty.to_tokens(tokens);
1612 self.generics.where_clause.to_tokens(tokens);
1613 self.brace_token.surround(tokens, |tokens| {
1614 tokens.append_all(&self.items);
1615 });
1616 }
1617 }
1618
1619 impl ToTokens for ItemMacro {
1620 fn to_tokens(&self, tokens: &mut Tokens) {
1621 tokens.append_all(self.attrs.outer());
1622 self.mac.path.to_tokens(tokens);
1623 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001624 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001625 match self.mac.delimiter {
1626 MacroDelimiter::Paren(ref paren) => {
1627 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1628 }
1629 MacroDelimiter::Brace(ref brace) => {
1630 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1631 }
1632 MacroDelimiter::Bracket(ref bracket) => {
1633 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1634 }
1635 }
David Tolnay57292da2017-12-27 21:03:33 -05001636 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001637 }
1638 }
David Tolnay42602292016-10-01 22:25:45 -07001639
David Tolnay500d8322017-12-18 00:32:51 -08001640 impl ToTokens for ItemMacro2 {
1641 fn to_tokens(&self, tokens: &mut Tokens) {
1642 tokens.append_all(self.attrs.outer());
1643 self.vis.to_tokens(tokens);
1644 self.macro_token.to_tokens(tokens);
1645 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001646 self.paren_token.surround(tokens, |tokens| {
1647 self.args.to_tokens(tokens);
1648 });
1649 self.brace_token.surround(tokens, |tokens| {
1650 self.body.to_tokens(tokens);
1651 });
David Tolnay500d8322017-12-18 00:32:51 -08001652 }
1653 }
1654
David Tolnay2ae520a2017-12-29 11:19:50 -05001655 impl ToTokens for ItemVerbatim {
1656 fn to_tokens(&self, tokens: &mut Tokens) {
1657 self.tts.to_tokens(tokens);
1658 }
1659 }
1660
David Tolnay5f332a92017-12-26 00:42:45 -05001661 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001662 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001663 self.ident.to_tokens(tokens);
1664 if let Some((ref as_token, ref rename)) = self.rename {
1665 as_token.to_tokens(tokens);
1666 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001667 }
David Tolnay4a057422016-10-08 00:02:31 -07001668 }
1669 }
1670
David Tolnay5f332a92017-12-26 00:42:45 -05001671 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001672 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001673 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001674 }
1675 }
1676
David Tolnay5f332a92017-12-26 00:42:45 -05001677 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001678 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001679 self.brace_token.surround(tokens, |tokens| {
1680 self.items.to_tokens(tokens);
1681 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001682 }
1683 }
1684
David Tolnay1bfa7332017-11-11 12:41:20 -08001685 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001686 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001687 tokens.append_all(self.attrs.outer());
1688 self.const_token.to_tokens(tokens);
1689 self.ident.to_tokens(tokens);
1690 self.colon_token.to_tokens(tokens);
1691 self.ty.to_tokens(tokens);
1692 if let Some((ref eq_token, ref default)) = self.default {
1693 eq_token.to_tokens(tokens);
1694 default.to_tokens(tokens);
1695 }
1696 self.semi_token.to_tokens(tokens);
1697 }
1698 }
1699
1700 impl ToTokens for TraitItemMethod {
1701 fn to_tokens(&self, tokens: &mut Tokens) {
1702 tokens.append_all(self.attrs.outer());
1703 self.sig.to_tokens(tokens);
1704 match self.default {
1705 Some(ref block) => {
1706 block.brace_token.surround(tokens, |tokens| {
1707 tokens.append_all(self.attrs.inner());
1708 tokens.append_all(&block.stmts);
1709 });
David Tolnayca085422016-10-04 00:12:38 -07001710 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001711 None => {
1712 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001713 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001714 }
1715 }
1716 }
1717
1718 impl ToTokens for TraitItemType {
1719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 tokens.append_all(self.attrs.outer());
1721 self.type_token.to_tokens(tokens);
1722 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001723 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001724 if !self.bounds.is_empty() {
1725 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1726 self.bounds.to_tokens(tokens);
1727 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001728 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001729 if let Some((ref eq_token, ref default)) = self.default {
1730 eq_token.to_tokens(tokens);
1731 default.to_tokens(tokens);
1732 }
1733 self.semi_token.to_tokens(tokens);
1734 }
1735 }
1736
1737 impl ToTokens for TraitItemMacro {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
1739 tokens.append_all(self.attrs.outer());
1740 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001741 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001742 }
1743 }
1744
David Tolnay2ae520a2017-12-29 11:19:50 -05001745 impl ToTokens for TraitItemVerbatim {
1746 fn to_tokens(&self, tokens: &mut Tokens) {
1747 self.tts.to_tokens(tokens);
1748 }
1749 }
1750
David Tolnay857628c2017-11-11 12:25:31 -08001751 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001752 fn to_tokens(&self, tokens: &mut Tokens) {
1753 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001754 self.vis.to_tokens(tokens);
1755 self.defaultness.to_tokens(tokens);
1756 self.const_token.to_tokens(tokens);
1757 self.ident.to_tokens(tokens);
1758 self.colon_token.to_tokens(tokens);
1759 self.ty.to_tokens(tokens);
1760 self.eq_token.to_tokens(tokens);
1761 self.expr.to_tokens(tokens);
1762 self.semi_token.to_tokens(tokens);
1763 }
1764 }
1765
1766 impl ToTokens for ImplItemMethod {
1767 fn to_tokens(&self, tokens: &mut Tokens) {
1768 tokens.append_all(self.attrs.outer());
1769 self.vis.to_tokens(tokens);
1770 self.defaultness.to_tokens(tokens);
1771 self.sig.to_tokens(tokens);
1772 self.block.brace_token.surround(tokens, |tokens| {
1773 tokens.append_all(self.attrs.inner());
1774 tokens.append_all(&self.block.stmts);
1775 });
1776 }
1777 }
1778
1779 impl ToTokens for ImplItemType {
1780 fn to_tokens(&self, tokens: &mut Tokens) {
1781 tokens.append_all(self.attrs.outer());
1782 self.vis.to_tokens(tokens);
1783 self.defaultness.to_tokens(tokens);
1784 self.type_token.to_tokens(tokens);
1785 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001786 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001787 self.eq_token.to_tokens(tokens);
1788 self.ty.to_tokens(tokens);
1789 self.semi_token.to_tokens(tokens);
1790 }
1791 }
1792
1793 impl ToTokens for ImplItemMacro {
1794 fn to_tokens(&self, tokens: &mut Tokens) {
1795 tokens.append_all(self.attrs.outer());
1796 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001797 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001798 }
1799 }
1800
David Tolnay2ae520a2017-12-29 11:19:50 -05001801 impl ToTokens for ImplItemVerbatim {
1802 fn to_tokens(&self, tokens: &mut Tokens) {
1803 self.tts.to_tokens(tokens);
1804 }
1805 }
1806
David Tolnay8894f602017-11-11 12:11:04 -08001807 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001808 fn to_tokens(&self, tokens: &mut Tokens) {
1809 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001811 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1812 self.semi_token.to_tokens(tokens);
1813 }
1814 }
1815
1816 impl ToTokens for ForeignItemStatic {
1817 fn to_tokens(&self, tokens: &mut Tokens) {
1818 tokens.append_all(self.attrs.outer());
1819 self.vis.to_tokens(tokens);
1820 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001821 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001822 self.ident.to_tokens(tokens);
1823 self.colon_token.to_tokens(tokens);
1824 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001825 self.semi_token.to_tokens(tokens);
1826 }
1827 }
1828
David Tolnay199bcbb2017-11-12 10:33:52 -08001829 impl ToTokens for ForeignItemType {
1830 fn to_tokens(&self, tokens: &mut Tokens) {
1831 tokens.append_all(self.attrs.outer());
1832 self.vis.to_tokens(tokens);
1833 self.type_token.to_tokens(tokens);
1834 self.ident.to_tokens(tokens);
1835 self.semi_token.to_tokens(tokens);
1836 }
1837 }
1838
David Tolnay2ae520a2017-12-29 11:19:50 -05001839 impl ToTokens for ForeignItemVerbatim {
1840 fn to_tokens(&self, tokens: &mut Tokens) {
1841 self.tts.to_tokens(tokens);
1842 }
1843 }
1844
David Tolnay570695e2017-06-03 16:15:13 -07001845 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001846 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001847 self.constness.to_tokens(tokens);
1848 self.unsafety.to_tokens(tokens);
1849 self.abi.to_tokens(tokens);
1850 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001851 }
1852 }
1853
David Tolnay570695e2017-06-03 16:15:13 -07001854 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001855
1856 impl<'a> ToTokens for NamedDecl<'a> {
1857 fn to_tokens(&self, tokens: &mut Tokens) {
1858 self.0.fn_token.to_tokens(tokens);
1859 self.1.to_tokens(tokens);
1860 self.0.generics.to_tokens(tokens);
1861 self.0.paren_token.surround(tokens, |tokens| {
1862 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001863 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1864 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001865 }
David Tolnayd2836e22017-12-27 23:13:00 -05001866 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001867 });
1868 self.0.output.to_tokens(tokens);
1869 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001870 }
1871 }
1872
Alex Crichton62a0a592017-05-22 13:58:53 -07001873 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001874 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001875 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001876 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001877 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001878 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001879 }
1880 }
1881
1882 impl ToTokens for ArgSelf {
1883 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001884 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001885 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001886 }
1887 }
1888
1889 impl ToTokens for ArgCaptured {
1890 fn to_tokens(&self, tokens: &mut Tokens) {
1891 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001892 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001893 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001894 }
1895 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001896}