blob: 3f8446af7ee6218a5e243aff3b7e21b68c447cf6 [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_struct! {
5 /// An item
6 ///
7 /// The name might be a dummy name in case of anonymous items
8 pub struct Item {
9 pub ident: Ident,
10 pub vis: Visibility,
11 pub attrs: Vec<Attribute>,
12 pub node: ItemKind,
13 }
David Tolnayb79ee962016-09-04 09:39:20 -070014}
15
Alex Crichton62a0a592017-05-22 13:58:53 -070016ast_enum_of_structs! {
17 pub enum ItemKind {
18 /// An`extern crate` item, with optional original crate name.
19 ///
20 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
21 pub ExternCrate(ItemExternCrate {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070022 pub extern_token: tokens::Extern,
23 pub crate_token: tokens::Crate,
24 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -070025 pub original: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070026 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070027 }),
28 /// A use declaration (`use` or `pub use`) item.
29 ///
30 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
31 pub Use(ItemUse {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070032 pub use_token: tokens::Use,
Alex Crichton62a0a592017-05-22 13:58:53 -070033 pub path: Box<ViewPath>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070034 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070035 }),
36 /// A static item (`static` or `pub static`).
37 ///
38 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
39 pub Static(ItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070040 pub static_token: tokens::Static,
41 pub colon_token: tokens::Colon,
42 pub eq_token: tokens::Eq,
43 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070044 pub ty: Box<Ty>,
45 pub mutbl: Mutability,
46 pub expr: Box<Expr>,
47 }),
48 /// A constant item (`const` or `pub const`).
49 ///
50 /// E.g. `const FOO: i32 = 42;`
51 pub Const(ItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 pub const_token: tokens::Const,
53 pub colon_token: tokens::Colon,
54 pub eq_token: tokens::Eq,
55 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070056 pub ty: Box<Ty>,
57 pub expr: Box<Expr>,
58 }),
59 /// A function declaration (`fn` or `pub fn`).
60 ///
61 /// E.g. `fn foo(bar: usize) -> usize { .. }`
62 pub Fn(ItemFn {
63 pub decl: Box<FnDecl>,
64 pub unsafety: Unsafety,
65 pub constness: Constness,
66 pub abi: Option<Abi>,
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub block: Box<Block>,
68 }),
69 /// A module declaration (`mod` or `pub mod`).
70 ///
71 /// E.g. `mod foo;` or `mod foo { .. }`
72 pub Mod(ItemMod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070073 pub mod_token: tokens::Mod,
74 pub semi_token: Option<tokens::Semi>,
75 pub items: Option<(Vec<Item>, tokens::Brace)>,
Alex Crichton62a0a592017-05-22 13:58:53 -070076 }),
77 /// An external module (`extern` or `pub extern`).
78 ///
79 /// E.g. `extern {}` or `extern "C" {}`
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080 pub ForeignMod(ItemForeignMod {
81 pub brace_token: tokens::Brace,
82 pub abi: Abi,
83 pub items: Vec<ForeignItem>,
84 }),
85
Alex Crichton62a0a592017-05-22 13:58:53 -070086 /// A type alias (`type` or `pub type`).
87 ///
88 /// E.g. `type Foo = Bar<u8>;`
89 pub Ty(ItemTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070090 pub type_token: tokens::Type,
91 pub eq_token: tokens::Eq,
92 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 pub ty: Box<Ty>,
94 pub generics: Generics,
95 }),
96 /// An enum definition (`enum` or `pub enum`).
97 ///
98 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
99 pub Enum(ItemEnum {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 pub enum_token: tokens::Enum,
101 pub brace_token: tokens::Brace,
102 pub variants: Delimited<Variant, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 pub generics: Generics,
104 }),
105 /// A struct definition (`struct` or `pub struct`).
106 ///
107 /// E.g. `struct Foo<A> { x: A }`
108 pub Struct(ItemStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700109 pub struct_token: tokens::Struct,
Alex Crichton62a0a592017-05-22 13:58:53 -0700110 pub data: VariantData,
111 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
114 /// A union definition (`union` or `pub union`).
115 ///
116 /// E.g. `union Foo<A, B> { x: A, y: B }`
117 pub Union(ItemUnion {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 pub union_token: tokens::Union,
Alex Crichton62a0a592017-05-22 13:58:53 -0700119 pub data: VariantData,
120 pub generics: Generics,
121 }),
122 /// A Trait declaration (`trait` or `pub trait`).
123 ///
124 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
125 pub Trait(ItemTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 pub trait_token: tokens::Trait,
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 pub unsafety: Unsafety,
128 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700129 pub colon_token: Option<tokens::Colon>,
130 pub supertraits: Delimited<TyParamBound, tokens::Add>,
131 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 pub items: Vec<TraitItem>,
133 }),
134 /// Default trait implementation.
135 ///
136 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
137 pub DefaultImpl(ItemDefaultImpl {
138 pub unsafety: Unsafety,
139 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700140 pub impl_token: tokens::Impl,
141 pub for_token: tokens::For,
142 pub dot2_token: tokens::Dot2,
143 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
145 /// An implementation.
146 ///
147 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
148 pub Impl(ItemImpl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700149 pub impl_token: tokens::Impl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700150 pub unsafety: Unsafety,
151 pub polarity: ImplPolarity,
152 pub generics: Generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700153 pub for_token: Option<tokens::For>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700154 pub trait_: Option<Path>, // (optional) trait this impl implements
155 pub self_ty: Box<Ty>, // self
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub items: Vec<ImplItem>,
158 }),
159 /// A macro invocation (which includes macro definition).
160 ///
161 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
162 pub Mac(Mac),
163 }
164
165 do_not_generate_to_tokens
David Tolnayb79ee962016-09-04 09:39:20 -0700166}
167
David Tolnay0e837402016-12-22 17:25:55 -0500168impl From<DeriveInput> for Item {
169 fn from(input: DeriveInput) -> Item {
David Tolnay453cfd12016-10-23 11:00:14 -0700170 Item {
171 ident: input.ident,
172 vis: input.vis,
173 attrs: input.attrs,
174 node: match input.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700175 Body::Enum(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 ItemEnum {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700177 variants: data.variants,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 generics: input.generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700179 brace_token: data.brace_token,
180 enum_token: data.enum_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 }.into()
182 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700183 Body::Struct(data) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 ItemStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700185 data: data.data,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 generics: input.generics,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700187 semi_token: data.semi_token,
188 struct_token: data.struct_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }.into()
190 }
David Tolnay453cfd12016-10-23 11:00:14 -0700191 },
192 }
193 }
194}
195
Alex Crichton62a0a592017-05-22 13:58:53 -0700196ast_enum_of_structs! {
197 pub enum ViewPath {
198 /// `foo::bar::baz as quux`
199 ///
200 /// or just
201 ///
202 /// `foo::bar::baz` (with `as baz` implicitly on the right)
203 pub Simple(PathSimple {
204 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700205 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub rename: Option<Ident>,
207 }),
208
209 /// `foo::bar::*`
210 pub Glob(PathGlob {
211 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700212 pub colon2_token: tokens::Colon2,
213 pub star_token: tokens::Star,
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 }),
215
216 /// `foo::bar::{a, b, c}`
217 pub List(PathList {
218 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700219 pub colon2_token: tokens::Colon2,
220 pub brace_token: tokens::Brace,
221 pub items: Delimited<PathListItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700222 }),
223 }
224}
225
226ast_struct! {
227 pub struct PathListItem {
228 pub name: Ident,
229 /// renamed in list, e.g. `use foo::{bar as baz};`
230 pub rename: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 pub as_token: Option<tokens::As>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }
233}
234
235ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700236 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 pub enum Constness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700238 Const(tokens::Const),
Alex Crichton62a0a592017-05-22 13:58:53 -0700239 NotConst,
240 }
241}
242
243ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700244 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 pub enum Defaultness {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700246 Default(tokens::Default),
Alex Crichton62a0a592017-05-22 13:58:53 -0700247 Final,
248 }
249}
250
251ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700252 pub struct ForeignItem {
253 pub ident: Ident,
254 pub attrs: Vec<Attribute>,
255 pub node: ForeignItemKind,
256 pub vis: Visibility,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700257 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 }
259}
260
261ast_enum_of_structs! {
262 /// An item within an `extern` block
263 pub enum ForeignItemKind {
264 /// A foreign function
265 pub Fn(ForeignItemFn {
266 pub decl: Box<FnDecl>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 }),
268 /// A foreign static item (`static ext: u8`)
269 pub Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700270 pub static_token: tokens::Static,
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 pub ty: Box<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700272 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 pub mutbl: Mutability,
274 }),
275 }
276
277 do_not_generate_to_tokens
278}
279
280ast_struct! {
281 /// Represents an item declaration within a trait declaration,
282 /// possibly including a default implementation. A trait item is
283 /// either required (meaning it doesn't have an implementation, just a
284 /// signature) or provided (meaning it has a default implementation).
285 pub struct TraitItem {
286 pub ident: Ident,
287 pub attrs: Vec<Attribute>,
288 pub node: TraitItemKind,
289 }
290}
291
292ast_enum_of_structs! {
293 pub enum TraitItemKind {
294 pub Const(TraitItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700295 pub const_token: tokens::Const,
296 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 pub ty: Ty,
298 pub default: Option<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700299 pub eq_token: Option<tokens::Eq>,
300 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700301 }),
302 pub Method(TraitItemMethod {
303 pub sig: MethodSig,
304 pub default: Option<Block>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700305 pub semi_token: Option<tokens::Semi>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 }),
307 pub Type(TraitItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700308 pub type_token: tokens::Type,
309 pub colon_token: Option<tokens::Colon>,
310 pub bounds: Delimited<TyParamBound, tokens::Add>,
311 pub eq_token: Option<tokens::Eq>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700312 pub default: Option<Ty>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700313 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 }),
315 pub Macro(Mac),
316 }
317
318 do_not_generate_to_tokens
319}
320
321ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700322 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 pub enum ImplPolarity {
324 /// `impl Trait for Type`
325 Positive,
326 /// `impl !Trait for Type`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700327 Negative(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700328 }
329}
330
331ast_struct! {
332 pub struct ImplItem {
333 pub ident: Ident,
334 pub vis: Visibility,
335 pub defaultness: Defaultness,
336 pub attrs: Vec<Attribute>,
337 pub node: ImplItemKind,
338 }
339}
340
341ast_enum_of_structs! {
342 pub enum ImplItemKind {
343 pub Const(ImplItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700344 pub const_token: tokens::Const,
345 pub colon_token: tokens::Colon,
346 pub eq_token: tokens::Eq,
347 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 pub ty: Ty,
349 pub expr: Expr,
350 }),
351 pub Method(ImplItemMethod {
352 pub sig: MethodSig,
353 pub block: Block,
354 }),
355 pub Type(ImplItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700356 pub type_token: tokens::Type,
357 pub eq_token: tokens::Eq,
358 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub ty: Ty,
360 }),
361 pub Macro(Mac),
362 }
363
364 do_not_generate_to_tokens
365}
366
367ast_struct! {
368 /// Represents a method's signature in a trait declaration,
369 /// or in an implementation.
370 pub struct MethodSig {
371 pub unsafety: Unsafety,
372 pub constness: Constness,
373 pub abi: Option<Abi>,
374 pub decl: FnDecl,
Alex Crichton62a0a592017-05-22 13:58:53 -0700375 }
376}
377
378ast_struct! {
379 /// Header (not the body) of a function declaration.
David Tolnayf38cdf62016-09-23 19:07:09 -0700380 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 /// E.g. `fn foo(bar: baz)`
382 pub struct FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700383 pub fn_token: tokens::Fn,
384 pub paren_token: tokens::Paren,
385 pub inputs: Delimited<FnArg, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 pub output: FunctionRetTy,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700387 pub generics: Generics,
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 pub variadic: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700389 pub dot_tokens: Option<tokens::Dot3>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 }
David Tolnayf38cdf62016-09-23 19:07:09 -0700391}
392
Alex Crichton62a0a592017-05-22 13:58:53 -0700393ast_enum_of_structs! {
394 /// An argument in a function header.
395 ///
396 /// E.g. `bar: usize` as in `fn foo(bar: usize)`
397 pub enum FnArg {
398 pub SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700399 pub and_token: tokens::And,
400 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 pub lifetime: Option<Lifetime>,
402 pub mutbl: Mutability,
403 }),
404 pub SelfValue(ArgSelf {
405 pub mutbl: Mutability,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700406 pub self_token: tokens::Self_,
Alex Crichton62a0a592017-05-22 13:58:53 -0700407 }),
408 pub Captured(ArgCaptured {
409 pub pat: Pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700410 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 pub ty: Ty,
412 }),
413 pub Ignored(Ty),
414 }
David Tolnay62f374c2016-10-02 13:37:00 -0700415}
416
David Tolnayedf2b992016-09-23 20:43:45 -0700417#[cfg(feature = "parsing")]
418pub mod parsing {
419 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700420 use {Block, Generics, Ident, Mac, Path, VariantData};
David Tolnay7b8009b2016-10-25 22:36:00 -0700421 use attr::parsing::{inner_attr, outer_attr};
David Tolnay2f9fa632016-10-03 22:08:48 -0700422 use data::parsing::{struct_like_body, visibility};
David Tolnay5859df12016-10-29 22:49:54 -0700423 use expr::parsing::{expr, pat, within_block};
David Tolnayca085422016-10-04 00:12:38 -0700424 use generics::parsing::{generics, lifetime, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700425 use ident::parsing::ident;
David Tolnay84aa0752016-10-02 23:01:13 -0700426 use mac::parsing::delimited;
David Tolnay0e837402016-12-22 17:25:55 -0500427 use derive::{Body, DeriveInput};
428 use derive::parsing::derive_input;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700429 use ty::parsing::{abi, mutability, path, ty, unsafety, fn_ret_ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700430
431 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700432 item_extern_crate
David Tolnay4a057422016-10-08 00:02:31 -0700433 |
434 item_use
David Tolnay47a877c2016-10-01 16:50:55 -0700435 |
436 item_static
437 |
438 item_const
David Tolnay42602292016-10-01 22:25:45 -0700439 |
440 item_fn
David Tolnay35902302016-10-06 01:11:08 -0700441 |
442 item_mod
443 |
444 item_foreign_mod
David Tolnay3cf52982016-10-01 17:11:37 -0700445 |
446 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700447 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700448 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700449 |
450 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700451 |
452 item_trait
David Tolnayf94e2362016-10-04 00:29:51 -0700453 |
454 item_default_impl
David Tolnay4c9be372016-10-06 00:47:37 -0700455 |
456 item_impl
David Tolnay84aa0752016-10-02 23:01:13 -0700457 |
458 item_mac
459 ));
460
David Tolnay453cfd12016-10-23 11:00:14 -0700461 named!(pub items -> Vec<Item>, many0!(item));
462
David Tolnay84aa0752016-10-02 23:01:13 -0700463 named!(item_mac -> Item, do_parse!(
464 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -0500465 what: path >>
David Tolnay84aa0752016-10-02 23:01:13 -0700466 punct!("!") >>
467 name: option!(ident) >>
468 body: delimited >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700469 cond!(!body.is_braced(), punct!(";")) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700470 (Item {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700471 ident: name.unwrap_or_else(|| Ident::from("")),
472 vis: VisInherited {}.into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700473 attrs: attrs,
474 node: ItemKind::Mac(Mac {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700475 bang_token: tokens::Bang::default(),
David Tolnay5d55ef72016-12-21 20:20:04 -0500476 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700477 tokens: vec![body],
David Tolnay84aa0752016-10-02 23:01:13 -0700478 }),
479 })
David Tolnayedf2b992016-09-23 20:43:45 -0700480 ));
481
David Tolnaya96a3fa2016-09-24 07:17:42 -0700482 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700483 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700484 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700485 keyword!("extern") >>
486 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700487 id: ident >>
488 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700489 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700490 ident
491 )) >>
492 punct!(";") >>
493 ({
494 let (name, original_name) = match rename {
495 Some(rename) => (rename, Some(id)),
496 None => (id, None),
497 };
498 Item {
499 ident: name,
500 vis: vis,
501 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700502 node: ItemExternCrate {
503 as_token: original_name.as_ref().map(|_| tokens::As::default()),
504 original: original_name,
505 extern_token: tokens::Extern::default(),
506 crate_token: tokens::Crate::default(),
507 semi_token: tokens::Semi::default(),
508 }.into(),
David Tolnayedf2b992016-09-23 20:43:45 -0700509 }
510 })
511 ));
512
David Tolnay4a057422016-10-08 00:02:31 -0700513 named!(item_use -> Item, do_parse!(
514 attrs: many0!(outer_attr) >>
515 vis: visibility >>
516 keyword!("use") >>
517 what: view_path >>
518 punct!(";") >>
519 (Item {
520 ident: "".into(),
521 vis: vis,
522 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700523 node: ItemUse {
524 path: Box::new(what),
525 use_token: tokens::Use::default(),
526 semi_token: tokens::Semi::default(),
527 }.into(),
David Tolnay4a057422016-10-08 00:02:31 -0700528 })
529 ));
530
531 named!(view_path -> ViewPath, alt!(
532 view_path_glob
533 |
534 view_path_list
535 |
536 view_path_list_root
537 |
538 view_path_simple // must be last
539 ));
540
541
542 named!(view_path_simple -> ViewPath, do_parse!(
543 path: path >>
544 rename: option!(preceded!(keyword!("as"), ident)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700545 (PathSimple {
546 path: path,
547 as_token: rename.as_ref().map(|_| tokens::As::default()),
548 rename: rename,
549 }.into())
David Tolnay4a057422016-10-08 00:02:31 -0700550 ));
551
552 named!(view_path_glob -> ViewPath, do_parse!(
553 path: path >>
554 punct!("::") >>
555 punct!("*") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 (PathGlob {
557 path: path,
558 colon2_token: tokens::Colon2::default(),
559 star_token: tokens::Star::default(),
560 }.into())
David Tolnay4a057422016-10-08 00:02:31 -0700561 ));
562
563 named!(view_path_list -> ViewPath, do_parse!(
564 path: path >>
565 punct!("::") >>
566 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700567 items: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
568 path_list_item) >>
David Tolnay4a057422016-10-08 00:02:31 -0700569 punct!("}") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 (PathList {
571 path: path,
572 items: items,
573 brace_token: tokens::Brace::default(),
574 colon2_token: tokens::Colon2::default(),
575 }.into())
David Tolnay4a057422016-10-08 00:02:31 -0700576 ));
577
578 named!(view_path_list_root -> ViewPath, do_parse!(
579 global: option!(punct!("::")) >>
580 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581 items: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
582 path_list_item) >>
David Tolnay4a057422016-10-08 00:02:31 -0700583 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 (PathList {
585 path: Path {
586 global: global.is_some(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 segments: Delimited::new(),
588 leading_colon: None,
Alex Crichton62a0a592017-05-22 13:58:53 -0700589 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700590 colon2_token: tokens::Colon2::default(),
591 brace_token: tokens::Brace::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 items: items,
593 }.into())
David Tolnay4a057422016-10-08 00:02:31 -0700594 ));
595
596 named!(path_list_item -> PathListItem, do_parse!(
David Tolnaye6e42542016-10-24 22:37:11 -0700597 name: alt!(
598 ident
599 |
600 map!(keyword!("self"), Into::into)
601 ) >>
David Tolnay4a057422016-10-08 00:02:31 -0700602 rename: option!(preceded!(keyword!("as"), ident)) >>
603 (PathListItem {
604 name: name,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 as_token: rename.as_ref().map(|_| tokens::As::default()),
David Tolnay4a057422016-10-08 00:02:31 -0700606 rename: rename,
607 })
608 ));
609
David Tolnay47a877c2016-10-01 16:50:55 -0700610 named!(item_static -> Item, do_parse!(
611 attrs: many0!(outer_attr) >>
612 vis: visibility >>
613 keyword!("static") >>
614 mutability: mutability >>
615 id: ident >>
616 punct!(":") >>
617 ty: ty >>
618 punct!("=") >>
619 value: expr >>
620 punct!(";") >>
621 (Item {
622 ident: id,
623 vis: vis,
624 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 node: ItemStatic {
626 ty: Box::new(ty),
627 mutbl: mutability,
628 expr: Box::new(value),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700629 static_token: tokens::Static::default(),
630 colon_token: tokens::Colon::default(),
631 eq_token: tokens::Eq::default(),
632 semi_token: tokens::Semi::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700634 })
635 ));
636
637 named!(item_const -> Item, do_parse!(
638 attrs: many0!(outer_attr) >>
639 vis: visibility >>
640 keyword!("const") >>
641 id: ident >>
642 punct!(":") >>
643 ty: ty >>
644 punct!("=") >>
645 value: expr >>
646 punct!(";") >>
647 (Item {
648 ident: id,
649 vis: vis,
650 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700651 node: ItemConst {
652 ty: Box::new(ty),
653 expr: Box::new(value),
654 const_token: tokens::Const::default(),
655 colon_token: tokens::Colon::default(),
656 eq_token: tokens::Eq::default(),
657 semi_token: tokens::Semi::default(),
658 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700659 })
660 ));
661
David Tolnay42602292016-10-01 22:25:45 -0700662 named!(item_fn -> Item, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -0700663 outer_attrs: many0!(outer_attr) >>
David Tolnay42602292016-10-01 22:25:45 -0700664 vis: visibility >>
665 constness: constness >>
666 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700667 abi: option!(abi) >>
David Tolnay42602292016-10-01 22:25:45 -0700668 keyword!("fn") >>
669 name: ident >>
670 generics: generics >>
671 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700672 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
673 fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700674 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700675 ret: fn_ret_ty >>
David Tolnay42602292016-10-01 22:25:45 -0700676 where_clause: where_clause >>
Michael Layzell416724e2017-05-24 21:12:34 -0400677 inner_attrs_stmts: delim!(Brace, tuple!(
678 many0!(inner_attr), within_block
679 )) >>
David Tolnay42602292016-10-01 22:25:45 -0700680 (Item {
681 ident: name,
682 vis: vis,
David Tolnay3b9783a2016-10-29 22:37:09 -0700683 attrs: {
684 let mut attrs = outer_attrs;
Michael Layzell416724e2017-05-24 21:12:34 -0400685 attrs.extend(inner_attrs_stmts.0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700686 attrs
687 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 node: ItemFn {
689 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700690 dot_tokens: None,
691 fn_token: tokens::Fn::default(),
692 paren_token: tokens::Paren::default(),
David Tolnay42602292016-10-01 22:25:45 -0700693 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700694 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700695 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700696 generics: Generics {
697 where_clause: where_clause,
698 .. generics
699 },
David Tolnay42602292016-10-01 22:25:45 -0700700 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 unsafety: unsafety,
702 constness: constness,
703 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 block: Box::new(Block {
David Tolnay3b9783a2016-10-29 22:37:09 -0700705 stmts: stmts,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700706 brace_token: tokens::Brace::default(),
David Tolnay3b9783a2016-10-29 22:37:09 -0700707 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700708 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700709 })
710 ));
711
David Tolnayca085422016-10-04 00:12:38 -0700712 named!(fn_arg -> FnArg, alt!(
713 do_parse!(
714 punct!("&") >>
715 lt: option!(lifetime) >>
716 mutability: mutability >>
717 keyword!("self") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500718 not!(punct!(":")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700719 (ArgSelfRef {
720 lifetime: lt,
721 mutbl: mutability,
722 and_token: tokens::And::default(),
723 self_token: tokens::Self_::default(),
724 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700725 )
726 |
727 do_parse!(
728 mutability: mutability >>
729 keyword!("self") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500730 not!(punct!(":")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700731 (ArgSelf {
732 mutbl: mutability,
733 self_token: tokens::Self_::default(),
734 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700735 )
736 |
737 do_parse!(
738 pat: pat >>
739 punct!(":") >>
740 ty: ty >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700741 (ArgCaptured {
742 pat: pat,
743 ty: ty,
744 colon_token: tokens::Colon::default(),
745 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700746 )
747 |
748 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700749 ));
750
David Tolnay35902302016-10-06 01:11:08 -0700751 named!(item_mod -> Item, do_parse!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700752 outer_attrs: many0!(outer_attr) >>
David Tolnay35902302016-10-06 01:11:08 -0700753 vis: visibility >>
754 keyword!("mod") >>
755 id: ident >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700756 content: alt!(
David Tolnay37d10332016-10-13 20:51:04 -0700757 punct!(";") => { |_| None }
758 |
Michael Layzell416724e2017-05-24 21:12:34 -0400759 delim!(
760 Brace,
David Tolnay7b8009b2016-10-25 22:36:00 -0700761 tuple!(
762 many0!(inner_attr),
763 items
Michael Layzell416724e2017-05-24 21:12:34 -0400764 )
David Tolnay7b8009b2016-10-25 22:36:00 -0700765 ) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700766 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700767 (match content {
768 Some((inner_attrs, items)) => Item {
769 ident: id,
770 vis: vis,
771 attrs: {
772 let mut attrs = outer_attrs;
773 attrs.extend(inner_attrs);
774 attrs
775 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700776 node: ItemMod {
777 mod_token: tokens::Mod::default(),
778 semi_token: None,
779 items: Some((items, tokens::Brace::default())),
780 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700781 },
782 None => Item {
783 ident: id,
784 vis: vis,
785 attrs: outer_attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700786 node: ItemMod {
787 items: None,
788 mod_token: tokens::Mod::default(),
789 semi_token: Some(tokens::Semi::default()),
790 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700791 },
David Tolnay35902302016-10-06 01:11:08 -0700792 })
793 ));
794
795 named!(item_foreign_mod -> Item, do_parse!(
796 attrs: many0!(outer_attr) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700797 abi: abi >>
Michael Layzell416724e2017-05-24 21:12:34 -0400798 items: delim!(Brace, many0!(foreign_item)) >>
David Tolnay35902302016-10-06 01:11:08 -0700799 (Item {
800 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700801 vis: VisInherited {}.into(),
David Tolnay35902302016-10-06 01:11:08 -0700802 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700803 node: ItemForeignMod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700804 brace_token: tokens::Brace::default(),
David Tolnayb8d8ef52016-10-29 14:30:08 -0700805 abi: abi,
David Tolnay35902302016-10-06 01:11:08 -0700806 items: items,
Alex Crichton62a0a592017-05-22 13:58:53 -0700807 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700808 })
809 ));
810
811 named!(foreign_item -> ForeignItem, alt!(
812 foreign_fn
813 |
814 foreign_static
815 ));
816
817 named!(foreign_fn -> ForeignItem, do_parse!(
818 attrs: many0!(outer_attr) >>
819 vis: visibility >>
820 keyword!("fn") >>
821 name: ident >>
822 generics: generics >>
823 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700824 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
825 fn_arg) >>
826 variadic: cond!(inputs.is_empty() || inputs.trailing_delim(),
827 option!(punct!("..."))) >>
David Tolnay35902302016-10-06 01:11:08 -0700828 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700829 ret: fn_ret_ty >>
David Tolnay35902302016-10-06 01:11:08 -0700830 where_clause: where_clause >>
831 punct!(";") >>
832 (ForeignItem {
833 ident: name,
834 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700835 semi_token: tokens::Semi::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700836 node: ForeignItemFn {
837 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700838 fn_token: tokens::Fn::default(),
839 paren_token: tokens::Paren::default(),
David Tolnay35902302016-10-06 01:11:08 -0700840 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700841 variadic: variadic.map(|m| m.is_some()).unwrap_or(false),
842 dot_tokens: if variadic.map(|m| m.is_some()).unwrap_or(false) {
843 Some(tokens::Dot3::default())
844 } else {
845 None
846 },
847 output: ret,
848 generics: Generics {
849 where_clause: where_clause,
850 .. generics
851 },
David Tolnay35902302016-10-06 01:11:08 -0700852 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700853 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700854 vis: vis,
855 })
856 ));
857
858 named!(foreign_static -> ForeignItem, do_parse!(
859 attrs: many0!(outer_attr) >>
860 vis: visibility >>
861 keyword!("static") >>
862 mutability: mutability >>
863 id: ident >>
864 punct!(":") >>
865 ty: ty >>
866 punct!(";") >>
867 (ForeignItem {
868 ident: id,
869 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700870 semi_token: tokens::Semi::default(),
871 node: ForeignItemStatic {
872 ty: Box::new(ty),
873 mutbl: mutability,
874 static_token: tokens::Static::default(),
875 colon_token: tokens::Colon::default(),
876 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700877 vis: vis,
878 })
879 ));
880
David Tolnay3cf52982016-10-01 17:11:37 -0700881 named!(item_ty -> Item, do_parse!(
882 attrs: many0!(outer_attr) >>
883 vis: visibility >>
884 keyword!("type") >>
885 id: ident >>
886 generics: generics >>
David Tolnay04bb3ad2016-10-30 10:59:01 -0700887 where_clause: where_clause >>
David Tolnay3cf52982016-10-01 17:11:37 -0700888 punct!("=") >>
889 ty: ty >>
890 punct!(";") >>
891 (Item {
892 ident: id,
893 vis: vis,
894 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700895 node: ItemTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 type_token: tokens::Type::default(),
897 eq_token: tokens::Eq::default(),
898 semi_token: tokens::Semi::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700899 ty: Box::new(ty),
900 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700901 where_clause: where_clause,
902 ..generics
903 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700904 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700905 })
906 ));
907
David Tolnaya96a3fa2016-09-24 07:17:42 -0700908 named!(item_struct_or_enum -> Item, map!(
David Tolnay0e837402016-12-22 17:25:55 -0500909 derive_input,
910 |def: DeriveInput| Item {
David Tolnayedf2b992016-09-23 20:43:45 -0700911 ident: def.ident,
912 vis: def.vis,
913 attrs: def.attrs,
914 node: match def.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700915 Body::Enum(data) => {
916 ItemEnum {
917 variants: data.variants,
918 brace_token: data.brace_token,
919 enum_token: data.enum_token,
920 generics: def.generics,
921 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700922 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700923 Body::Struct(data) => {
924 ItemStruct {
925 data: data.data,
926 struct_token: data.struct_token,
927 semi_token: data.semi_token,
928 generics: def.generics,
929 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700930 }
931 }
932 }
933 ));
David Tolnay42602292016-10-01 22:25:45 -0700934
David Tolnay2f9fa632016-10-03 22:08:48 -0700935 named!(item_union -> Item, do_parse!(
936 attrs: many0!(outer_attr) >>
937 vis: visibility >>
938 keyword!("union") >>
939 id: ident >>
940 generics: generics >>
941 where_clause: where_clause >>
942 fields: struct_like_body >>
943 (Item {
944 ident: id,
945 vis: vis,
946 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700947 node: ItemUnion {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 union_token: tokens::Union::default(),
949 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700951 where_clause: where_clause,
952 .. generics
953 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700955 })
956 ));
957
David Tolnay0aecb732016-10-03 23:03:50 -0700958 named!(item_trait -> Item, do_parse!(
959 attrs: many0!(outer_attr) >>
960 vis: visibility >>
961 unsafety: unsafety >>
962 keyword!("trait") >>
963 id: ident >>
964 generics: generics >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700965 colon: option!(punct!(":")) >>
966 bounds: cond!(colon.is_some(),
967 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
968 ty_param_bound)
969 ) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700970 where_clause: where_clause >>
Michael Layzell416724e2017-05-24 21:12:34 -0400971 body: delim!(Brace, many0!(trait_item)) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700972 (Item {
973 ident: id,
974 vis: vis,
975 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700976 node: ItemTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700977 trait_token: tokens::Trait::default(),
978 brace_token: tokens::Brace::default(),
979 colon_token: colon.map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700980 unsafety: unsafety,
981 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -0700982 where_clause: where_clause,
983 .. generics
984 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700985 supertraits: bounds.unwrap_or_default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 items: body,
987 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -0700988 })
989 ));
990
David Tolnayf94e2362016-10-04 00:29:51 -0700991 named!(item_default_impl -> Item, do_parse!(
992 attrs: many0!(outer_attr) >>
993 unsafety: unsafety >>
994 keyword!("impl") >>
995 path: path >>
996 keyword!("for") >>
997 punct!("..") >>
Michael Layzell416724e2017-05-24 21:12:34 -0400998 delim!(Brace, epsilon!()) >>
David Tolnayf94e2362016-10-04 00:29:51 -0700999 (Item {
1000 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001001 vis: VisInherited {}.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001002 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001003 node: ItemDefaultImpl {
1004 unsafety: unsafety,
1005 path: path,
1006 impl_token: tokens::Impl::default(),
1007 for_token: tokens::For::default(),
1008 dot2_token: tokens::Dot2::default(),
1009 brace_token: tokens::Brace::default(),
1010 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001011 })
1012 ));
1013
David Tolnay0aecb732016-10-03 23:03:50 -07001014 named!(trait_item -> TraitItem, alt!(
1015 trait_item_const
1016 |
1017 trait_item_method
1018 |
1019 trait_item_type
1020 |
1021 trait_item_mac
1022 ));
1023
1024 named!(trait_item_const -> TraitItem, do_parse!(
1025 attrs: many0!(outer_attr) >>
1026 keyword!("const") >>
1027 id: ident >>
1028 punct!(":") >>
1029 ty: ty >>
1030 value: option!(preceded!(punct!("="), expr)) >>
1031 punct!(";") >>
1032 (TraitItem {
1033 ident: id,
1034 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001035 node: TraitItemConst {
1036 ty: ty,
1037 const_token: tokens::Const::default(),
1038 colon_token: tokens::Colon::default(),
1039 eq_token: value.as_ref().map(|_| tokens::Eq::default()),
1040 default: value,
1041 semi_token: tokens::Semi::default(),
1042 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001043 })
1044 ));
1045
1046 named!(trait_item_method -> TraitItem, do_parse!(
David Tolnay5859df12016-10-29 22:49:54 -07001047 outer_attrs: many0!(outer_attr) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001048 constness: constness >>
1049 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -07001050 abi: option!(abi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001051 keyword!("fn") >>
1052 name: ident >>
1053 generics: generics >>
1054 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001055 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001056 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001057 ret: fn_ret_ty >>
David Tolnay0aecb732016-10-03 23:03:50 -07001058 where_clause: where_clause >>
Michael Layzell416724e2017-05-24 21:12:34 -04001059 body: option!(delim!(
1060 Brace,
1061 tuple!(many0!(inner_attr), within_block)
David Tolnay5859df12016-10-29 22:49:54 -07001062 )) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001063 semi: cond!(body.is_none(), punct!(";")) >>
David Tolnay5859df12016-10-29 22:49:54 -07001064 ({
1065 let (inner_attrs, stmts) = match body {
1066 Some((inner_attrs, stmts)) => (inner_attrs, Some(stmts)),
1067 None => (Vec::new(), None),
1068 };
1069 TraitItem {
1070 ident: name,
1071 attrs: {
1072 let mut attrs = outer_attrs;
1073 attrs.extend(inner_attrs);
1074 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001075 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001076 node: TraitItemMethod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001077 semi_token: semi.map(|_| tokens::Semi::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001078 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001079 unsafety: unsafety,
1080 constness: constness,
1081 abi: abi,
1082 decl: FnDecl {
1083 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001084 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001085 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001086 fn_token: tokens::Fn::default(),
1087 paren_token: tokens::Paren::default(),
1088 dot_tokens: None,
1089 generics: Generics {
1090 where_clause: where_clause,
1091 .. generics
1092 },
David Tolnay5859df12016-10-29 22:49:54 -07001093 },
1094 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001095 default: stmts.map(|stmts| {
1096 Block {
1097 stmts: stmts,
1098 brace_token: tokens::Brace::default(),
1099 }
1100 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001101 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001102 }
David Tolnay0aecb732016-10-03 23:03:50 -07001103 })
1104 ));
1105
1106 named!(trait_item_type -> TraitItem, do_parse!(
1107 attrs: many0!(outer_attr) >>
1108 keyword!("type") >>
1109 id: ident >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001110 colon: option!(punct!(":")) >>
1111 bounds: cond!(colon.is_some(),
1112 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
1113 ty_param_bound)
1114 ) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001115 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -07001116 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -07001117 (TraitItem {
1118 ident: id,
1119 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001120 node: TraitItemType {
1121 type_token: tokens::Type::default(),
1122 colon_token: colon.map(|_| tokens::Colon::default()),
1123 bounds: bounds.unwrap_or_default(),
1124 eq_token: default.as_ref().map(|_| tokens::Eq::default()),
1125 semi_token: tokens::Semi::default(),
1126 default: default,
1127 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001128 })
1129 ));
1130
1131 named!(trait_item_mac -> TraitItem, do_parse!(
1132 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001133 what: path >>
David Tolnay0aecb732016-10-03 23:03:50 -07001134 punct!("!") >>
1135 body: delimited >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001136 cond!(!body.is_braced(), punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001137 (TraitItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001138 ident: "".into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001139 attrs: attrs,
1140 node: TraitItemKind::Macro(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001141 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001142 bang_token: tokens::Bang::default(),
1143 tokens: vec![body],
David Tolnay0aecb732016-10-03 23:03:50 -07001144 }),
1145 })
1146 ));
1147
David Tolnay4c9be372016-10-06 00:47:37 -07001148 named!(item_impl -> Item, do_parse!(
1149 attrs: many0!(outer_attr) >>
1150 unsafety: unsafety >>
1151 keyword!("impl") >>
1152 generics: generics >>
1153 polarity_path: alt!(
1154 do_parse!(
1155 polarity: impl_polarity >>
1156 path: path >>
1157 keyword!("for") >>
David Tolnay02a8d472017-02-19 12:59:44 -08001158 (polarity, Some(path))
David Tolnay4c9be372016-10-06 00:47:37 -07001159 )
1160 |
1161 epsilon!() => { |_| (ImplPolarity::Positive, None) }
1162 ) >>
1163 self_ty: ty >>
1164 where_clause: where_clause >>
Michael Layzell416724e2017-05-24 21:12:34 -04001165 body: delim!(Brace, many0!(impl_item)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001166 (Item {
1167 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001168 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001169 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001170 node: ItemImpl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001171 impl_token: tokens::Impl::default(),
1172 brace_token: tokens::Brace::default(),
1173 for_token: polarity_path.1.as_ref().map(|_| tokens::For::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001174 unsafety: unsafety,
1175 polarity: polarity_path.0,
1176 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001177 where_clause: where_clause,
1178 .. generics
1179 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001180 trait_: polarity_path.1,
1181 self_ty: Box::new(self_ty),
1182 items: body,
1183 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001184 })
1185 ));
1186
1187 named!(impl_item -> ImplItem, alt!(
1188 impl_item_const
1189 |
1190 impl_item_method
1191 |
1192 impl_item_type
1193 |
1194 impl_item_macro
1195 ));
1196
1197 named!(impl_item_const -> ImplItem, do_parse!(
1198 attrs: many0!(outer_attr) >>
1199 vis: visibility >>
1200 defaultness: defaultness >>
1201 keyword!("const") >>
1202 id: ident >>
1203 punct!(":") >>
1204 ty: ty >>
1205 punct!("=") >>
1206 value: expr >>
1207 punct!(";") >>
1208 (ImplItem {
1209 ident: id,
1210 vis: vis,
1211 defaultness: defaultness,
1212 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001213 node: ImplItemConst {
1214 ty: ty,
1215 expr: value,
1216 const_token: tokens::Const::default(),
1217 colon_token: tokens::Colon::default(),
1218 eq_token: tokens::Eq::default(),
1219 semi_token: tokens::Semi::default(),
1220 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001221 })
1222 ));
1223
1224 named!(impl_item_method -> ImplItem, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -07001225 outer_attrs: many0!(outer_attr) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001226 vis: visibility >>
1227 defaultness: defaultness >>
1228 constness: constness >>
1229 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -07001230 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001231 keyword!("fn") >>
1232 name: ident >>
1233 generics: generics >>
1234 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001235 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1236 fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001237 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001238 ret: fn_ret_ty >>
David Tolnay4c9be372016-10-06 00:47:37 -07001239 where_clause: where_clause >>
Michael Layzell416724e2017-05-24 21:12:34 -04001240 inner_attrs_stmts: delim!(Brace, tuple!(
1241 many0!(inner_attr), within_block
1242 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001243 (ImplItem {
1244 ident: name,
1245 vis: vis,
1246 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -07001247 attrs: {
1248 let mut attrs = outer_attrs;
Michael Layzell416724e2017-05-24 21:12:34 -04001249 attrs.extend(inner_attrs_stmts.0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001250 attrs
1251 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001252 node: ImplItemMethod {
1253 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001254 unsafety: unsafety,
1255 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001256 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -07001257 decl: FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001258 fn_token: tokens::Fn::default(),
1259 paren_token: tokens::Paren::default(),
David Tolnay4c9be372016-10-06 00:47:37 -07001260 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001261 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001262 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001263 generics: Generics {
1264 where_clause: where_clause,
1265 .. generics
1266 },
1267 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001268 },
1269 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001270 block: Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001271 brace_token: tokens::Brace::default(),
David Tolnay3b9783a2016-10-29 22:37:09 -07001272 stmts: stmts,
1273 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001274 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001275 })
1276 ));
1277
1278 named!(impl_item_type -> ImplItem, do_parse!(
1279 attrs: many0!(outer_attr) >>
1280 vis: visibility >>
1281 defaultness: defaultness >>
1282 keyword!("type") >>
1283 id: ident >>
1284 punct!("=") >>
1285 ty: ty >>
1286 punct!(";") >>
1287 (ImplItem {
1288 ident: id,
1289 vis: vis,
1290 defaultness: defaultness,
1291 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001292 node: ImplItemType {
1293 type_token: tokens::Type::default(),
1294 eq_token: tokens::Eq::default(),
1295 semi_token: tokens::Semi::default(),
1296 ty: ty,
1297 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001298 })
1299 ));
1300
1301 named!(impl_item_macro -> ImplItem, do_parse!(
1302 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001303 what: path >>
David Tolnay4c9be372016-10-06 00:47:37 -07001304 punct!("!") >>
1305 body: delimited >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001306 cond!(!body.is_braced(), punct!(";")) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001307 (ImplItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001308 ident: "".into(),
1309 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001310 defaultness: Defaultness::Final,
1311 attrs: attrs,
1312 node: ImplItemKind::Macro(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001313 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001314 bang_token: tokens::Bang::default(),
1315 tokens: vec![body],
David Tolnay4c9be372016-10-06 00:47:37 -07001316 }),
1317 })
1318 ));
1319
1320 named!(impl_polarity -> ImplPolarity, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001321 punct!("!") => { |_| ImplPolarity::Negative(tokens::Bang::default()) }
David Tolnay4c9be372016-10-06 00:47:37 -07001322 |
1323 epsilon!() => { |_| ImplPolarity::Positive }
1324 ));
1325
David Tolnay42602292016-10-01 22:25:45 -07001326 named!(constness -> Constness, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001327 keyword!("const") => { |_| Constness::Const(tokens::Const::default()) }
David Tolnay42602292016-10-01 22:25:45 -07001328 |
1329 epsilon!() => { |_| Constness::NotConst }
1330 ));
1331
David Tolnay4c9be372016-10-06 00:47:37 -07001332 named!(defaultness -> Defaultness, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001333 keyword!("default") => { |_| Defaultness::Default(tokens::Default::default()) }
David Tolnay4c9be372016-10-06 00:47:37 -07001334 |
1335 epsilon!() => { |_| Defaultness::Final }
1336 ));
David Tolnayedf2b992016-09-23 20:43:45 -07001337}
David Tolnay4a51dc72016-10-01 00:40:31 -07001338
1339#[cfg(feature = "printing")]
1340mod printing {
1341 use super::*;
1342 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001343 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001344 use quote::{Tokens, ToTokens};
1345
1346 impl ToTokens for Item {
1347 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001348 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001349 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001350 ItemKind::ExternCrate(ref item) => {
David Tolnaycbd9f7d2016-10-30 00:26:29 -07001351 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001352 item.extern_token.to_tokens(tokens);
1353 item.crate_token.to_tokens(tokens);
1354 item.original.to_tokens(tokens);
1355 item.as_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001356 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001357 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001358 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001359 ItemKind::Use(ref item) => {
David Tolnay4a057422016-10-08 00:02:31 -07001360 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001361 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001362 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001363 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001364 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001365 ItemKind::Static(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001366 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001367 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001368 item.mutbl.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001369 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001370 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001371 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001372 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001373 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001375 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001376 ItemKind::Const(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001377 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001378 item.const_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001379 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001380 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001381 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001382 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001383 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001384 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001385 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001386 ItemKind::Fn(ref item) => {
David Tolnay42602292016-10-01 22:25:45 -07001387 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001388 item.constness.to_tokens(tokens);
1389 item.unsafety.to_tokens(tokens);
1390 item.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001391 NamedDecl(&item.decl, &self.ident).to_tokens(tokens);
1392 item.block.brace_token.surround(tokens, |tokens| {
1393 tokens.append_all(self.attrs.inner());
1394 tokens.append_all(&item.block.stmts);
1395 });
David Tolnay42602292016-10-01 22:25:45 -07001396 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001397 ItemKind::Mod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001398 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001399 item.mod_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001400 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001401 if let Some((ref items, ref brace)) = item.items {
1402 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001403 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001404 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001405 });
David Tolnay37d10332016-10-13 20:51:04 -07001406 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001407 item.semi_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001408 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001409 ItemKind::ForeignMod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001410 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001411 item.abi.to_tokens(tokens);
1412 item.brace_token.surround(tokens, |tokens| {
1413 tokens.append_all(&item.items);
1414 });
David Tolnay35902302016-10-06 01:11:08 -07001415 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001416 ItemKind::Ty(ref item) => {
David Tolnay3cf52982016-10-01 17:11:37 -07001417 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001418 item.type_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001419 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001420 item.generics.to_tokens(tokens);
1421 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001422 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001423 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001425 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001426 ItemKind::Enum(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001427 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001428 item.enum_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001429 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001430 item.generics.to_tokens(tokens);
1431 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001432 item.brace_token.surround(tokens, |tokens| {
1433 item.variants.to_tokens(tokens);
1434 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001435 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001436 ItemKind::Struct(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001437 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001438 item.struct_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001439 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001440 item.generics.to_tokens(tokens);
1441 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001442 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001443 item.generics.where_clause.to_tokens(tokens);
1444 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001445 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001446 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001447 item.data.to_tokens(tokens);
1448 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001449 }
1450 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001451 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001452 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001453 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001454 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001455 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001456 ItemKind::Union(ref item) => {
David Tolnay2f9fa632016-10-03 22:08:48 -07001457 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001458 item.union_token.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001459 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001460 item.generics.to_tokens(tokens);
1461 item.generics.where_clause.to_tokens(tokens);
1462 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001463 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001464 ItemKind::Trait(ref item) => {
David Tolnayca085422016-10-04 00:12:38 -07001465 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001466 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001467 item.trait_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001468 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001469 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001470 item.colon_token.to_tokens(tokens);
1471 item.supertraits.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001472 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001473 item.brace_token.surround(tokens, |tokens| {
1474 tokens.append_all(&item.items);
1475 });
David Tolnayca085422016-10-04 00:12:38 -07001476 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001477 ItemKind::DefaultImpl(ref item) => {
1478 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001479 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001480 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001481 item.for_token.to_tokens(tokens);
1482 item.dot2_token.to_tokens(tokens);
1483 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001484 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001485 ItemKind::Impl(ref item) => {
1486 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001487 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001488 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001489 item.polarity.to_tokens(tokens);
1490 item.trait_.to_tokens(tokens);
1491 item.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001492 item.self_ty.to_tokens(tokens);
1493 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001494 item.brace_token.surround(tokens, |tokens| {
1495 tokens.append_all(&item.items);
1496 });
David Tolnay4c9be372016-10-06 00:47:37 -07001497 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001498 ItemKind::Mac(ref mac) => {
1499 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001500 mac.bang_token.to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001501 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001502 tokens.append_all(&mac.tokens);
1503 if !mac.is_braced() {
1504 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001505 }
1506 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001507 }
1508 }
1509 }
David Tolnay42602292016-10-01 22:25:45 -07001510
Alex Crichton62a0a592017-05-22 13:58:53 -07001511 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001512 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001513 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001514 self.as_token.to_tokens(tokens);
1515 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001516 }
1517 }
1518
Alex Crichton62a0a592017-05-22 13:58:53 -07001519 impl ToTokens for PathGlob {
1520 fn to_tokens(&self, tokens: &mut Tokens) {
1521 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001522 self.colon2_token.to_tokens(tokens);
1523 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001524 }
1525 }
1526
1527 impl ToTokens for PathList {
1528 fn to_tokens(&self, tokens: &mut Tokens) {
1529 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001530 self.colon2_token.to_tokens(tokens);
1531 self.brace_token.surround(tokens, |tokens| {
1532 self.items.to_tokens(tokens);
1533 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001534 }
1535 }
1536
David Tolnay4a057422016-10-08 00:02:31 -07001537 impl ToTokens for PathListItem {
1538 fn to_tokens(&self, tokens: &mut Tokens) {
1539 self.name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001540 self.as_token.to_tokens(tokens);
1541 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001542 }
1543 }
1544
David Tolnayca085422016-10-04 00:12:38 -07001545 impl ToTokens for TraitItem {
1546 fn to_tokens(&self, tokens: &mut Tokens) {
1547 tokens.append_all(self.attrs.outer());
1548 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001549 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001550 item.const_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001551 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001552 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001553 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001554 item.eq_token.to_tokens(tokens);
1555 item.default.to_tokens(tokens);
1556 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001557 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001558 TraitItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001561 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001562 block.brace_token.surround(tokens, |tokens| {
1563 tokens.append_all(self.attrs.inner());
1564 tokens.append_all(&block.stmts);
1565 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001566 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001567 None => {
1568 item.semi_token.to_tokens(tokens);
1569 }
David Tolnayca085422016-10-04 00:12:38 -07001570 }
1571 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001572 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001573 item.type_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001574 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001575 item.colon_token.to_tokens(tokens);
1576 item.bounds.to_tokens(tokens);
1577 item.eq_token.to_tokens(tokens);
1578 item.default.to_tokens(tokens);
1579 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001580 }
1581 TraitItemKind::Macro(ref mac) => {
1582 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001583 if !mac.is_braced() {
1584 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001585 }
David Tolnayca085422016-10-04 00:12:38 -07001586 }
1587 }
1588 }
1589 }
1590
David Tolnay4c9be372016-10-06 00:47:37 -07001591 impl ToTokens for ImplItem {
1592 fn to_tokens(&self, tokens: &mut Tokens) {
1593 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001594 self.vis.to_tokens(tokens);
1595 self.defaultness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001596 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001597 ImplItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001598 item.const_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001599 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001600 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001601 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001602 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001603 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001604 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001605 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001606 ImplItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001607 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
1608 item.block.brace_token.surround(tokens, |tokens| {
1609 tokens.append_all(self.attrs.inner());
1610 tokens.append_all(&item.block.stmts);
1611 });
David Tolnay4c9be372016-10-06 00:47:37 -07001612 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001613 ImplItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001614 item.type_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001615 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001616 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001617 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001618 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001619 }
1620 ImplItemKind::Macro(ref mac) => {
1621 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001622 if !mac.is_braced() {
1623 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001624 }
1625 }
1626 }
1627 }
1628 }
1629
David Tolnay35902302016-10-06 01:11:08 -07001630 impl ToTokens for ForeignItem {
1631 fn to_tokens(&self, tokens: &mut Tokens) {
1632 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001633 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001634 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001635 ForeignItemKind::Fn(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001636 NamedDecl(&item.decl, &self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001637 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001638 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001639 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001640 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001641 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001642 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001643 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001644 }
1645 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001646 self.semi_token.to_tokens(tokens);
1647 }
1648 }
1649
1650 struct NamedMethod<'a>(&'a MethodSig, &'a Ident);
1651
1652 impl<'a> ToTokens for NamedMethod<'a> {
1653 fn to_tokens(&self, tokens: &mut Tokens) {
1654 self.0.constness.to_tokens(tokens);
1655 self.0.unsafety.to_tokens(tokens);
1656 self.0.abi.to_tokens(tokens);
1657 NamedDecl(&self.0.decl, self.1).to_tokens(tokens);
1658 }
1659 }
1660
1661 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
1662
1663 impl<'a> ToTokens for NamedDecl<'a> {
1664 fn to_tokens(&self, tokens: &mut Tokens) {
1665 self.0.fn_token.to_tokens(tokens);
1666 self.1.to_tokens(tokens);
1667 self.0.generics.to_tokens(tokens);
1668 self.0.paren_token.surround(tokens, |tokens| {
1669 self.0.inputs.to_tokens(tokens);
1670 self.0.dot_tokens.to_tokens(tokens);
1671 });
1672 self.0.output.to_tokens(tokens);
1673 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001674 }
1675 }
1676
Alex Crichton62a0a592017-05-22 13:58:53 -07001677 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001678 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001679 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001680 self.lifetime.to_tokens(tokens);
1681 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001682 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001683 }
1684 }
1685
1686 impl ToTokens for ArgSelf {
1687 fn to_tokens(&self, tokens: &mut Tokens) {
1688 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001689 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001690 }
1691 }
1692
1693 impl ToTokens for ArgCaptured {
1694 fn to_tokens(&self, tokens: &mut Tokens) {
1695 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001696 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001697 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001698 }
1699 }
1700
David Tolnay42602292016-10-01 22:25:45 -07001701 impl ToTokens for Constness {
1702 fn to_tokens(&self, tokens: &mut Tokens) {
1703 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001704 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001705 Constness::NotConst => {
1706 // nothing
1707 }
David Tolnay42602292016-10-01 22:25:45 -07001708 }
1709 }
1710 }
1711
David Tolnay4c9be372016-10-06 00:47:37 -07001712 impl ToTokens for Defaultness {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001715 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001716 Defaultness::Final => {
1717 // nothing
1718 }
1719 }
1720 }
1721 }
1722
1723 impl ToTokens for ImplPolarity {
1724 fn to_tokens(&self, tokens: &mut Tokens) {
1725 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001726 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001727 ImplPolarity::Positive => {
1728 // nothing
1729 }
1730 }
1731 }
1732 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001733}