blob: 74ed6124f710b37edecb559693fe93c9b77756ba [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay9c76bcb2017-12-26 23:14:59 -05003use proc_macro2::TokenTree;
4
5#[cfg(feature = "extra-traits")]
6use mac::TokenTreeHelper;
7#[cfg(feature = "extra-traits")]
8use std::hash::{Hash, Hasher};
David Tolnayb79ee962016-09-04 09:39:20 -07009
Alex Crichton62a0a592017-05-22 13:58:53 -070010ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -080011 /// Things that can appear directly inside of a module.
12 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -070013 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -070014 ///
15 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
16 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080017 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070018 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080019 pub extern_token: Token![extern],
20 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070021 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 pub rename: Option<(Token![as], Ident)>,
23 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }),
25 /// A use declaration (`use` or `pub use`) item.
26 ///
27 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
28 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080029 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070030 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080031 pub use_token: Token![use],
David Tolnay5f332a92017-12-26 00:42:45 -050032 pub leading_colon: Option<Token![::]>,
33 pub prefix: Delimited<Ident, Token![::]>,
34 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
37 /// A static item (`static` or `pub static`).
38 ///
39 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
40 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080041 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070042 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050044 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070045 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080046 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070049 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070051 }),
52 /// A constant item (`const` or `pub const`).
53 ///
54 /// E.g. `const FOO: i32 = 42;`
55 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080056 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070057 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080058 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070059 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080061 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070063 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }),
66 /// A function declaration (`fn` or `pub fn`).
67 ///
68 /// E.g. `fn foo(bar: usize) -> usize { .. }`
69 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080070 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070071 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050072 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050073 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070075 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050076 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub block: Box<Block>,
78 }),
79 /// A module declaration (`mod` or `pub mod`).
80 ///
81 /// E.g. `mod foo;` or `mod foo { .. }`
82 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080083 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070084 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080085 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070086 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -050087 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080088 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070089 }),
90 /// An external module (`extern` or `pub extern`).
91 ///
92 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070093 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080094 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -050096 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 pub items: Vec<ForeignItem>,
98 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070099 /// A type alias (`type` or `pub type`).
100 ///
101 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800102 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800103 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700104 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800105 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700106 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800109 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 }),
112 /// An enum definition (`enum` or `pub enum`).
113 ///
114 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
115 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800116 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700117 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800118 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700119 pub ident: Ident,
120 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500121 pub brace_token: token::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 }),
124 /// A struct definition (`struct` or `pub struct`).
125 ///
126 /// E.g. `struct Foo<A> { x: A }`
127 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800128 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700129 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 pub struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700135 }),
136 /// A union definition (`union` or `pub union`).
137 ///
138 /// E.g. `union Foo<A, B> { x: A, y: B }`
139 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800140 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700141 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800142 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700143 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700145 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700146 }),
147 /// A Trait declaration (`trait` or `pub trait`).
148 ///
149 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
150 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800151 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700152 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500153 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500154 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700156 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800159 pub supertraits: Delimited<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500160 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 pub items: Vec<TraitItem>,
162 }),
163 /// Default trait implementation.
164 ///
165 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
166 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800167 pub attrs: Vec<Attribute>,
David Tolnay9b258702017-12-29 02:24:41 -0500168 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800169 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700170 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800171 pub for_token: Token![for],
172 pub dot2_token: Token![..],
David Tolnay32954ef2017-12-26 22:43:16 -0500173 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
175 /// An implementation.
176 ///
177 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
178 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800179 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500180 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500181 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800182 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700184 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500185 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700186 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800187 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500188 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 pub items: Vec<ImplItem>,
190 }),
191 /// A macro invocation (which includes macro definition).
192 ///
193 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800194 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800195 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800196 /// The `example` in `macro_rules! example { ... }`.
197 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800198 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500199 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800200 }),
David Tolnay500d8322017-12-18 00:32:51 -0800201 /// FIXME will need to revisit what this looks like in the AST.
David Tolnay9c76bcb2017-12-26 23:14:59 -0500202 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800203 pub attrs: Vec<Attribute>,
204 pub vis: Visibility,
205 pub macro_token: Token![macro],
206 pub ident: Ident,
207 pub args: TokenTree,
208 pub body: TokenTree,
209 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700210 }
David Tolnayb79ee962016-09-04 09:39:20 -0700211}
212
David Tolnay9c76bcb2017-12-26 23:14:59 -0500213#[cfg(feature = "extra-traits")]
214impl Eq for ItemMacro2 {}
215
216#[cfg(feature = "extra-traits")]
217impl PartialEq for ItemMacro2 {
218 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -0500219 self.attrs == other.attrs && self.vis == other.vis && self.macro_token == other.macro_token
David Tolnay9c76bcb2017-12-26 23:14:59 -0500220 && self.ident == other.ident
221 && TokenTreeHelper(&self.args) == TokenTreeHelper(&other.args)
222 && TokenTreeHelper(&self.body) == TokenTreeHelper(&other.body)
223 }
224}
225
226#[cfg(feature = "extra-traits")]
227impl Hash for ItemMacro2 {
228 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500229 where
230 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500231 {
232 self.attrs.hash(state);
233 self.vis.hash(state);
234 self.macro_token.hash(state);
235 self.ident.hash(state);
236 TokenTreeHelper(&self.args).hash(state);
237 TokenTreeHelper(&self.body).hash(state);
238 }
239}
240
David Tolnay0e837402016-12-22 17:25:55 -0500241impl From<DeriveInput> for Item {
242 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800243 match input.body {
David Tolnay51382052017-12-27 13:46:21 -0500244 Body::Enum(data) => Item::Enum(ItemEnum {
245 attrs: input.attrs,
246 vis: input.vis,
247 enum_token: data.enum_token,
248 ident: input.ident,
249 generics: input.generics,
250 brace_token: data.brace_token,
251 variants: data.variants,
252 }),
253 Body::Struct(data) => Item::Struct(ItemStruct {
254 attrs: input.attrs,
255 vis: input.vis,
256 struct_token: data.struct_token,
257 ident: input.ident,
258 generics: input.generics,
259 data: data.data,
260 semi_token: data.semi_token,
261 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700262 }
263 }
264}
265
Alex Crichton62a0a592017-05-22 13:58:53 -0700266ast_enum_of_structs! {
David Tolnay5f332a92017-12-26 00:42:45 -0500267 /// Things that can appear directly inside of a module.
268 pub enum UseTree {
269 /// `use prefix::Ty` or `use prefix::Ty as Renamed`
270 pub Path(UsePath {
271 pub ident: Ident,
272 pub rename: Option<(Token![as], Ident)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500274 /// `use prefix::*`
275 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800276 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 }),
David Tolnay5f332a92017-12-26 00:42:45 -0500278 /// `use prefix::{a, b, c}`
279 pub List(UseList {
David Tolnay32954ef2017-12-26 22:43:16 -0500280 pub brace_token: token::Brace,
David Tolnay5f332a92017-12-26 00:42:45 -0500281 pub items: Delimited<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 }),
283 }
284}
285
Alex Crichton62a0a592017-05-22 13:58:53 -0700286ast_enum_of_structs! {
287 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800288 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 /// A foreign function
290 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800291 pub attrs: Vec<Attribute>,
292 pub vis: Visibility,
293 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
297 /// A foreign static item (`static ext: u8`)
298 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800299 pub attrs: Vec<Attribute>,
300 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500302 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800303 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800304 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800305 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800306 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800308 /// A foreign type
309 pub Type(ForeignItemType {
310 pub attrs: Vec<Attribute>,
311 pub vis: Visibility,
312 pub type_token: Token![type],
313 pub ident: Ident,
314 pub semi_token: Token![;],
315 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700317}
318
David Tolnayda705bd2017-11-10 21:58:05 -0800319ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 /// Represents an item declaration within a trait declaration,
321 /// possibly including a default implementation. A trait item is
322 /// either required (meaning it doesn't have an implementation, just a
323 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800324 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800326 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800327 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700328 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800329 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800330 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800331 pub default: Option<(Token![=], Expr)>,
332 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 }),
334 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800335 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 pub sig: MethodSig,
337 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800338 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 }),
340 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800341 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800342 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700343 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500344 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800345 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800346 pub bounds: Delimited<TypeParamBound, Token![+]>,
347 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800348 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800350 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800351 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800352 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500353 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800354 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700356}
357
Alex Crichton62a0a592017-05-22 13:58:53 -0700358ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800359 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700360 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800361 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700362 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500363 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700365 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800367 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }),
372 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800373 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700374 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500375 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 pub sig: MethodSig,
377 pub block: Block,
378 }),
379 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800380 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700381 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500382 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800383 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700384 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500385 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800386 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800387 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800388 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 }),
David Tolnay857628c2017-11-11 12:25:31 -0800390 pub Macro(ImplItemMacro {
391 pub attrs: Vec<Attribute>,
392 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500393 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800394 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700396}
397
398ast_struct! {
399 /// Represents a method's signature in a trait declaration,
400 /// or in an implementation.
401 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500402 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500403 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700405 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700407 }
408}
409
410ast_struct! {
411 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700412 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 /// E.g. `fn foo(bar: baz)`
414 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800415 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500416 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500417 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500419 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500420 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700421 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700422}
423
Alex Crichton62a0a592017-05-22 13:58:53 -0700424ast_enum_of_structs! {
425 /// An argument in a function header.
426 ///
427 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
428 pub enum FnArg {
429 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800430 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500432 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500433 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }),
435 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500436 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 }),
439 pub Captured(ArgCaptured {
440 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800442 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 }),
David Tolnay80ed55f2017-12-27 22:54:40 -0500444 pub Inferred(Pat),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800445 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }
David Tolnay62f374c2016-10-02 13:37:00 -0700447}
448
David Tolnayedf2b992016-09-23 20:43:45 -0700449#[cfg(feature = "parsing")]
450pub mod parsing {
451 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700452
Michael Layzell92639a52017-06-01 00:07:44 -0400453 use synom::Synom;
David Tolnay57292da2017-12-27 21:03:33 -0500454 use proc_macro2::{TokenNode, Delimiter};
David Tolnay84aa0752016-10-02 23:01:13 -0700455
David Tolnay4c614be2017-11-10 00:02:38 -0800456 impl_synom!(Item "item" alt!(
457 syn!(ItemExternCrate) => { Item::ExternCrate }
458 |
459 syn!(ItemUse) => { Item::Use }
460 |
461 syn!(ItemStatic) => { Item::Static }
462 |
463 syn!(ItemConst) => { Item::Const }
464 |
465 syn!(ItemFn) => { Item::Fn }
466 |
467 syn!(ItemMod) => { Item::Mod }
468 |
469 syn!(ItemForeignMod) => { Item::ForeignMod }
470 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800471 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800472 |
473 syn!(ItemStruct) => { Item::Struct }
474 |
475 syn!(ItemEnum) => { Item::Enum }
476 |
477 syn!(ItemUnion) => { Item::Union }
478 |
479 syn!(ItemTrait) => { Item::Trait }
480 |
481 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
482 |
483 syn!(ItemImpl) => { Item::Impl }
484 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800485 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800486 |
487 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800488 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700489
David Tolnaydecf28d2017-11-11 11:56:45 -0800490 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500491 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700492 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800493 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700494 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500495 body: call!(tt::delimited) >>
David Tolnay57292da2017-12-27 21:03:33 -0500496 semi: cond!(!is_braced(&body), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800497 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700498 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800499 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800500 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500501 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700502 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500503 tokens: body,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800504 },
David Tolnay57292da2017-12-27 21:03:33 -0500505 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800506 })
David Tolnayedf2b992016-09-23 20:43:45 -0700507 ));
508
David Tolnay500d8322017-12-18 00:32:51 -0800509 // TODO: figure out the actual grammar; is body required to be braced?
510 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500511 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800512 vis: syn!(Visibility) >>
513 macro_: keyword!(macro) >>
514 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500515 args: call!(tt::parenthesized) >>
516 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800517 (ItemMacro2 {
518 attrs: attrs,
519 vis: vis,
520 macro_token: macro_,
521 ident: ident,
522 args: args,
523 body: body,
524 })
525 ));
526
David Tolnay4c614be2017-11-10 00:02:38 -0800527 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500528 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700529 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800530 extern_: keyword!(extern) >>
531 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700532 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800533 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
534 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800535 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700536 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800537 vis: vis,
538 extern_token: extern_,
539 crate_token: crate_,
540 ident: ident,
541 rename: rename,
542 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800543 })
David Tolnayedf2b992016-09-23 20:43:45 -0700544 ));
545
David Tolnay4c614be2017-11-10 00:02:38 -0800546 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500547 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700548 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800549 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500550 leading_colon: option!(punct!(::)) >>
551 mut prefix: call!(Delimited::parse_terminated_with, use_prefix) >>
David Tolnay8edcef12017-12-28 12:06:52 -0500552 tree: switch!(value!(prefix.empty_or_trailing()),
David Tolnay5f332a92017-12-26 00:42:45 -0500553 true => syn!(UseTree)
554 |
David Tolnay8edcef12017-12-28 12:06:52 -0500555 false => alt!(
556 tuple!(keyword!(as), syn!(Ident)) => {
557 |rename| UseTree::Path(UsePath {
558 ident: prefix.pop().unwrap().into_item(),
559 rename: Some(rename),
560 })
561 }
David Tolnay5f332a92017-12-26 00:42:45 -0500562 |
David Tolnay8edcef12017-12-28 12:06:52 -0500563 epsilon!() => {
564 |_| UseTree::Path(UsePath {
565 ident: prefix.pop().unwrap().into_item(),
566 rename: None,
567 })
568 }
David Tolnay5f332a92017-12-26 00:42:45 -0500569 )
570 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800571 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800572 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700573 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800574 vis: vis,
575 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500576 leading_colon: leading_colon,
577 prefix: prefix,
578 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800579 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800580 })
David Tolnay4a057422016-10-08 00:02:31 -0700581 ));
582
David Tolnay5f332a92017-12-26 00:42:45 -0500583 named!(use_prefix -> Ident, alt!(
584 syn!(Ident)
585 |
586 keyword!(self) => { Into::into }
587 |
588 keyword!(super) => { Into::into }
589 |
590 keyword!(crate) => { Into::into }
591 ));
592
593 impl_synom!(UseTree "use tree" alt!(
594 syn!(UsePath) => { UseTree::Path }
595 |
596 syn!(UseGlob) => { UseTree::Glob }
597 |
598 syn!(UseList) => { UseTree::List }
599 ));
600
601 impl_synom!(UsePath "use path" do_parse!(
602 ident: alt!(
603 syn!(Ident)
Michael Layzell92639a52017-06-01 00:07:44 -0400604 |
David Tolnay5f332a92017-12-26 00:42:45 -0500605 keyword!(self) => { Into::into }
606 ) >>
607 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
608 (UsePath {
609 ident: ident,
610 rename: rename,
611 })
612 ));
David Tolnay4a057422016-10-08 00:02:31 -0700613
David Tolnay5f332a92017-12-26 00:42:45 -0500614 impl_synom!(UseGlob "use glob" do_parse!(
615 star: punct!(*) >>
616 (UseGlob {
617 star_token: star,
618 })
619 ));
David Tolnay4a057422016-10-08 00:02:31 -0700620
David Tolnay5f332a92017-12-26 00:42:45 -0500621 impl_synom!(UseList "use list" do_parse!(
622 list: braces!(Delimited::parse_terminated) >>
623 (UseList {
624 brace_token: list.1,
625 items: list.0,
626 })
627 ));
David Tolnay4a057422016-10-08 00:02:31 -0700628
David Tolnay4c614be2017-11-10 00:02:38 -0800629 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500630 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700631 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800632 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500633 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700634 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800635 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800636 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800637 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700638 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800639 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800640 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700641 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800642 vis: vis,
643 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500644 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800645 ident: ident,
646 colon_token: colon,
647 ty: Box::new(ty),
648 eq_token: eq,
649 expr: Box::new(value),
650 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800651 })
David Tolnay47a877c2016-10-01 16:50:55 -0700652 ));
653
David Tolnay4c614be2017-11-10 00:02:38 -0800654 impl_synom!(ItemConst "const item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500655 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700656 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800657 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700658 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800659 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800660 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800661 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700662 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800664 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700665 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800666 vis: vis,
667 const_token: const_,
668 ident: ident,
669 colon_token: colon,
670 ty: Box::new(ty),
671 eq_token: eq,
672 expr: Box::new(value),
673 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800674 })
David Tolnay47a877c2016-10-01 16:50:55 -0700675 ));
676
David Tolnay4c614be2017-11-10 00:02:38 -0800677 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500678 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700679 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500680 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500681 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700682 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800683 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700684 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700685 generics: syn!(Generics) >>
686 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800687 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500688 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700689 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500690 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700691 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400692 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800693 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700694 attrs: {
695 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700696 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700697 attrs
698 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800699 vis: vis,
700 constness: constness,
701 unsafety: unsafety,
702 abi: abi,
703 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800704 fn_token: fn_,
705 paren_token: inputs.1,
706 inputs: inputs.0,
707 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500708 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800709 generics: Generics {
710 where_clause: where_clause,
711 .. generics
712 },
713 }),
714 ident: ident,
715 block: Box::new(Block {
716 brace_token: inner_attrs_stmts.1,
717 stmts: (inner_attrs_stmts.0).1,
718 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800719 })
David Tolnay42602292016-10-01 22:25:45 -0700720 ));
721
Alex Crichton954046c2017-05-30 21:49:42 -0700722 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400723 named!(parse -> Self, alt!(
724 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800725 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400726 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500727 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800728 self_: keyword!(self) >>
729 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400730 (ArgSelfRef {
731 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -0500732 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400733 and_token: and,
734 self_token: self_,
735 }.into())
736 )
737 |
738 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -0500739 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 self_: keyword!(self) >>
741 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400742 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500743 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -0400744 self_token: self_,
745 }.into())
746 )
747 |
748 do_parse!(
749 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800750 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800751 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400752 (ArgCaptured {
753 pat: pat,
754 ty: ty,
755 colon_token: colon,
756 }.into())
757 )
758 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800759 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400760 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700761 }
David Tolnay62f374c2016-10-02 13:37:00 -0700762
David Tolnay4c614be2017-11-10 00:02:38 -0800763 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500764 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700765 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700767 ident: syn!(Ident) >>
768 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800769 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700770 Vec::new(),
771 None,
772 Some(semi),
773 )}
David Tolnay37d10332016-10-13 20:51:04 -0700774 |
Alex Crichton954046c2017-05-30 21:49:42 -0700775 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700776 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500777 many0!(Attribute::parse_inner),
778 many0!(Item::parse)
Michael Layzell416724e2017-05-24 21:12:34 -0400779 )
David Tolnay570695e2017-06-03 16:15:13 -0700780 ) => {|((inner_attrs, items), brace)| (
781 inner_attrs,
782 Some((brace, items)),
783 None,
784 )}
David Tolnay37d10332016-10-13 20:51:04 -0700785 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800786 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700787 attrs: {
788 let mut attrs = outer_attrs;
789 attrs.extend(content_semi.0);
790 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700791 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800792 vis: vis,
793 mod_token: mod_,
794 ident: ident,
795 content: content_semi.1,
796 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800797 })
David Tolnay35902302016-10-06 01:11:08 -0700798 ));
799
David Tolnay4c614be2017-11-10 00:02:38 -0800800 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500801 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700802 abi: syn!(Abi) >>
David Tolnay2c136452017-12-27 14:13:32 -0500803 items: braces!(many0!(ForeignItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800804 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700805 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800806 abi: abi,
807 brace_token: items.1,
808 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800809 })
David Tolnay35902302016-10-06 01:11:08 -0700810 ));
811
David Tolnay8894f602017-11-11 12:11:04 -0800812 impl_synom!(ForeignItem "foreign item" alt!(
813 syn!(ForeignItemFn) => { ForeignItem::Fn }
814 |
815 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800816 |
817 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800818 ));
David Tolnay35902302016-10-06 01:11:08 -0700819
David Tolnay8894f602017-11-11 12:11:04 -0800820 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500821 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700822 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800823 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700824 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700825 generics: syn!(Generics) >>
826 inputs: parens!(do_parse!(
827 args: call!(Delimited::parse_terminated) >>
828 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800829 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700830 (args, variadic)
831 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800832 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500833 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800834 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700835 ({
836 let ((inputs, variadic), parens) = inputs;
837 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800838 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700839 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700840 attrs: attrs,
841 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800842 decl: Box::new(FnDecl {
843 fn_token: fn_,
844 paren_token: parens,
845 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -0500846 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -0800847 output: ret,
848 generics: Generics {
849 where_clause: where_clause,
850 .. generics
851 },
852 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700853 vis: vis,
854 }
David Tolnay35902302016-10-06 01:11:08 -0700855 })
856 ));
857
David Tolnay8894f602017-11-11 12:11:04 -0800858 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500859 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700860 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500862 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700863 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800864 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800865 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800867 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700868 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700869 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700870 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800871 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -0500872 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800873 static_token: static_,
874 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700875 vis: vis,
876 })
877 ));
878
David Tolnay199bcbb2017-11-12 10:33:52 -0800879 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500880 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -0800881 vis: syn!(Visibility) >>
882 type_: keyword!(type) >>
883 ident: syn!(Ident) >>
884 semi: punct!(;) >>
885 (ForeignItemType {
886 attrs: attrs,
887 vis: vis,
888 type_token: type_,
889 ident: ident,
890 semi_token: semi,
891 })
892 ));
893
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800894 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500895 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700896 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800897 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700898 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700899 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500900 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800901 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800902 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800904 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700905 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800906 vis: vis,
907 type_token: type_,
908 ident: ident,
909 generics: Generics {
910 where_clause: where_clause,
911 ..generics
912 },
913 eq_token: eq,
914 ty: Box::new(ty),
915 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800916 })
David Tolnay3cf52982016-10-01 17:11:37 -0700917 ));
918
David Tolnay4c614be2017-11-10 00:02:38 -0800919 impl_synom!(ItemStruct "struct item" switch!(
920 map!(syn!(DeriveInput), Into::into),
921 Item::Struct(item) => value!(item)
922 |
923 _ => reject!()
924 ));
David Tolnay42602292016-10-01 22:25:45 -0700925
David Tolnay4c614be2017-11-10 00:02:38 -0800926 impl_synom!(ItemEnum "enum item" switch!(
927 map!(syn!(DeriveInput), Into::into),
928 Item::Enum(item) => value!(item)
929 |
930 _ => reject!()
931 ));
932
933 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500934 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700935 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800936 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700937 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700938 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500939 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700940 fields: braces!(call!(Delimited::parse_terminated_with,
941 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800942 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700943 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800944 vis: vis,
945 union_token: union_,
946 ident: ident,
947 generics: Generics {
948 where_clause: where_clause,
949 .. generics
950 },
951 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800952 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700953 ));
954
David Tolnay4c614be2017-11-10 00:02:38 -0800955 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500956 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700957 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -0500958 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -0500959 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800960 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -0700961 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700962 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700965 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500967 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -0500968 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800969 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -0700970 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800971 vis: vis,
972 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500973 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800974 trait_token: trait_,
975 ident: ident,
976 generics: Generics {
977 where_clause: where_clause,
978 .. generics
979 },
980 colon_token: colon,
981 supertraits: bounds.unwrap_or_default(),
982 brace_token: body.1,
983 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800984 })
David Tolnay0aecb732016-10-03 23:03:50 -0700985 ));
986
David Tolnay4c614be2017-11-10 00:02:38 -0800987 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500988 attrs: many0!(Attribute::parse_outer) >>
David Tolnay9b258702017-12-29 02:24:41 -0500989 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700991 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800992 for_: keyword!(for) >>
993 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700994 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800995 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -0700996 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800997 unsafety: unsafety,
998 impl_token: impl_,
999 path: path,
1000 for_token: for_,
1001 dot2_token: dot2,
1002 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001003 })
David Tolnayf94e2362016-10-04 00:29:51 -07001004 ));
1005
David Tolnayda705bd2017-11-10 21:58:05 -08001006 impl_synom!(TraitItem "trait item" alt!(
1007 syn!(TraitItemConst) => { TraitItem::Const }
1008 |
1009 syn!(TraitItemMethod) => { TraitItem::Method }
1010 |
1011 syn!(TraitItemType) => { TraitItem::Type }
1012 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001013 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001014 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001015
David Tolnayda705bd2017-11-10 21:58:05 -08001016 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001017 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001018 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001019 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001021 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1023 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001024 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001025 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001026 const_token: const_,
1027 ident: ident,
1028 colon_token: colon,
1029 ty: ty,
1030 default: default,
1031 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001032 })
1033 ));
1034
David Tolnayda705bd2017-11-10 21:58:05 -08001035 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001036 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001037 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001038 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001039 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001040 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001041 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001042 generics: syn!(Generics) >>
1043 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001044 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001045 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001046 body: option!(braces!(
David Tolnay2c136452017-12-27 14:13:32 -05001047 tuple!(many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001048 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001049 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001051 ({
1052 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001053 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001054 None => (Vec::new(), None),
1055 };
David Tolnayda705bd2017-11-10 21:58:05 -08001056 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001057 attrs: {
1058 let mut attrs = outer_attrs;
1059 attrs.extend(inner_attrs);
1060 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001061 },
David Tolnayda705bd2017-11-10 21:58:05 -08001062 sig: MethodSig {
1063 constness: constness,
1064 unsafety: unsafety,
1065 abi: abi,
1066 ident: ident,
1067 decl: FnDecl {
1068 inputs: inputs.0,
1069 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001070 fn_token: fn_,
1071 paren_token: inputs.1,
David Tolnayd2836e22017-12-27 23:13:00 -05001072 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001073 generics: Generics {
1074 where_clause: where_clause,
1075 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001076 },
1077 },
David Tolnayda705bd2017-11-10 21:58:05 -08001078 },
1079 default: stmts.map(|stmts| {
1080 Block {
1081 stmts: stmts.0,
1082 brace_token: stmts.1,
1083 }
1084 }),
1085 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001086 }
David Tolnay0aecb732016-10-03 23:03:50 -07001087 })
1088 ));
1089
David Tolnayda705bd2017-11-10 21:58:05 -08001090 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001091 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001092 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001093 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001094 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001095 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001096 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001097 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001098 ) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001099 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001100 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001101 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001102 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001103 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001104 type_token: type_,
1105 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001106 generics: Generics {
1107 where_clause: where_clause,
1108 .. generics
1109 },
David Tolnayda705bd2017-11-10 21:58:05 -08001110 colon_token: colon,
1111 bounds: bounds.unwrap_or_default(),
1112 default: default,
1113 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001114 })
1115 ));
1116
David Tolnaydecf28d2017-11-11 11:56:45 -08001117 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001118 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001119 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001120 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001121 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001122 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001123 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001124 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001125 })
1126 ));
1127
David Tolnay4c614be2017-11-10 00:02:38 -08001128 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001129 attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001130 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001131 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001132 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001133 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001134 polarity_path: alt!(
1135 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001136 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001137 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001138 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001139 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001140 )
1141 |
David Tolnay570695e2017-06-03 16:15:13 -07001142 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001143 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001144 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001145 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001146 body: braces!(many0!(ImplItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001147 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001148 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001149 defaultness: defaultness,
1150 unsafety: unsafety,
1151 impl_token: impl_,
1152 generics: Generics {
1153 where_clause: where_clause,
1154 .. generics
1155 },
1156 trait_: polarity_path,
1157 self_ty: Box::new(self_ty),
1158 brace_token: body.1,
1159 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001160 })
David Tolnay4c9be372016-10-06 00:47:37 -07001161 ));
1162
David Tolnay857628c2017-11-11 12:25:31 -08001163 impl_synom!(ImplItem "item in impl block" alt!(
1164 syn!(ImplItemConst) => { ImplItem::Const }
1165 |
1166 syn!(ImplItemMethod) => { ImplItem::Method }
1167 |
1168 syn!(ImplItemType) => { ImplItem::Type }
1169 |
1170 syn!(ImplItemMacro) => { ImplItem::Macro }
1171 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001172
David Tolnay857628c2017-11-11 12:25:31 -08001173 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001174 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001175 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001176 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001177 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001178 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001180 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001181 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001182 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001184 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001185 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001186 vis: vis,
1187 defaultness: defaultness,
1188 const_token: const_,
1189 ident: ident,
1190 colon_token: colon,
1191 ty: ty,
1192 eq_token: eq,
1193 expr: value,
1194 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001195 })
1196 ));
1197
David Tolnay857628c2017-11-11 12:25:31 -08001198 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001199 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001200 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001201 defaultness: option!(keyword!(default)) >>
1202 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001203 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001204 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001205 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001206 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001207 generics: syn!(Generics) >>
1208 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001209 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001210 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001211 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001212 many0!(Attribute::parse_inner),
Alex Crichton954046c2017-05-30 21:49:42 -07001213 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001214 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001215 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001216 attrs: {
1217 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001218 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001219 attrs
1220 },
David Tolnay857628c2017-11-11 12:25:31 -08001221 vis: vis,
1222 defaultness: defaultness,
1223 sig: MethodSig {
1224 constness: constness,
1225 unsafety: unsafety,
1226 abi: abi,
1227 ident: ident,
1228 decl: FnDecl {
1229 fn_token: fn_,
1230 paren_token: inputs.1,
1231 inputs: inputs.0,
1232 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001233 generics: Generics {
1234 where_clause: where_clause,
1235 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001236 },
David Tolnayd2836e22017-12-27 23:13:00 -05001237 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001238 },
David Tolnay857628c2017-11-11 12:25:31 -08001239 },
1240 block: Block {
1241 brace_token: inner_attrs_stmts.1,
1242 stmts: (inner_attrs_stmts.0).1,
1243 },
David Tolnay4c9be372016-10-06 00:47:37 -07001244 })
1245 ));
1246
David Tolnay857628c2017-11-11 12:25:31 -08001247 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001248 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001249 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001250 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001251 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001252 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001253 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001254 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001255 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001256 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001257 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001258 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001259 vis: vis,
1260 defaultness: defaultness,
1261 type_token: type_,
1262 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001263 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001264 eq_token: eq,
1265 ty: ty,
1266 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001267 })
1268 ));
1269
David Tolnay857628c2017-11-11 12:25:31 -08001270 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001271 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001272 mac: syn!(Macro) >>
David Tolnay57292da2017-12-27 21:03:33 -05001273 semi: cond!(!is_braced(&mac.tokens), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001274 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001275 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001276 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001277 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001278 })
1279 ));
1280
David Tolnay57292da2017-12-27 21:03:33 -05001281 fn is_braced(tt: &TokenTree) -> bool {
1282 match tt.kind {
1283 TokenNode::Group(Delimiter::Brace, _) => true,
1284 _ => false,
1285 }
1286 }
David Tolnayedf2b992016-09-23 20:43:45 -07001287}
David Tolnay4a51dc72016-10-01 00:40:31 -07001288
1289#[cfg(feature = "printing")]
1290mod printing {
1291 use super::*;
1292 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001293 use data::VariantData;
David Tolnay51382052017-12-27 13:46:21 -05001294 use quote::{ToTokens, Tokens};
David Tolnay4a51dc72016-10-01 00:40:31 -07001295
David Tolnay1bfa7332017-11-11 12:41:20 -08001296 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001297 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001298 tokens.append_all(self.attrs.outer());
1299 self.vis.to_tokens(tokens);
1300 self.extern_token.to_tokens(tokens);
1301 self.crate_token.to_tokens(tokens);
1302 self.ident.to_tokens(tokens);
1303 if let Some((ref as_token, ref rename)) = self.rename {
1304 as_token.to_tokens(tokens);
1305 rename.to_tokens(tokens);
1306 }
1307 self.semi_token.to_tokens(tokens);
1308 }
1309 }
1310
1311 impl ToTokens for ItemUse {
1312 fn to_tokens(&self, tokens: &mut Tokens) {
1313 tokens.append_all(self.attrs.outer());
1314 self.vis.to_tokens(tokens);
1315 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001316 self.leading_colon.to_tokens(tokens);
1317 self.prefix.to_tokens(tokens);
1318 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001319 self.semi_token.to_tokens(tokens);
1320 }
1321 }
1322
1323 impl ToTokens for ItemStatic {
1324 fn to_tokens(&self, tokens: &mut Tokens) {
1325 tokens.append_all(self.attrs.outer());
1326 self.vis.to_tokens(tokens);
1327 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001328 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001329 self.ident.to_tokens(tokens);
1330 self.colon_token.to_tokens(tokens);
1331 self.ty.to_tokens(tokens);
1332 self.eq_token.to_tokens(tokens);
1333 self.expr.to_tokens(tokens);
1334 self.semi_token.to_tokens(tokens);
1335 }
1336 }
1337
1338 impl ToTokens for ItemConst {
1339 fn to_tokens(&self, tokens: &mut Tokens) {
1340 tokens.append_all(self.attrs.outer());
1341 self.vis.to_tokens(tokens);
1342 self.const_token.to_tokens(tokens);
1343 self.ident.to_tokens(tokens);
1344 self.colon_token.to_tokens(tokens);
1345 self.ty.to_tokens(tokens);
1346 self.eq_token.to_tokens(tokens);
1347 self.expr.to_tokens(tokens);
1348 self.semi_token.to_tokens(tokens);
1349 }
1350 }
1351
1352 impl ToTokens for ItemFn {
1353 fn to_tokens(&self, tokens: &mut Tokens) {
1354 tokens.append_all(self.attrs.outer());
1355 self.vis.to_tokens(tokens);
1356 self.constness.to_tokens(tokens);
1357 self.unsafety.to_tokens(tokens);
1358 self.abi.to_tokens(tokens);
1359 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1360 self.block.brace_token.surround(tokens, |tokens| {
1361 tokens.append_all(self.attrs.inner());
1362 tokens.append_all(&self.block.stmts);
1363 });
1364 }
1365 }
1366
1367 impl ToTokens for ItemMod {
1368 fn to_tokens(&self, tokens: &mut Tokens) {
1369 tokens.append_all(self.attrs.outer());
1370 self.vis.to_tokens(tokens);
1371 self.mod_token.to_tokens(tokens);
1372 self.ident.to_tokens(tokens);
1373 if let Some((ref brace, ref items)) = self.content {
1374 brace.surround(tokens, |tokens| {
1375 tokens.append_all(self.attrs.inner());
1376 tokens.append_all(items);
1377 });
1378 } else {
1379 TokensOrDefault(&self.semi).to_tokens(tokens);
1380 }
1381 }
1382 }
1383
1384 impl ToTokens for ItemForeignMod {
1385 fn to_tokens(&self, tokens: &mut Tokens) {
1386 tokens.append_all(self.attrs.outer());
1387 self.abi.to_tokens(tokens);
1388 self.brace_token.surround(tokens, |tokens| {
1389 tokens.append_all(&self.items);
1390 });
1391 }
1392 }
1393
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001394 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001395 fn to_tokens(&self, tokens: &mut Tokens) {
1396 tokens.append_all(self.attrs.outer());
1397 self.vis.to_tokens(tokens);
1398 self.type_token.to_tokens(tokens);
1399 self.ident.to_tokens(tokens);
1400 self.generics.to_tokens(tokens);
1401 self.generics.where_clause.to_tokens(tokens);
1402 self.eq_token.to_tokens(tokens);
1403 self.ty.to_tokens(tokens);
1404 self.semi_token.to_tokens(tokens);
1405 }
1406 }
1407
1408 impl ToTokens for ItemEnum {
1409 fn to_tokens(&self, tokens: &mut Tokens) {
1410 tokens.append_all(self.attrs.outer());
1411 self.vis.to_tokens(tokens);
1412 self.enum_token.to_tokens(tokens);
1413 self.ident.to_tokens(tokens);
1414 self.generics.to_tokens(tokens);
1415 self.generics.where_clause.to_tokens(tokens);
1416 self.brace_token.surround(tokens, |tokens| {
1417 self.variants.to_tokens(tokens);
1418 });
1419 }
1420 }
1421
1422 impl ToTokens for ItemStruct {
1423 fn to_tokens(&self, tokens: &mut Tokens) {
1424 tokens.append_all(self.attrs.outer());
1425 self.vis.to_tokens(tokens);
1426 self.struct_token.to_tokens(tokens);
1427 self.ident.to_tokens(tokens);
1428 self.generics.to_tokens(tokens);
1429 match self.data {
1430 VariantData::Struct(..) => {
1431 self.generics.where_clause.to_tokens(tokens);
1432 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001433 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001434 VariantData::Tuple(..) => {
1435 self.data.to_tokens(tokens);
1436 self.generics.where_clause.to_tokens(tokens);
1437 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001438 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001439 VariantData::Unit => {
1440 self.generics.where_clause.to_tokens(tokens);
1441 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001442 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001443 }
1444 }
1445 }
1446
1447 impl ToTokens for ItemUnion {
1448 fn to_tokens(&self, tokens: &mut Tokens) {
1449 tokens.append_all(self.attrs.outer());
1450 self.vis.to_tokens(tokens);
1451 self.union_token.to_tokens(tokens);
1452 self.ident.to_tokens(tokens);
1453 self.generics.to_tokens(tokens);
1454 self.generics.where_clause.to_tokens(tokens);
1455 // XXX: Should we handle / complain when using a
1456 // non-VariantData::Struct Variant in Union?
1457 self.data.to_tokens(tokens);
1458 }
1459 }
1460
1461 impl ToTokens for ItemTrait {
1462 fn to_tokens(&self, tokens: &mut Tokens) {
1463 tokens.append_all(self.attrs.outer());
1464 self.vis.to_tokens(tokens);
1465 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001466 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001467 self.trait_token.to_tokens(tokens);
1468 self.ident.to_tokens(tokens);
1469 self.generics.to_tokens(tokens);
1470 if !self.supertraits.is_empty() {
1471 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1472 self.supertraits.to_tokens(tokens);
1473 }
1474 self.generics.where_clause.to_tokens(tokens);
1475 self.brace_token.surround(tokens, |tokens| {
1476 tokens.append_all(&self.items);
1477 });
1478 }
1479 }
1480
1481 impl ToTokens for ItemDefaultImpl {
1482 fn to_tokens(&self, tokens: &mut Tokens) {
1483 tokens.append_all(self.attrs.outer());
1484 self.unsafety.to_tokens(tokens);
1485 self.impl_token.to_tokens(tokens);
1486 self.path.to_tokens(tokens);
1487 self.for_token.to_tokens(tokens);
1488 self.dot2_token.to_tokens(tokens);
1489 self.brace_token.surround(tokens, |_tokens| {});
1490 }
1491 }
1492
1493 impl ToTokens for ItemImpl {
1494 fn to_tokens(&self, tokens: &mut Tokens) {
1495 tokens.append_all(self.attrs.outer());
1496 self.defaultness.to_tokens(tokens);
1497 self.unsafety.to_tokens(tokens);
1498 self.impl_token.to_tokens(tokens);
1499 self.generics.to_tokens(tokens);
1500 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1501 polarity.to_tokens(tokens);
1502 path.to_tokens(tokens);
1503 for_token.to_tokens(tokens);
1504 }
1505 self.self_ty.to_tokens(tokens);
1506 self.generics.where_clause.to_tokens(tokens);
1507 self.brace_token.surround(tokens, |tokens| {
1508 tokens.append_all(&self.items);
1509 });
1510 }
1511 }
1512
1513 impl ToTokens for ItemMacro {
1514 fn to_tokens(&self, tokens: &mut Tokens) {
1515 tokens.append_all(self.attrs.outer());
1516 self.mac.path.to_tokens(tokens);
1517 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001518 self.ident.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -05001519 self.mac.tokens.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001520 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001521 }
1522 }
David Tolnay42602292016-10-01 22:25:45 -07001523
David Tolnay500d8322017-12-18 00:32:51 -08001524 impl ToTokens for ItemMacro2 {
1525 fn to_tokens(&self, tokens: &mut Tokens) {
1526 tokens.append_all(self.attrs.outer());
1527 self.vis.to_tokens(tokens);
1528 self.macro_token.to_tokens(tokens);
1529 self.ident.to_tokens(tokens);
1530 self.args.to_tokens(tokens);
1531 self.body.to_tokens(tokens);
1532 }
1533 }
1534
David Tolnay5f332a92017-12-26 00:42:45 -05001535 impl ToTokens for UsePath {
David Tolnay4a057422016-10-08 00:02:31 -07001536 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5f332a92017-12-26 00:42:45 -05001537 self.ident.to_tokens(tokens);
1538 if let Some((ref as_token, ref rename)) = self.rename {
1539 as_token.to_tokens(tokens);
1540 rename.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001541 }
David Tolnay4a057422016-10-08 00:02:31 -07001542 }
1543 }
1544
David Tolnay5f332a92017-12-26 00:42:45 -05001545 impl ToTokens for UseGlob {
Alex Crichton62a0a592017-05-22 13:58:53 -07001546 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001547 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001548 }
1549 }
1550
David Tolnay5f332a92017-12-26 00:42:45 -05001551 impl ToTokens for UseList {
Alex Crichton62a0a592017-05-22 13:58:53 -07001552 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001553 self.brace_token.surround(tokens, |tokens| {
1554 self.items.to_tokens(tokens);
1555 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001556 }
1557 }
1558
David Tolnay1bfa7332017-11-11 12:41:20 -08001559 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001560 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001561 tokens.append_all(self.attrs.outer());
1562 self.const_token.to_tokens(tokens);
1563 self.ident.to_tokens(tokens);
1564 self.colon_token.to_tokens(tokens);
1565 self.ty.to_tokens(tokens);
1566 if let Some((ref eq_token, ref default)) = self.default {
1567 eq_token.to_tokens(tokens);
1568 default.to_tokens(tokens);
1569 }
1570 self.semi_token.to_tokens(tokens);
1571 }
1572 }
1573
1574 impl ToTokens for TraitItemMethod {
1575 fn to_tokens(&self, tokens: &mut Tokens) {
1576 tokens.append_all(self.attrs.outer());
1577 self.sig.to_tokens(tokens);
1578 match self.default {
1579 Some(ref block) => {
1580 block.brace_token.surround(tokens, |tokens| {
1581 tokens.append_all(self.attrs.inner());
1582 tokens.append_all(&block.stmts);
1583 });
David Tolnayca085422016-10-04 00:12:38 -07001584 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001585 None => {
1586 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001587 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001588 }
1589 }
1590 }
1591
1592 impl ToTokens for TraitItemType {
1593 fn to_tokens(&self, tokens: &mut Tokens) {
1594 tokens.append_all(self.attrs.outer());
1595 self.type_token.to_tokens(tokens);
1596 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001597 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001598 if !self.bounds.is_empty() {
1599 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1600 self.bounds.to_tokens(tokens);
1601 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001602 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001603 if let Some((ref eq_token, ref default)) = self.default {
1604 eq_token.to_tokens(tokens);
1605 default.to_tokens(tokens);
1606 }
1607 self.semi_token.to_tokens(tokens);
1608 }
1609 }
1610
1611 impl ToTokens for TraitItemMacro {
1612 fn to_tokens(&self, tokens: &mut Tokens) {
1613 tokens.append_all(self.attrs.outer());
1614 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001615 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001616 }
1617 }
1618
David Tolnay857628c2017-11-11 12:25:31 -08001619 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001620 fn to_tokens(&self, tokens: &mut Tokens) {
1621 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001622 self.vis.to_tokens(tokens);
1623 self.defaultness.to_tokens(tokens);
1624 self.const_token.to_tokens(tokens);
1625 self.ident.to_tokens(tokens);
1626 self.colon_token.to_tokens(tokens);
1627 self.ty.to_tokens(tokens);
1628 self.eq_token.to_tokens(tokens);
1629 self.expr.to_tokens(tokens);
1630 self.semi_token.to_tokens(tokens);
1631 }
1632 }
1633
1634 impl ToTokens for ImplItemMethod {
1635 fn to_tokens(&self, tokens: &mut Tokens) {
1636 tokens.append_all(self.attrs.outer());
1637 self.vis.to_tokens(tokens);
1638 self.defaultness.to_tokens(tokens);
1639 self.sig.to_tokens(tokens);
1640 self.block.brace_token.surround(tokens, |tokens| {
1641 tokens.append_all(self.attrs.inner());
1642 tokens.append_all(&self.block.stmts);
1643 });
1644 }
1645 }
1646
1647 impl ToTokens for ImplItemType {
1648 fn to_tokens(&self, tokens: &mut Tokens) {
1649 tokens.append_all(self.attrs.outer());
1650 self.vis.to_tokens(tokens);
1651 self.defaultness.to_tokens(tokens);
1652 self.type_token.to_tokens(tokens);
1653 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001654 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001655 self.eq_token.to_tokens(tokens);
1656 self.ty.to_tokens(tokens);
1657 self.semi_token.to_tokens(tokens);
1658 }
1659 }
1660
1661 impl ToTokens for ImplItemMacro {
1662 fn to_tokens(&self, tokens: &mut Tokens) {
1663 tokens.append_all(self.attrs.outer());
1664 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05001665 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001666 }
1667 }
1668
David Tolnay8894f602017-11-11 12:11:04 -08001669 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001670 fn to_tokens(&self, tokens: &mut Tokens) {
1671 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001672 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001673 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1674 self.semi_token.to_tokens(tokens);
1675 }
1676 }
1677
1678 impl ToTokens for ForeignItemStatic {
1679 fn to_tokens(&self, tokens: &mut Tokens) {
1680 tokens.append_all(self.attrs.outer());
1681 self.vis.to_tokens(tokens);
1682 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001683 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001684 self.ident.to_tokens(tokens);
1685 self.colon_token.to_tokens(tokens);
1686 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001687 self.semi_token.to_tokens(tokens);
1688 }
1689 }
1690
David Tolnay199bcbb2017-11-12 10:33:52 -08001691 impl ToTokens for ForeignItemType {
1692 fn to_tokens(&self, tokens: &mut Tokens) {
1693 tokens.append_all(self.attrs.outer());
1694 self.vis.to_tokens(tokens);
1695 self.type_token.to_tokens(tokens);
1696 self.ident.to_tokens(tokens);
1697 self.semi_token.to_tokens(tokens);
1698 }
1699 }
1700
David Tolnay570695e2017-06-03 16:15:13 -07001701 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001702 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001703 self.constness.to_tokens(tokens);
1704 self.unsafety.to_tokens(tokens);
1705 self.abi.to_tokens(tokens);
1706 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001707 }
1708 }
1709
David Tolnay570695e2017-06-03 16:15:13 -07001710 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711
1712 impl<'a> ToTokens for NamedDecl<'a> {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 self.0.fn_token.to_tokens(tokens);
1715 self.1.to_tokens(tokens);
1716 self.0.generics.to_tokens(tokens);
1717 self.0.paren_token.surround(tokens, |tokens| {
1718 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05001719 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
1720 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001721 }
David Tolnayd2836e22017-12-27 23:13:00 -05001722 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001723 });
1724 self.0.output.to_tokens(tokens);
1725 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001726 }
1727 }
1728
Alex Crichton62a0a592017-05-22 13:58:53 -07001729 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001730 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001731 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001732 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001733 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001734 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001735 }
1736 }
1737
1738 impl ToTokens for ArgSelf {
1739 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05001740 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001741 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001742 }
1743 }
1744
1745 impl ToTokens for ArgCaptured {
1746 fn to_tokens(&self, tokens: &mut Tokens) {
1747 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001748 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001749 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001750 }
1751 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001752}