blob: ffbd606827174f00e09c0824a0b7030f4ba3c605 [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 Tolnaye303b7c2018-05-20 16:46:35 -070011use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070012use punctuated::Punctuated;
David Tolnay61037c62018-01-05 16:21:03 -080013use token::{Brace, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050014
15#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050016use std::hash::{Hash, Hasher};
David Tolnay94d2b792018-04-29 12:26:10 -070017#[cfg(feature = "extra-traits")]
18use tt::TokenStreamHelper;
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 Tolnay5f332a92017-12-26 00:42:45 -050052 pub tree: UseTree,
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
David Tolnay2b214082018-01-07 01:30:18 -080055
56 /// A static item: `static BIKE: Shed = Shed(42)`.
David Tolnay461d98e2018-01-07 11:07:19 -080057 ///
58 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070059 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080060 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070061 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -050063 pub mutability: Option<Token![mut]>,
David Tolnay570695e2017-06-03 16:15:13 -070064 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080066 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070068 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
David Tolnay2b214082018-01-07 01:30:18 -080071
72 /// A constant item: `const MAX: u16 = 65535`.
David Tolnay461d98e2018-01-07 11:07:19 -080073 ///
74 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub Const(ItemConst {
David Tolnayc6b55bc2017-11-09 22:48:38 -080076 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070077 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070079 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080081 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070083 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070085 }),
David Tolnay2b214082018-01-07 01:30:18 -080086
David Tolnay461d98e2018-01-07 11:07:19 -080087 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88 /// }`.
89 ///
90 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070091 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080092 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070093 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -050094 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -050095 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070096 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070097 pub ident: Ident,
David Tolnay4a3f59a2017-12-28 21:21:12 -050098 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub block: Box<Block>,
100 }),
David Tolnay2b214082018-01-07 01:30:18 -0800101
102 /// A module or module declaration: `mod m` or `mod m { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800103 ///
104 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800106 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700107 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500110 pub content: Option<(token::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
David Tolnay2b214082018-01-07 01:30:18 -0800113
114 /// A block of foreign items: `extern "C" { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800115 ///
116 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700117 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800118 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700119 pub abi: Abi,
David Tolnay32954ef2017-12-26 22:43:16 -0500120 pub brace_token: token::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121 pub items: Vec<ForeignItem>,
122 }),
David Tolnay2b214082018-01-07 01:30:18 -0800123
124 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800125 ///
126 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800127 pub Type(ItemType {
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 type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700131 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800134 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
David Tolnay2b214082018-01-07 01:30:18 -0800137
138 /// A struct definition: `struct Foo<A> { x: A }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800139 ///
140 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye3d41b72017-12-31 15:24:00 -0500141 pub Struct(ItemStruct {
142 pub attrs: Vec<Attribute>,
143 pub vis: Visibility,
144 pub struct_token: Token![struct],
145 pub ident: Ident,
146 pub generics: Generics,
147 pub fields: Fields,
148 pub semi_token: Option<Token![;]>,
149 }),
David Tolnay2b214082018-01-07 01:30:18 -0800150
151 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800152 ///
153 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700154 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800155 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700156 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700158 pub ident: Ident,
159 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500160 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500161 pub variants: Punctuated<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 }),
David Tolnay2b214082018-01-07 01:30:18 -0800163
164 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800165 ///
166 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800168 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700169 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800170 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700171 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub generics: Generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500173 pub fields: FieldsNamed,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
David Tolnay2b214082018-01-07 01:30:18 -0800175
176 /// A trait definition: `pub trait Iterator { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800177 ///
178 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800180 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700181 pub vis: Visibility,
David Tolnay9b258702017-12-29 02:24:41 -0500182 pub unsafety: Option<Token![unsafe]>,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500183 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800184 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700185 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500188 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500189 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 pub items: Vec<TraitItem>,
191 }),
David Tolnay2b214082018-01-07 01:30:18 -0800192
193 /// An impl block providing trait or associated items: `impl<A> Trait
194 /// for Data<A> { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800195 ///
196 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800198 pub attrs: Vec<Attribute>,
David Tolnay360a6342017-12-29 02:22:11 -0500199 pub defaultness: Option<Token![default]>,
David Tolnay9b258702017-12-29 02:24:41 -0500200 pub unsafety: Option<Token![unsafe]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700203 /// Trait this impl implements.
David Tolnay360a6342017-12-29 02:22:11 -0500204 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700205 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub self_ty: Box<Type>,
David Tolnay32954ef2017-12-26 22:43:16 -0500207 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700208 pub items: Vec<ImplItem>,
209 }),
David Tolnay2b214082018-01-07 01:30:18 -0800210
211 /// A macro invocation, which includes `macro_rules!` definitions.
David Tolnay461d98e2018-01-07 11:07:19 -0800212 ///
213 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800214 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800215 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800216 /// The `example` in `macro_rules! example { ... }`.
217 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800218 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500219 pub semi_token: Option<Token![;]>,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800220 }),
David Tolnay2b214082018-01-07 01:30:18 -0800221
222 /// A 2.0-style declarative macro introduced by the `macro` keyword.
David Tolnay461d98e2018-01-07 11:07:19 -0800223 ///
224 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9c76bcb2017-12-26 23:14:59 -0500225 pub Macro2(ItemMacro2 #manual_extra_traits {
David Tolnay500d8322017-12-18 00:32:51 -0800226 pub attrs: Vec<Attribute>,
227 pub vis: Visibility,
228 pub macro_token: Token![macro],
229 pub ident: Ident,
David Tolnayab919512017-12-30 23:31:51 -0500230 pub paren_token: Paren,
231 pub args: TokenStream,
232 pub brace_token: Brace,
233 pub body: TokenStream,
David Tolnay500d8322017-12-18 00:32:51 -0800234 }),
David Tolnay2b214082018-01-07 01:30:18 -0800235
236 /// Tokens forming an item not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800237 ///
238 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500239 pub Verbatim(ItemVerbatim #manual_extra_traits {
240 pub tts: TokenStream,
241 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 }
David Tolnayb79ee962016-09-04 09:39:20 -0700243}
244
David Tolnay9c76bcb2017-12-26 23:14:59 -0500245#[cfg(feature = "extra-traits")]
246impl Eq for ItemMacro2 {}
247
248#[cfg(feature = "extra-traits")]
249impl PartialEq for ItemMacro2 {
250 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -0700251 self.attrs == other.attrs
252 && self.vis == other.vis
253 && self.macro_token == other.macro_token
254 && self.ident == other.ident
255 && self.paren_token == other.paren_token
David Tolnayab919512017-12-30 23:31:51 -0500256 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
257 && self.brace_token == other.brace_token
258 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
David Tolnay9c76bcb2017-12-26 23:14:59 -0500259 }
260}
261
262#[cfg(feature = "extra-traits")]
263impl Hash for ItemMacro2 {
264 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -0500265 where
266 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -0500267 {
268 self.attrs.hash(state);
269 self.vis.hash(state);
270 self.macro_token.hash(state);
271 self.ident.hash(state);
David Tolnayab919512017-12-30 23:31:51 -0500272 self.paren_token.hash(state);
273 TokenStreamHelper(&self.args).hash(state);
274 self.brace_token.hash(state);
275 TokenStreamHelper(&self.body).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500276 }
277}
278
David Tolnay2ae520a2017-12-29 11:19:50 -0500279#[cfg(feature = "extra-traits")]
280impl Eq for ItemVerbatim {}
281
282#[cfg(feature = "extra-traits")]
283impl PartialEq for ItemVerbatim {
284 fn eq(&self, other: &Self) -> bool {
285 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
286 }
287}
288
289#[cfg(feature = "extra-traits")]
290impl Hash for ItemVerbatim {
291 fn hash<H>(&self, state: &mut H)
292 where
293 H: Hasher,
294 {
295 TokenStreamHelper(&self.tts).hash(state);
296 }
297}
298
David Tolnay0e837402016-12-22 17:25:55 -0500299impl From<DeriveInput> for Item {
300 fn from(input: DeriveInput) -> Item {
David Tolnaye3d41b72017-12-31 15:24:00 -0500301 match input.data {
302 Data::Struct(data) => Item::Struct(ItemStruct {
303 attrs: input.attrs,
304 vis: input.vis,
305 struct_token: data.struct_token,
306 ident: input.ident,
307 generics: input.generics,
308 fields: data.fields,
309 semi_token: data.semi_token,
310 }),
311 Data::Enum(data) => Item::Enum(ItemEnum {
David Tolnay51382052017-12-27 13:46:21 -0500312 attrs: input.attrs,
313 vis: input.vis,
314 enum_token: data.enum_token,
315 ident: input.ident,
316 generics: input.generics,
317 brace_token: data.brace_token,
318 variants: data.variants,
319 }),
David Tolnaye3d41b72017-12-31 15:24:00 -0500320 Data::Union(data) => Item::Union(ItemUnion {
David Tolnay51382052017-12-27 13:46:21 -0500321 attrs: input.attrs,
322 vis: input.vis,
David Tolnaye3d41b72017-12-31 15:24:00 -0500323 union_token: data.union_token,
David Tolnay51382052017-12-27 13:46:21 -0500324 ident: input.ident,
325 generics: input.generics,
David Tolnaye3d41b72017-12-31 15:24:00 -0500326 fields: data.fields,
David Tolnay51382052017-12-27 13:46:21 -0500327 }),
David Tolnay453cfd12016-10-23 11:00:14 -0700328 }
329 }
330}
331
Alex Crichton62a0a592017-05-22 13:58:53 -0700332ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800333 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
David Tolnay614a0142018-01-07 10:25:43 -0800334 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800335 /// *This type is available if Syn is built with the `"full"` feature.*
336 ///
David Tolnay614a0142018-01-07 10:25:43 -0800337 /// # Syntax tree enum
338 ///
339 /// This type is a [syntax tree enum].
340 ///
341 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay5f332a92017-12-26 00:42:45 -0500342 pub enum UseTree {
David Tolnayd97a7d22018-03-31 19:17:01 +0200343 /// A path prefix of imports in a `use` item: `std::...`.
David Tolnay461d98e2018-01-07 11:07:19 -0800344 ///
345 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500346 pub Path(UsePath {
347 pub ident: Ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200348 pub colon2_token: Token![::],
349 pub tree: Box<UseTree>,
350 }),
351
352 /// An identifier imported by a `use` item: `HashMap`.
353 ///
354 /// *This type is available if Syn is built with the `"full"` feature.*
355 pub Name(UseName {
356 pub ident: Ident,
357 }),
358
359 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
360 ///
361 /// *This type is available if Syn is built with the `"full"` feature.*
362 pub Rename(UseRename {
363 pub ident: Ident,
364 pub as_token: Token![as],
365 pub rename: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800367
David Tolnay05658502018-01-07 09:56:37 -0800368 /// A glob import in a `use` item: `*`.
David Tolnay461d98e2018-01-07 11:07:19 -0800369 ///
370 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay5f332a92017-12-26 00:42:45 -0500371 pub Glob(UseGlob {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800372 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800374
David Tolnayd97a7d22018-03-31 19:17:01 +0200375 /// A braced group of imports in a `use` item: `{A, B, C}`.
David Tolnay461d98e2018-01-07 11:07:19 -0800376 ///
377 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd97a7d22018-03-31 19:17:01 +0200378 pub Group(UseGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500379 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500380 pub items: Punctuated<UseTree, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
382 }
383}
384
Alex Crichton62a0a592017-05-22 13:58:53 -0700385ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800386 /// An item within an `extern` block.
David Tolnay614a0142018-01-07 10:25:43 -0800387 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800388 /// *This type is available if Syn is built with the `"full"` feature.*
389 ///
David Tolnay614a0142018-01-07 10:25:43 -0800390 /// # Syntax tree enum
391 ///
392 /// This type is a [syntax tree enum].
393 ///
394 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay8894f602017-11-11 12:11:04 -0800395 pub enum ForeignItem {
David Tolnayebb72722018-01-07 01:14:13 -0800396 /// A foreign function in an `extern` block.
David Tolnay461d98e2018-01-07 11:07:19 -0800397 ///
398 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800400 pub attrs: Vec<Attribute>,
401 pub vis: Visibility,
402 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800404 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }),
David Tolnayebb72722018-01-07 01:14:13 -0800406
407 /// A foreign static item in an `extern` block: `static ext: u8`.
David Tolnay461d98e2018-01-07 11:07:19 -0800408 ///
409 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700410 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800411 pub attrs: Vec<Attribute>,
412 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800413 pub static_token: Token![static],
David Tolnay24237fb2017-12-29 02:15:26 -0500414 pub mutability: Option<Token![mut]>,
David Tolnay8894f602017-11-11 12:11:04 -0800415 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800416 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800417 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800418 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 }),
David Tolnayebb72722018-01-07 01:14:13 -0800420
421 /// A foreign type in an `extern` block: `type void`.
David Tolnay461d98e2018-01-07 11:07:19 -0800422 ///
423 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay199bcbb2017-11-12 10:33:52 -0800424 pub Type(ForeignItemType {
425 pub attrs: Vec<Attribute>,
426 pub vis: Visibility,
427 pub type_token: Token![type],
428 pub ident: Ident,
429 pub semi_token: Token![;],
430 }),
David Tolnayebb72722018-01-07 01:14:13 -0800431
David Tolnay435c1782018-08-24 16:15:44 -0400432 /// A macro invocation within an extern block.
433 ///
434 /// *This type is available if Syn is built with the `"full"` feature.*
435 pub Macro(ForeignItemMacro {
436 pub attrs: Vec<Attribute>,
437 pub mac: Macro,
438 pub semi_token: Option<Token![;]>,
439 }),
440
David Tolnayebb72722018-01-07 01:14:13 -0800441 /// Tokens in an `extern` block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800442 ///
443 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500444 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
445 pub tts: TokenStream,
446 }),
447 }
448}
449
450#[cfg(feature = "extra-traits")]
451impl Eq for ForeignItemVerbatim {}
452
453#[cfg(feature = "extra-traits")]
454impl PartialEq for ForeignItemVerbatim {
455 fn eq(&self, other: &Self) -> bool {
456 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
457 }
458}
459
460#[cfg(feature = "extra-traits")]
461impl Hash for ForeignItemVerbatim {
462 fn hash<H>(&self, state: &mut H)
463 where
464 H: Hasher,
465 {
466 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700468}
469
David Tolnayda705bd2017-11-10 21:58:05 -0800470ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800471 /// An item declaration within the definition of a trait.
David Tolnay614a0142018-01-07 10:25:43 -0800472 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800473 /// *This type is available if Syn is built with the `"full"` feature.*
474 ///
David Tolnay614a0142018-01-07 10:25:43 -0800475 /// # Syntax tree enum
476 ///
477 /// This type is a [syntax tree enum].
478 ///
479 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayda705bd2017-11-10 21:58:05 -0800480 pub enum TraitItem {
David Tolnayebb72722018-01-07 01:14:13 -0800481 /// An associated constant within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800482 ///
483 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800485 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800486 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700487 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800488 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800489 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800490 pub default: Option<(Token![=], Expr)>,
491 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700492 }),
David Tolnayebb72722018-01-07 01:14:13 -0800493
494 /// A trait method within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800495 ///
496 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800498 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700499 pub sig: MethodSig,
500 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800501 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700502 }),
David Tolnayebb72722018-01-07 01:14:13 -0800503
504 /// An associated type within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800505 ///
506 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800508 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700510 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500511 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800512 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500513 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800514 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 }),
David Tolnayebb72722018-01-07 01:14:13 -0800517
518 /// A macro invocation within the definition of a trait.
David Tolnay461d98e2018-01-07 11:07:19 -0800519 ///
520 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaydecf28d2017-11-11 11:56:45 -0800521 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800522 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800523 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500524 pub semi_token: Option<Token![;]>,
David Tolnayda705bd2017-11-10 21:58:05 -0800525 }),
David Tolnayebb72722018-01-07 01:14:13 -0800526
527 /// Tokens within the definition of a trait not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800528 ///
529 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500530 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
531 pub tts: TokenStream,
532 }),
533 }
534}
535
536#[cfg(feature = "extra-traits")]
537impl Eq for TraitItemVerbatim {}
538
539#[cfg(feature = "extra-traits")]
540impl PartialEq for TraitItemVerbatim {
541 fn eq(&self, other: &Self) -> bool {
542 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
543 }
544}
545
546#[cfg(feature = "extra-traits")]
547impl Hash for TraitItemVerbatim {
548 fn hash<H>(&self, state: &mut H)
549 where
550 H: Hasher,
551 {
552 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700553 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700554}
555
Alex Crichton62a0a592017-05-22 13:58:53 -0700556ast_enum_of_structs! {
David Tolnayebb72722018-01-07 01:14:13 -0800557 /// An item within an impl block.
David Tolnay614a0142018-01-07 10:25:43 -0800558 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800559 /// *This type is available if Syn is built with the `"full"` feature.*
560 ///
David Tolnay614a0142018-01-07 10:25:43 -0800561 /// # Syntax tree enum
562 ///
563 /// This type is a [syntax tree enum].
564 ///
565 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnay857628c2017-11-11 12:25:31 -0800566 pub enum ImplItem {
David Tolnayebb72722018-01-07 01:14:13 -0800567 /// An associated constant within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800568 ///
569 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700570 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800571 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700572 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500573 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800574 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700575 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800577 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800578 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700579 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800580 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700581 }),
David Tolnayebb72722018-01-07 01:14:13 -0800582
583 /// A method within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800584 ///
585 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800587 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700588 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500589 pub defaultness: Option<Token![default]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700590 pub sig: MethodSig,
591 pub block: Block,
592 }),
David Tolnayebb72722018-01-07 01:14:13 -0800593
594 /// An associated type within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800595 ///
596 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800598 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700599 pub vis: Visibility,
David Tolnay360a6342017-12-29 02:22:11 -0500600 pub defaultness: Option<Token![default]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800601 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700602 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500603 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800604 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800605 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700607 }),
David Tolnayebb72722018-01-07 01:14:13 -0800608
609 /// A macro invocation within an impl block.
David Tolnay461d98e2018-01-07 11:07:19 -0800610 ///
611 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay857628c2017-11-11 12:25:31 -0800612 pub Macro(ImplItemMacro {
613 pub attrs: Vec<Attribute>,
614 pub mac: Macro,
David Tolnay57292da2017-12-27 21:03:33 -0500615 pub semi_token: Option<Token![;]>,
David Tolnay857628c2017-11-11 12:25:31 -0800616 }),
David Tolnayebb72722018-01-07 01:14:13 -0800617
618 /// Tokens within an impl block not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800619 ///
620 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500621 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
622 pub tts: TokenStream,
623 }),
624 }
625}
626
627#[cfg(feature = "extra-traits")]
628impl Eq for ImplItemVerbatim {}
629
630#[cfg(feature = "extra-traits")]
631impl PartialEq for ImplItemVerbatim {
632 fn eq(&self, other: &Self) -> bool {
633 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
634 }
635}
636
637#[cfg(feature = "extra-traits")]
638impl Hash for ImplItemVerbatim {
639 fn hash<H>(&self, state: &mut H)
640 where
641 H: Hasher,
642 {
643 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700645}
646
647ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800648 /// A method's signature in a trait or implementation: `unsafe fn
649 /// initialize(&self)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800650 ///
651 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 pub struct MethodSig {
David Tolnay360a6342017-12-29 02:22:11 -0500653 pub constness: Option<Token![const]>,
David Tolnay9b258702017-12-29 02:24:41 -0500654 pub unsafety: Option<Token![unsafe]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700656 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700658 }
659}
660
661ast_struct! {
David Tolnayebb72722018-01-07 01:14:13 -0800662 /// Header of a function declaration, without including the body.
David Tolnay461d98e2018-01-07 11:07:19 -0800663 ///
664 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700665 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666 pub fn_token: Token![fn],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500667 pub generics: Generics,
David Tolnay32954ef2017-12-26 22:43:16 -0500668 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500669 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayd2836e22017-12-27 23:13:00 -0500670 pub variadic: Option<Token![...]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500671 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700673}
674
Alex Crichton62a0a592017-05-22 13:58:53 -0700675ast_enum_of_structs! {
David Tolnayc0435192018-01-07 11:46:08 -0800676 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
David Tolnay614a0142018-01-07 10:25:43 -0800677 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800678 /// *This type is available if Syn is built with the `"full"` feature.*
679 ///
David Tolnay614a0142018-01-07 10:25:43 -0800680 /// # Syntax tree enum
681 ///
682 /// This type is a [syntax tree enum].
683 ///
684 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700685 pub enum FnArg {
David Tolnay3f559052018-01-06 23:59:48 -0800686 /// Self captured by reference in a function signature: `&self` or `&mut
687 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800688 ///
689 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800691 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 pub lifetime: Option<Lifetime>,
David Tolnay24237fb2017-12-29 02:15:26 -0500693 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500694 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800696
David Tolnay3f559052018-01-06 23:59:48 -0800697 /// Self captured by value in a function signature: `self` or `mut
698 /// self`.
David Tolnay461d98e2018-01-07 11:07:19 -0800699 ///
700 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 pub SelfValue(ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -0500702 pub mutability: Option<Token![mut]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800703 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800705
David Tolnay3f559052018-01-06 23:59:48 -0800706 /// An explicitly typed pattern captured by a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800707 ///
708 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 pub Captured(ArgCaptured {
710 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800711 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800712 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 }),
David Tolnay461d98e2018-01-07 11:07:19 -0800714
David Tolnay3f559052018-01-06 23:59:48 -0800715 /// A pattern whose type is inferred captured by a function signature.
David Tolnay80ed55f2017-12-27 22:54:40 -0500716 pub Inferred(Pat),
David Tolnay3f559052018-01-06 23:59:48 -0800717 /// A type not bound to any pattern in a function signature.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800718 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700719 }
David Tolnay62f374c2016-10-02 13:37:00 -0700720}
721
David Tolnayedf2b992016-09-23 20:43:45 -0700722#[cfg(feature = "parsing")]
723pub mod parsing {
724 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700725
David Tolnay9be32582018-07-31 22:37:26 -0700726 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700727
David Tolnay4c614be2017-11-10 00:02:38 -0800728 impl_synom!(Item "item" alt!(
729 syn!(ItemExternCrate) => { Item::ExternCrate }
730 |
731 syn!(ItemUse) => { Item::Use }
732 |
733 syn!(ItemStatic) => { Item::Static }
734 |
735 syn!(ItemConst) => { Item::Const }
736 |
737 syn!(ItemFn) => { Item::Fn }
738 |
Yusuke Sasaki2dec3152018-07-31 20:41:50 +0900739 call!(unstable_async_fn) => { Item::Verbatim }
740 |
David Tolnay4c614be2017-11-10 00:02:38 -0800741 syn!(ItemMod) => { Item::Mod }
742 |
743 syn!(ItemForeignMod) => { Item::ForeignMod }
744 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800745 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800746 |
David Tolnay758ee132018-08-21 21:29:40 -0400747 call!(unstable_existential_type) => { Item::Verbatim }
748 |
David Tolnay4c614be2017-11-10 00:02:38 -0800749 syn!(ItemStruct) => { Item::Struct }
750 |
751 syn!(ItemEnum) => { Item::Enum }
752 |
753 syn!(ItemUnion) => { Item::Union }
754 |
755 syn!(ItemTrait) => { Item::Trait }
756 |
David Tolnay4c614be2017-11-10 00:02:38 -0800757 syn!(ItemImpl) => { Item::Impl }
758 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800759 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800760 |
761 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800762 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700763
David Tolnaydecf28d2017-11-11 11:56:45 -0800764 impl_synom!(ItemMacro "macro item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500765 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -0800766 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700768 ident: option!(syn!(Ident)) >>
David Tolnaye0824032017-12-27 15:25:56 -0500769 body: call!(tt::delimited) >>
David Tolnayab919512017-12-30 23:31:51 -0500770 semi: cond!(!is_brace(&body.0), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800771 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700772 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800773 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800774 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500775 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700776 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500777 delimiter: body.0,
778 tts: body.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800779 },
David Tolnay57292da2017-12-27 21:03:33 -0500780 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800781 })
David Tolnayedf2b992016-09-23 20:43:45 -0700782 ));
783
David Tolnay500d8322017-12-18 00:32:51 -0800784 // TODO: figure out the actual grammar; is body required to be braced?
785 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500786 attrs: many0!(Attribute::parse_outer) >>
David Tolnay500d8322017-12-18 00:32:51 -0800787 vis: syn!(Visibility) >>
788 macro_: keyword!(macro) >>
789 ident: syn!(Ident) >>
David Tolnaye0824032017-12-27 15:25:56 -0500790 args: call!(tt::parenthesized) >>
791 body: call!(tt::braced) >>
David Tolnay500d8322017-12-18 00:32:51 -0800792 (ItemMacro2 {
793 attrs: attrs,
794 vis: vis,
795 macro_token: macro_,
796 ident: ident,
David Tolnayab919512017-12-30 23:31:51 -0500797 paren_token: args.0,
798 args: args.1,
799 brace_token: body.0,
800 body: body.1,
David Tolnay500d8322017-12-18 00:32:51 -0800801 })
802 ));
803
David Tolnay4c614be2017-11-10 00:02:38 -0800804 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500805 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700806 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800807 extern_: keyword!(extern) >>
808 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700809 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800810 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
811 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800812 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700813 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800814 vis: vis,
815 extern_token: extern_,
816 crate_token: crate_,
817 ident: ident,
818 rename: rename,
819 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800820 })
David Tolnayedf2b992016-09-23 20:43:45 -0700821 ));
822
David Tolnay4c614be2017-11-10 00:02:38 -0800823 impl_synom!(ItemUse "use item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500824 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700825 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800826 use_: keyword!(use) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500827 leading_colon: option!(punct!(::)) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200828 tree: syn!(UseTree) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800829 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800830 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700831 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800832 vis: vis,
833 use_token: use_,
David Tolnay5f332a92017-12-26 00:42:45 -0500834 leading_colon: leading_colon,
David Tolnay5f332a92017-12-26 00:42:45 -0500835 tree: tree,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800836 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800837 })
David Tolnay4a057422016-10-08 00:02:31 -0700838 ));
839
David Tolnayd97a7d22018-03-31 19:17:01 +0200840 named!(use_element -> Ident, alt!(
David Tolnay5f332a92017-12-26 00:42:45 -0500841 syn!(Ident)
842 |
843 keyword!(self) => { Into::into }
844 |
845 keyword!(super) => { Into::into }
846 |
847 keyword!(crate) => { Into::into }
David Tolnay0a4d4e92018-07-21 15:31:45 -0700848 |
849 keyword!(extern) => { Into::into }
David Tolnay5f332a92017-12-26 00:42:45 -0500850 ));
851
852 impl_synom!(UseTree "use tree" alt!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200853 syn!(UseRename) => { UseTree::Rename }
854 |
David Tolnay5f332a92017-12-26 00:42:45 -0500855 syn!(UsePath) => { UseTree::Path }
856 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200857 syn!(UseName) => { UseTree::Name }
858 |
David Tolnay5f332a92017-12-26 00:42:45 -0500859 syn!(UseGlob) => { UseTree::Glob }
860 |
David Tolnayd97a7d22018-03-31 19:17:01 +0200861 syn!(UseGroup) => { UseTree::Group }
David Tolnay5f332a92017-12-26 00:42:45 -0500862 ));
863
864 impl_synom!(UsePath "use path" do_parse!(
David Tolnayd97a7d22018-03-31 19:17:01 +0200865 ident: call!(use_element) >>
866 colon2_token: punct!(::) >>
867 tree: syn!(UseTree) >>
David Tolnay5f332a92017-12-26 00:42:45 -0500868 (UsePath {
869 ident: ident,
David Tolnayd97a7d22018-03-31 19:17:01 +0200870 colon2_token: colon2_token,
871 tree: Box::new(tree),
872 })
873 ));
874
875 impl_synom!(UseName "use name" do_parse!(
876 ident: call!(use_element) >>
877 (UseName {
878 ident: ident,
879 })
880 ));
881
882 impl_synom!(UseRename "use rename" do_parse!(
883 ident: call!(use_element) >>
884 as_token: keyword!(as) >>
885 rename: syn!(Ident) >>
886 (UseRename {
887 ident: ident,
888 as_token: as_token,
David Tolnay5f332a92017-12-26 00:42:45 -0500889 rename: rename,
890 })
891 ));
David Tolnay4a057422016-10-08 00:02:31 -0700892
David Tolnay5f332a92017-12-26 00:42:45 -0500893 impl_synom!(UseGlob "use glob" do_parse!(
894 star: punct!(*) >>
895 (UseGlob {
896 star_token: star,
897 })
898 ));
David Tolnay4a057422016-10-08 00:02:31 -0700899
David Tolnayd97a7d22018-03-31 19:17:01 +0200900 impl_synom!(UseGroup "use group" do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500901 list: braces!(Punctuated::parse_terminated) >>
David Tolnayd97a7d22018-03-31 19:17:01 +0200902 (UseGroup {
David Tolnay8875fca2017-12-31 13:52:37 -0500903 brace_token: list.0,
904 items: list.1,
David Tolnay5f332a92017-12-26 00:42:45 -0500905 })
906 ));
David Tolnay4a057422016-10-08 00:02:31 -0700907
David Tolnay4c614be2017-11-10 00:02:38 -0800908 impl_synom!(ItemStatic "static item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500909 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700910 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800911 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -0500912 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -0700913 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800914 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800915 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800916 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700917 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800918 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800919 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700920 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800921 vis: vis,
922 static_token: static_,
David Tolnay24237fb2017-12-29 02:15:26 -0500923 mutability: mutability,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800924 ident: ident,
925 colon_token: colon,
926 ty: Box::new(ty),
927 eq_token: eq,
928 expr: Box::new(value),
929 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800930 })
David Tolnay47a877c2016-10-01 16:50:55 -0700931 ));
932
David Tolnay4c614be2017-11-10 00:02:38 -0800933 impl_synom!(ItemConst "const 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 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700937 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800938 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800939 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800940 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700941 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800942 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800943 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700944 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800945 vis: vis,
946 const_token: const_,
947 ident: ident,
948 colon_token: colon,
949 ty: Box::new(ty),
950 eq_token: eq,
951 expr: Box::new(value),
952 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800953 })
David Tolnay47a877c2016-10-01 16:50:55 -0700954 ));
955
David Tolnay4c614be2017-11-10 00:02:38 -0800956 impl_synom!(ItemFn "fn item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -0500957 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700958 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -0500959 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -0500960 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700961 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700963 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700964 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -0500965 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800966 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -0500967 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700968 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -0500969 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -0700970 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -0400971 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800972 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700973 attrs: {
974 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -0500975 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700976 attrs
977 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800978 vis: vis,
979 constness: constness,
980 unsafety: unsafety,
981 abi: abi,
982 decl: Box::new(FnDecl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800983 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -0500984 paren_token: inputs.0,
985 inputs: inputs.1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800986 output: ret,
David Tolnayd2836e22017-12-27 23:13:00 -0500987 variadic: None,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800988 generics: Generics {
989 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -0700990 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -0800991 },
992 }),
993 ident: ident,
994 block: Box::new(Block {
David Tolnay8875fca2017-12-31 13:52:37 -0500995 brace_token: inner_attrs_stmts.0,
996 stmts: (inner_attrs_stmts.1).1,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800997 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800998 })
David Tolnay42602292016-10-01 22:25:45 -0700999 ));
1000
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001001 named!(unstable_async_fn -> ItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07001002 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07001003 many0!(Attribute::parse_outer) >>
1004 syn!(Visibility) >>
1005 option!(keyword!(const)) >>
1006 option!(keyword!(unsafe)) >>
1007 keyword!(async) >>
1008 option!(syn!(Abi)) >>
1009 keyword!(fn) >>
1010 syn!(Ident) >>
1011 syn!(Generics) >>
1012 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1013 syn!(ReturnType) >>
1014 option!(syn!(WhereClause)) >>
1015 braces!(tuple!(
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001016 many0!(Attribute::parse_inner),
1017 call!(Block::parse_within),
1018 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001019 end: call!(verbatim::grab_cursor) >>
1020 (ItemVerbatim {
1021 tts: verbatim::token_range(begin..end),
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09001022 })
1023 ));
1024
Alex Crichton954046c2017-05-30 21:49:42 -07001025 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -04001026 named!(parse -> Self, alt!(
1027 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001028 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001029 lt: option!(syn!(Lifetime)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001030 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 self_: keyword!(self) >>
1032 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001033 (ArgSelfRef {
1034 lifetime: lt,
David Tolnay24237fb2017-12-29 02:15:26 -05001035 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001036 and_token: and,
1037 self_token: self_,
1038 }.into())
1039 )
1040 |
1041 do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05001042 mutability: option!(keyword!(mut)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001043 self_: keyword!(self) >>
1044 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001045 (ArgSelf {
David Tolnay24237fb2017-12-29 02:15:26 -05001046 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04001047 self_token: self_,
1048 }.into())
1049 )
1050 |
1051 do_parse!(
1052 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001053 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001054 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001055 (ArgCaptured {
1056 pat: pat,
1057 ty: ty,
1058 colon_token: colon,
1059 }.into())
1060 )
1061 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001062 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -04001063 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001064
1065 fn description() -> Option<&'static str> {
1066 Some("function argument")
1067 }
Alex Crichton954046c2017-05-30 21:49:42 -07001068 }
David Tolnay62f374c2016-10-02 13:37:00 -07001069
David Tolnay4c614be2017-11-10 00:02:38 -08001070 impl_synom!(ItemMod "mod item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001071 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001072 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001073 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -07001074 ident: syn!(Ident) >>
1075 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001076 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -07001077 Vec::new(),
1078 None,
1079 Some(semi),
1080 )}
David Tolnay37d10332016-10-13 20:51:04 -07001081 |
Alex Crichton954046c2017-05-30 21:49:42 -07001082 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -07001083 tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001084 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001085 many0!(Item::parse),
Michael Layzell416724e2017-05-24 21:12:34 -04001086 )
David Tolnay8875fca2017-12-31 13:52:37 -05001087 ) => {|(brace, (inner_attrs, items))| (
David Tolnay570695e2017-06-03 16:15:13 -07001088 inner_attrs,
1089 Some((brace, items)),
1090 None,
1091 )}
David Tolnay37d10332016-10-13 20:51:04 -07001092 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001093 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -07001094 attrs: {
1095 let mut attrs = outer_attrs;
1096 attrs.extend(content_semi.0);
1097 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -07001098 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001099 vis: vis,
1100 mod_token: mod_,
1101 ident: ident,
1102 content: content_semi.1,
1103 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -08001104 })
David Tolnay35902302016-10-06 01:11:08 -07001105 ));
1106
David Tolnay4c614be2017-11-10 00:02:38 -08001107 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
David Tolnay5c4613a2018-07-21 15:40:17 -07001108 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001109 abi: syn!(Abi) >>
David Tolnay5c4613a2018-07-21 15:40:17 -07001110 braced_content: braces!(tuple!(
1111 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001112 many0!(ForeignItem::parse),
David Tolnay5c4613a2018-07-21 15:40:17 -07001113 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001114 (ItemForeignMod {
David Tolnay5c4613a2018-07-21 15:40:17 -07001115 attrs: {
1116 let mut attrs = outer_attrs;
1117 attrs.extend((braced_content.1).0);
1118 attrs
1119 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001120 abi: abi,
David Tolnay5c4613a2018-07-21 15:40:17 -07001121 brace_token: braced_content.0,
1122 items: (braced_content.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001123 })
David Tolnay35902302016-10-06 01:11:08 -07001124 ));
1125
David Tolnay8894f602017-11-11 12:11:04 -08001126 impl_synom!(ForeignItem "foreign item" alt!(
1127 syn!(ForeignItemFn) => { ForeignItem::Fn }
1128 |
1129 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -08001130 |
1131 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay78572112018-08-01 00:36:18 -07001132 |
David Tolnay435c1782018-08-24 16:15:44 -04001133 syn!(ForeignItemMacro) => { ForeignItem::Macro }
David Tolnay8894f602017-11-11 12:11:04 -08001134 ));
David Tolnay35902302016-10-06 01:11:08 -07001135
David Tolnay8894f602017-11-11 12:11:04 -08001136 impl_synom!(ForeignItemFn "foreign function" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001137 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001138 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001139 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001140 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001141 generics: syn!(Generics) >>
1142 inputs: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001143 args: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001144 variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001145 (args, variadic)
1146 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001147 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001148 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001149 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001150 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001151 let (parens, (inputs, variadic)) = inputs;
David Tolnay8894f602017-11-11 12:11:04 -08001152 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -07001153 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -07001154 attrs: attrs,
1155 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001156 decl: Box::new(FnDecl {
1157 fn_token: fn_,
1158 paren_token: parens,
1159 inputs: inputs,
David Tolnayd2836e22017-12-27 23:13:00 -05001160 variadic: variadic,
David Tolnay8894f602017-11-11 12:11:04 -08001161 output: ret,
1162 generics: Generics {
1163 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001164 ..generics
David Tolnay8894f602017-11-11 12:11:04 -08001165 },
1166 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001167 vis: vis,
1168 }
David Tolnay35902302016-10-06 01:11:08 -07001169 })
1170 ));
1171
David Tolnay8894f602017-11-11 12:11:04 -08001172 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001173 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001174 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001175 static_: keyword!(static) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001176 mutability: option!(keyword!(mut)) >>
David Tolnay570695e2017-06-03 16:15:13 -07001177 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001178 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001179 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001180 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -08001181 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -07001182 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -07001183 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001184 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -08001185 ty: Box::new(ty),
David Tolnay24237fb2017-12-29 02:15:26 -05001186 mutability: mutability,
David Tolnay8894f602017-11-11 12:11:04 -08001187 static_token: static_,
1188 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -07001189 vis: vis,
1190 })
1191 ));
1192
David Tolnay199bcbb2017-11-12 10:33:52 -08001193 impl_synom!(ForeignItemType "foreign type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001194 attrs: many0!(Attribute::parse_outer) >>
David Tolnay199bcbb2017-11-12 10:33:52 -08001195 vis: syn!(Visibility) >>
1196 type_: keyword!(type) >>
1197 ident: syn!(Ident) >>
1198 semi: punct!(;) >>
1199 (ForeignItemType {
1200 attrs: attrs,
1201 vis: vis,
1202 type_token: type_,
1203 ident: ident,
1204 semi_token: semi,
1205 })
1206 ));
1207
David Tolnay435c1782018-08-24 16:15:44 -04001208 impl_synom!(ForeignItemMacro "macro in extern block" do_parse!(
1209 attrs: many0!(Attribute::parse_outer) >>
David Tolnay78572112018-08-01 00:36:18 -07001210 mac: syn!(Macro) >>
David Tolnay435c1782018-08-24 16:15:44 -04001211 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1212 (ForeignItemMacro {
1213 attrs: attrs,
1214 mac: mac,
1215 semi_token: semi,
David Tolnay78572112018-08-01 00:36:18 -07001216 })
1217 ));
1218
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001219 impl_synom!(ItemType "type item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001220 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001221 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001222 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001223 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001224 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001225 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001226 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001227 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001228 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001229 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -07001230 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001231 vis: vis,
1232 type_token: type_,
1233 ident: ident,
1234 generics: Generics {
1235 where_clause: where_clause,
1236 ..generics
1237 },
1238 eq_token: eq,
1239 ty: Box::new(ty),
1240 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -08001241 })
David Tolnay3cf52982016-10-01 17:11:37 -07001242 ));
1243
David Tolnay758ee132018-08-21 21:29:40 -04001244 named!(existential_type_helper(vis: bool) -> TokenStream, do_parse!(
1245 begin: call!(verbatim::grab_cursor) >>
1246 many0!(Attribute::parse_outer) >>
1247 cond_reduce!(vis, syn!(Visibility)) >>
1248 custom_keyword!(existential) >>
1249 keyword!(type) >>
1250 syn!(Ident) >>
1251 syn!(Generics) >>
1252 option!(syn!(WhereClause)) >>
1253 colon: option!(punct!(:)) >>
1254 cond!(
1255 colon.is_some(),
1256 Punctuated::<TypeParamBound, Token![+]>::parse_separated_nonempty
1257 ) >>
1258 punct!(;) >>
1259 end: call!(verbatim::grab_cursor) >>
1260 (verbatim::token_range(begin..end))
1261 ));
1262
1263 named!(unstable_existential_type -> ItemVerbatim, map!(
1264 call!(existential_type_helper, true),
1265 |tts| ItemVerbatim { tts: tts }
1266 ));
1267
David Tolnay4c614be2017-11-10 00:02:38 -08001268 impl_synom!(ItemStruct "struct item" switch!(
1269 map!(syn!(DeriveInput), Into::into),
1270 Item::Struct(item) => value!(item)
1271 |
1272 _ => reject!()
1273 ));
David Tolnay42602292016-10-01 22:25:45 -07001274
David Tolnay4c614be2017-11-10 00:02:38 -08001275 impl_synom!(ItemEnum "enum item" switch!(
1276 map!(syn!(DeriveInput), Into::into),
1277 Item::Enum(item) => value!(item)
1278 |
1279 _ => reject!()
1280 ));
1281
1282 impl_synom!(ItemUnion "union item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001283 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001284 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001285 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -07001286 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001287 generics: syn!(Generics) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001288 where_clause: option!(syn!(WhereClause)) >>
David Tolnaye3d41b72017-12-31 15:24:00 -05001289 fields: syn!(FieldsNamed) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001290 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -07001291 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001292 vis: vis,
1293 union_token: union_,
1294 ident: ident,
1295 generics: Generics {
1296 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001297 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001298 },
David Tolnaye3d41b72017-12-31 15:24:00 -05001299 fields: fields,
David Tolnay4c614be2017-11-10 00:02:38 -08001300 })
David Tolnay2f9fa632016-10-03 22:08:48 -07001301 ));
1302
David Tolnay4c614be2017-11-10 00:02:38 -08001303 impl_synom!(ItemTrait "trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001304 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001305 vis: syn!(Visibility) >>
David Tolnay9b258702017-12-29 02:24:41 -05001306 unsafety: option!(keyword!(unsafe)) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001307 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001309 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001310 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001311 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001312 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001313 where_clause: option!(syn!(WhereClause)) >>
David Tolnay2c136452017-12-27 14:13:32 -05001314 body: braces!(many0!(TraitItem::parse)) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001315 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001316 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001317 vis: vis,
1318 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001319 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001320 trait_token: trait_,
1321 ident: ident,
1322 generics: Generics {
1323 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001324 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001325 },
1326 colon_token: colon,
1327 supertraits: bounds.unwrap_or_default(),
David Tolnay8875fca2017-12-31 13:52:37 -05001328 brace_token: body.0,
1329 items: body.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001330 })
David Tolnay0aecb732016-10-03 23:03:50 -07001331 ));
1332
David Tolnayda705bd2017-11-10 21:58:05 -08001333 impl_synom!(TraitItem "trait item" alt!(
1334 syn!(TraitItemConst) => { TraitItem::Const }
1335 |
1336 syn!(TraitItemMethod) => { TraitItem::Method }
1337 |
1338 syn!(TraitItemType) => { TraitItem::Type }
1339 |
David Tolnay758ee132018-08-21 21:29:40 -04001340 call!(unstable_trait_existential_type) => { TraitItem::Verbatim }
1341 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001342 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001343 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001344
David Tolnayda705bd2017-11-10 21:58:05 -08001345 impl_synom!(TraitItemConst "const trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001346 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001348 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001349 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001350 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001351 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1352 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001353 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001354 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001355 const_token: const_,
1356 ident: ident,
1357 colon_token: colon,
1358 ty: ty,
1359 default: default,
1360 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001361 })
1362 ));
1363
David Tolnayda705bd2017-11-10 21:58:05 -08001364 impl_synom!(TraitItemMethod "method trait item" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001365 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001366 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001367 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001368 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001369 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001370 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001371 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001372 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001373 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001374 where_clause: option!(syn!(WhereClause)) >>
David Tolnay76178be2018-07-31 23:06:15 -07001375 body: option!(braces!(tuple!(
1376 many0!(Attribute::parse_inner),
1377 call!(Block::parse_within),
1378 ))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001379 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001380 ({
1381 let (inner_attrs, stmts) = match body {
David Tolnay8875fca2017-12-31 13:52:37 -05001382 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001383 None => (Vec::new(), None),
1384 };
David Tolnayda705bd2017-11-10 21:58:05 -08001385 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001386 attrs: {
1387 let mut attrs = outer_attrs;
1388 attrs.extend(inner_attrs);
1389 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001390 },
David Tolnayda705bd2017-11-10 21:58:05 -08001391 sig: MethodSig {
1392 constness: constness,
1393 unsafety: unsafety,
1394 abi: abi,
1395 ident: ident,
1396 decl: FnDecl {
David Tolnay8875fca2017-12-31 13:52:37 -05001397 inputs: inputs.1,
David Tolnayda705bd2017-11-10 21:58:05 -08001398 output: ret,
David Tolnayda705bd2017-11-10 21:58:05 -08001399 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001400 paren_token: inputs.0,
David Tolnayd2836e22017-12-27 23:13:00 -05001401 variadic: None,
David Tolnayda705bd2017-11-10 21:58:05 -08001402 generics: Generics {
1403 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001404 ..generics
David Tolnay5859df12016-10-29 22:49:54 -07001405 },
1406 },
David Tolnayda705bd2017-11-10 21:58:05 -08001407 },
1408 default: stmts.map(|stmts| {
1409 Block {
1410 stmts: stmts.0,
1411 brace_token: stmts.1,
1412 }
1413 }),
1414 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001415 }
David Tolnay0aecb732016-10-03 23:03:50 -07001416 })
1417 ));
1418
David Tolnayda705bd2017-11-10 21:58:05 -08001419 impl_synom!(TraitItemType "trait item type" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001420 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001421 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001422 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001423 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001424 colon: option!(punct!(:)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001425 bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001426 where_clause: option!(syn!(WhereClause)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001427 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001428 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001429 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001430 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001431 type_token: type_,
1432 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001433 generics: Generics {
1434 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001435 ..generics
Nika Layzell0183ca32017-12-05 15:24:01 -05001436 },
David Tolnayda705bd2017-11-10 21:58:05 -08001437 colon_token: colon,
1438 bounds: bounds.unwrap_or_default(),
1439 default: default,
1440 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001441 })
1442 ));
1443
David Tolnay758ee132018-08-21 21:29:40 -04001444 named!(unstable_trait_existential_type -> TraitItemVerbatim, map!(
1445 call!(existential_type_helper, false),
1446 |tts| TraitItemVerbatim { tts: tts }
1447 ));
1448
David Tolnaydecf28d2017-11-11 11:56:45 -08001449 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001450 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001451 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001452 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001453 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001454 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001455 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001456 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001457 })
1458 ));
1459
David Tolnay4c614be2017-11-10 00:02:38 -08001460 impl_synom!(ItemImpl "impl item" do_parse!(
David Tolnaycf3697a2018-03-31 20:51:15 +02001461 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnay360a6342017-12-29 02:22:11 -05001462 defaultness: option!(keyword!(default)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001463 unsafety: option!(keyword!(unsafe)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001464 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001465 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001466 polarity_path: alt!(
1467 do_parse!(
David Tolnay360a6342017-12-29 02:22:11 -05001468 polarity: option!(punct!(!)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001469 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001470 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001471 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001472 )
1473 |
David Tolnay570695e2017-06-03 16:15:13 -07001474 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001475 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001476 self_ty: syn!(Type) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001477 where_clause: option!(syn!(WhereClause)) >>
David Tolnaycf3697a2018-03-31 20:51:15 +02001478 inner: braces!(tuple!(
1479 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001480 many0!(ImplItem::parse),
David Tolnaycf3697a2018-03-31 20:51:15 +02001481 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001482 (ItemImpl {
David Tolnaycf3697a2018-03-31 20:51:15 +02001483 attrs: {
1484 let mut attrs = outer_attrs;
1485 attrs.extend((inner.1).0);
1486 attrs
1487 },
David Tolnayc6b55bc2017-11-09 22:48:38 -08001488 defaultness: defaultness,
1489 unsafety: unsafety,
1490 impl_token: impl_,
1491 generics: Generics {
1492 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001493 ..generics
David Tolnayc6b55bc2017-11-09 22:48:38 -08001494 },
1495 trait_: polarity_path,
1496 self_ty: Box::new(self_ty),
David Tolnaycf3697a2018-03-31 20:51:15 +02001497 brace_token: inner.0,
1498 items: (inner.1).1,
David Tolnay4c614be2017-11-10 00:02:38 -08001499 })
David Tolnay4c9be372016-10-06 00:47:37 -07001500 ));
1501
David Tolnay857628c2017-11-11 12:25:31 -08001502 impl_synom!(ImplItem "item in impl block" alt!(
1503 syn!(ImplItemConst) => { ImplItem::Const }
1504 |
1505 syn!(ImplItemMethod) => { ImplItem::Method }
1506 |
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001507 call!(unstable_async_method) => { ImplItem::Verbatim }
1508 |
David Tolnay857628c2017-11-11 12:25:31 -08001509 syn!(ImplItemType) => { ImplItem::Type }
1510 |
David Tolnay758ee132018-08-21 21:29:40 -04001511 call!(unstable_impl_existential_type) => { ImplItem::Verbatim }
1512 |
David Tolnay857628c2017-11-11 12:25:31 -08001513 syn!(ImplItemMacro) => { ImplItem::Macro }
1514 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001515
David Tolnay857628c2017-11-11 12:25:31 -08001516 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001517 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001518 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001519 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001520 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001521 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001522 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001523 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001524 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001525 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001527 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001528 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001529 vis: vis,
1530 defaultness: defaultness,
1531 const_token: const_,
1532 ident: ident,
1533 colon_token: colon,
1534 ty: ty,
1535 eq_token: eq,
1536 expr: value,
1537 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001538 })
1539 ));
1540
David Tolnay857628c2017-11-11 12:25:31 -08001541 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001542 outer_attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001543 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001544 defaultness: option!(keyword!(default)) >>
1545 constness: option!(keyword!(const)) >>
David Tolnay9b258702017-12-29 02:24:41 -05001546 unsafety: option!(keyword!(unsafe)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001547 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001548 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001549 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001550 generics: syn!(Generics) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001551 inputs: parens!(Punctuated::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001552 ret: syn!(ReturnType) >>
David Tolnayac997dd2017-12-27 23:18:22 -05001553 where_clause: option!(syn!(WhereClause)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001554 inner_attrs_stmts: braces!(tuple!(
David Tolnay2c136452017-12-27 14:13:32 -05001555 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001556 call!(Block::parse_within),
Michael Layzell416724e2017-05-24 21:12:34 -04001557 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001558 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001559 attrs: {
1560 let mut attrs = outer_attrs;
David Tolnay8875fca2017-12-31 13:52:37 -05001561 attrs.extend((inner_attrs_stmts.1).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001562 attrs
1563 },
David Tolnay857628c2017-11-11 12:25:31 -08001564 vis: vis,
1565 defaultness: defaultness,
1566 sig: MethodSig {
1567 constness: constness,
1568 unsafety: unsafety,
1569 abi: abi,
1570 ident: ident,
1571 decl: FnDecl {
1572 fn_token: fn_,
David Tolnay8875fca2017-12-31 13:52:37 -05001573 paren_token: inputs.0,
1574 inputs: inputs.1,
David Tolnay857628c2017-11-11 12:25:31 -08001575 output: ret,
David Tolnay857628c2017-11-11 12:25:31 -08001576 generics: Generics {
1577 where_clause: where_clause,
David Tolnayd142fc52018-07-21 15:09:53 -07001578 ..generics
David Tolnay4c9be372016-10-06 00:47:37 -07001579 },
David Tolnayd2836e22017-12-27 23:13:00 -05001580 variadic: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001581 },
David Tolnay857628c2017-11-11 12:25:31 -08001582 },
1583 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001584 brace_token: inner_attrs_stmts.0,
1585 stmts: (inner_attrs_stmts.1).1,
David Tolnay857628c2017-11-11 12:25:31 -08001586 },
David Tolnay4c9be372016-10-06 00:47:37 -07001587 })
1588 ));
1589
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001590 named!(unstable_async_method -> ImplItemVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07001591 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07001592 many0!(Attribute::parse_outer) >>
1593 syn!(Visibility) >>
1594 option!(keyword!(default)) >>
1595 option!(keyword!(const)) >>
1596 option!(keyword!(unsafe)) >>
1597 keyword!(async) >>
1598 option!(syn!(Abi)) >>
1599 keyword!(fn) >>
1600 syn!(Ident) >>
1601 syn!(Generics) >>
1602 parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1603 syn!(ReturnType) >>
1604 option!(syn!(WhereClause)) >>
1605 braces!(tuple!(
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001606 many0!(Attribute::parse_inner),
1607 call!(Block::parse_within),
1608 )) >>
David Tolnay9be32582018-07-31 22:37:26 -07001609 end: call!(verbatim::grab_cursor) >>
1610 (ImplItemVerbatim {
1611 tts: verbatim::token_range(begin..end),
Yusuke Sasaki01dc0992018-08-01 05:26:31 +09001612 })
1613 ));
1614
David Tolnay857628c2017-11-11 12:25:31 -08001615 impl_synom!(ImplItemType "type in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001616 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001617 vis: syn!(Visibility) >>
David Tolnay360a6342017-12-29 02:22:11 -05001618 defaultness: option!(keyword!(default)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001619 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001620 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001621 generics: syn!(Generics) >>
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001622 where_clause: option!(syn!(WhereClause)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001623 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001624 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001625 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001626 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001627 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001628 vis: vis,
1629 defaultness: defaultness,
1630 type_token: type_,
1631 ident: ident,
David Tolnaycaa2a6d2018-07-21 15:08:07 -07001632 generics: Generics {
1633 where_clause: where_clause,
1634 ..generics
1635 },
David Tolnay857628c2017-11-11 12:25:31 -08001636 eq_token: eq,
1637 ty: ty,
1638 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001639 })
1640 ));
1641
David Tolnay758ee132018-08-21 21:29:40 -04001642 named!(unstable_impl_existential_type -> ImplItemVerbatim, map!(
1643 call!(existential_type_helper, true),
1644 |tts| ImplItemVerbatim { tts: tts }
1645 ));
1646
David Tolnay857628c2017-11-11 12:25:31 -08001647 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001648 attrs: many0!(Attribute::parse_outer) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001649 mac: syn!(Macro) >>
David Tolnayab919512017-12-30 23:31:51 -05001650 semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001651 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001652 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001653 mac: mac,
David Tolnay57292da2017-12-27 21:03:33 -05001654 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001655 })
1656 ));
1657
David Tolnayab919512017-12-30 23:31:51 -05001658 fn is_brace(delimiter: &MacroDelimiter) -> bool {
1659 match *delimiter {
1660 MacroDelimiter::Brace(_) => true,
1661 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
David Tolnay57292da2017-12-27 21:03:33 -05001662 }
1663 }
David Tolnayedf2b992016-09-23 20:43:45 -07001664}
David Tolnay4a51dc72016-10-01 00:40:31 -07001665
1666#[cfg(feature = "printing")]
1667mod printing {
1668 use super::*;
1669 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07001670 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -07001671 use quote::{ToTokens, TokenStreamExt};
David Tolnay4a51dc72016-10-01 00:40:31 -07001672
David Tolnay1bfa7332017-11-11 12:41:20 -08001673 impl ToTokens for ItemExternCrate {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001674 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001675 tokens.append_all(self.attrs.outer());
1676 self.vis.to_tokens(tokens);
1677 self.extern_token.to_tokens(tokens);
1678 self.crate_token.to_tokens(tokens);
1679 self.ident.to_tokens(tokens);
1680 if let Some((ref as_token, ref rename)) = self.rename {
1681 as_token.to_tokens(tokens);
1682 rename.to_tokens(tokens);
1683 }
1684 self.semi_token.to_tokens(tokens);
1685 }
1686 }
1687
1688 impl ToTokens for ItemUse {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001689 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001690 tokens.append_all(self.attrs.outer());
1691 self.vis.to_tokens(tokens);
1692 self.use_token.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001693 self.leading_colon.to_tokens(tokens);
David Tolnay5f332a92017-12-26 00:42:45 -05001694 self.tree.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001695 self.semi_token.to_tokens(tokens);
1696 }
1697 }
1698
1699 impl ToTokens for ItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001700 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001701 tokens.append_all(self.attrs.outer());
1702 self.vis.to_tokens(tokens);
1703 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05001704 self.mutability.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001705 self.ident.to_tokens(tokens);
1706 self.colon_token.to_tokens(tokens);
1707 self.ty.to_tokens(tokens);
1708 self.eq_token.to_tokens(tokens);
1709 self.expr.to_tokens(tokens);
1710 self.semi_token.to_tokens(tokens);
1711 }
1712 }
1713
1714 impl ToTokens for ItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001715 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001716 tokens.append_all(self.attrs.outer());
1717 self.vis.to_tokens(tokens);
1718 self.const_token.to_tokens(tokens);
1719 self.ident.to_tokens(tokens);
1720 self.colon_token.to_tokens(tokens);
1721 self.ty.to_tokens(tokens);
1722 self.eq_token.to_tokens(tokens);
1723 self.expr.to_tokens(tokens);
1724 self.semi_token.to_tokens(tokens);
1725 }
1726 }
1727
1728 impl ToTokens for ItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001729 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001730 tokens.append_all(self.attrs.outer());
1731 self.vis.to_tokens(tokens);
1732 self.constness.to_tokens(tokens);
1733 self.unsafety.to_tokens(tokens);
1734 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07001735 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001736 self.block.brace_token.surround(tokens, |tokens| {
1737 tokens.append_all(self.attrs.inner());
1738 tokens.append_all(&self.block.stmts);
1739 });
1740 }
1741 }
1742
1743 impl ToTokens for ItemMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001744 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001745 tokens.append_all(self.attrs.outer());
1746 self.vis.to_tokens(tokens);
1747 self.mod_token.to_tokens(tokens);
1748 self.ident.to_tokens(tokens);
1749 if let Some((ref brace, ref items)) = self.content {
1750 brace.surround(tokens, |tokens| {
1751 tokens.append_all(self.attrs.inner());
1752 tokens.append_all(items);
1753 });
1754 } else {
1755 TokensOrDefault(&self.semi).to_tokens(tokens);
1756 }
1757 }
1758 }
1759
1760 impl ToTokens for ItemForeignMod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001761 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001762 tokens.append_all(self.attrs.outer());
1763 self.abi.to_tokens(tokens);
1764 self.brace_token.surround(tokens, |tokens| {
David Tolnay5c4613a2018-07-21 15:40:17 -07001765 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001766 tokens.append_all(&self.items);
1767 });
1768 }
1769 }
1770
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001771 impl ToTokens for ItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001772 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001773 tokens.append_all(self.attrs.outer());
1774 self.vis.to_tokens(tokens);
1775 self.type_token.to_tokens(tokens);
1776 self.ident.to_tokens(tokens);
1777 self.generics.to_tokens(tokens);
1778 self.generics.where_clause.to_tokens(tokens);
1779 self.eq_token.to_tokens(tokens);
1780 self.ty.to_tokens(tokens);
1781 self.semi_token.to_tokens(tokens);
1782 }
1783 }
1784
1785 impl ToTokens for ItemEnum {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001786 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001787 tokens.append_all(self.attrs.outer());
1788 self.vis.to_tokens(tokens);
1789 self.enum_token.to_tokens(tokens);
1790 self.ident.to_tokens(tokens);
1791 self.generics.to_tokens(tokens);
1792 self.generics.where_clause.to_tokens(tokens);
1793 self.brace_token.surround(tokens, |tokens| {
1794 self.variants.to_tokens(tokens);
1795 });
1796 }
1797 }
1798
1799 impl ToTokens for ItemStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001800 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001801 tokens.append_all(self.attrs.outer());
1802 self.vis.to_tokens(tokens);
1803 self.struct_token.to_tokens(tokens);
1804 self.ident.to_tokens(tokens);
1805 self.generics.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001806 match self.fields {
1807 Fields::Named(ref fields) => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001808 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001809 fields.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001810 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001811 Fields::Unnamed(ref fields) => {
1812 fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001813 self.generics.where_clause.to_tokens(tokens);
1814 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001815 }
David Tolnaye3d41b72017-12-31 15:24:00 -05001816 Fields::Unit => {
David Tolnay1bfa7332017-11-11 12:41:20 -08001817 self.generics.where_clause.to_tokens(tokens);
1818 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001819 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001820 }
1821 }
1822 }
1823
1824 impl ToTokens for ItemUnion {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001825 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001826 tokens.append_all(self.attrs.outer());
1827 self.vis.to_tokens(tokens);
1828 self.union_token.to_tokens(tokens);
1829 self.ident.to_tokens(tokens);
1830 self.generics.to_tokens(tokens);
1831 self.generics.where_clause.to_tokens(tokens);
David Tolnaye3d41b72017-12-31 15:24:00 -05001832 self.fields.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001833 }
1834 }
1835
1836 impl ToTokens for ItemTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001837 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001838 tokens.append_all(self.attrs.outer());
1839 self.vis.to_tokens(tokens);
1840 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001841 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001842 self.trait_token.to_tokens(tokens);
1843 self.ident.to_tokens(tokens);
1844 self.generics.to_tokens(tokens);
1845 if !self.supertraits.is_empty() {
1846 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1847 self.supertraits.to_tokens(tokens);
1848 }
1849 self.generics.where_clause.to_tokens(tokens);
1850 self.brace_token.surround(tokens, |tokens| {
1851 tokens.append_all(&self.items);
1852 });
1853 }
1854 }
1855
David Tolnay1bfa7332017-11-11 12:41:20 -08001856 impl ToTokens for ItemImpl {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001857 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001858 tokens.append_all(self.attrs.outer());
1859 self.defaultness.to_tokens(tokens);
1860 self.unsafety.to_tokens(tokens);
1861 self.impl_token.to_tokens(tokens);
1862 self.generics.to_tokens(tokens);
1863 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1864 polarity.to_tokens(tokens);
1865 path.to_tokens(tokens);
1866 for_token.to_tokens(tokens);
1867 }
1868 self.self_ty.to_tokens(tokens);
1869 self.generics.where_clause.to_tokens(tokens);
1870 self.brace_token.surround(tokens, |tokens| {
David Tolnaycf3697a2018-03-31 20:51:15 +02001871 tokens.append_all(self.attrs.inner());
David Tolnay1bfa7332017-11-11 12:41:20 -08001872 tokens.append_all(&self.items);
1873 });
1874 }
1875 }
1876
1877 impl ToTokens for ItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001878 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001879 tokens.append_all(self.attrs.outer());
1880 self.mac.path.to_tokens(tokens);
1881 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001882 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001883 match self.mac.delimiter {
1884 MacroDelimiter::Paren(ref paren) => {
1885 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1886 }
1887 MacroDelimiter::Brace(ref brace) => {
1888 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1889 }
1890 MacroDelimiter::Bracket(ref bracket) => {
1891 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1892 }
1893 }
David Tolnay57292da2017-12-27 21:03:33 -05001894 self.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001895 }
1896 }
David Tolnay42602292016-10-01 22:25:45 -07001897
David Tolnay500d8322017-12-18 00:32:51 -08001898 impl ToTokens for ItemMacro2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001899 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay500d8322017-12-18 00:32:51 -08001900 tokens.append_all(self.attrs.outer());
1901 self.vis.to_tokens(tokens);
1902 self.macro_token.to_tokens(tokens);
1903 self.ident.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -05001904 self.paren_token.surround(tokens, |tokens| {
1905 self.args.to_tokens(tokens);
1906 });
1907 self.brace_token.surround(tokens, |tokens| {
1908 self.body.to_tokens(tokens);
1909 });
David Tolnay500d8322017-12-18 00:32:51 -08001910 }
1911 }
1912
David Tolnay2ae520a2017-12-29 11:19:50 -05001913 impl ToTokens for ItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001914 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05001915 self.tts.to_tokens(tokens);
1916 }
1917 }
1918
David Tolnay5f332a92017-12-26 00:42:45 -05001919 impl ToTokens for UsePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001920 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5f332a92017-12-26 00:42:45 -05001921 self.ident.to_tokens(tokens);
David Tolnayd97a7d22018-03-31 19:17:01 +02001922 self.colon2_token.to_tokens(tokens);
1923 self.tree.to_tokens(tokens);
1924 }
1925 }
1926
1927 impl ToTokens for UseName {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001928 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001929 self.ident.to_tokens(tokens);
1930 }
1931 }
1932
1933 impl ToTokens for UseRename {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001934 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd97a7d22018-03-31 19:17:01 +02001935 self.ident.to_tokens(tokens);
1936 self.as_token.to_tokens(tokens);
1937 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001938 }
1939 }
1940
David Tolnay5f332a92017-12-26 00:42:45 -05001941 impl ToTokens for UseGlob {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001942 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001943 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001944 }
1945 }
1946
David Tolnayd97a7d22018-03-31 19:17:01 +02001947 impl ToTokens for UseGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001948 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001949 self.brace_token.surround(tokens, |tokens| {
1950 self.items.to_tokens(tokens);
1951 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001952 }
1953 }
1954
David Tolnay1bfa7332017-11-11 12:41:20 -08001955 impl ToTokens for TraitItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001956 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001957 tokens.append_all(self.attrs.outer());
1958 self.const_token.to_tokens(tokens);
1959 self.ident.to_tokens(tokens);
1960 self.colon_token.to_tokens(tokens);
1961 self.ty.to_tokens(tokens);
1962 if let Some((ref eq_token, ref default)) = self.default {
1963 eq_token.to_tokens(tokens);
1964 default.to_tokens(tokens);
1965 }
1966 self.semi_token.to_tokens(tokens);
1967 }
1968 }
1969
1970 impl ToTokens for TraitItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001971 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001972 tokens.append_all(self.attrs.outer());
1973 self.sig.to_tokens(tokens);
1974 match self.default {
1975 Some(ref block) => {
1976 block.brace_token.surround(tokens, |tokens| {
1977 tokens.append_all(self.attrs.inner());
1978 tokens.append_all(&block.stmts);
1979 });
David Tolnayca085422016-10-04 00:12:38 -07001980 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001981 None => {
1982 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001983 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001984 }
1985 }
1986 }
1987
1988 impl ToTokens for TraitItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001989 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001990 tokens.append_all(self.attrs.outer());
1991 self.type_token.to_tokens(tokens);
1992 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001993 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001994 if !self.bounds.is_empty() {
1995 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1996 self.bounds.to_tokens(tokens);
1997 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001998 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001999 if let Some((ref eq_token, ref default)) = self.default {
2000 eq_token.to_tokens(tokens);
2001 default.to_tokens(tokens);
2002 }
2003 self.semi_token.to_tokens(tokens);
2004 }
2005 }
2006
2007 impl ToTokens for TraitItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002008 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay1bfa7332017-11-11 12:41:20 -08002009 tokens.append_all(self.attrs.outer());
2010 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002011 self.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07002012 }
2013 }
2014
David Tolnay2ae520a2017-12-29 11:19:50 -05002015 impl ToTokens for TraitItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002016 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002017 self.tts.to_tokens(tokens);
2018 }
2019 }
2020
David Tolnay857628c2017-11-11 12:25:31 -08002021 impl ToTokens for ImplItemConst {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002022 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay4c9be372016-10-06 00:47:37 -07002023 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08002024 self.vis.to_tokens(tokens);
2025 self.defaultness.to_tokens(tokens);
2026 self.const_token.to_tokens(tokens);
2027 self.ident.to_tokens(tokens);
2028 self.colon_token.to_tokens(tokens);
2029 self.ty.to_tokens(tokens);
2030 self.eq_token.to_tokens(tokens);
2031 self.expr.to_tokens(tokens);
2032 self.semi_token.to_tokens(tokens);
2033 }
2034 }
2035
2036 impl ToTokens for ImplItemMethod {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002037 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002038 tokens.append_all(self.attrs.outer());
2039 self.vis.to_tokens(tokens);
2040 self.defaultness.to_tokens(tokens);
2041 self.sig.to_tokens(tokens);
2042 self.block.brace_token.surround(tokens, |tokens| {
2043 tokens.append_all(self.attrs.inner());
2044 tokens.append_all(&self.block.stmts);
2045 });
2046 }
2047 }
2048
2049 impl ToTokens for ImplItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002050 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002051 tokens.append_all(self.attrs.outer());
2052 self.vis.to_tokens(tokens);
2053 self.defaultness.to_tokens(tokens);
2054 self.type_token.to_tokens(tokens);
2055 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05002056 self.generics.to_tokens(tokens);
David Tolnaycaa2a6d2018-07-21 15:08:07 -07002057 self.generics.where_clause.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08002058 self.eq_token.to_tokens(tokens);
2059 self.ty.to_tokens(tokens);
2060 self.semi_token.to_tokens(tokens);
2061 }
2062 }
2063
2064 impl ToTokens for ImplItemMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002065 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay857628c2017-11-11 12:25:31 -08002066 tokens.append_all(self.attrs.outer());
2067 self.mac.to_tokens(tokens);
David Tolnay57292da2017-12-27 21:03:33 -05002068 self.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07002069 }
2070 }
2071
David Tolnay2ae520a2017-12-29 11:19:50 -05002072 impl ToTokens for ImplItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002073 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002074 self.tts.to_tokens(tokens);
2075 }
2076 }
2077
David Tolnay8894f602017-11-11 12:11:04 -08002078 impl ToTokens for ForeignItemFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002079 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay35902302016-10-06 01:11:08 -07002080 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002081 self.vis.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002082 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002083 self.semi_token.to_tokens(tokens);
2084 }
2085 }
2086
2087 impl ToTokens for ForeignItemStatic {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002088 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8894f602017-11-11 12:11:04 -08002089 tokens.append_all(self.attrs.outer());
2090 self.vis.to_tokens(tokens);
2091 self.static_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002092 self.mutability.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08002093 self.ident.to_tokens(tokens);
2094 self.colon_token.to_tokens(tokens);
2095 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002096 self.semi_token.to_tokens(tokens);
2097 }
2098 }
2099
David Tolnay199bcbb2017-11-12 10:33:52 -08002100 impl ToTokens for ForeignItemType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002101 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay199bcbb2017-11-12 10:33:52 -08002102 tokens.append_all(self.attrs.outer());
2103 self.vis.to_tokens(tokens);
2104 self.type_token.to_tokens(tokens);
2105 self.ident.to_tokens(tokens);
2106 self.semi_token.to_tokens(tokens);
2107 }
2108 }
2109
David Tolnay435c1782018-08-24 16:15:44 -04002110 impl ToTokens for ForeignItemMacro {
2111 fn to_tokens(&self, tokens: &mut TokenStream) {
2112 tokens.append_all(self.attrs.outer());
2113 self.mac.to_tokens(tokens);
2114 self.semi_token.to_tokens(tokens);
2115 }
2116 }
2117
David Tolnay2ae520a2017-12-29 11:19:50 -05002118 impl ToTokens for ForeignItemVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002119 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05002120 self.tts.to_tokens(tokens);
2121 }
2122 }
2123
David Tolnay570695e2017-06-03 16:15:13 -07002124 impl ToTokens for MethodSig {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002125 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay570695e2017-06-03 16:15:13 -07002126 self.constness.to_tokens(tokens);
2127 self.unsafety.to_tokens(tokens);
2128 self.abi.to_tokens(tokens);
Alex Crichtona74a1c82018-05-16 10:20:44 -07002129 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002130 }
2131 }
2132
Alex Crichtona74a1c82018-05-16 10:20:44 -07002133 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002134
2135 impl<'a> ToTokens for NamedDecl<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002136 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002137 self.0.fn_token.to_tokens(tokens);
2138 self.1.to_tokens(tokens);
2139 self.0.generics.to_tokens(tokens);
2140 self.0.paren_token.surround(tokens, |tokens| {
2141 self.0.inputs.to_tokens(tokens);
David Tolnayd2836e22017-12-27 23:13:00 -05002142 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2143 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002144 }
David Tolnayd2836e22017-12-27 23:13:00 -05002145 self.0.variadic.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002146 });
2147 self.0.output.to_tokens(tokens);
2148 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07002149 }
2150 }
2151
Alex Crichton62a0a592017-05-22 13:58:53 -07002152 impl ToTokens for ArgSelfRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002153 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002154 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002155 self.lifetime.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002156 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002157 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002158 }
2159 }
2160
2161 impl ToTokens for ArgSelf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002162 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05002163 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002164 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002165 }
2166 }
2167
2168 impl ToTokens for ArgCaptured {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002169 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002170 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002171 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002172 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07002173 }
2174 }
David Tolnay4a51dc72016-10-01 00:40:31 -07002175}