blob: 4fef2b44083519c4fed7f6e17ccd036af158c533 [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 Tolnay614a0142018-01-07 10:25:43 -080022 ///
David Tolnay461d98e2018-01-07 11:07:19 -080023 /// *This type is available if Syn is built with the `"full"` feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enum
26 ///
27 /// This type is a [syntax tree enum].
28 ///
29 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc6b55bc2017-11-09 22:48:38 -080030 pub enum Item {
David Tolnay2b214082018-01-07 01:30:18 -080031 /// An `extern crate` item: `extern crate serde`.
David Tolnay461d98e2018-01-07 11:07:19 -080032 ///
33 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080035 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070036 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 pub extern_token: Token![extern],
38 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070039 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub rename: Option<(Token![as], Ident)>,
41 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
David Tolnay2b214082018-01-07 01:30:18 -080043
44 /// A use declaration: `use std::collections::HashMap`.
David Tolnay461d98e2018-01-07 11:07:19 -080045 ///
46 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080048 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070049 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050051 pub leading_colon: Option<Token![::]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050052 pub prefix: Punctuated<Ident, Token![::]>,
David Tolnay5f332a92017-12-26 00:42:45 -050053 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070055 }),
David Tolnay2b214082018-01-07 01:30:18 -080056
57 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080058 ///
59 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070060 pub Static(ItemStatic {
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 static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050064 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070065 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080066 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080067 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080068 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080070 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070071 }),
David Tolnay2b214082018-01-07 01:30:18 -080072
73 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080074 ///
75 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070076 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080077 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070078 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080079 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070080 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080081 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080082 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080083 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070084 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080085 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070086 }),
David Tolnay2b214082018-01-07 01:30:18 -080087
David Tolnay461d98e2018-01-07 11:07:19 -080088 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
89 /// }`.
90 ///
91 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070092 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080093 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070094 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050095 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050096 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050099 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 pub block: Box<Block>,
101 }),
David Tolnay2b214082018-01-07 01:30:18 -0800102
103 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800104 ///
105 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800107 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700108 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800109 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700110 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500111 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
David Tolnay2b214082018-01-07 01:30:18 -0800114
115 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800116 ///
117 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800119 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700120 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 pub items: Vec<ForeignItem>,
123 }),
David Tolnay2b214082018-01-07 01:30:18 -0800124
125 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800126 ///
127 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800128 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800129 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700130 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700132 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800135 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
David Tolnay2b214082018-01-07 01:30:18 -0800138
139 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800140 ///
141 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500142 pub Struct(ItemStruct {
143 pub attrs: Vec<Attribute>,
144 pub vis: Visibility,
145 pub struct_token: Token![struct],
146 pub ident: Ident,
147 pub generics: Generics,
148 pub fields: Fields,
149 pub semi_token: Option<Token![;]>,
150 }),
David Tolnay2b214082018-01-07 01:30:18 -0800151
152 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800153 ///
154 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800156 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700157 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700159 pub ident: Ident,
160 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500161 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500162 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 }),
David Tolnay2b214082018-01-07 01:30:18 -0800164
165 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800166 ///
167 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800169 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700170 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700172 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500174 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
David Tolnay2b214082018-01-07 01:30:18 -0800176
177 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800178 ///
179 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800181 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700182 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500183 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500184 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700186 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800188 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500189 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500190 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 pub items: Vec<TraitItem>,
192 }),
David Tolnay2b214082018-01-07 01:30:18 -0800193
194 /// An impl block providing trait or associated items: `impl<A> Trait
195 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800196 ///
197 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800199 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500200 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500201 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800202 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700204 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500205 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700206 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800207 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500208 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700209 pub items: Vec<ImplItem>,
210 }),
David Tolnay2b214082018-01-07 01:30:18 -0800211
212 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800213 ///
214 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800215 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800216 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800217 /// The `example` in `macro_rules! example { ... }`.
218 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800219 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500220 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800221 }),
David Tolnay2b214082018-01-07 01:30:18 -0800222
223 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500226 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800227 pub attrs: Vec<Attribute>,
228 pub vis: Visibility,
229 pub macro_token: Token![macro],
230 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500231 pub paren_token: Paren,
232 pub args: TokenStream,
233 pub brace_token: Brace,
234 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800235 }),
David Tolnay2b214082018-01-07 01:30:18 -0800236
237 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800238 ///
239 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500240 pub Verbatim(ItemVerbatim #manual_extra_traits {
241 pub tts: TokenStream,
242 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 }
David Tolnayb79ee962016-09-04 09:39:20 -0700244}
245
David Tolnay9c76bcb2017-12-26 23:14:59 -0500246#[cfg(feature = "extra-traits")]
247impl Eq for ItemMacro2 {}
248
249#[cfg(feature = "extra-traits")]
250impl PartialEq for ItemMacro2 {
251 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500252 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnayab919512017-12-30 23:31:51 -0500253 && self.ident == other.ident && self.paren_token == other.paren_token
254 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
255 && self.brace_token == other.brace_token
256 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500257 }
258}
259
260#[cfg(feature = "extra-traits")]
261impl Hash for ItemMacro2 {
262 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500263 where
264 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500265 {
266 self.attrs.hash(state);
267 self.vis.hash(state);
268 self.macro_token.hash(state);
269 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500270 self.paren_token.hash(state);
271 TokenStreamHelper(&self.args).hash(state);
272 self.brace_token.hash(state);
273 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500274 }
275}
276
David Tolnay2ae520a2017-12-29 11:19:50 -0500277#[cfg(feature = "extra-traits")]
278impl Eq for ItemVerbatim {}
279
280#[cfg(feature = "extra-traits")]
281impl PartialEq for ItemVerbatim {
282 fn eq(&self, other: &Self) -> bool {
283 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
284 }
285}
286
287#[cfg(feature = "extra-traits")]
288impl Hash for ItemVerbatim {
289 fn hash<H>(&self, state: &mut H)
290 where
291 H: Hasher,
292 {
293 TokenStreamHelper(&self.tts).hash(state);
294 }
295}
296
David Tolnay0e837402016-12-22 17:25:55 -0500297impl From<DeriveInput> for Item {
298 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500299 match input.data {
300 Data::Struct(data) => Item::Struct(ItemStruct {
301 attrs: input.attrs,
302 vis: input.vis,
303 struct_token: data.struct_token,
304 ident: input.ident,
305 generics: input.generics,
306 fields: data.fields,
307 semi_token: data.semi_token,
308 }),
309 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500310 attrs: input.attrs,
311 vis: input.vis,
312 enum_token: data.enum_token,
313 ident: input.ident,
314 generics: input.generics,
315 brace_token: data.brace_token,
316 variants: data.variants,
317 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500318 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500319 attrs: input.attrs,
320 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500321 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500322 ident: input.ident,
323 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500324 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500325 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700326 }
327 }
328}
329
Alex Crichton62a0a592017-05-22 13:58:53 -0700330ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800331 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800332 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800333 /// *This type is available if Syn is built with the `"full"` feature.*
334 ///
David Tolnay614a0142018-01-07 10:25:43 -0800335 /// # Syntax tree enum
336 ///
337 /// This type is a [syntax tree enum].
338 ///
339 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500340 pub enum UseTree {
David Tolnay05658502018-01-07 09:56:37 -0800341 /// An identifier imported by a `use` item: `Type` or `Type as Renamed`.
David Tolnay461d98e2018-01-07 11:07:19 -0800342 ///
343 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500344 pub Path(UsePath {
345 pub ident: Ident,
346 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800348
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800350 ///
351 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500352 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800353 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800355
David Tolnay05658502018-01-07 09:56:37 -0800356 /// A braced list of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800357 ///
358 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500359 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500360 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500361 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 }),
363 }
364}
365
Alex Crichton62a0a592017-05-22 13:58:53 -0700366ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800367 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800368 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800369 /// *This type is available if Syn is built with the `"full"` feature.*
370 ///
David Tolnay614a0142018-01-07 10:25:43 -0800371 /// # Syntax tree enum
372 ///
373 /// This type is a [syntax tree enum].
374 ///
375 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800376 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800377 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800378 ///
379 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800381 pub attrs: Vec<Attribute>,
382 pub vis: Visibility,
383 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 }),
David Tolnayebb72722018-01-07 01:14:13 -0800387
388 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800389 ///
390 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800392 pub attrs: Vec<Attribute>,
393 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500395 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800396 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800397 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800398 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800399 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 }),
David Tolnayebb72722018-01-07 01:14:13 -0800401
402 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800403 ///
404 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800405 pub Type(ForeignItemType {
406 pub attrs: Vec<Attribute>,
407 pub vis: Visibility,
408 pub type_token: Token![type],
409 pub ident: Ident,
410 pub semi_token: Token![;],
411 }),
David Tolnayebb72722018-01-07 01:14:13 -0800412
413 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800414 ///
415 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500416 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
417 pub tts: TokenStream,
418 }),
419 }
420}
421
422#[cfg(feature = "extra-traits")]
423impl Eq for ForeignItemVerbatim {}
424
425#[cfg(feature = "extra-traits")]
426impl PartialEq for ForeignItemVerbatim {
427 fn eq(&self, other: &Self) -> bool {
428 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
429 }
430}
431
432#[cfg(feature = "extra-traits")]
433impl Hash for ForeignItemVerbatim {
434 fn hash<H>(&self, state: &mut H)
435 where
436 H: Hasher,
437 {
438 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700440}
441
David Tolnayda705bd2017-11-10 21:58:05 -0800442ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800443 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800444 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800445 /// *This type is available if Syn is built with the `"full"` feature.*
446 ///
David Tolnay614a0142018-01-07 10:25:43 -0800447 /// # Syntax tree enum
448 ///
449 /// This type is a [syntax tree enum].
450 ///
451 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800452 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800453 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800454 ///
455 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700456 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800457 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800458 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700459 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800460 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800461 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800462 pub default: Option<(Token![=], Expr)>,
463 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 }),
David Tolnayebb72722018-01-07 01:14:13 -0800465
466 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800467 ///
468 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800470 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700471 pub sig: MethodSig,
472 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800473 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700474 }),
David Tolnayebb72722018-01-07 01:14:13 -0800475
476 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800477 ///
478 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700479 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800480 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800481 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700482 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500483 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800484 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500485 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800486 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800487 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700488 }),
David Tolnayebb72722018-01-07 01:14:13 -0800489
490 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800491 ///
492 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800493 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800494 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800495 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500496 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800497 }),
David Tolnayebb72722018-01-07 01:14:13 -0800498
499 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800500 ///
501 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500502 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
503 pub tts: TokenStream,
504 }),
505 }
506}
507
508#[cfg(feature = "extra-traits")]
509impl Eq for TraitItemVerbatim {}
510
511#[cfg(feature = "extra-traits")]
512impl PartialEq for TraitItemVerbatim {
513 fn eq(&self, other: &Self) -> bool {
514 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
515 }
516}
517
518#[cfg(feature = "extra-traits")]
519impl Hash for TraitItemVerbatim {
520 fn hash<H>(&self, state: &mut H)
521 where
522 H: Hasher,
523 {
524 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700526}
527
Alex Crichton62a0a592017-05-22 13:58:53 -0700528ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800529 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800530 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800531 /// *This type is available if Syn is built with the `"full"` feature.*
532 ///
David Tolnay614a0142018-01-07 10:25:43 -0800533 /// # Syntax tree enum
534 ///
535 /// This type is a [syntax tree enum].
536 ///
537 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800538 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800539 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800540 ///
541 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700542 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800543 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700544 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500545 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800546 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700547 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800548 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800549 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800550 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700551 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800552 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700553 }),
David Tolnayebb72722018-01-07 01:14:13 -0800554
555 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800556 ///
557 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700558 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800559 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700560 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500561 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700562 pub sig: MethodSig,
563 pub block: Block,
564 }),
David Tolnayebb72722018-01-07 01:14:13 -0800565
566 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800567 ///
568 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800570 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700571 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500572 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800573 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700574 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500575 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800577 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800578 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700579 }),
David Tolnayebb72722018-01-07 01:14:13 -0800580
581 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800582 ///
583 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800584 pub Macro(ImplItemMacro {
585 pub attrs: Vec<Attribute>,
586 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500587 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800588 }),
David Tolnayebb72722018-01-07 01:14:13 -0800589
590 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800591 ///
592 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500593 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
594 pub tts: TokenStream,
595 }),
596 }
597}
598
599#[cfg(feature = "extra-traits")]
600impl Eq for ImplItemVerbatim {}
601
602#[cfg(feature = "extra-traits")]
603impl PartialEq for ImplItemVerbatim {
604 fn eq(&self, other: &Self) -> bool {
605 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
606 }
607}
608
609#[cfg(feature = "extra-traits")]
610impl Hash for ImplItemVerbatim {
611 fn hash<H>(&self, state: &mut H)
612 where
613 H: Hasher,
614 {
615 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700616 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700617}
618
619ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800620 /// A method's signature in a trait or implementation: `unsafe fn
621 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800622 ///
623 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500625 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500626 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700627 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700628 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700629 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700630 }
631}
632
633ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800634 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800635 ///
636 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700637 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800638 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500639 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500640 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500641 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500642 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500643 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700645}
646
Alex Crichton62a0a592017-05-22 13:58:53 -0700647ast_enum_of_structs! {
David Tolnay3f559052018-01-06 23:59:48 -0800648 /// An argument in a function signature.
Alex Crichton62a0a592017-05-22 13:58:53 -0700649 ///
650 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
David Tolnay614a0142018-01-07 10:25:43 -0800651 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800652 /// *This type is available if Syn is built with the `"full"` feature.*
653 ///
David Tolnay614a0142018-01-07 10:25:43 -0800654 /// # Syntax tree enum
655 ///
656 /// This type is a [syntax tree enum].
657 ///
658 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700659 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800660 /// Self captured by reference in a function signature: `&self` or `&mut
661 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800662 ///
663 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700664 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700666 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500667 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500668 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700669 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800670
David Tolnay3f559052018-01-06 23:59:48 -0800671 /// Self captured by value in a function signature: `self` or `mut
672 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800673 ///
674 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700675 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500676 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800677 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700678 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800679
David Tolnay3f559052018-01-06 23:59:48 -0800680 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800681 ///
682 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700683 pub Captured(ArgCaptured {
684 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800685 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800686 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800688
David Tolnay3f559052018-01-06 23:59:48 -0800689 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500690 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800691 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800692 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700693 }
David Tolnay62f374c2016-10-02 13:37:00 -0700694}
695
David Tolnayedf2b992016-09-23 20:43:45 -0700696#[cfg(feature = "parsing")]
697pub mod parsing {
698 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700699
David Tolnaydfc886b2018-01-06 08:03:09 -0800700 use buffer::Cursor;
701 use synom::{PResult, Synom};
David Tolnay84aa0752016-10-02 23:01:13 -0700702
David Tolnay4c614be2017-11-10 00:02:38 -0800703 impl_synom!(Item "item" alt!(
704 syn!(ItemExternCrate) => { Item::ExternCrate }
705 |
706 syn!(ItemUse) => { Item::Use }
707 |
708 syn!(ItemStatic) => { Item::Static }
709 |
710 syn!(ItemConst) => { Item::Const }
711 |
712 syn!(ItemFn) => { Item::Fn }
713 |
714 syn!(ItemMod) => { Item::Mod }
715 |
716 syn!(ItemForeignMod) => { Item::ForeignMod }
717 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800718 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800719 |
720 syn!(ItemStruct) => { Item::Struct }
721 |
722 syn!(ItemEnum) => { Item::Enum }
723 |
724 syn!(ItemUnion) => { Item::Union }
725 |
726 syn!(ItemTrait) => { Item::Trait }
727 |
David Tolnay03342952017-12-29 11:52:00 -0500728 call!(deprecated_default_impl) => { Item::Verbatim }
David Tolnay4c614be2017-11-10 00:02:38 -0800729 |
730 syn!(ItemImpl) => { Item::Impl }
731 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800732 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800733 |
734 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800735 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700736
David Tolnaydecf28d2017-11-11 11:56:45 -0800737 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500738 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700739 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700741 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500742 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500743 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800744 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700745 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800746 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800747 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500748 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700749 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500750 delimiter: body.0,
751 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800752 },
David Tolnay57292da2017-12-27 21:03:33 -0500753 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800754 })
David Tolnayedf2b992016-09-23 20:43:45 -0700755 ));
756
David Tolnay500d8322017-12-18 00:32:51 -0800757 // TODO: figure out the actual grammar; is body required to be braced?
758 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500759 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800760 vis: syn!(Visibility) >>
761 macro_: keyword!(macro) >>
762 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500763 args: call!(tt::parenthesized) >>
764 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800765 (ItemMacro2 {
766 attrs: attrs,
767 vis: vis,
768 macro_token: macro_,
769 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500770 paren_token: args.0,
771 args: args.1,
772 brace_token: body.0,
773 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800774 })
775 ));
776
David Tolnay4c614be2017-11-10 00:02:38 -0800777 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500778 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700779 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800780 extern_: keyword!(extern) >>
781 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700782 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800783 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
784 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800785 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700786 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800787 vis: vis,
788 extern_token: extern_,
789 crate_token: crate_,
790 ident: ident,
791 rename: rename,
792 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800793 })
David Tolnayedf2b992016-09-23 20:43:45 -0700794 ));
795
David Tolnay4c614be2017-11-10 00:02:38 -0800796 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500797 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700798 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800799 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500800 leading_colon: option!(punct!(::)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500801 mut prefix: call!(Punctuated::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500802 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500803 true => syn!(UseTree)
804 |
David Tolnay8edcef12017-12-28 12:06:52 -0500805 false => alt!(
806 tuple!(keyword!(as), syn!(Ident)) => {
807 |rename| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800808 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500809 rename: Some(rename),
810 })
811 }
David Tolnay5f332a92017-12-26 00:42:45 -0500812 |
David Tolnay8edcef12017-12-28 12:06:52 -0500813 epsilon!() => {
814 |_| UseTree::Path(UsePath {
David Tolnay56080682018-01-06 14:01:52 -0800815 ident: prefix.pop().unwrap().into_value(),
David Tolnay8edcef12017-12-28 12:06:52 -0500816 rename: None,
817 })
818 }
David Tolnay5f332a92017-12-26 00:42:45 -0500819 )
820 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800821 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800822 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700823 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800824 vis: vis,
825 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500826 leading_colon: leading_colon,
827 prefix: prefix,
828 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800829 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800830 })
David Tolnay4a057422016-10-08 00:02:31 -0700831 ));
832
David Tolnay5f332a92017-12-26 00:42:45 -0500833 named!(use_prefix -> Ident, alt!(
834 syn!(Ident)
835 |
836 keyword!(self) => { Into::into }
837 |
838 keyword!(super) => { Into::into }
839 |
840 keyword!(crate) => { Into::into }
841 ));
842
843 impl_synom!(UseTree "use tree" alt!(
844 syn!(UsePath) => { UseTree::Path }
845 |
846 syn!(UseGlob) => { UseTree::Glob }
847 |
848 syn!(UseList) => { UseTree::List }
849 ));
850
851 impl_synom!(UsePath "use path" do_parse!(
852 ident: alt!(
853 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400854 |
David Tolnay5f332a92017-12-26 00:42:45 -0500855 keyword!(self) => { Into::into }
856 ) >>
857 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
858 (UsePath {
859 ident: ident,
860 rename: rename,
861 })
862 ));
David Tolnay4a057422016-10-08 00:02:31 -0700863
David Tolnay5f332a92017-12-26 00:42:45 -0500864 impl_synom!(UseGlob "use glob" do_parse!(
865 star: punct!(*) >>
866 (UseGlob {
867 star_token: star,
868 })
869 ));
David Tolnay4a057422016-10-08 00:02:31 -0700870
David Tolnay5f332a92017-12-26 00:42:45 -0500871 impl_synom!(UseList "use list" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500872 list: braces!(Punctuated::parse_terminated) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500873 (UseList {
David Tolnay8875fca2017-12-31 13:52:37 -0500874 brace_token: list.0,
875 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500876 })
877 ));
David Tolnay4a057422016-10-08 00:02:31 -0700878
David Tolnay4c614be2017-11-10 00:02:38 -0800879 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500880 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700881 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800882 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500883 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700884 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800885 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800886 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800887 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700888 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800889 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800890 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700891 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800892 vis: vis,
893 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500894 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800895 ident: ident,
896 colon_token: colon,
897 ty: Box::new(ty),
898 eq_token: eq,
899 expr: Box::new(value),
900 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800901 })
David Tolnay47a877c2016-10-01 16:50:55 -0700902 ));
903
David Tolnay4c614be2017-11-10 00:02:38 -0800904 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500905 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700906 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800907 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700908 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800909 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800910 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800911 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700912 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800913 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800914 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700915 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800916 vis: vis,
917 const_token: const_,
918 ident: ident,
919 colon_token: colon,
920 ty: Box::new(ty),
921 eq_token: eq,
922 expr: Box::new(value),
923 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800924 })
David Tolnay47a877c2016-10-01 16:50:55 -0700925 ));
926
David Tolnay4c614be2017-11-10 00:02:38 -0800927 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500928 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700929 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500930 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500931 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700932 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800933 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700934 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700935 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500936 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800937 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500938 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700939 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500940 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700941 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400942 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800943 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700944 attrs: {
945 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500946 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700947 attrs
948 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800949 vis: vis,
950 constness: constness,
951 unsafety: unsafety,
952 abi: abi,
953 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800954 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500955 paren_token: inputs.0,
956 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800957 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500958 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800959 generics: Generics {
960 where_clause: where_clause,
961 .. generics
962 },
963 }),
964 ident: ident,
965 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500966 brace_token: inner_attrs_stmts.0,
967 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800968 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800969 })
David Tolnay42602292016-10-01 22:25:45 -0700970 ));
971
Alex Crichton954046c2017-05-30 21:49:42 -0700972 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400973 named!(parse -> Self, alt!(
974 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400976 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500977 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800978 self_: keyword!(self) >>
979 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400980 (ArgSelfRef {
981 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500982 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400983 and_token: and,
984 self_token: self_,
985 }.into())
986 )
987 |
988 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500989 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 self_: keyword!(self) >>
991 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400992 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500993 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400994 self_token: self_,
995 }.into())
996 )
997 |
998 do_parse!(
999 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001000 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001001 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001002 (ArgCaptured {
1003 pat: pat,
1004 ty: ty,
1005 colon_token: colon,
1006 }.into())
1007 )
1008 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001009 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -04001010 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001011
1012 fn description() -> Option<&'static str> {
1013 Some("function argument")
1014 }
Alex Crichton954046c2017-05-30 21:49:42 -07001015 }
David Tolnay62f374c2016-10-02 13:37:00 -07001016
David Tolnay4c614be2017-11-10 00:02:38 -08001017 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001018 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001019 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -07001021 ident: syn!(Ident) >>
1022 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -07001024 Vec::new(),
1025 None,
1026 Some(semi),
1027 )}
David Tolnay37d10332016-10-13 20:51:04 -07001028 |
Alex Crichton954046c2017-05-30 21:49:42 -07001029 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -07001030 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001031 many0!(Attribute::parse_inner),
1032 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -04001033 )
David Tolnay8875fca2017-12-31 13:52:37 -05001034 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -07001035 inner_attrs,
1036 Some((brace, items)),
1037 None,
1038 )}
David Tolnay37d10332016-10-13 20:51:04 -07001039 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001040 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -07001041 attrs: {
1042 let mut attrs = outer_attrs;
1043 attrs.extend(content_semi.0);
1044 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -07001045 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001046 vis: vis,
1047 mod_token: mod_,
1048 ident: ident,
1049 content: content_semi.1,
1050 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -08001051 })
David Tolnay35902302016-10-06 01:11:08 -07001052 ));
1053
David Tolnay4c614be2017-11-10 00:02:38 -08001054 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001055 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001056 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -05001057 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001058 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -07001059 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001060 abi: abi,
David Tolnay8875fca2017-12-31 13:52:37 -05001061 brace_token: items.0,
1062 items: items.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001063 })
David Tolnay35902302016-10-06 01:11:08 -07001064 ));
1065
David Tolnay8894f602017-11-11 12:11:04 -08001066 impl_synom!(ForeignItem "foreign item" alt!(
1067 syn!(ForeignItemFn) => { ForeignItem::Fn }
1068 |
1069 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001070 |
1071 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -08001072 ));
David Tolnay35902302016-10-06 01:11:08 -07001073
David Tolnay8894f602017-11-11 12:11:04 -08001074 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001075 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001076 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001077 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001078 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001079 generics: syn!(Generics) >>
1080 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001081 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001082 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001083 (args, variadic)
1084 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001085 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001086 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001087 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001088 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001089 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -08001090 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -07001091 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001092 attrs: attrs,
1093 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001094 decl: Box::new(FnDecl {
1095 fn_token: fn_,
1096 paren_token: parens,
1097 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -05001098 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001099 output: ret,
1100 generics: Generics {
1101 where_clause: where_clause,
1102 .. generics
1103 },
1104 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001105 vis: vis,
1106 }
David Tolnay35902302016-10-06 01:11:08 -07001107 })
1108 ));
1109
David Tolnay8894f602017-11-11 12:11:04 -08001110 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001111 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001112 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001113 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001114 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -07001115 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001116 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001117 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001118 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -08001119 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001120 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001121 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001122 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001123 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001124 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001125 static_token: static_,
1126 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001127 vis: vis,
1128 })
1129 ));
1130
David Tolnay199bcbb2017-11-12 10:33:52 -08001131 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001132 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001133 vis: syn!(Visibility) >>
1134 type_: keyword!(type) >>
1135 ident: syn!(Ident) >>
1136 semi: punct!(;) >>
1137 (ForeignItemType {
1138 attrs: attrs,
1139 vis: vis,
1140 type_token: type_,
1141 ident: ident,
1142 semi_token: semi,
1143 })
1144 ));
1145
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001146 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001147 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001148 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001149 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001150 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001151 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001152 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001153 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001154 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001155 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001156 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001157 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001158 vis: vis,
1159 type_token: type_,
1160 ident: ident,
1161 generics: Generics {
1162 where_clause: where_clause,
1163 ..generics
1164 },
1165 eq_token: eq,
1166 ty: Box::new(ty),
1167 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001168 })
David Tolnay3cf52982016-10-01 17:11:37 -07001169 ));
1170
David Tolnay4c614be2017-11-10 00:02:38 -08001171 impl_synom!(ItemStruct "struct item" switch!(
1172 map!(syn!(DeriveInput), Into::into),
1173 Item::Struct(item) => value!(item)
1174 |
1175 _ => reject!()
1176 ));
David Tolnay42602292016-10-01 22:25:45 -07001177
David Tolnay4c614be2017-11-10 00:02:38 -08001178 impl_synom!(ItemEnum "enum item" switch!(
1179 map!(syn!(DeriveInput), Into::into),
1180 Item::Enum(item) => value!(item)
1181 |
1182 _ => reject!()
1183 ));
1184
1185 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001186 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001187 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001188 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001189 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001190 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001191 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001192 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001193 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001194 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001195 vis: vis,
1196 union_token: union_,
1197 ident: ident,
1198 generics: Generics {
1199 where_clause: where_clause,
1200 .. generics
1201 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001202 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001203 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001204 ));
1205
David Tolnay4c614be2017-11-10 00:02:38 -08001206 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001207 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001208 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001209 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001210 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001211 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001212 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001213 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001214 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001215 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001216 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001217 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001218 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001219 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001220 vis: vis,
1221 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001222 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001223 trait_token: trait_,
1224 ident: ident,
1225 generics: Generics {
1226 where_clause: where_clause,
1227 .. generics
1228 },
1229 colon_token: colon,
1230 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001231 brace_token: body.0,
1232 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001233 })
David Tolnay0aecb732016-10-03 23:03:50 -07001234 ));
1235
David Tolnay03342952017-12-29 11:52:00 -05001236 fn grab_cursor(cursor: Cursor) -> PResult<Cursor> {
1237 Ok((cursor, cursor))
1238 }
1239
1240 named!(deprecated_default_impl -> ItemVerbatim, do_parse!(
1241 begin: call!(grab_cursor) >>
1242 many0!(Attribute::parse_outer) >>
1243 option!(keyword!(unsafe)) >>
1244 keyword!(impl) >>
1245 syn!(Path) >>
1246 keyword!(for) >>
1247 punct!(..) >>
1248 braces!(epsilon!()) >>
1249 end: call!(grab_cursor) >>
1250 ({
David Tolnay61037c62018-01-05 16:21:03 -08001251 let tts = begin.token_stream().into_iter().collect::<Vec<_>>();
David Tolnay03342952017-12-29 11:52:00 -05001252 let len = tts.len() - end.token_stream().into_iter().count();
1253 ItemVerbatim {
1254 tts: tts.into_iter().take(len).collect(),
1255 }
David Tolnay4c614be2017-11-10 00:02:38 -08001256 })
David Tolnayf94e2362016-10-04 00:29:51 -07001257 ));
1258
David Tolnayda705bd2017-11-10 21:58:05 -08001259 impl_synom!(TraitItem "trait item" alt!(
1260 syn!(TraitItemConst) => { TraitItem::Const }
1261 |
1262 syn!(TraitItemMethod) => { TraitItem::Method }
1263 |
1264 syn!(TraitItemType) => { TraitItem::Type }
1265 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001266 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001267 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001268
David Tolnayda705bd2017-11-10 21:58:05 -08001269 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001270 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001271 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001272 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001273 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001274 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001275 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1276 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001277 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001278 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001279 const_token: const_,
1280 ident: ident,
1281 colon_token: colon,
1282 ty: ty,
1283 default: default,
1284 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001285 })
1286 ));
1287
David Tolnayda705bd2017-11-10 21:58:05 -08001288 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001289 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001290 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001291 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001292 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001293 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001294 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001295 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001296 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001297 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001298 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001299 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001300 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001301 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001302 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001303 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001304 ({
1305 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001306 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001307 None => (Vec::new(), None),
1308 };
David Tolnayda705bd2017-11-10 21:58:05 -08001309 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001310 attrs: {
1311 let mut attrs = outer_attrs;
1312 attrs.extend(inner_attrs);
1313 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001314 },
David Tolnayda705bd2017-11-10 21:58:05 -08001315 sig: MethodSig {
1316 constness: constness,
1317 unsafety: unsafety,
1318 abi: abi,
1319 ident: ident,
1320 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001321 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001322 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001323 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001324 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001325 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001326 generics: Generics {
1327 where_clause: where_clause,
1328 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001329 },
1330 },
David Tolnayda705bd2017-11-10 21:58:05 -08001331 },
1332 default: stmts.map(|stmts| {
1333 Block {
1334 stmts: stmts.0,
1335 brace_token: stmts.1,
1336 }
1337 }),
1338 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001339 }
David Tolnay0aecb732016-10-03 23:03:50 -07001340 })
1341 ));
1342
David Tolnayda705bd2017-11-10 21:58:05 -08001343 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001344 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001345 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001346 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001347 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001348 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001349 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001350 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001351 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001352 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001353 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001354 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001355 type_token: type_,
1356 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001357 generics: Generics {
1358 where_clause: where_clause,
1359 .. generics
1360 },
David Tolnayda705bd2017-11-10 21:58:05 -08001361 colon_token: colon,
1362 bounds: bounds.unwrap_or_default(),
1363 default: default,
1364 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001365 })
1366 ));
1367
David Tolnaydecf28d2017-11-11 11:56:45 -08001368 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001369 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001370 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001371 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001372 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001373 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001374 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001375 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001376 })
1377 ));
1378
David Tolnay4c614be2017-11-10 00:02:38 -08001379 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001380 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001381 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001382 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001383 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001384 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001385 polarity_path: alt!(
1386 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001387 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001388 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001389 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001390 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001391 )
1392 |
David Tolnay570695e2017-06-03 16:15:13 -07001393 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001394 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001395 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001396 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001397 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001398 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001399 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001400 defaultness: defaultness,
1401 unsafety: unsafety,
1402 impl_token: impl_,
1403 generics: Generics {
1404 where_clause: where_clause,
1405 .. generics
1406 },
1407 trait_: polarity_path,
1408 self_ty: Box::new(self_ty),
David Tolnay8875fca2017-12-31 13:52:37 -05001409 brace_token: body.0,
1410 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001411 })
David Tolnay4c9be372016-10-06 00:47:37 -07001412 ));
1413
David Tolnay857628c2017-11-11 12:25:31 -08001414 impl_synom!(ImplItem "item in impl block" alt!(
1415 syn!(ImplItemConst) => { ImplItem::Const }
1416 |
1417 syn!(ImplItemMethod) => { ImplItem::Method }
1418 |
1419 syn!(ImplItemType) => { ImplItem::Type }
1420 |
1421 syn!(ImplItemMacro) => { ImplItem::Macro }
1422 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001423
David Tolnay857628c2017-11-11 12:25:31 -08001424 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001425 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001426 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001427 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001428 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001429 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001430 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001431 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001432 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001433 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001434 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001435 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001436 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001437 vis: vis,
1438 defaultness: defaultness,
1439 const_token: const_,
1440 ident: ident,
1441 colon_token: colon,
1442 ty: ty,
1443 eq_token: eq,
1444 expr: value,
1445 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001446 })
1447 ));
1448
David Tolnay857628c2017-11-11 12:25:31 -08001449 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001450 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001451 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001452 defaultness: option!(keyword!(default)) >>
1453 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001454 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001455 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001456 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001457 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001458 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001459 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001460 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001461 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001462 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001463 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001464 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001465 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001466 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001467 attrs: {
1468 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001469 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001470 attrs
1471 },
David Tolnay857628c2017-11-11 12:25:31 -08001472 vis: vis,
1473 defaultness: defaultness,
1474 sig: MethodSig {
1475 constness: constness,
1476 unsafety: unsafety,
1477 abi: abi,
1478 ident: ident,
1479 decl: FnDecl {
1480 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001481 paren_token: inputs.0,
1482 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001483 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001484 generics: Generics {
1485 where_clause: where_clause,
1486 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001487 },
David Tolnayd2836e22017-12-27 23:13:00 -05001488 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001489 },
David Tolnay857628c2017-11-11 12:25:31 -08001490 },
1491 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001492 brace_token: inner_attrs_stmts.0,
1493 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001494 },
David Tolnay4c9be372016-10-06 00:47:37 -07001495 })
1496 ));
1497
David Tolnay857628c2017-11-11 12:25:31 -08001498 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001499 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001500 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001501 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001502 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001503 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001504 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001505 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001506 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001507 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001508 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001509 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001510 vis: vis,
1511 defaultness: defaultness,
1512 type_token: type_,
1513 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001514 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001515 eq_token: eq,
1516 ty: ty,
1517 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001518 })
1519 ));
1520
David Tolnay857628c2017-11-11 12:25:31 -08001521 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001522 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001523 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001524 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001525 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001526 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001527 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001528 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001529 })
1530 ));
1531
David Tolnayab919512017-12-30 23:31:51 -05001532 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1533 match *delimiter {
1534 MacroDelimiter::Brace(_) => true,
1535 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001536 }
1537 }
David Tolnayedf2b992016-09-23 20:43:45 -07001538}
David Tolnay4a51dc72016-10-01 00:40:31 -07001539
1540#[cfg(feature = "printing")]
1541mod printing {
1542 use super::*;
1543 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05001544 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001545
David Tolnay1bfa7332017-11-11 12:41:20 -08001546 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001547 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001548 tokens.append_all(self.attrs.outer());
1549 self.vis.to_tokens(tokens);
1550 self.extern_token.to_tokens(tokens);
1551 self.crate_token.to_tokens(tokens);
1552 self.ident.to_tokens(tokens);
1553 if let Some((ref as_token, ref rename)) = self.rename {
1554 as_token.to_tokens(tokens);
1555 rename.to_tokens(tokens);
1556 }
1557 self.semi_token.to_tokens(tokens);
1558 }
1559 }
1560
1561 impl ToTokens for ItemUse {
1562 fn to_tokens(&self, tokens: &mut Tokens) {
1563 tokens.append_all(self.attrs.outer());
1564 self.vis.to_tokens(tokens);
1565 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001566 self.leading_colon.to_tokens(tokens);
1567 self.prefix.to_tokens(tokens);
1568 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001569 self.semi_token.to_tokens(tokens);
1570 }
1571 }
1572
1573 impl ToTokens for ItemStatic {
1574 fn to_tokens(&self, tokens: &mut Tokens) {
1575 tokens.append_all(self.attrs.outer());
1576 self.vis.to_tokens(tokens);
1577 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001578 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001579 self.ident.to_tokens(tokens);
1580 self.colon_token.to_tokens(tokens);
1581 self.ty.to_tokens(tokens);
1582 self.eq_token.to_tokens(tokens);
1583 self.expr.to_tokens(tokens);
1584 self.semi_token.to_tokens(tokens);
1585 }
1586 }
1587
1588 impl ToTokens for ItemConst {
1589 fn to_tokens(&self, tokens: &mut Tokens) {
1590 tokens.append_all(self.attrs.outer());
1591 self.vis.to_tokens(tokens);
1592 self.const_token.to_tokens(tokens);
1593 self.ident.to_tokens(tokens);
1594 self.colon_token.to_tokens(tokens);
1595 self.ty.to_tokens(tokens);
1596 self.eq_token.to_tokens(tokens);
1597 self.expr.to_tokens(tokens);
1598 self.semi_token.to_tokens(tokens);
1599 }
1600 }
1601
1602 impl ToTokens for ItemFn {
1603 fn to_tokens(&self, tokens: &mut Tokens) {
1604 tokens.append_all(self.attrs.outer());
1605 self.vis.to_tokens(tokens);
1606 self.constness.to_tokens(tokens);
1607 self.unsafety.to_tokens(tokens);
1608 self.abi.to_tokens(tokens);
1609 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1610 self.block.brace_token.surround(tokens, |tokens| {
1611 tokens.append_all(self.attrs.inner());
1612 tokens.append_all(&self.block.stmts);
1613 });
1614 }
1615 }
1616
1617 impl ToTokens for ItemMod {
1618 fn to_tokens(&self, tokens: &mut Tokens) {
1619 tokens.append_all(self.attrs.outer());
1620 self.vis.to_tokens(tokens);
1621 self.mod_token.to_tokens(tokens);
1622 self.ident.to_tokens(tokens);
1623 if let Some((ref brace, ref items)) = self.content {
1624 brace.surround(tokens, |tokens| {
1625 tokens.append_all(self.attrs.inner());
1626 tokens.append_all(items);
1627 });
1628 } else {
1629 TokensOrDefault(&self.semi).to_tokens(tokens);
1630 }
1631 }
1632 }
1633
1634 impl ToTokens for ItemForeignMod {
1635 fn to_tokens(&self, tokens: &mut Tokens) {
1636 tokens.append_all(self.attrs.outer());
1637 self.abi.to_tokens(tokens);
1638 self.brace_token.surround(tokens, |tokens| {
1639 tokens.append_all(&self.items);
1640 });
1641 }
1642 }
1643
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001644 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001645 fn to_tokens(&self, tokens: &mut Tokens) {
1646 tokens.append_all(self.attrs.outer());
1647 self.vis.to_tokens(tokens);
1648 self.type_token.to_tokens(tokens);
1649 self.ident.to_tokens(tokens);
1650 self.generics.to_tokens(tokens);
1651 self.generics.where_clause.to_tokens(tokens);
1652 self.eq_token.to_tokens(tokens);
1653 self.ty.to_tokens(tokens);
1654 self.semi_token.to_tokens(tokens);
1655 }
1656 }
1657
1658 impl ToTokens for ItemEnum {
1659 fn to_tokens(&self, tokens: &mut Tokens) {
1660 tokens.append_all(self.attrs.outer());
1661 self.vis.to_tokens(tokens);
1662 self.enum_token.to_tokens(tokens);
1663 self.ident.to_tokens(tokens);
1664 self.generics.to_tokens(tokens);
1665 self.generics.where_clause.to_tokens(tokens);
1666 self.brace_token.surround(tokens, |tokens| {
1667 self.variants.to_tokens(tokens);
1668 });
1669 }
1670 }
1671
1672 impl ToTokens for ItemStruct {
1673 fn to_tokens(&self, tokens: &mut Tokens) {
1674 tokens.append_all(self.attrs.outer());
1675 self.vis.to_tokens(tokens);
1676 self.struct_token.to_tokens(tokens);
1677 self.ident.to_tokens(tokens);
1678 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001679 match self.fields {
1680 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001681 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001682 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001683 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001684 Fields::Unnamed(ref fields) => {
1685 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001686 self.generics.where_clause.to_tokens(tokens);
1687 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001688 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001689 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001690 self.generics.where_clause.to_tokens(tokens);
1691 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001692 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001693 }
1694 }
1695 }
1696
1697 impl ToTokens for ItemUnion {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 tokens.append_all(self.attrs.outer());
1700 self.vis.to_tokens(tokens);
1701 self.union_token.to_tokens(tokens);
1702 self.ident.to_tokens(tokens);
1703 self.generics.to_tokens(tokens);
1704 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001705 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001706 }
1707 }
1708
1709 impl ToTokens for ItemTrait {
1710 fn to_tokens(&self, tokens: &mut Tokens) {
1711 tokens.append_all(self.attrs.outer());
1712 self.vis.to_tokens(tokens);
1713 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001714 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001715 self.trait_token.to_tokens(tokens);
1716 self.ident.to_tokens(tokens);
1717 self.generics.to_tokens(tokens);
1718 if !self.supertraits.is_empty() {
1719 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1720 self.supertraits.to_tokens(tokens);
1721 }
1722 self.generics.where_clause.to_tokens(tokens);
1723 self.brace_token.surround(tokens, |tokens| {
1724 tokens.append_all(&self.items);
1725 });
1726 }
1727 }
1728
David Tolnay1bfa7332017-11-11 12:41:20 -08001729 impl ToTokens for ItemImpl {
1730 fn to_tokens(&self, tokens: &mut Tokens) {
1731 tokens.append_all(self.attrs.outer());
1732 self.defaultness.to_tokens(tokens);
1733 self.unsafety.to_tokens(tokens);
1734 self.impl_token.to_tokens(tokens);
1735 self.generics.to_tokens(tokens);
1736 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1737 polarity.to_tokens(tokens);
1738 path.to_tokens(tokens);
1739 for_token.to_tokens(tokens);
1740 }
1741 self.self_ty.to_tokens(tokens);
1742 self.generics.where_clause.to_tokens(tokens);
1743 self.brace_token.surround(tokens, |tokens| {
1744 tokens.append_all(&self.items);
1745 });
1746 }
1747 }
1748
1749 impl ToTokens for ItemMacro {
1750 fn to_tokens(&self, tokens: &mut Tokens) {
1751 tokens.append_all(self.attrs.outer());
1752 self.mac.path.to_tokens(tokens);
1753 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001754 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001755 match self.mac.delimiter {
1756 MacroDelimiter::Paren(ref paren) => {
1757 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1758 }
1759 MacroDelimiter::Brace(ref brace) => {
1760 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1761 }
1762 MacroDelimiter::Bracket(ref bracket) => {
1763 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1764 }
1765 }
David Tolnay57292da2017-12-27 21:03:33 -05001766 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001767 }
1768 }
David Tolnay42602292016-10-01 22:25:45 -07001769
David Tolnay500d8322017-12-18 00:32:51 -08001770 impl ToTokens for ItemMacro2 {
1771 fn to_tokens(&self, tokens: &mut Tokens) {
1772 tokens.append_all(self.attrs.outer());
1773 self.vis.to_tokens(tokens);
1774 self.macro_token.to_tokens(tokens);
1775 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001776 self.paren_token.surround(tokens, |tokens| {
1777 self.args.to_tokens(tokens);
1778 });
1779 self.brace_token.surround(tokens, |tokens| {
1780 self.body.to_tokens(tokens);
1781 });
David Tolnay500d8322017-12-18 00:32:51 -08001782 }
1783 }
1784
David Tolnay2ae520a2017-12-29 11:19:50 -05001785 impl ToTokens for ItemVerbatim {
1786 fn to_tokens(&self, tokens: &mut Tokens) {
1787 self.tts.to_tokens(tokens);
1788 }
1789 }
1790
David Tolnay5f332a92017-12-26 00:42:45 -05001791 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001792 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001793 self.ident.to_tokens(tokens);
1794 if let Some((ref as_token, ref rename)) = self.rename {
1795 as_token.to_tokens(tokens);
1796 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001797 }
David Tolnay4a057422016-10-08 00:02:31 -07001798 }
1799 }
1800
David Tolnay5f332a92017-12-26 00:42:45 -05001801 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001802 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001803 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001804 }
1805 }
1806
David Tolnay5f332a92017-12-26 00:42:45 -05001807 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001808 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001809 self.brace_token.surround(tokens, |tokens| {
1810 self.items.to_tokens(tokens);
1811 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001812 }
1813 }
1814
David Tolnay1bfa7332017-11-11 12:41:20 -08001815 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001816 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001817 tokens.append_all(self.attrs.outer());
1818 self.const_token.to_tokens(tokens);
1819 self.ident.to_tokens(tokens);
1820 self.colon_token.to_tokens(tokens);
1821 self.ty.to_tokens(tokens);
1822 if let Some((ref eq_token, ref default)) = self.default {
1823 eq_token.to_tokens(tokens);
1824 default.to_tokens(tokens);
1825 }
1826 self.semi_token.to_tokens(tokens);
1827 }
1828 }
1829
1830 impl ToTokens for TraitItemMethod {
1831 fn to_tokens(&self, tokens: &mut Tokens) {
1832 tokens.append_all(self.attrs.outer());
1833 self.sig.to_tokens(tokens);
1834 match self.default {
1835 Some(ref block) => {
1836 block.brace_token.surround(tokens, |tokens| {
1837 tokens.append_all(self.attrs.inner());
1838 tokens.append_all(&block.stmts);
1839 });
David Tolnayca085422016-10-04 00:12:38 -07001840 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001841 None => {
1842 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001843 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001844 }
1845 }
1846 }
1847
1848 impl ToTokens for TraitItemType {
1849 fn to_tokens(&self, tokens: &mut Tokens) {
1850 tokens.append_all(self.attrs.outer());
1851 self.type_token.to_tokens(tokens);
1852 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001853 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001854 if !self.bounds.is_empty() {
1855 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1856 self.bounds.to_tokens(tokens);
1857 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001858 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001859 if let Some((ref eq_token, ref default)) = self.default {
1860 eq_token.to_tokens(tokens);
1861 default.to_tokens(tokens);
1862 }
1863 self.semi_token.to_tokens(tokens);
1864 }
1865 }
1866
1867 impl ToTokens for TraitItemMacro {
1868 fn to_tokens(&self, tokens: &mut Tokens) {
1869 tokens.append_all(self.attrs.outer());
1870 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001871 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001872 }
1873 }
1874
David Tolnay2ae520a2017-12-29 11:19:50 -05001875 impl ToTokens for TraitItemVerbatim {
1876 fn to_tokens(&self, tokens: &mut Tokens) {
1877 self.tts.to_tokens(tokens);
1878 }
1879 }
1880
David Tolnay857628c2017-11-11 12:25:31 -08001881 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001882 fn to_tokens(&self, tokens: &mut Tokens) {
1883 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001884 self.vis.to_tokens(tokens);
1885 self.defaultness.to_tokens(tokens);
1886 self.const_token.to_tokens(tokens);
1887 self.ident.to_tokens(tokens);
1888 self.colon_token.to_tokens(tokens);
1889 self.ty.to_tokens(tokens);
1890 self.eq_token.to_tokens(tokens);
1891 self.expr.to_tokens(tokens);
1892 self.semi_token.to_tokens(tokens);
1893 }
1894 }
1895
1896 impl ToTokens for ImplItemMethod {
1897 fn to_tokens(&self, tokens: &mut Tokens) {
1898 tokens.append_all(self.attrs.outer());
1899 self.vis.to_tokens(tokens);
1900 self.defaultness.to_tokens(tokens);
1901 self.sig.to_tokens(tokens);
1902 self.block.brace_token.surround(tokens, |tokens| {
1903 tokens.append_all(self.attrs.inner());
1904 tokens.append_all(&self.block.stmts);
1905 });
1906 }
1907 }
1908
1909 impl ToTokens for ImplItemType {
1910 fn to_tokens(&self, tokens: &mut Tokens) {
1911 tokens.append_all(self.attrs.outer());
1912 self.vis.to_tokens(tokens);
1913 self.defaultness.to_tokens(tokens);
1914 self.type_token.to_tokens(tokens);
1915 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001916 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001917 self.eq_token.to_tokens(tokens);
1918 self.ty.to_tokens(tokens);
1919 self.semi_token.to_tokens(tokens);
1920 }
1921 }
1922
1923 impl ToTokens for ImplItemMacro {
1924 fn to_tokens(&self, tokens: &mut Tokens) {
1925 tokens.append_all(self.attrs.outer());
1926 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001927 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001928 }
1929 }
1930
David Tolnay2ae520a2017-12-29 11:19:50 -05001931 impl ToTokens for ImplItemVerbatim {
1932 fn to_tokens(&self, tokens: &mut Tokens) {
1933 self.tts.to_tokens(tokens);
1934 }
1935 }
1936
David Tolnay8894f602017-11-11 12:11:04 -08001937 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001938 fn to_tokens(&self, tokens: &mut Tokens) {
1939 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001940 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001941 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1942 self.semi_token.to_tokens(tokens);
1943 }
1944 }
1945
1946 impl ToTokens for ForeignItemStatic {
1947 fn to_tokens(&self, tokens: &mut Tokens) {
1948 tokens.append_all(self.attrs.outer());
1949 self.vis.to_tokens(tokens);
1950 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001951 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001952 self.ident.to_tokens(tokens);
1953 self.colon_token.to_tokens(tokens);
1954 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001955 self.semi_token.to_tokens(tokens);
1956 }
1957 }
1958
David Tolnay199bcbb2017-11-12 10:33:52 -08001959 impl ToTokens for ForeignItemType {
1960 fn to_tokens(&self, tokens: &mut Tokens) {
1961 tokens.append_all(self.attrs.outer());
1962 self.vis.to_tokens(tokens);
1963 self.type_token.to_tokens(tokens);
1964 self.ident.to_tokens(tokens);
1965 self.semi_token.to_tokens(tokens);
1966 }
1967 }
1968
David Tolnay2ae520a2017-12-29 11:19:50 -05001969 impl ToTokens for ForeignItemVerbatim {
1970 fn to_tokens(&self, tokens: &mut Tokens) {
1971 self.tts.to_tokens(tokens);
1972 }
1973 }
1974
David Tolnay570695e2017-06-03 16:15:13 -07001975 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001976 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001977 self.constness.to_tokens(tokens);
1978 self.unsafety.to_tokens(tokens);
1979 self.abi.to_tokens(tokens);
1980 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001981 }
1982 }
1983
David Tolnay570695e2017-06-03 16:15:13 -07001984 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001985
1986 impl<'a> ToTokens for NamedDecl<'a> {
1987 fn to_tokens(&self, tokens: &mut Tokens) {
1988 self.0.fn_token.to_tokens(tokens);
1989 self.1.to_tokens(tokens);
1990 self.0.generics.to_tokens(tokens);
1991 self.0.paren_token.surround(tokens, |tokens| {
1992 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001993 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1994 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001995 }
David Tolnayd2836e22017-12-27 23:13:00 -05001996 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001997 });
1998 self.0.output.to_tokens(tokens);
1999 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002000 }
2001 }
2002
Alex Crichton62a0a592017-05-22 13:58:53 -07002003 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07002004 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002005 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002006 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002007 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002008 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002009 }
2010 }
2011
2012 impl ToTokens for ArgSelf {
2013 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05002014 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002015 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002016 }
2017 }
2018
2019 impl ToTokens for ArgCaptured {
2020 fn to_tokens(&self, tokens: &mut Tokens) {
2021 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002022 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002023 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002024 }
2025 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002026}