blob: f7e3e913b85092e5a9a136fb7b6d8db2962b2ec4 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_enum_of_structs! {
David Tolnayc6b55bc2017-11-09 22:48:38 -08005 /// Things that can appear directly inside of a module.
6 pub enum Item {
David Tolnay570695e2017-06-03 16:15:13 -07007 /// An `extern crate` item, with optional original crate name.
Alex Crichton62a0a592017-05-22 13:58:53 -07008 ///
9 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
10 pub ExternCrate(ItemExternCrate {
David Tolnayc6b55bc2017-11-09 22:48:38 -080011 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070012 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 pub extern_token: Token![extern],
14 pub crate_token: Token![crate],
David Tolnay570695e2017-06-03 16:15:13 -070015 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub rename: Option<(Token![as], Ident)>,
17 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070018 }),
19 /// A use declaration (`use` or `pub use`) item.
20 ///
21 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
22 pub Use(ItemUse {
David Tolnayc6b55bc2017-11-09 22:48:38 -080023 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070024 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 pub use_token: Token![use],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub path: Box<ViewPath>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070028 }),
29 /// A static item (`static` or `pub static`).
30 ///
31 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
32 pub Static(ItemStatic {
David Tolnayc6b55bc2017-11-09 22:48:38 -080033 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070034 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub mutbl: Mutability,
David Tolnay570695e2017-06-03 16:15:13 -070037 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080038 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080039 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070041 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
44 /// A constant item (`const` or `pub const`).
45 ///
46 /// E.g. `const FOO: i32 = 42;`
47 pub Const(ItemConst {
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 const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -070051 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -070055 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
58 /// A function declaration (`fn` or `pub fn`).
59 ///
60 /// E.g. `fn foo(bar: usize) -> usize { .. }`
61 pub Fn(ItemFn {
David Tolnayc6b55bc2017-11-09 22:48:38 -080062 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070063 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -070065 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -070067 pub decl: Box<FnDecl>,
68 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub block: Box<Block>,
70 }),
71 /// A module declaration (`mod` or `pub mod`).
72 ///
73 /// E.g. `mod foo;` or `mod foo { .. }`
74 pub Mod(ItemMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080075 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070076 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 pub mod_token: Token![mod],
David Tolnay570695e2017-06-03 16:15:13 -070078 pub ident: Ident,
79 pub content: Option<(tokens::Brace, Vec<Item>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080080 pub semi: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070081 }),
82 /// An external module (`extern` or `pub extern`).
83 ///
84 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 pub ForeignMod(ItemForeignMod {
David Tolnayc6b55bc2017-11-09 22:48:38 -080086 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070087 pub abi: Abi,
David Tolnay570695e2017-06-03 16:15:13 -070088 pub brace_token: tokens::Brace,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070089 pub items: Vec<ForeignItem>,
90 }),
Alex Crichton62a0a592017-05-22 13:58:53 -070091 /// A type alias (`type` or `pub type`).
92 ///
93 /// E.g. `type Foo = Bar<u8>;`
David Tolnayfd6bf5c2017-11-12 09:41:14 -080094 pub Type(ItemType {
David Tolnayc6b55bc2017-11-09 22:48:38 -080095 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -070096 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -080097 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -070098 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800101 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 }),
104 /// An enum definition (`enum` or `pub enum`).
105 ///
106 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
107 pub Enum(ItemEnum {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800108 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700109 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 pub enum_token: Token![enum],
David Tolnay570695e2017-06-03 16:15:13 -0700111 pub ident: Ident,
112 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700113 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 pub variants: Delimited<Variant, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 }),
116 /// A struct definition (`struct` or `pub struct`).
117 ///
118 /// E.g. `struct Foo<A> { x: A }`
119 pub Struct(ItemStruct {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800120 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700121 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub struct_token: Token![struct],
David Tolnay570695e2017-06-03 16:15:13 -0700123 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700125 pub data: VariantData,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 }),
128 /// A union definition (`union` or `pub union`).
129 ///
130 /// E.g. `union Foo<A, B> { x: A, y: B }`
131 pub Union(ItemUnion {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800132 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700133 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800134 pub union_token: Token![union],
David Tolnay570695e2017-06-03 16:15:13 -0700135 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700137 pub data: VariantData,
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 }),
139 /// A Trait declaration (`trait` or `pub trait`).
140 ///
141 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
142 pub Trait(ItemTrait {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800143 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700144 pub vis: Visibility,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 pub unsafety: Unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -0500146 pub auto_token: Option<Token![auto]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800147 pub trait_token: Token![trait],
David Tolnay570695e2017-06-03 16:15:13 -0700148 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800150 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800151 pub supertraits: Delimited<TypeParamBound, Token![+]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700152 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 pub items: Vec<TraitItem>,
154 }),
155 /// Default trait implementation.
156 ///
157 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
158 pub DefaultImpl(ItemDefaultImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800159 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800161 pub impl_token: Token![impl],
David Tolnay570695e2017-06-03 16:15:13 -0700162 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800163 pub for_token: Token![for],
164 pub dot2_token: Token![..],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700165 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
167 /// An implementation.
168 ///
169 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
170 pub Impl(ItemImpl {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800171 pub attrs: Vec<Attribute>,
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -0700172 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub unsafety: Unsafety,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800174 pub impl_token: Token![impl],
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 pub generics: Generics,
David Tolnay570695e2017-06-03 16:15:13 -0700176 /// Trait this impl implements.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800177 pub trait_: Option<(ImplPolarity, Path, Token![for])>,
David Tolnay570695e2017-06-03 16:15:13 -0700178 /// The Self type of the impl.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800179 pub self_ty: Box<Type>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700180 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 pub items: Vec<ImplItem>,
182 }),
183 /// A macro invocation (which includes macro definition).
184 ///
185 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
David Tolnaydecf28d2017-11-11 11:56:45 -0800186 pub Macro(ItemMacro {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800187 pub attrs: Vec<Attribute>,
David Tolnay99a953d2017-11-11 12:51:43 -0800188 /// The `example` in `macro_rules! example { ... }`.
189 pub ident: Option<Ident>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800190 pub mac: Macro,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800191 }),
David Tolnay500d8322017-12-18 00:32:51 -0800192 /// FIXME will need to revisit what this looks like in the AST.
193 pub Macro2(ItemMacro2 {
194 pub attrs: Vec<Attribute>,
195 pub vis: Visibility,
196 pub macro_token: Token![macro],
197 pub ident: Ident,
198 pub args: TokenTree,
199 pub body: TokenTree,
200 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700201 }
David Tolnayb79ee962016-09-04 09:39:20 -0700202}
203
David Tolnay0e837402016-12-22 17:25:55 -0500204impl From<DeriveInput> for Item {
205 fn from(input: DeriveInput) -> Item {
David Tolnayc6b55bc2017-11-09 22:48:38 -0800206 match input.body {
207 Body::Enum(data) => {
208 Item::Enum(ItemEnum {
209 attrs: input.attrs,
210 vis: input.vis,
211 enum_token: data.enum_token,
212 ident: input.ident,
213 generics: input.generics,
214 brace_token: data.brace_token,
215 variants: data.variants,
216 })
217 }
218 Body::Struct(data) => {
219 Item::Struct(ItemStruct {
220 attrs: input.attrs,
221 vis: input.vis,
222 struct_token: data.struct_token,
223 ident: input.ident,
224 generics: input.generics,
225 data: data.data,
226 semi_token: data.semi_token,
227 })
228 }
David Tolnay453cfd12016-10-23 11:00:14 -0700229 }
230 }
231}
232
Alex Crichton62a0a592017-05-22 13:58:53 -0700233ast_enum_of_structs! {
234 pub enum ViewPath {
235 /// `foo::bar::baz as quux`
236 ///
237 /// or just
238 ///
239 /// `foo::bar::baz` (with `as baz` implicitly on the right)
240 pub Simple(PathSimple {
241 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800242 pub as_token: Option<Token![as]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 pub rename: Option<Ident>,
244 }),
245
246 /// `foo::bar::*`
247 pub Glob(PathGlob {
248 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800249 pub colon2_token: Option<Token![::]>,
250 pub star_token: Token![*],
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 }),
252
253 /// `foo::bar::{a, b, c}`
254 pub List(PathList {
255 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800256 pub colon2_token: Token![::],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700257 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub items: Delimited<PathListItem, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 }),
260 }
261}
262
263ast_struct! {
264 pub struct PathListItem {
265 pub name: Ident,
266 /// renamed in list, e.g. `use foo::{bar as baz};`
267 pub rename: Option<Ident>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800268 pub as_token: Option<Token![as]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 }
270}
271
272ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700273 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 pub enum Constness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800275 Const(Token![const]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 NotConst,
277 }
278}
279
280ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700281 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub enum Defaultness {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800283 Default(Token![default]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 Final,
285 }
286}
287
Alex Crichton62a0a592017-05-22 13:58:53 -0700288ast_enum_of_structs! {
289 /// An item within an `extern` block
David Tolnay8894f602017-11-11 12:11:04 -0800290 pub enum ForeignItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 /// A foreign function
292 pub Fn(ForeignItemFn {
David Tolnay8894f602017-11-11 12:11:04 -0800293 pub attrs: Vec<Attribute>,
294 pub vis: Visibility,
295 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 pub decl: Box<FnDecl>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800297 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
299 /// A foreign static item (`static ext: u8`)
300 pub Static(ForeignItemStatic {
David Tolnay8894f602017-11-11 12:11:04 -0800301 pub attrs: Vec<Attribute>,
302 pub vis: Visibility,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800303 pub static_token: Token![static],
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 pub mutbl: Mutability,
David Tolnay8894f602017-11-11 12:11:04 -0800305 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800306 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800307 pub ty: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800308 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 }),
David Tolnay199bcbb2017-11-12 10:33:52 -0800310 /// A foreign type
311 pub Type(ForeignItemType {
312 pub attrs: Vec<Attribute>,
313 pub vis: Visibility,
314 pub type_token: Token![type],
315 pub ident: Ident,
316 pub semi_token: Token![;],
317 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700319}
320
David Tolnayda705bd2017-11-10 21:58:05 -0800321ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 /// Represents an item declaration within a trait declaration,
323 /// possibly including a default implementation. A trait item is
324 /// either required (meaning it doesn't have an implementation, just a
325 /// signature) or provided (meaning it has a default implementation).
David Tolnayda705bd2017-11-10 21:58:05 -0800326 pub enum TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub Const(TraitItemConst {
David Tolnayda705bd2017-11-10 21:58:05 -0800328 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800329 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700330 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800331 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800332 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800333 pub default: Option<(Token![=], Expr)>,
334 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 }),
336 pub Method(TraitItemMethod {
David Tolnayda705bd2017-11-10 21:58:05 -0800337 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 pub sig: MethodSig,
339 pub default: Option<Block>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800340 pub semi_token: Option<Token![;]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
342 pub Type(TraitItemType {
David Tolnayda705bd2017-11-10 21:58:05 -0800343 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800344 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700345 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500346 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800348 pub bounds: Delimited<TypeParamBound, Token![+]>,
349 pub default: Option<(Token![=], Type)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 }),
David Tolnaydecf28d2017-11-11 11:56:45 -0800352 pub Macro(TraitItemMacro {
David Tolnayda705bd2017-11-10 21:58:05 -0800353 pub attrs: Vec<Attribute>,
David Tolnaydecf28d2017-11-11 11:56:45 -0800354 pub mac: Macro,
David Tolnayda705bd2017-11-10 21:58:05 -0800355 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700357}
358
359ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700360 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub enum ImplPolarity {
362 /// `impl Trait for Type`
363 Positive,
364 /// `impl !Trait for Type`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800365 Negative(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 }
367}
368
Alex Crichton62a0a592017-05-22 13:58:53 -0700369ast_enum_of_structs! {
David Tolnay857628c2017-11-11 12:25:31 -0800370 pub enum ImplItem {
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 pub Const(ImplItemConst {
David Tolnay857628c2017-11-11 12:25:31 -0800372 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700373 pub vis: Visibility,
374 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800375 pub const_token: Token![const],
David Tolnay570695e2017-06-03 16:15:13 -0700376 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800378 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800379 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub expr: Expr,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800381 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
383 pub Method(ImplItemMethod {
David Tolnay857628c2017-11-11 12:25:31 -0800384 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700385 pub vis: Visibility,
386 pub defaultness: Defaultness,
Alex Crichton62a0a592017-05-22 13:58:53 -0700387 pub sig: MethodSig,
388 pub block: Block,
389 }),
390 pub Type(ImplItemType {
David Tolnay857628c2017-11-11 12:25:31 -0800391 pub attrs: Vec<Attribute>,
David Tolnay570695e2017-06-03 16:15:13 -0700392 pub vis: Visibility,
393 pub defaultness: Defaultness,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800394 pub type_token: Token![type],
David Tolnay570695e2017-06-03 16:15:13 -0700395 pub ident: Ident,
Nika Layzell591528a2017-12-05 12:47:37 -0500396 pub generics: Generics,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800397 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800398 pub ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800399 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 }),
David Tolnay857628c2017-11-11 12:25:31 -0800401 pub Macro(ImplItemMacro {
402 pub attrs: Vec<Attribute>,
403 pub mac: Macro,
404 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700406}
407
408ast_struct! {
409 /// Represents a method's signature in a trait declaration,
410 /// or in an implementation.
411 pub struct MethodSig {
Alex Crichton62a0a592017-05-22 13:58:53 -0700412 pub constness: Constness,
David Tolnay570695e2017-06-03 16:15:13 -0700413 pub unsafety: Unsafety,
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 pub abi: Option<Abi>,
David Tolnay570695e2017-06-03 16:15:13 -0700415 pub ident: Ident,
Alex Crichton62a0a592017-05-22 13:58:53 -0700416 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 }
418}
419
420ast_struct! {
421 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700422 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 /// E.g. `fn foo(bar: baz)`
424 pub struct FnDecl {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800425 pub fn_token: Token![fn],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700426 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800427 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf93b90d2017-11-11 19:21:26 -0800428 pub output: ReturnType,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700429 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 pub variadic: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800431 pub dot_tokens: Option<Token![...]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700433}
434
Alex Crichton62a0a592017-05-22 13:58:53 -0700435ast_enum_of_structs! {
436 /// An argument in a function header.
437 ///
438 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
439 pub enum FnArg {
440 pub SelfRef(ArgSelfRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub and_token: Token![&],
442 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 pub lifetime: Option<Lifetime>,
444 pub mutbl: Mutability,
445 }),
446 pub SelfValue(ArgSelf {
447 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800448 pub self_token: Token![self],
Alex Crichton62a0a592017-05-22 13:58:53 -0700449 }),
450 pub Captured(ArgCaptured {
451 pub pat: Pat,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800452 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800453 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700454 }),
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800455 pub Ignored(Type),
Alex Crichton62a0a592017-05-22 13:58:53 -0700456 }
David Tolnay62f374c2016-10-02 13:37:00 -0700457}
458
David Tolnayedf2b992016-09-23 20:43:45 -0700459#[cfg(feature = "parsing")]
460pub mod parsing {
461 use super::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700462
Michael Layzell92639a52017-06-01 00:07:44 -0400463 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700464
David Tolnay4c614be2017-11-10 00:02:38 -0800465 impl_synom!(Item "item" alt!(
466 syn!(ItemExternCrate) => { Item::ExternCrate }
467 |
468 syn!(ItemUse) => { Item::Use }
469 |
470 syn!(ItemStatic) => { Item::Static }
471 |
472 syn!(ItemConst) => { Item::Const }
473 |
474 syn!(ItemFn) => { Item::Fn }
475 |
476 syn!(ItemMod) => { Item::Mod }
477 |
478 syn!(ItemForeignMod) => { Item::ForeignMod }
479 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800480 syn!(ItemType) => { Item::Type }
David Tolnay4c614be2017-11-10 00:02:38 -0800481 |
482 syn!(ItemStruct) => { Item::Struct }
483 |
484 syn!(ItemEnum) => { Item::Enum }
485 |
486 syn!(ItemUnion) => { Item::Union }
487 |
488 syn!(ItemTrait) => { Item::Trait }
489 |
490 syn!(ItemDefaultImpl) => { Item::DefaultImpl }
491 |
492 syn!(ItemImpl) => { Item::Impl }
493 |
David Tolnaydecf28d2017-11-11 11:56:45 -0800494 syn!(ItemMacro) => { Item::Macro }
David Tolnay500d8322017-12-18 00:32:51 -0800495 |
496 syn!(ItemMacro2) => { Item::Macro2 }
David Tolnay4c614be2017-11-10 00:02:38 -0800497 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700498
David Tolnaydecf28d2017-11-11 11:56:45 -0800499 impl_synom!(ItemMacro "macro item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700500 attrs: many0!(call!(Attribute::parse_outer)) >>
501 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800502 bang: punct!(!) >>
David Tolnay570695e2017-06-03 16:15:13 -0700503 ident: option!(syn!(Ident)) >>
David Tolnay500d8322017-12-18 00:32:51 -0800504 body: call!(TokenTree::parse_delimited) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800505 cond!(!body.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800506 (ItemMacro {
David Tolnay84aa0752016-10-02 23:01:13 -0700507 attrs: attrs,
David Tolnay99a953d2017-11-11 12:51:43 -0800508 ident: ident,
David Tolnaydecf28d2017-11-11 11:56:45 -0800509 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -0500510 path: what,
David Tolnay570695e2017-06-03 16:15:13 -0700511 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700512 tokens: vec![body],
David Tolnayc6b55bc2017-11-09 22:48:38 -0800513 },
David Tolnay4c614be2017-11-10 00:02:38 -0800514 })
David Tolnayedf2b992016-09-23 20:43:45 -0700515 ));
516
David Tolnay500d8322017-12-18 00:32:51 -0800517 // TODO: figure out the actual grammar; is body required to be braced?
518 impl_synom!(ItemMacro2 "macro2 item" do_parse!(
519 attrs: many0!(call!(Attribute::parse_outer)) >>
520 vis: syn!(Visibility) >>
521 macro_: keyword!(macro) >>
522 ident: syn!(Ident) >>
523 args: call!(TokenTree::parse_delimited) >>
524 body: call!(TokenTree::parse_delimited) >>
525 (ItemMacro2 {
526 attrs: attrs,
527 vis: vis,
528 macro_token: macro_,
529 ident: ident,
530 args: args,
531 body: body,
532 })
533 ));
534
David Tolnay4c614be2017-11-10 00:02:38 -0800535 impl_synom!(ItemExternCrate "extern crate item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700536 attrs: many0!(call!(Attribute::parse_outer)) >>
537 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800538 extern_: keyword!(extern) >>
539 crate_: keyword!(crate) >>
David Tolnay570695e2017-06-03 16:15:13 -0700540 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800541 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
542 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800543 (ItemExternCrate {
David Tolnay570695e2017-06-03 16:15:13 -0700544 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800545 vis: vis,
546 extern_token: extern_,
547 crate_token: crate_,
548 ident: ident,
549 rename: rename,
550 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800551 })
David Tolnayedf2b992016-09-23 20:43:45 -0700552 ));
553
David Tolnay4c614be2017-11-10 00:02:38 -0800554 impl_synom!(ItemUse "use item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700555 attrs: many0!(call!(Attribute::parse_outer)) >>
556 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800557 use_: keyword!(use) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700558 what: syn!(ViewPath) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800559 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800560 (ItemUse {
David Tolnay4a057422016-10-08 00:02:31 -0700561 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800562 vis: vis,
563 use_token: use_,
564 path: Box::new(what),
565 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800566 })
David Tolnay4a057422016-10-08 00:02:31 -0700567 ));
568
Alex Crichton954046c2017-05-30 21:49:42 -0700569 impl Synom for ViewPath {
Michael Layzell92639a52017-06-01 00:07:44 -0400570 named!(parse -> Self, alt!(
571 syn!(PathGlob) => { ViewPath::Glob }
572 |
573 syn!(PathList) => { ViewPath::List }
574 |
575 syn!(PathSimple) => { ViewPath::Simple } // must be last
576 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700577 }
David Tolnay4a057422016-10-08 00:02:31 -0700578
Alex Crichton954046c2017-05-30 21:49:42 -0700579 impl Synom for PathSimple {
Michael Layzell92639a52017-06-01 00:07:44 -0400580 named!(parse -> Self, do_parse!(
581 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800582 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400583 (PathSimple {
584 path: path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800585 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400586 rename: rename.map(|p| p.1),
587 })
588 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700589 }
David Tolnay4a057422016-10-08 00:02:31 -0700590
Alex Crichton954046c2017-05-30 21:49:42 -0700591 impl Synom for PathGlob {
Michael Layzell92639a52017-06-01 00:07:44 -0400592 named!(parse -> Self, do_parse!(
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700593 path: option!(do_parse!(
594 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 colon2: punct!(::) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700596 (path, colon2)
597 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800598 star: punct!(*) >>
Alex Crichtonaf9d2952017-08-27 10:19:54 -0700599 ({
600 match path {
601 Some((path, colon2)) => {
602 PathGlob {
603 path: path,
604 colon2_token: Some(colon2),
605 star_token: star,
606 }
607 }
608 None => {
609 PathGlob {
610 path: Path {
611 leading_colon: None,
612 segments: Default::default(),
613 },
614 colon2_token: None,
615 star_token: star,
616 }
617 }
618 }
Michael Layzell92639a52017-06-01 00:07:44 -0400619 })
620 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700621 }
David Tolnay4a057422016-10-08 00:02:31 -0700622
Alex Crichton954046c2017-05-30 21:49:42 -0700623 impl Synom for PathList {
Michael Layzell92639a52017-06-01 00:07:44 -0400624 named!(parse -> Self, alt!(
625 do_parse!(
626 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 colon2: punct!(::) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400628 items: braces!(call!(Delimited::parse_terminated)) >>
629 (PathList {
630 path: path,
631 items: items.0,
632 brace_token: items.1,
633 colon2_token: colon2,
634 })
635 )
636 |
637 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800638 colon: option!(punct!(::)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400639 items: braces!(call!(Delimited::parse_terminated)) >>
640 (PathList {
641 path: Path {
Michael Layzell92639a52017-06-01 00:07:44 -0400642 leading_colon: None,
David Tolnay570695e2017-06-03 16:15:13 -0700643 segments: Delimited::new(),
Michael Layzell92639a52017-06-01 00:07:44 -0400644 },
David Tolnay570695e2017-06-03 16:15:13 -0700645 colon2_token: colon.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -0400646 brace_token: items.1,
647 items: items.0,
648 })
649 )
650 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700651 }
David Tolnay4a057422016-10-08 00:02:31 -0700652
Alex Crichton954046c2017-05-30 21:49:42 -0700653 impl Synom for PathListItem {
Michael Layzell92639a52017-06-01 00:07:44 -0400654 named!(parse -> Self, do_parse!(
655 name: alt!(
656 syn!(Ident)
657 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800658 map!(keyword!(self), Into::into)
Michael Layzell92639a52017-06-01 00:07:44 -0400659 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800660 rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400661 (PathListItem {
662 name: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 as_token: rename.as_ref().map(|p| Token![as]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400664 rename: rename.map(|p| p.1),
665 })
666 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700667 }
David Tolnay4a057422016-10-08 00:02:31 -0700668
David Tolnay4c614be2017-11-10 00:02:38 -0800669 impl_synom!(ItemStatic "static item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700670 attrs: many0!(call!(Attribute::parse_outer)) >>
671 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800672 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700673 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700674 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800675 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800676 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800677 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700678 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800679 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800680 (ItemStatic {
David Tolnay47a877c2016-10-01 16:50:55 -0700681 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800682 vis: vis,
683 static_token: static_,
684 mutbl: mutability,
685 ident: ident,
686 colon_token: colon,
687 ty: Box::new(ty),
688 eq_token: eq,
689 expr: Box::new(value),
690 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800691 })
David Tolnay47a877c2016-10-01 16:50:55 -0700692 ));
693
David Tolnay4c614be2017-11-10 00:02:38 -0800694 impl_synom!(ItemConst "const item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700695 attrs: many0!(call!(Attribute::parse_outer)) >>
696 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800697 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -0700698 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800699 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800700 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800701 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700702 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800703 semi: punct!(;) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800704 (ItemConst {
David Tolnay47a877c2016-10-01 16:50:55 -0700705 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800706 vis: vis,
707 const_token: const_,
708 ident: ident,
709 colon_token: colon,
710 ty: Box::new(ty),
711 eq_token: eq,
712 expr: Box::new(value),
713 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800714 })
David Tolnay47a877c2016-10-01 16:50:55 -0700715 ));
716
David Tolnay4c614be2017-11-10 00:02:38 -0800717 impl_synom!(ItemFn "fn item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700718 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
719 vis: syn!(Visibility) >>
720 constness: syn!(Constness) >>
721 unsafety: syn!(Unsafety) >>
722 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800723 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700724 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700725 generics: syn!(Generics) >>
726 inputs: parens!(Delimited::parse_terminated) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800727 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700728 where_clause: syn!(WhereClause) >>
729 inner_attrs_stmts: braces!(tuple!(
730 many0!(call!(Attribute::parse_inner)),
731 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400732 )) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800733 (ItemFn {
David Tolnay3b9783a2016-10-29 22:37:09 -0700734 attrs: {
735 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700736 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700737 attrs
738 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800739 vis: vis,
740 constness: constness,
741 unsafety: unsafety,
742 abi: abi,
743 decl: Box::new(FnDecl {
744 dot_tokens: None,
745 fn_token: fn_,
746 paren_token: inputs.1,
747 inputs: inputs.0,
748 output: ret,
749 variadic: false,
750 generics: Generics {
751 where_clause: where_clause,
752 .. generics
753 },
754 }),
755 ident: ident,
756 block: Box::new(Block {
757 brace_token: inner_attrs_stmts.1,
758 stmts: (inner_attrs_stmts.0).1,
759 }),
David Tolnay4c614be2017-11-10 00:02:38 -0800760 })
David Tolnay42602292016-10-01 22:25:45 -0700761 ));
762
Alex Crichton954046c2017-05-30 21:49:42 -0700763 impl Synom for FnArg {
Michael Layzell92639a52017-06-01 00:07:44 -0400764 named!(parse -> Self, alt!(
765 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400767 lt: option!(syn!(Lifetime)) >>
768 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800769 self_: keyword!(self) >>
770 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400771 (ArgSelfRef {
772 lifetime: lt,
773 mutbl: mutability,
774 and_token: and,
775 self_token: self_,
776 }.into())
777 )
778 |
779 do_parse!(
780 mutability: syn!(Mutability) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800781 self_: keyword!(self) >>
782 not!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400783 (ArgSelf {
784 mutbl: mutability,
785 self_token: self_,
786 }.into())
787 )
788 |
789 do_parse!(
790 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800791 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800792 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400793 (ArgCaptured {
794 pat: pat,
795 ty: ty,
796 colon_token: colon,
797 }.into())
798 )
799 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800800 syn!(Type) => { FnArg::Ignored }
Michael Layzell92639a52017-06-01 00:07:44 -0400801 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700802 }
David Tolnay62f374c2016-10-02 13:37:00 -0700803
David Tolnay4c614be2017-11-10 00:02:38 -0800804 impl_synom!(ItemMod "mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700805 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
806 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800807 mod_: keyword!(mod) >>
David Tolnay570695e2017-06-03 16:15:13 -0700808 ident: syn!(Ident) >>
809 content_semi: alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800810 punct!(;) => {|semi| (
David Tolnay570695e2017-06-03 16:15:13 -0700811 Vec::new(),
812 None,
813 Some(semi),
814 )}
David Tolnay37d10332016-10-13 20:51:04 -0700815 |
Alex Crichton954046c2017-05-30 21:49:42 -0700816 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700817 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700818 many0!(call!(Attribute::parse_inner)),
819 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400820 )
David Tolnay570695e2017-06-03 16:15:13 -0700821 ) => {|((inner_attrs, items), brace)| (
822 inner_attrs,
823 Some((brace, items)),
824 None,
825 )}
David Tolnay37d10332016-10-13 20:51:04 -0700826 ) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800827 (ItemMod {
David Tolnay570695e2017-06-03 16:15:13 -0700828 attrs: {
829 let mut attrs = outer_attrs;
830 attrs.extend(content_semi.0);
831 attrs
David Tolnay7b8009b2016-10-25 22:36:00 -0700832 },
David Tolnayc6b55bc2017-11-09 22:48:38 -0800833 vis: vis,
834 mod_token: mod_,
835 ident: ident,
836 content: content_semi.1,
837 semi: content_semi.2,
David Tolnay4c614be2017-11-10 00:02:38 -0800838 })
David Tolnay35902302016-10-06 01:11:08 -0700839 ));
840
David Tolnay4c614be2017-11-10 00:02:38 -0800841 impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700842 attrs: many0!(call!(Attribute::parse_outer)) >>
843 abi: syn!(Abi) >>
844 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800845 (ItemForeignMod {
David Tolnay35902302016-10-06 01:11:08 -0700846 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800847 abi: abi,
848 brace_token: items.1,
849 items: items.0,
David Tolnay4c614be2017-11-10 00:02:38 -0800850 })
David Tolnay35902302016-10-06 01:11:08 -0700851 ));
852
David Tolnay8894f602017-11-11 12:11:04 -0800853 impl_synom!(ForeignItem "foreign item" alt!(
854 syn!(ForeignItemFn) => { ForeignItem::Fn }
855 |
856 syn!(ForeignItemStatic) => { ForeignItem::Static }
David Tolnay199bcbb2017-11-12 10:33:52 -0800857 |
858 syn!(ForeignItemType) => { ForeignItem::Type }
David Tolnay8894f602017-11-11 12:11:04 -0800859 ));
David Tolnay35902302016-10-06 01:11:08 -0700860
David Tolnay8894f602017-11-11 12:11:04 -0800861 impl_synom!(ForeignItemFn "foreign function" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700862 attrs: many0!(call!(Attribute::parse_outer)) >>
863 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800864 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -0700865 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700866 generics: syn!(Generics) >>
867 inputs: parens!(do_parse!(
868 args: call!(Delimited::parse_terminated) >>
869 variadic: cond!(args.is_empty() || args.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800870 option!(punct!(...))) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700871 (args, variadic)
872 )) >>
David Tolnayf93b90d2017-11-11 19:21:26 -0800873 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700874 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800875 semi: punct!(;) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700876 ({
877 let ((inputs, variadic), parens) = inputs;
878 let variadic = variadic.and_then(|v| v);
David Tolnay8894f602017-11-11 12:11:04 -0800879 ForeignItemFn {
David Tolnay570695e2017-06-03 16:15:13 -0700880 ident: ident,
Alex Crichton954046c2017-05-30 21:49:42 -0700881 attrs: attrs,
882 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800883 decl: Box::new(FnDecl {
884 fn_token: fn_,
885 paren_token: parens,
886 inputs: inputs,
887 variadic: variadic.is_some(),
888 dot_tokens: variadic,
889 output: ret,
890 generics: Generics {
891 where_clause: where_clause,
892 .. generics
893 },
894 }),
Alex Crichton954046c2017-05-30 21:49:42 -0700895 vis: vis,
896 }
David Tolnay35902302016-10-06 01:11:08 -0700897 })
898 ));
899
David Tolnay8894f602017-11-11 12:11:04 -0800900 impl_synom!(ForeignItemStatic "foreign static" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700901 attrs: many0!(call!(Attribute::parse_outer)) >>
902 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 static_: keyword!(static) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700904 mutability: syn!(Mutability) >>
David Tolnay570695e2017-06-03 16:15:13 -0700905 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800906 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800907 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800908 semi: punct!(;) >>
David Tolnay8894f602017-11-11 12:11:04 -0800909 (ForeignItemStatic {
David Tolnay570695e2017-06-03 16:15:13 -0700910 ident: ident,
David Tolnay35902302016-10-06 01:11:08 -0700911 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700912 semi_token: semi,
David Tolnay8894f602017-11-11 12:11:04 -0800913 ty: Box::new(ty),
914 mutbl: mutability,
915 static_token: static_,
916 colon_token: colon,
David Tolnay35902302016-10-06 01:11:08 -0700917 vis: vis,
918 })
919 ));
920
David Tolnay199bcbb2017-11-12 10:33:52 -0800921 impl_synom!(ForeignItemType "foreign type" do_parse!(
922 attrs: many0!(call!(Attribute::parse_outer)) >>
923 vis: syn!(Visibility) >>
924 type_: keyword!(type) >>
925 ident: syn!(Ident) >>
926 semi: punct!(;) >>
927 (ForeignItemType {
928 attrs: attrs,
929 vis: vis,
930 type_token: type_,
931 ident: ident,
932 semi_token: semi,
933 })
934 ));
935
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800936 impl_synom!(ItemType "type item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700937 attrs: many0!(call!(Attribute::parse_outer)) >>
938 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800939 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -0700940 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700941 generics: syn!(Generics) >>
942 where_clause: syn!(WhereClause) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800943 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800944 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800945 semi: punct!(;) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800946 (ItemType {
David Tolnay3cf52982016-10-01 17:11:37 -0700947 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800948 vis: vis,
949 type_token: type_,
950 ident: ident,
951 generics: Generics {
952 where_clause: where_clause,
953 ..generics
954 },
955 eq_token: eq,
956 ty: Box::new(ty),
957 semi_token: semi,
David Tolnay4c614be2017-11-10 00:02:38 -0800958 })
David Tolnay3cf52982016-10-01 17:11:37 -0700959 ));
960
David Tolnay4c614be2017-11-10 00:02:38 -0800961 impl_synom!(ItemStruct "struct item" switch!(
962 map!(syn!(DeriveInput), Into::into),
963 Item::Struct(item) => value!(item)
964 |
965 _ => reject!()
966 ));
David Tolnay42602292016-10-01 22:25:45 -0700967
David Tolnay4c614be2017-11-10 00:02:38 -0800968 impl_synom!(ItemEnum "enum item" switch!(
969 map!(syn!(DeriveInput), Into::into),
970 Item::Enum(item) => value!(item)
971 |
972 _ => reject!()
973 ));
974
975 impl_synom!(ItemUnion "union item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700976 attrs: many0!(call!(Attribute::parse_outer)) >>
977 vis: syn!(Visibility) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800978 union_: keyword!(union) >>
David Tolnay570695e2017-06-03 16:15:13 -0700979 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700980 generics: syn!(Generics) >>
981 where_clause: syn!(WhereClause) >>
982 fields: braces!(call!(Delimited::parse_terminated_with,
983 Field::parse_struct)) >>
David Tolnay4c614be2017-11-10 00:02:38 -0800984 (ItemUnion {
David Tolnay2f9fa632016-10-03 22:08:48 -0700985 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -0800986 vis: vis,
987 union_token: union_,
988 ident: ident,
989 generics: Generics {
990 where_clause: where_clause,
991 .. generics
992 },
993 data: VariantData::Struct(fields.0, fields.1),
David Tolnay4c614be2017-11-10 00:02:38 -0800994 })
David Tolnay2f9fa632016-10-03 22:08:48 -0700995 ));
996
David Tolnay4c614be2017-11-10 00:02:38 -0800997 impl_synom!(ItemTrait "trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700998 attrs: many0!(call!(Attribute::parse_outer)) >>
999 vis: syn!(Visibility) >>
1000 unsafety: syn!(Unsafety) >>
Nika Layzell0dc6e632017-11-18 12:55:25 -05001001 auto_: option!(keyword!(auto)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001002 trait_: keyword!(trait) >>
David Tolnay570695e2017-06-03 16:15:13 -07001003 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001004 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001006 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001007 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001009 where_clause: syn!(WhereClause) >>
1010 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001011 (ItemTrait {
David Tolnay0aecb732016-10-03 23:03:50 -07001012 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001013 vis: vis,
1014 unsafety: unsafety,
Nika Layzell0dc6e632017-11-18 12:55:25 -05001015 auto_token: auto_,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001016 trait_token: trait_,
1017 ident: ident,
1018 generics: Generics {
1019 where_clause: where_clause,
1020 .. generics
1021 },
1022 colon_token: colon,
1023 supertraits: bounds.unwrap_or_default(),
1024 brace_token: body.1,
1025 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001026 })
David Tolnay0aecb732016-10-03 23:03:50 -07001027 ));
1028
David Tolnay4c614be2017-11-10 00:02:38 -08001029 impl_synom!(ItemDefaultImpl "default impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001030 attrs: many0!(call!(Attribute::parse_outer)) >>
1031 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001033 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001034 for_: keyword!(for) >>
1035 dot2: punct!(..) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001036 braces: braces!(epsilon!()) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001037 (ItemDefaultImpl {
David Tolnayf94e2362016-10-04 00:29:51 -07001038 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001039 unsafety: unsafety,
1040 impl_token: impl_,
1041 path: path,
1042 for_token: for_,
1043 dot2_token: dot2,
1044 brace_token: braces.1,
David Tolnay4c614be2017-11-10 00:02:38 -08001045 })
David Tolnayf94e2362016-10-04 00:29:51 -07001046 ));
1047
David Tolnayda705bd2017-11-10 21:58:05 -08001048 impl_synom!(TraitItem "trait item" alt!(
1049 syn!(TraitItemConst) => { TraitItem::Const }
1050 |
1051 syn!(TraitItemMethod) => { TraitItem::Method }
1052 |
1053 syn!(TraitItemType) => { TraitItem::Type }
1054 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001055 syn!(TraitItemMacro) => { TraitItem::Macro }
David Tolnayda705bd2017-11-10 21:58:05 -08001056 ));
David Tolnay0aecb732016-10-03 23:03:50 -07001057
David Tolnayda705bd2017-11-10 21:58:05 -08001058 impl_synom!(TraitItemConst "const trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001059 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001060 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001061 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001062 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001063 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001064 default: option!(tuple!(punct!(=), syn!(Expr))) >>
1065 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001066 (TraitItemConst {
David Tolnay0aecb732016-10-03 23:03:50 -07001067 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001068 const_token: const_,
1069 ident: ident,
1070 colon_token: colon,
1071 ty: ty,
1072 default: default,
1073 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001074 })
1075 ));
1076
David Tolnayda705bd2017-11-10 21:58:05 -08001077 impl_synom!(TraitItemMethod "method trait item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001078 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1079 constness: syn!(Constness) >>
1080 unsafety: syn!(Unsafety) >>
1081 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001082 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001083 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001084 generics: syn!(Generics) >>
1085 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001086 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001087 where_clause: syn!(WhereClause) >>
1088 body: option!(braces!(
1089 tuple!(many0!(call!(Attribute::parse_inner)),
1090 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001091 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001092 semi: cond!(body.is_none(), punct!(;)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001093 ({
1094 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001095 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001096 None => (Vec::new(), None),
1097 };
David Tolnayda705bd2017-11-10 21:58:05 -08001098 TraitItemMethod {
David Tolnay5859df12016-10-29 22:49:54 -07001099 attrs: {
1100 let mut attrs = outer_attrs;
1101 attrs.extend(inner_attrs);
1102 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001103 },
David Tolnayda705bd2017-11-10 21:58:05 -08001104 sig: MethodSig {
1105 constness: constness,
1106 unsafety: unsafety,
1107 abi: abi,
1108 ident: ident,
1109 decl: FnDecl {
1110 inputs: inputs.0,
1111 output: ret,
1112 variadic: false,
1113 fn_token: fn_,
1114 paren_token: inputs.1,
1115 dot_tokens: None,
1116 generics: Generics {
1117 where_clause: where_clause,
1118 .. generics
David Tolnay5859df12016-10-29 22:49:54 -07001119 },
1120 },
David Tolnayda705bd2017-11-10 21:58:05 -08001121 },
1122 default: stmts.map(|stmts| {
1123 Block {
1124 stmts: stmts.0,
1125 brace_token: stmts.1,
1126 }
1127 }),
1128 semi_token: semi,
David Tolnay5859df12016-10-29 22:49:54 -07001129 }
David Tolnay0aecb732016-10-03 23:03:50 -07001130 })
1131 ));
1132
David Tolnayda705bd2017-11-10 21:58:05 -08001133 impl_synom!(TraitItemType "trait item type" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001134 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001135 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001136 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001137 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001138 colon: option!(punct!(:)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001139 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001140 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001141 ) >>
Nika Layzell6bd36812017-12-05 13:46:07 -05001142 where_clause: syn!(WhereClause) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001143 default: option!(tuple!(punct!(=), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001144 semi: punct!(;) >>
David Tolnayda705bd2017-11-10 21:58:05 -08001145 (TraitItemType {
David Tolnay0aecb732016-10-03 23:03:50 -07001146 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001147 type_token: type_,
1148 ident: ident,
Nika Layzell0183ca32017-12-05 15:24:01 -05001149 generics: Generics {
1150 where_clause: where_clause,
1151 .. generics
1152 },
David Tolnayda705bd2017-11-10 21:58:05 -08001153 colon_token: colon,
1154 bounds: bounds.unwrap_or_default(),
1155 default: default,
1156 semi_token: semi,
David Tolnay0aecb732016-10-03 23:03:50 -07001157 })
1158 ));
1159
David Tolnaydecf28d2017-11-11 11:56:45 -08001160 impl_synom!(TraitItemMacro "trait item macro" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001161 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001162 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001163 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001164 (TraitItemMacro {
David Tolnay0aecb732016-10-03 23:03:50 -07001165 attrs: attrs,
David Tolnayda705bd2017-11-10 21:58:05 -08001166 mac: mac,
David Tolnay0aecb732016-10-03 23:03:50 -07001167 })
1168 ));
1169
David Tolnay4c614be2017-11-10 00:02:38 -08001170 impl_synom!(ItemImpl "impl item" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001171 attrs: many0!(call!(Attribute::parse_outer)) >>
Alex Crichtonf3d9ccc2017-08-28 09:40:13 -07001172 defaultness: syn!(Defaultness) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001173 unsafety: syn!(Unsafety) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001174 impl_: keyword!(impl) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001175 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001176 polarity_path: alt!(
1177 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001178 polarity: syn!(ImplPolarity) >>
1179 path: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001180 for_: keyword!(for) >>
David Tolnay570695e2017-06-03 16:15:13 -07001181 (Some((polarity, path, for_)))
David Tolnay4c9be372016-10-06 00:47:37 -07001182 )
1183 |
David Tolnay570695e2017-06-03 16:15:13 -07001184 epsilon!() => { |_| None }
David Tolnay4c9be372016-10-06 00:47:37 -07001185 ) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001186 self_ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001187 where_clause: syn!(WhereClause) >>
1188 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c614be2017-11-10 00:02:38 -08001189 (ItemImpl {
David Tolnay4c9be372016-10-06 00:47:37 -07001190 attrs: attrs,
David Tolnayc6b55bc2017-11-09 22:48:38 -08001191 defaultness: defaultness,
1192 unsafety: unsafety,
1193 impl_token: impl_,
1194 generics: Generics {
1195 where_clause: where_clause,
1196 .. generics
1197 },
1198 trait_: polarity_path,
1199 self_ty: Box::new(self_ty),
1200 brace_token: body.1,
1201 items: body.0,
David Tolnay4c614be2017-11-10 00:02:38 -08001202 })
David Tolnay4c9be372016-10-06 00:47:37 -07001203 ));
1204
David Tolnay857628c2017-11-11 12:25:31 -08001205 impl_synom!(ImplItem "item in impl block" alt!(
1206 syn!(ImplItemConst) => { ImplItem::Const }
1207 |
1208 syn!(ImplItemMethod) => { ImplItem::Method }
1209 |
1210 syn!(ImplItemType) => { ImplItem::Type }
1211 |
1212 syn!(ImplItemMacro) => { ImplItem::Macro }
1213 ));
David Tolnay4c9be372016-10-06 00:47:37 -07001214
David Tolnay857628c2017-11-11 12:25:31 -08001215 impl_synom!(ImplItemConst "const item in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001216 attrs: many0!(call!(Attribute::parse_outer)) >>
1217 vis: syn!(Visibility) >>
1218 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001219 const_: keyword!(const) >>
David Tolnay570695e2017-06-03 16:15:13 -07001220 ident: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001221 colon: punct!(:) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001222 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001223 eq: punct!(=) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001224 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001225 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001226 (ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001227 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001228 vis: vis,
1229 defaultness: defaultness,
1230 const_token: const_,
1231 ident: ident,
1232 colon_token: colon,
1233 ty: ty,
1234 eq_token: eq,
1235 expr: value,
1236 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001237 })
1238 ));
1239
David Tolnay857628c2017-11-11 12:25:31 -08001240 impl_synom!(ImplItemMethod "method in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001241 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1242 vis: syn!(Visibility) >>
1243 defaultness: syn!(Defaultness) >>
1244 constness: syn!(Constness) >>
1245 unsafety: syn!(Unsafety) >>
1246 abi: option!(syn!(Abi)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001247 fn_: keyword!(fn) >>
David Tolnay570695e2017-06-03 16:15:13 -07001248 ident: syn!(Ident) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001249 generics: syn!(Generics) >>
1250 inputs: parens!(call!(Delimited::parse_terminated)) >>
David Tolnayf93b90d2017-11-11 19:21:26 -08001251 ret: syn!(ReturnType) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001252 where_clause: syn!(WhereClause) >>
1253 inner_attrs_stmts: braces!(tuple!(
1254 many0!(call!(Attribute::parse_inner)),
1255 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001256 )) >>
David Tolnay857628c2017-11-11 12:25:31 -08001257 (ImplItemMethod {
David Tolnay3b9783a2016-10-29 22:37:09 -07001258 attrs: {
1259 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001260 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001261 attrs
1262 },
David Tolnay857628c2017-11-11 12:25:31 -08001263 vis: vis,
1264 defaultness: defaultness,
1265 sig: MethodSig {
1266 constness: constness,
1267 unsafety: unsafety,
1268 abi: abi,
1269 ident: ident,
1270 decl: FnDecl {
1271 fn_token: fn_,
1272 paren_token: inputs.1,
1273 inputs: inputs.0,
1274 output: ret,
1275 variadic: false,
1276 generics: Generics {
1277 where_clause: where_clause,
1278 .. generics
David Tolnay4c9be372016-10-06 00:47:37 -07001279 },
David Tolnay857628c2017-11-11 12:25:31 -08001280 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001281 },
David Tolnay857628c2017-11-11 12:25:31 -08001282 },
1283 block: Block {
1284 brace_token: inner_attrs_stmts.1,
1285 stmts: (inner_attrs_stmts.0).1,
1286 },
David Tolnay4c9be372016-10-06 00:47:37 -07001287 })
1288 ));
1289
David Tolnay857628c2017-11-11 12:25:31 -08001290 impl_synom!(ImplItemType "type in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001291 attrs: many0!(call!(Attribute::parse_outer)) >>
1292 vis: syn!(Visibility) >>
1293 defaultness: syn!(Defaultness) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 type_: keyword!(type) >>
David Tolnay570695e2017-06-03 16:15:13 -07001295 ident: syn!(Ident) >>
Nika Layzell591528a2017-12-05 12:47:37 -05001296 generics: syn!(Generics) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001297 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001298 ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001299 semi: punct!(;) >>
David Tolnay857628c2017-11-11 12:25:31 -08001300 (ImplItemType {
David Tolnay4c9be372016-10-06 00:47:37 -07001301 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001302 vis: vis,
1303 defaultness: defaultness,
1304 type_token: type_,
1305 ident: ident,
Nika Layzell591528a2017-12-05 12:47:37 -05001306 generics: generics,
David Tolnay857628c2017-11-11 12:25:31 -08001307 eq_token: eq,
1308 ty: ty,
1309 semi_token: semi,
David Tolnay4c9be372016-10-06 00:47:37 -07001310 })
1311 ));
1312
David Tolnay857628c2017-11-11 12:25:31 -08001313 impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001314 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001315 mac: syn!(Macro) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001316 cond!(!mac.is_braced(), punct!(;)) >>
David Tolnay857628c2017-11-11 12:25:31 -08001317 (ImplItemMacro {
David Tolnay4c9be372016-10-06 00:47:37 -07001318 attrs: attrs,
David Tolnay857628c2017-11-11 12:25:31 -08001319 mac: mac,
David Tolnay4c9be372016-10-06 00:47:37 -07001320 })
1321 ));
1322
Alex Crichton954046c2017-05-30 21:49:42 -07001323 impl Synom for ImplPolarity {
Michael Layzell92639a52017-06-01 00:07:44 -04001324 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001325 punct!(!) => { ImplPolarity::Negative }
Michael Layzell92639a52017-06-01 00:07:44 -04001326 |
1327 epsilon!() => { |_| ImplPolarity::Positive }
1328 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001329 }
David Tolnay4c9be372016-10-06 00:47:37 -07001330
Alex Crichton954046c2017-05-30 21:49:42 -07001331 impl Synom for Constness {
Michael Layzell92639a52017-06-01 00:07:44 -04001332 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001333 keyword!(const) => { Constness::Const }
Michael Layzell92639a52017-06-01 00:07:44 -04001334 |
1335 epsilon!() => { |_| Constness::NotConst }
1336 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001337 }
David Tolnay42602292016-10-01 22:25:45 -07001338
Alex Crichton954046c2017-05-30 21:49:42 -07001339 impl Synom for Defaultness {
Michael Layzell92639a52017-06-01 00:07:44 -04001340 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001341 keyword!(default) => { Defaultness::Default }
Michael Layzell92639a52017-06-01 00:07:44 -04001342 |
1343 epsilon!() => { |_| Defaultness::Final }
1344 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001345 }
David Tolnayedf2b992016-09-23 20:43:45 -07001346}
David Tolnay4a51dc72016-10-01 00:40:31 -07001347
1348#[cfg(feature = "printing")]
1349mod printing {
1350 use super::*;
1351 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001352 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001353 use quote::{Tokens, ToTokens};
1354
David Tolnay1bfa7332017-11-11 12:41:20 -08001355 impl ToTokens for ItemExternCrate {
David Tolnay4a51dc72016-10-01 00:40:31 -07001356 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001357 tokens.append_all(self.attrs.outer());
1358 self.vis.to_tokens(tokens);
1359 self.extern_token.to_tokens(tokens);
1360 self.crate_token.to_tokens(tokens);
1361 self.ident.to_tokens(tokens);
1362 if let Some((ref as_token, ref rename)) = self.rename {
1363 as_token.to_tokens(tokens);
1364 rename.to_tokens(tokens);
1365 }
1366 self.semi_token.to_tokens(tokens);
1367 }
1368 }
1369
1370 impl ToTokens for ItemUse {
1371 fn to_tokens(&self, tokens: &mut Tokens) {
1372 tokens.append_all(self.attrs.outer());
1373 self.vis.to_tokens(tokens);
1374 self.use_token.to_tokens(tokens);
1375 self.path.to_tokens(tokens);
1376 self.semi_token.to_tokens(tokens);
1377 }
1378 }
1379
1380 impl ToTokens for ItemStatic {
1381 fn to_tokens(&self, tokens: &mut Tokens) {
1382 tokens.append_all(self.attrs.outer());
1383 self.vis.to_tokens(tokens);
1384 self.static_token.to_tokens(tokens);
1385 self.mutbl.to_tokens(tokens);
1386 self.ident.to_tokens(tokens);
1387 self.colon_token.to_tokens(tokens);
1388 self.ty.to_tokens(tokens);
1389 self.eq_token.to_tokens(tokens);
1390 self.expr.to_tokens(tokens);
1391 self.semi_token.to_tokens(tokens);
1392 }
1393 }
1394
1395 impl ToTokens for ItemConst {
1396 fn to_tokens(&self, tokens: &mut Tokens) {
1397 tokens.append_all(self.attrs.outer());
1398 self.vis.to_tokens(tokens);
1399 self.const_token.to_tokens(tokens);
1400 self.ident.to_tokens(tokens);
1401 self.colon_token.to_tokens(tokens);
1402 self.ty.to_tokens(tokens);
1403 self.eq_token.to_tokens(tokens);
1404 self.expr.to_tokens(tokens);
1405 self.semi_token.to_tokens(tokens);
1406 }
1407 }
1408
1409 impl ToTokens for ItemFn {
1410 fn to_tokens(&self, tokens: &mut Tokens) {
1411 tokens.append_all(self.attrs.outer());
1412 self.vis.to_tokens(tokens);
1413 self.constness.to_tokens(tokens);
1414 self.unsafety.to_tokens(tokens);
1415 self.abi.to_tokens(tokens);
1416 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1417 self.block.brace_token.surround(tokens, |tokens| {
1418 tokens.append_all(self.attrs.inner());
1419 tokens.append_all(&self.block.stmts);
1420 });
1421 }
1422 }
1423
1424 impl ToTokens for ItemMod {
1425 fn to_tokens(&self, tokens: &mut Tokens) {
1426 tokens.append_all(self.attrs.outer());
1427 self.vis.to_tokens(tokens);
1428 self.mod_token.to_tokens(tokens);
1429 self.ident.to_tokens(tokens);
1430 if let Some((ref brace, ref items)) = self.content {
1431 brace.surround(tokens, |tokens| {
1432 tokens.append_all(self.attrs.inner());
1433 tokens.append_all(items);
1434 });
1435 } else {
1436 TokensOrDefault(&self.semi).to_tokens(tokens);
1437 }
1438 }
1439 }
1440
1441 impl ToTokens for ItemForeignMod {
1442 fn to_tokens(&self, tokens: &mut Tokens) {
1443 tokens.append_all(self.attrs.outer());
1444 self.abi.to_tokens(tokens);
1445 self.brace_token.surround(tokens, |tokens| {
1446 tokens.append_all(&self.items);
1447 });
1448 }
1449 }
1450
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001451 impl ToTokens for ItemType {
David Tolnay1bfa7332017-11-11 12:41:20 -08001452 fn to_tokens(&self, tokens: &mut Tokens) {
1453 tokens.append_all(self.attrs.outer());
1454 self.vis.to_tokens(tokens);
1455 self.type_token.to_tokens(tokens);
1456 self.ident.to_tokens(tokens);
1457 self.generics.to_tokens(tokens);
1458 self.generics.where_clause.to_tokens(tokens);
1459 self.eq_token.to_tokens(tokens);
1460 self.ty.to_tokens(tokens);
1461 self.semi_token.to_tokens(tokens);
1462 }
1463 }
1464
1465 impl ToTokens for ItemEnum {
1466 fn to_tokens(&self, tokens: &mut Tokens) {
1467 tokens.append_all(self.attrs.outer());
1468 self.vis.to_tokens(tokens);
1469 self.enum_token.to_tokens(tokens);
1470 self.ident.to_tokens(tokens);
1471 self.generics.to_tokens(tokens);
1472 self.generics.where_clause.to_tokens(tokens);
1473 self.brace_token.surround(tokens, |tokens| {
1474 self.variants.to_tokens(tokens);
1475 });
1476 }
1477 }
1478
1479 impl ToTokens for ItemStruct {
1480 fn to_tokens(&self, tokens: &mut Tokens) {
1481 tokens.append_all(self.attrs.outer());
1482 self.vis.to_tokens(tokens);
1483 self.struct_token.to_tokens(tokens);
1484 self.ident.to_tokens(tokens);
1485 self.generics.to_tokens(tokens);
1486 match self.data {
1487 VariantData::Struct(..) => {
1488 self.generics.where_clause.to_tokens(tokens);
1489 self.data.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001490 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001491 VariantData::Tuple(..) => {
1492 self.data.to_tokens(tokens);
1493 self.generics.where_clause.to_tokens(tokens);
1494 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001495 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001496 VariantData::Unit => {
1497 self.generics.where_clause.to_tokens(tokens);
1498 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001499 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001500 }
1501 }
1502 }
1503
1504 impl ToTokens for ItemUnion {
1505 fn to_tokens(&self, tokens: &mut Tokens) {
1506 tokens.append_all(self.attrs.outer());
1507 self.vis.to_tokens(tokens);
1508 self.union_token.to_tokens(tokens);
1509 self.ident.to_tokens(tokens);
1510 self.generics.to_tokens(tokens);
1511 self.generics.where_clause.to_tokens(tokens);
1512 // XXX: Should we handle / complain when using a
1513 // non-VariantData::Struct Variant in Union?
1514 self.data.to_tokens(tokens);
1515 }
1516 }
1517
1518 impl ToTokens for ItemTrait {
1519 fn to_tokens(&self, tokens: &mut Tokens) {
1520 tokens.append_all(self.attrs.outer());
1521 self.vis.to_tokens(tokens);
1522 self.unsafety.to_tokens(tokens);
Nika Layzell0dc6e632017-11-18 12:55:25 -05001523 self.auto_token.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001524 self.trait_token.to_tokens(tokens);
1525 self.ident.to_tokens(tokens);
1526 self.generics.to_tokens(tokens);
1527 if !self.supertraits.is_empty() {
1528 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1529 self.supertraits.to_tokens(tokens);
1530 }
1531 self.generics.where_clause.to_tokens(tokens);
1532 self.brace_token.surround(tokens, |tokens| {
1533 tokens.append_all(&self.items);
1534 });
1535 }
1536 }
1537
1538 impl ToTokens for ItemDefaultImpl {
1539 fn to_tokens(&self, tokens: &mut Tokens) {
1540 tokens.append_all(self.attrs.outer());
1541 self.unsafety.to_tokens(tokens);
1542 self.impl_token.to_tokens(tokens);
1543 self.path.to_tokens(tokens);
1544 self.for_token.to_tokens(tokens);
1545 self.dot2_token.to_tokens(tokens);
1546 self.brace_token.surround(tokens, |_tokens| {});
1547 }
1548 }
1549
1550 impl ToTokens for ItemImpl {
1551 fn to_tokens(&self, tokens: &mut Tokens) {
1552 tokens.append_all(self.attrs.outer());
1553 self.defaultness.to_tokens(tokens);
1554 self.unsafety.to_tokens(tokens);
1555 self.impl_token.to_tokens(tokens);
1556 self.generics.to_tokens(tokens);
1557 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1558 polarity.to_tokens(tokens);
1559 path.to_tokens(tokens);
1560 for_token.to_tokens(tokens);
1561 }
1562 self.self_ty.to_tokens(tokens);
1563 self.generics.where_clause.to_tokens(tokens);
1564 self.brace_token.surround(tokens, |tokens| {
1565 tokens.append_all(&self.items);
1566 });
1567 }
1568 }
1569
1570 impl ToTokens for ItemMacro {
1571 fn to_tokens(&self, tokens: &mut Tokens) {
1572 tokens.append_all(self.attrs.outer());
1573 self.mac.path.to_tokens(tokens);
1574 self.mac.bang_token.to_tokens(tokens);
David Tolnay99a953d2017-11-11 12:51:43 -08001575 self.ident.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001576 tokens.append_all(&self.mac.tokens);
1577 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001578 <Token![;]>::default().to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001579 }
1580 }
1581 }
David Tolnay42602292016-10-01 22:25:45 -07001582
David Tolnay500d8322017-12-18 00:32:51 -08001583 impl ToTokens for ItemMacro2 {
1584 fn to_tokens(&self, tokens: &mut Tokens) {
1585 tokens.append_all(self.attrs.outer());
1586 self.vis.to_tokens(tokens);
1587 self.macro_token.to_tokens(tokens);
1588 self.ident.to_tokens(tokens);
1589 self.args.to_tokens(tokens);
1590 self.body.to_tokens(tokens);
1591 }
1592 }
1593
Alex Crichton62a0a592017-05-22 13:58:53 -07001594 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001595 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001596 self.path.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001597 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001598 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001599 self.rename.to_tokens(tokens);
1600 }
David Tolnay4a057422016-10-08 00:02:31 -07001601 }
1602 }
1603
Alex Crichton62a0a592017-05-22 13:58:53 -07001604 impl ToTokens for PathGlob {
1605 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonaf9d2952017-08-27 10:19:54 -07001606 if self.path.segments.len() > 0 {
1607 self.path.to_tokens(tokens);
1608 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
1609 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001610 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001611 }
1612 }
1613
1614 impl ToTokens for PathList {
1615 fn to_tokens(&self, tokens: &mut Tokens) {
1616 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001617 self.colon2_token.to_tokens(tokens);
1618 self.brace_token.surround(tokens, |tokens| {
1619 self.items.to_tokens(tokens);
1620 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001621 }
1622 }
1623
David Tolnay4a057422016-10-08 00:02:31 -07001624 impl ToTokens for PathListItem {
1625 fn to_tokens(&self, tokens: &mut Tokens) {
1626 self.name.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001627 if self.rename.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001628 TokensOrDefault(&self.as_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001629 self.rename.to_tokens(tokens);
1630 }
David Tolnay4a057422016-10-08 00:02:31 -07001631 }
1632 }
1633
David Tolnay1bfa7332017-11-11 12:41:20 -08001634 impl ToTokens for TraitItemConst {
David Tolnayca085422016-10-04 00:12:38 -07001635 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay1bfa7332017-11-11 12:41:20 -08001636 tokens.append_all(self.attrs.outer());
1637 self.const_token.to_tokens(tokens);
1638 self.ident.to_tokens(tokens);
1639 self.colon_token.to_tokens(tokens);
1640 self.ty.to_tokens(tokens);
1641 if let Some((ref eq_token, ref default)) = self.default {
1642 eq_token.to_tokens(tokens);
1643 default.to_tokens(tokens);
1644 }
1645 self.semi_token.to_tokens(tokens);
1646 }
1647 }
1648
1649 impl ToTokens for TraitItemMethod {
1650 fn to_tokens(&self, tokens: &mut Tokens) {
1651 tokens.append_all(self.attrs.outer());
1652 self.sig.to_tokens(tokens);
1653 match self.default {
1654 Some(ref block) => {
1655 block.brace_token.surround(tokens, |tokens| {
1656 tokens.append_all(self.attrs.inner());
1657 tokens.append_all(&block.stmts);
1658 });
David Tolnayca085422016-10-04 00:12:38 -07001659 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001660 None => {
1661 TokensOrDefault(&self.semi_token).to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001662 }
David Tolnay1bfa7332017-11-11 12:41:20 -08001663 }
1664 }
1665 }
1666
1667 impl ToTokens for TraitItemType {
1668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 tokens.append_all(self.attrs.outer());
1670 self.type_token.to_tokens(tokens);
1671 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001672 self.generics.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001673 if !self.bounds.is_empty() {
1674 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1675 self.bounds.to_tokens(tokens);
1676 }
Nika Layzell0183ca32017-12-05 15:24:01 -05001677 self.generics.where_clause.to_tokens(tokens);
David Tolnay1bfa7332017-11-11 12:41:20 -08001678 if let Some((ref eq_token, ref default)) = self.default {
1679 eq_token.to_tokens(tokens);
1680 default.to_tokens(tokens);
1681 }
1682 self.semi_token.to_tokens(tokens);
1683 }
1684 }
1685
1686 impl ToTokens for TraitItemMacro {
1687 fn to_tokens(&self, tokens: &mut Tokens) {
1688 tokens.append_all(self.attrs.outer());
1689 self.mac.to_tokens(tokens);
1690 if !self.mac.is_braced() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001691 <Token![;]>::default().to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001692 }
1693 }
1694 }
1695
David Tolnay857628c2017-11-11 12:25:31 -08001696 impl ToTokens for ImplItemConst {
David Tolnay4c9be372016-10-06 00:47:37 -07001697 fn to_tokens(&self, tokens: &mut Tokens) {
1698 tokens.append_all(self.attrs.outer());
David Tolnay857628c2017-11-11 12:25:31 -08001699 self.vis.to_tokens(tokens);
1700 self.defaultness.to_tokens(tokens);
1701 self.const_token.to_tokens(tokens);
1702 self.ident.to_tokens(tokens);
1703 self.colon_token.to_tokens(tokens);
1704 self.ty.to_tokens(tokens);
1705 self.eq_token.to_tokens(tokens);
1706 self.expr.to_tokens(tokens);
1707 self.semi_token.to_tokens(tokens);
1708 }
1709 }
1710
1711 impl ToTokens for ImplItemMethod {
1712 fn to_tokens(&self, tokens: &mut Tokens) {
1713 tokens.append_all(self.attrs.outer());
1714 self.vis.to_tokens(tokens);
1715 self.defaultness.to_tokens(tokens);
1716 self.sig.to_tokens(tokens);
1717 self.block.brace_token.surround(tokens, |tokens| {
1718 tokens.append_all(self.attrs.inner());
1719 tokens.append_all(&self.block.stmts);
1720 });
1721 }
1722 }
1723
1724 impl ToTokens for ImplItemType {
1725 fn to_tokens(&self, tokens: &mut Tokens) {
1726 tokens.append_all(self.attrs.outer());
1727 self.vis.to_tokens(tokens);
1728 self.defaultness.to_tokens(tokens);
1729 self.type_token.to_tokens(tokens);
1730 self.ident.to_tokens(tokens);
Nika Layzell591528a2017-12-05 12:47:37 -05001731 self.generics.to_tokens(tokens);
David Tolnay857628c2017-11-11 12:25:31 -08001732 self.eq_token.to_tokens(tokens);
1733 self.ty.to_tokens(tokens);
1734 self.semi_token.to_tokens(tokens);
1735 }
1736 }
1737
1738 impl ToTokens for ImplItemMacro {
1739 fn to_tokens(&self, tokens: &mut Tokens) {
1740 tokens.append_all(self.attrs.outer());
1741 self.mac.to_tokens(tokens);
1742 if !self.mac.is_braced() {
1743 // FIXME needs a span
David Tolnayf8db7ba2017-11-11 22:52:16 -08001744 <Token![;]>::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001745 }
1746 }
1747 }
1748
David Tolnay8894f602017-11-11 12:11:04 -08001749 impl ToTokens for ForeignItemFn {
David Tolnay35902302016-10-06 01:11:08 -07001750 fn to_tokens(&self, tokens: &mut Tokens) {
1751 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001752 self.vis.to_tokens(tokens);
David Tolnay8894f602017-11-11 12:11:04 -08001753 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
1754 self.semi_token.to_tokens(tokens);
1755 }
1756 }
1757
1758 impl ToTokens for ForeignItemStatic {
1759 fn to_tokens(&self, tokens: &mut Tokens) {
1760 tokens.append_all(self.attrs.outer());
1761 self.vis.to_tokens(tokens);
1762 self.static_token.to_tokens(tokens);
1763 self.mutbl.to_tokens(tokens);
1764 self.ident.to_tokens(tokens);
1765 self.colon_token.to_tokens(tokens);
1766 self.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001767 self.semi_token.to_tokens(tokens);
1768 }
1769 }
1770
David Tolnay199bcbb2017-11-12 10:33:52 -08001771 impl ToTokens for ForeignItemType {
1772 fn to_tokens(&self, tokens: &mut Tokens) {
1773 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.semi_token.to_tokens(tokens);
1778 }
1779 }
1780
David Tolnay570695e2017-06-03 16:15:13 -07001781 impl ToTokens for MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001782 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay570695e2017-06-03 16:15:13 -07001783 self.constness.to_tokens(tokens);
1784 self.unsafety.to_tokens(tokens);
1785 self.abi.to_tokens(tokens);
1786 NamedDecl(&self.decl, self.ident).to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001787 }
1788 }
1789
David Tolnay570695e2017-06-03 16:15:13 -07001790 struct NamedDecl<'a>(&'a FnDecl, Ident);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001791
1792 impl<'a> ToTokens for NamedDecl<'a> {
1793 fn to_tokens(&self, tokens: &mut Tokens) {
1794 self.0.fn_token.to_tokens(tokens);
1795 self.1.to_tokens(tokens);
1796 self.0.generics.to_tokens(tokens);
1797 self.0.paren_token.surround(tokens, |tokens| {
1798 self.0.inputs.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001799
1800 if self.0.variadic {
1801 if !self.0.inputs.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001802 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001803 }
Alex Crichton259ee532017-07-14 06:51:02 -07001804 TokensOrDefault(&self.0.dot_tokens).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001805 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001806 });
1807 self.0.output.to_tokens(tokens);
1808 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001809 }
1810 }
1811
Alex Crichton62a0a592017-05-22 13:58:53 -07001812 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001813 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001814 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001815 self.lifetime.to_tokens(tokens);
1816 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001818 }
1819 }
1820
1821 impl ToTokens for ArgSelf {
1822 fn to_tokens(&self, tokens: &mut Tokens) {
1823 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001824 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001825 }
1826 }
1827
1828 impl ToTokens for ArgCaptured {
1829 fn to_tokens(&self, tokens: &mut Tokens) {
1830 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001831 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001832 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001833 }
1834 }
1835
David Tolnay42602292016-10-01 22:25:45 -07001836 impl ToTokens for Constness {
1837 fn to_tokens(&self, tokens: &mut Tokens) {
1838 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001839 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001840 Constness::NotConst => {
1841 // nothing
1842 }
David Tolnay42602292016-10-01 22:25:45 -07001843 }
1844 }
1845 }
1846
David Tolnay4c9be372016-10-06 00:47:37 -07001847 impl ToTokens for Defaultness {
1848 fn to_tokens(&self, tokens: &mut Tokens) {
1849 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001850 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001851 Defaultness::Final => {
1852 // nothing
1853 }
1854 }
1855 }
1856 }
1857
1858 impl ToTokens for ImplPolarity {
1859 fn to_tokens(&self, tokens: &mut Tokens) {
1860 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001861 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001862 ImplPolarity::Positive => {
1863 // nothing
1864 }
1865 }
1866 }
1867 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001868}