blob: f7373a671f4dab1f71ba0e41a707772872314e2f [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 >>
David Tolnay3b9783a2016-10-29 22:37:09 -0700677 punct!("{") >>
678 inner_attrs: many0!(inner_attr) >>
679 stmts: within_block >>
680 punct!("}") >>
David Tolnay42602292016-10-01 22:25:45 -0700681 (Item {
682 ident: name,
683 vis: vis,
David Tolnay3b9783a2016-10-29 22:37:09 -0700684 attrs: {
685 let mut attrs = outer_attrs;
686 attrs.extend(inner_attrs);
687 attrs
688 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 node: ItemFn {
690 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700691 dot_tokens: None,
692 fn_token: tokens::Fn::default(),
693 paren_token: tokens::Paren::default(),
David Tolnay42602292016-10-01 22:25:45 -0700694 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700695 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700696 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700697 generics: Generics {
698 where_clause: where_clause,
699 .. generics
700 },
David Tolnay42602292016-10-01 22:25:45 -0700701 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 unsafety: unsafety,
703 constness: constness,
704 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700705 block: Box::new(Block {
David Tolnay3b9783a2016-10-29 22:37:09 -0700706 stmts: stmts,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700707 brace_token: tokens::Brace::default(),
David Tolnay3b9783a2016-10-29 22:37:09 -0700708 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700710 })
711 ));
712
David Tolnayca085422016-10-04 00:12:38 -0700713 named!(fn_arg -> FnArg, alt!(
714 do_parse!(
715 punct!("&") >>
716 lt: option!(lifetime) >>
717 mutability: mutability >>
718 keyword!("self") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500719 not!(punct!(":")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700720 (ArgSelfRef {
721 lifetime: lt,
722 mutbl: mutability,
723 and_token: tokens::And::default(),
724 self_token: tokens::Self_::default(),
725 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700726 )
727 |
728 do_parse!(
729 mutability: mutability >>
730 keyword!("self") >>
David Tolnay1f16b602017-02-07 20:06:55 -0500731 not!(punct!(":")) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700732 (ArgSelf {
733 mutbl: mutability,
734 self_token: tokens::Self_::default(),
735 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700736 )
737 |
738 do_parse!(
739 pat: pat >>
740 punct!(":") >>
741 ty: ty >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700742 (ArgCaptured {
743 pat: pat,
744 ty: ty,
745 colon_token: tokens::Colon::default(),
746 }.into())
David Tolnayca085422016-10-04 00:12:38 -0700747 )
748 |
749 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700750 ));
751
David Tolnay35902302016-10-06 01:11:08 -0700752 named!(item_mod -> Item, do_parse!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700753 outer_attrs: many0!(outer_attr) >>
David Tolnay35902302016-10-06 01:11:08 -0700754 vis: visibility >>
755 keyword!("mod") >>
756 id: ident >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700757 content: alt!(
David Tolnay37d10332016-10-13 20:51:04 -0700758 punct!(";") => { |_| None }
759 |
David Tolnay7b8009b2016-10-25 22:36:00 -0700760 delimited!(
761 punct!("{"),
762 tuple!(
763 many0!(inner_attr),
764 items
765 ),
766 punct!("}")
767 ) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700768 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700769 (match content {
770 Some((inner_attrs, items)) => Item {
771 ident: id,
772 vis: vis,
773 attrs: {
774 let mut attrs = outer_attrs;
775 attrs.extend(inner_attrs);
776 attrs
777 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700778 node: ItemMod {
779 mod_token: tokens::Mod::default(),
780 semi_token: None,
781 items: Some((items, tokens::Brace::default())),
782 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700783 },
784 None => Item {
785 ident: id,
786 vis: vis,
787 attrs: outer_attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700788 node: ItemMod {
789 items: None,
790 mod_token: tokens::Mod::default(),
791 semi_token: Some(tokens::Semi::default()),
792 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700793 },
David Tolnay35902302016-10-06 01:11:08 -0700794 })
795 ));
796
797 named!(item_foreign_mod -> Item, do_parse!(
798 attrs: many0!(outer_attr) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700799 abi: abi >>
David Tolnay35902302016-10-06 01:11:08 -0700800 punct!("{") >>
801 items: many0!(foreign_item) >>
802 punct!("}") >>
803 (Item {
804 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 vis: VisInherited {}.into(),
David Tolnay35902302016-10-06 01:11:08 -0700806 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700807 node: ItemForeignMod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700808 brace_token: tokens::Brace::default(),
David Tolnayb8d8ef52016-10-29 14:30:08 -0700809 abi: abi,
David Tolnay35902302016-10-06 01:11:08 -0700810 items: items,
Alex Crichton62a0a592017-05-22 13:58:53 -0700811 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700812 })
813 ));
814
815 named!(foreign_item -> ForeignItem, alt!(
816 foreign_fn
817 |
818 foreign_static
819 ));
820
821 named!(foreign_fn -> ForeignItem, do_parse!(
822 attrs: many0!(outer_attr) >>
823 vis: visibility >>
824 keyword!("fn") >>
825 name: ident >>
826 generics: generics >>
827 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700828 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
829 fn_arg) >>
830 variadic: cond!(inputs.is_empty() || inputs.trailing_delim(),
831 option!(punct!("..."))) >>
David Tolnay35902302016-10-06 01:11:08 -0700832 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700833 ret: fn_ret_ty >>
David Tolnay35902302016-10-06 01:11:08 -0700834 where_clause: where_clause >>
835 punct!(";") >>
836 (ForeignItem {
837 ident: name,
838 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700839 semi_token: tokens::Semi::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700840 node: ForeignItemFn {
841 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700842 fn_token: tokens::Fn::default(),
843 paren_token: tokens::Paren::default(),
David Tolnay35902302016-10-06 01:11:08 -0700844 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700845 variadic: variadic.map(|m| m.is_some()).unwrap_or(false),
846 dot_tokens: if variadic.map(|m| m.is_some()).unwrap_or(false) {
847 Some(tokens::Dot3::default())
848 } else {
849 None
850 },
851 output: ret,
852 generics: Generics {
853 where_clause: where_clause,
854 .. generics
855 },
David Tolnay35902302016-10-06 01:11:08 -0700856 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700857 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700858 vis: vis,
859 })
860 ));
861
862 named!(foreign_static -> ForeignItem, do_parse!(
863 attrs: many0!(outer_attr) >>
864 vis: visibility >>
865 keyword!("static") >>
866 mutability: mutability >>
867 id: ident >>
868 punct!(":") >>
869 ty: ty >>
870 punct!(";") >>
871 (ForeignItem {
872 ident: id,
873 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700874 semi_token: tokens::Semi::default(),
875 node: ForeignItemStatic {
876 ty: Box::new(ty),
877 mutbl: mutability,
878 static_token: tokens::Static::default(),
879 colon_token: tokens::Colon::default(),
880 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700881 vis: vis,
882 })
883 ));
884
David Tolnay3cf52982016-10-01 17:11:37 -0700885 named!(item_ty -> Item, do_parse!(
886 attrs: many0!(outer_attr) >>
887 vis: visibility >>
888 keyword!("type") >>
889 id: ident >>
890 generics: generics >>
David Tolnay04bb3ad2016-10-30 10:59:01 -0700891 where_clause: where_clause >>
David Tolnay3cf52982016-10-01 17:11:37 -0700892 punct!("=") >>
893 ty: ty >>
894 punct!(";") >>
895 (Item {
896 ident: id,
897 vis: vis,
898 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700899 node: ItemTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 type_token: tokens::Type::default(),
901 eq_token: tokens::Eq::default(),
902 semi_token: tokens::Semi::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700903 ty: Box::new(ty),
904 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700905 where_clause: where_clause,
906 ..generics
907 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700908 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700909 })
910 ));
911
David Tolnaya96a3fa2016-09-24 07:17:42 -0700912 named!(item_struct_or_enum -> Item, map!(
David Tolnay0e837402016-12-22 17:25:55 -0500913 derive_input,
914 |def: DeriveInput| Item {
David Tolnayedf2b992016-09-23 20:43:45 -0700915 ident: def.ident,
916 vis: def.vis,
917 attrs: def.attrs,
918 node: match def.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700919 Body::Enum(data) => {
920 ItemEnum {
921 variants: data.variants,
922 brace_token: data.brace_token,
923 enum_token: data.enum_token,
924 generics: def.generics,
925 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700926 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700927 Body::Struct(data) => {
928 ItemStruct {
929 data: data.data,
930 struct_token: data.struct_token,
931 semi_token: data.semi_token,
932 generics: def.generics,
933 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700934 }
935 }
936 }
937 ));
David Tolnay42602292016-10-01 22:25:45 -0700938
David Tolnay2f9fa632016-10-03 22:08:48 -0700939 named!(item_union -> Item, do_parse!(
940 attrs: many0!(outer_attr) >>
941 vis: visibility >>
942 keyword!("union") >>
943 id: ident >>
944 generics: generics >>
945 where_clause: where_clause >>
946 fields: struct_like_body >>
947 (Item {
948 ident: id,
949 vis: vis,
950 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 node: ItemUnion {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700952 union_token: tokens::Union::default(),
953 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700955 where_clause: where_clause,
956 .. generics
957 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700958 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700959 })
960 ));
961
David Tolnay0aecb732016-10-03 23:03:50 -0700962 named!(item_trait -> Item, do_parse!(
963 attrs: many0!(outer_attr) >>
964 vis: visibility >>
965 unsafety: unsafety >>
966 keyword!("trait") >>
967 id: ident >>
968 generics: generics >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700969 colon: option!(punct!(":")) >>
970 bounds: cond!(colon.is_some(),
971 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
972 ty_param_bound)
973 ) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700974 where_clause: where_clause >>
975 punct!("{") >>
976 body: many0!(trait_item) >>
977 punct!("}") >>
978 (Item {
979 ident: id,
980 vis: vis,
981 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700982 node: ItemTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700983 trait_token: tokens::Trait::default(),
984 brace_token: tokens::Brace::default(),
985 colon_token: colon.map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 unsafety: unsafety,
987 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -0700988 where_clause: where_clause,
989 .. generics
990 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 supertraits: bounds.unwrap_or_default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700992 items: body,
993 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -0700994 })
995 ));
996
David Tolnayf94e2362016-10-04 00:29:51 -0700997 named!(item_default_impl -> Item, do_parse!(
998 attrs: many0!(outer_attr) >>
999 unsafety: unsafety >>
1000 keyword!("impl") >>
1001 path: path >>
1002 keyword!("for") >>
1003 punct!("..") >>
1004 punct!("{") >>
1005 punct!("}") >>
1006 (Item {
1007 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 vis: VisInherited {}.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001009 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001010 node: ItemDefaultImpl {
1011 unsafety: unsafety,
1012 path: path,
1013 impl_token: tokens::Impl::default(),
1014 for_token: tokens::For::default(),
1015 dot2_token: tokens::Dot2::default(),
1016 brace_token: tokens::Brace::default(),
1017 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001018 })
1019 ));
1020
David Tolnay0aecb732016-10-03 23:03:50 -07001021 named!(trait_item -> TraitItem, alt!(
1022 trait_item_const
1023 |
1024 trait_item_method
1025 |
1026 trait_item_type
1027 |
1028 trait_item_mac
1029 ));
1030
1031 named!(trait_item_const -> TraitItem, do_parse!(
1032 attrs: many0!(outer_attr) >>
1033 keyword!("const") >>
1034 id: ident >>
1035 punct!(":") >>
1036 ty: ty >>
1037 value: option!(preceded!(punct!("="), expr)) >>
1038 punct!(";") >>
1039 (TraitItem {
1040 ident: id,
1041 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001042 node: TraitItemConst {
1043 ty: ty,
1044 const_token: tokens::Const::default(),
1045 colon_token: tokens::Colon::default(),
1046 eq_token: value.as_ref().map(|_| tokens::Eq::default()),
1047 default: value,
1048 semi_token: tokens::Semi::default(),
1049 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001050 })
1051 ));
1052
1053 named!(trait_item_method -> TraitItem, do_parse!(
David Tolnay5859df12016-10-29 22:49:54 -07001054 outer_attrs: many0!(outer_attr) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001055 constness: constness >>
1056 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -07001057 abi: option!(abi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001058 keyword!("fn") >>
1059 name: ident >>
1060 generics: generics >>
1061 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001062 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001063 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001064 ret: fn_ret_ty >>
David Tolnay0aecb732016-10-03 23:03:50 -07001065 where_clause: where_clause >>
David Tolnay5859df12016-10-29 22:49:54 -07001066 body: option!(delimited!(
1067 punct!("{"),
1068 tuple!(many0!(inner_attr), within_block),
1069 punct!("}")
1070 )) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 semi: cond!(body.is_none(), punct!(";")) >>
David Tolnay5859df12016-10-29 22:49:54 -07001072 ({
1073 let (inner_attrs, stmts) = match body {
1074 Some((inner_attrs, stmts)) => (inner_attrs, Some(stmts)),
1075 None => (Vec::new(), None),
1076 };
1077 TraitItem {
1078 ident: name,
1079 attrs: {
1080 let mut attrs = outer_attrs;
1081 attrs.extend(inner_attrs);
1082 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001083 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001084 node: TraitItemMethod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001085 semi_token: semi.map(|_| tokens::Semi::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001086 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001087 unsafety: unsafety,
1088 constness: constness,
1089 abi: abi,
1090 decl: FnDecl {
1091 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001092 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001093 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001094 fn_token: tokens::Fn::default(),
1095 paren_token: tokens::Paren::default(),
1096 dot_tokens: None,
1097 generics: Generics {
1098 where_clause: where_clause,
1099 .. generics
1100 },
David Tolnay5859df12016-10-29 22:49:54 -07001101 },
1102 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001103 default: stmts.map(|stmts| {
1104 Block {
1105 stmts: stmts,
1106 brace_token: tokens::Brace::default(),
1107 }
1108 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001109 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001110 }
David Tolnay0aecb732016-10-03 23:03:50 -07001111 })
1112 ));
1113
1114 named!(trait_item_type -> TraitItem, do_parse!(
1115 attrs: many0!(outer_attr) >>
1116 keyword!("type") >>
1117 id: ident >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001118 colon: option!(punct!(":")) >>
1119 bounds: cond!(colon.is_some(),
1120 separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()),
1121 ty_param_bound)
1122 ) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001123 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -07001124 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -07001125 (TraitItem {
1126 ident: id,
1127 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001128 node: TraitItemType {
1129 type_token: tokens::Type::default(),
1130 colon_token: colon.map(|_| tokens::Colon::default()),
1131 bounds: bounds.unwrap_or_default(),
1132 eq_token: default.as_ref().map(|_| tokens::Eq::default()),
1133 semi_token: tokens::Semi::default(),
1134 default: default,
1135 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001136 })
1137 ));
1138
1139 named!(trait_item_mac -> TraitItem, do_parse!(
1140 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001141 what: path >>
David Tolnay0aecb732016-10-03 23:03:50 -07001142 punct!("!") >>
1143 body: delimited >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001144 cond!(!body.is_braced(), punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001145 (TraitItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001146 ident: "".into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001147 attrs: attrs,
1148 node: TraitItemKind::Macro(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001149 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001150 bang_token: tokens::Bang::default(),
1151 tokens: vec![body],
David Tolnay0aecb732016-10-03 23:03:50 -07001152 }),
1153 })
1154 ));
1155
David Tolnay4c9be372016-10-06 00:47:37 -07001156 named!(item_impl -> Item, do_parse!(
1157 attrs: many0!(outer_attr) >>
1158 unsafety: unsafety >>
1159 keyword!("impl") >>
1160 generics: generics >>
1161 polarity_path: alt!(
1162 do_parse!(
1163 polarity: impl_polarity >>
1164 path: path >>
1165 keyword!("for") >>
David Tolnay02a8d472017-02-19 12:59:44 -08001166 (polarity, Some(path))
David Tolnay4c9be372016-10-06 00:47:37 -07001167 )
1168 |
1169 epsilon!() => { |_| (ImplPolarity::Positive, None) }
1170 ) >>
1171 self_ty: ty >>
1172 where_clause: where_clause >>
1173 punct!("{") >>
1174 body: many0!(impl_item) >>
1175 punct!("}") >>
1176 (Item {
1177 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001178 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001179 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001180 node: ItemImpl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001181 impl_token: tokens::Impl::default(),
1182 brace_token: tokens::Brace::default(),
1183 for_token: polarity_path.1.as_ref().map(|_| tokens::For::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001184 unsafety: unsafety,
1185 polarity: polarity_path.0,
1186 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001187 where_clause: where_clause,
1188 .. generics
1189 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001190 trait_: polarity_path.1,
1191 self_ty: Box::new(self_ty),
1192 items: body,
1193 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001194 })
1195 ));
1196
1197 named!(impl_item -> ImplItem, alt!(
1198 impl_item_const
1199 |
1200 impl_item_method
1201 |
1202 impl_item_type
1203 |
1204 impl_item_macro
1205 ));
1206
1207 named!(impl_item_const -> ImplItem, do_parse!(
1208 attrs: many0!(outer_attr) >>
1209 vis: visibility >>
1210 defaultness: defaultness >>
1211 keyword!("const") >>
1212 id: ident >>
1213 punct!(":") >>
1214 ty: ty >>
1215 punct!("=") >>
1216 value: expr >>
1217 punct!(";") >>
1218 (ImplItem {
1219 ident: id,
1220 vis: vis,
1221 defaultness: defaultness,
1222 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001223 node: ImplItemConst {
1224 ty: ty,
1225 expr: value,
1226 const_token: tokens::Const::default(),
1227 colon_token: tokens::Colon::default(),
1228 eq_token: tokens::Eq::default(),
1229 semi_token: tokens::Semi::default(),
1230 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001231 })
1232 ));
1233
1234 named!(impl_item_method -> ImplItem, do_parse!(
David Tolnay3b9783a2016-10-29 22:37:09 -07001235 outer_attrs: many0!(outer_attr) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001236 vis: visibility >>
1237 defaultness: defaultness >>
1238 constness: constness >>
1239 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -07001240 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001241 keyword!("fn") >>
1242 name: ident >>
1243 generics: generics >>
1244 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001245 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1246 fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001247 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001248 ret: fn_ret_ty >>
David Tolnay4c9be372016-10-06 00:47:37 -07001249 where_clause: where_clause >>
David Tolnay3b9783a2016-10-29 22:37:09 -07001250 punct!("{") >>
1251 inner_attrs: many0!(inner_attr) >>
1252 stmts: within_block >>
1253 punct!("}") >>
David Tolnay4c9be372016-10-06 00:47:37 -07001254 (ImplItem {
1255 ident: name,
1256 vis: vis,
1257 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -07001258 attrs: {
1259 let mut attrs = outer_attrs;
1260 attrs.extend(inner_attrs);
1261 attrs
1262 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001263 node: ImplItemMethod {
1264 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001265 unsafety: unsafety,
1266 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001267 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -07001268 decl: FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001269 fn_token: tokens::Fn::default(),
1270 paren_token: tokens::Paren::default(),
David Tolnay4c9be372016-10-06 00:47:37 -07001271 inputs: inputs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001272 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001273 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001274 generics: Generics {
1275 where_clause: where_clause,
1276 .. generics
1277 },
1278 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001279 },
1280 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001281 block: Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001282 brace_token: tokens::Brace::default(),
David Tolnay3b9783a2016-10-29 22:37:09 -07001283 stmts: stmts,
1284 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001285 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001286 })
1287 ));
1288
1289 named!(impl_item_type -> ImplItem, do_parse!(
1290 attrs: many0!(outer_attr) >>
1291 vis: visibility >>
1292 defaultness: defaultness >>
1293 keyword!("type") >>
1294 id: ident >>
1295 punct!("=") >>
1296 ty: ty >>
1297 punct!(";") >>
1298 (ImplItem {
1299 ident: id,
1300 vis: vis,
1301 defaultness: defaultness,
1302 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001303 node: ImplItemType {
1304 type_token: tokens::Type::default(),
1305 eq_token: tokens::Eq::default(),
1306 semi_token: tokens::Semi::default(),
1307 ty: ty,
1308 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001309 })
1310 ));
1311
1312 named!(impl_item_macro -> ImplItem, do_parse!(
1313 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001314 what: path >>
David Tolnay4c9be372016-10-06 00:47:37 -07001315 punct!("!") >>
1316 body: delimited >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001317 cond!(!body.is_braced(), punct!(";")) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001318 (ImplItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001319 ident: "".into(),
1320 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001321 defaultness: Defaultness::Final,
1322 attrs: attrs,
1323 node: ImplItemKind::Macro(Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001324 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001325 bang_token: tokens::Bang::default(),
1326 tokens: vec![body],
David Tolnay4c9be372016-10-06 00:47:37 -07001327 }),
1328 })
1329 ));
1330
1331 named!(impl_polarity -> ImplPolarity, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001332 punct!("!") => { |_| ImplPolarity::Negative(tokens::Bang::default()) }
David Tolnay4c9be372016-10-06 00:47:37 -07001333 |
1334 epsilon!() => { |_| ImplPolarity::Positive }
1335 ));
1336
David Tolnay42602292016-10-01 22:25:45 -07001337 named!(constness -> Constness, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001338 keyword!("const") => { |_| Constness::Const(tokens::Const::default()) }
David Tolnay42602292016-10-01 22:25:45 -07001339 |
1340 epsilon!() => { |_| Constness::NotConst }
1341 ));
1342
David Tolnay4c9be372016-10-06 00:47:37 -07001343 named!(defaultness -> Defaultness, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001344 keyword!("default") => { |_| Defaultness::Default(tokens::Default::default()) }
David Tolnay4c9be372016-10-06 00:47:37 -07001345 |
1346 epsilon!() => { |_| Defaultness::Final }
1347 ));
David Tolnayedf2b992016-09-23 20:43:45 -07001348}
David Tolnay4a51dc72016-10-01 00:40:31 -07001349
1350#[cfg(feature = "printing")]
1351mod printing {
1352 use super::*;
1353 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001354 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001355 use quote::{Tokens, ToTokens};
1356
1357 impl ToTokens for Item {
1358 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001359 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001360 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001361 ItemKind::ExternCrate(ref item) => {
David Tolnaycbd9f7d2016-10-30 00:26:29 -07001362 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001363 item.extern_token.to_tokens(tokens);
1364 item.crate_token.to_tokens(tokens);
1365 item.original.to_tokens(tokens);
1366 item.as_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001367 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001368 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001369 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001370 ItemKind::Use(ref item) => {
David Tolnay4a057422016-10-08 00:02:31 -07001371 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001372 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001373 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001374 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001375 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001376 ItemKind::Static(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001377 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001378 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001379 item.mutbl.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001380 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001381 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001382 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001383 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001384 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001385 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001386 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001387 ItemKind::Const(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001388 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001389 item.const_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001390 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001391 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001392 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001393 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001394 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001396 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001397 ItemKind::Fn(ref item) => {
David Tolnay42602292016-10-01 22:25:45 -07001398 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001399 item.constness.to_tokens(tokens);
1400 item.unsafety.to_tokens(tokens);
1401 item.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001402 NamedDecl(&item.decl, &self.ident).to_tokens(tokens);
1403 item.block.brace_token.surround(tokens, |tokens| {
1404 tokens.append_all(self.attrs.inner());
1405 tokens.append_all(&item.block.stmts);
1406 });
David Tolnay42602292016-10-01 22:25:45 -07001407 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 ItemKind::Mod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001409 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001410 item.mod_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001411 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001412 if let Some((ref items, ref brace)) = item.items {
1413 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001414 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001415 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416 });
David Tolnay37d10332016-10-13 20:51:04 -07001417 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001418 item.semi_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001419 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001420 ItemKind::ForeignMod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001421 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001422 item.abi.to_tokens(tokens);
1423 item.brace_token.surround(tokens, |tokens| {
1424 tokens.append_all(&item.items);
1425 });
David Tolnay35902302016-10-06 01:11:08 -07001426 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001427 ItemKind::Ty(ref item) => {
David Tolnay3cf52982016-10-01 17:11:37 -07001428 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001429 item.type_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001430 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001431 item.generics.to_tokens(tokens);
1432 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001433 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001434 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001435 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001436 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001437 ItemKind::Enum(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001438 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001439 item.enum_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001440 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001441 item.generics.to_tokens(tokens);
1442 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001443 item.brace_token.surround(tokens, |tokens| {
1444 item.variants.to_tokens(tokens);
1445 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001446 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001447 ItemKind::Struct(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001448 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001449 item.struct_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001450 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001451 item.generics.to_tokens(tokens);
1452 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001453 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001454 item.generics.where_clause.to_tokens(tokens);
1455 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001456 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001457 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001458 item.data.to_tokens(tokens);
1459 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001460 }
1461 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001462 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001463 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001464 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001465 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001466 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001467 ItemKind::Union(ref item) => {
David Tolnay2f9fa632016-10-03 22:08:48 -07001468 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001469 item.union_token.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001470 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001471 item.generics.to_tokens(tokens);
1472 item.generics.where_clause.to_tokens(tokens);
1473 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001474 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 ItemKind::Trait(ref item) => {
David Tolnayca085422016-10-04 00:12:38 -07001476 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001477 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001478 item.trait_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001479 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001480 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001481 item.colon_token.to_tokens(tokens);
1482 item.supertraits.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001484 item.brace_token.surround(tokens, |tokens| {
1485 tokens.append_all(&item.items);
1486 });
David Tolnayca085422016-10-04 00:12:38 -07001487 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001488 ItemKind::DefaultImpl(ref item) => {
1489 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001490 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001491 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001492 item.for_token.to_tokens(tokens);
1493 item.dot2_token.to_tokens(tokens);
1494 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001495 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001496 ItemKind::Impl(ref item) => {
1497 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001498 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001499 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001500 item.polarity.to_tokens(tokens);
1501 item.trait_.to_tokens(tokens);
1502 item.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001503 item.self_ty.to_tokens(tokens);
1504 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001505 item.brace_token.surround(tokens, |tokens| {
1506 tokens.append_all(&item.items);
1507 });
David Tolnay4c9be372016-10-06 00:47:37 -07001508 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001509 ItemKind::Mac(ref mac) => {
1510 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001511 mac.bang_token.to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001512 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001513 tokens.append_all(&mac.tokens);
1514 if !mac.is_braced() {
1515 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001516 }
1517 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001518 }
1519 }
1520 }
David Tolnay42602292016-10-01 22:25:45 -07001521
Alex Crichton62a0a592017-05-22 13:58:53 -07001522 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001523 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001524 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001525 self.as_token.to_tokens(tokens);
1526 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001527 }
1528 }
1529
Alex Crichton62a0a592017-05-22 13:58:53 -07001530 impl ToTokens for PathGlob {
1531 fn to_tokens(&self, tokens: &mut Tokens) {
1532 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001533 self.colon2_token.to_tokens(tokens);
1534 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001535 }
1536 }
1537
1538 impl ToTokens for PathList {
1539 fn to_tokens(&self, tokens: &mut Tokens) {
1540 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001541 self.colon2_token.to_tokens(tokens);
1542 self.brace_token.surround(tokens, |tokens| {
1543 self.items.to_tokens(tokens);
1544 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001545 }
1546 }
1547
David Tolnay4a057422016-10-08 00:02:31 -07001548 impl ToTokens for PathListItem {
1549 fn to_tokens(&self, tokens: &mut Tokens) {
1550 self.name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001551 self.as_token.to_tokens(tokens);
1552 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001553 }
1554 }
1555
David Tolnayca085422016-10-04 00:12:38 -07001556 impl ToTokens for TraitItem {
1557 fn to_tokens(&self, tokens: &mut Tokens) {
1558 tokens.append_all(self.attrs.outer());
1559 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001561 item.const_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001562 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001563 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001564 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001565 item.eq_token.to_tokens(tokens);
1566 item.default.to_tokens(tokens);
1567 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001568 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001569 TraitItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001570 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001571 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001572 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001573 block.brace_token.surround(tokens, |tokens| {
1574 tokens.append_all(self.attrs.inner());
1575 tokens.append_all(&block.stmts);
1576 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001577 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001578 None => {
1579 item.semi_token.to_tokens(tokens);
1580 }
David Tolnayca085422016-10-04 00:12:38 -07001581 }
1582 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001583 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 item.type_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001585 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001586 item.colon_token.to_tokens(tokens);
1587 item.bounds.to_tokens(tokens);
1588 item.eq_token.to_tokens(tokens);
1589 item.default.to_tokens(tokens);
1590 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001591 }
1592 TraitItemKind::Macro(ref mac) => {
1593 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001594 if !mac.is_braced() {
1595 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001596 }
David Tolnayca085422016-10-04 00:12:38 -07001597 }
1598 }
1599 }
1600 }
1601
David Tolnay4c9be372016-10-06 00:47:37 -07001602 impl ToTokens for ImplItem {
1603 fn to_tokens(&self, tokens: &mut Tokens) {
1604 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001605 self.vis.to_tokens(tokens);
1606 self.defaultness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001607 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001608 ImplItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001609 item.const_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001610 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001611 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001612 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001613 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001614 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001615 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001616 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001617 ImplItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001618 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
1619 item.block.brace_token.surround(tokens, |tokens| {
1620 tokens.append_all(self.attrs.inner());
1621 tokens.append_all(&item.block.stmts);
1622 });
David Tolnay4c9be372016-10-06 00:47:37 -07001623 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001624 ImplItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001625 item.type_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001626 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001627 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001628 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001629 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001630 }
1631 ImplItemKind::Macro(ref mac) => {
1632 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001633 if !mac.is_braced() {
1634 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001635 }
1636 }
1637 }
1638 }
1639 }
1640
David Tolnay35902302016-10-06 01:11:08 -07001641 impl ToTokens for ForeignItem {
1642 fn to_tokens(&self, tokens: &mut Tokens) {
1643 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001644 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001645 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001646 ForeignItemKind::Fn(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001647 NamedDecl(&item.decl, &self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001648 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001650 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001651 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001652 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001653 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001655 }
1656 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001657 self.semi_token.to_tokens(tokens);
1658 }
1659 }
1660
1661 struct NamedMethod<'a>(&'a MethodSig, &'a Ident);
1662
1663 impl<'a> ToTokens for NamedMethod<'a> {
1664 fn to_tokens(&self, tokens: &mut Tokens) {
1665 self.0.constness.to_tokens(tokens);
1666 self.0.unsafety.to_tokens(tokens);
1667 self.0.abi.to_tokens(tokens);
1668 NamedDecl(&self.0.decl, self.1).to_tokens(tokens);
1669 }
1670 }
1671
1672 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
1673
1674 impl<'a> ToTokens for NamedDecl<'a> {
1675 fn to_tokens(&self, tokens: &mut Tokens) {
1676 self.0.fn_token.to_tokens(tokens);
1677 self.1.to_tokens(tokens);
1678 self.0.generics.to_tokens(tokens);
1679 self.0.paren_token.surround(tokens, |tokens| {
1680 self.0.inputs.to_tokens(tokens);
1681 self.0.dot_tokens.to_tokens(tokens);
1682 });
1683 self.0.output.to_tokens(tokens);
1684 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001685 }
1686 }
1687
Alex Crichton62a0a592017-05-22 13:58:53 -07001688 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001689 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001690 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001691 self.lifetime.to_tokens(tokens);
1692 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001693 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001694 }
1695 }
1696
1697 impl ToTokens for ArgSelf {
1698 fn to_tokens(&self, tokens: &mut Tokens) {
1699 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001700 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001701 }
1702 }
1703
1704 impl ToTokens for ArgCaptured {
1705 fn to_tokens(&self, tokens: &mut Tokens) {
1706 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001707 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001708 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001709 }
1710 }
1711
David Tolnay42602292016-10-01 22:25:45 -07001712 impl ToTokens for Constness {
1713 fn to_tokens(&self, tokens: &mut Tokens) {
1714 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001715 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001716 Constness::NotConst => {
1717 // nothing
1718 }
David Tolnay42602292016-10-01 22:25:45 -07001719 }
1720 }
1721 }
1722
David Tolnay4c9be372016-10-06 00:47:37 -07001723 impl ToTokens for Defaultness {
1724 fn to_tokens(&self, tokens: &mut Tokens) {
1725 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001726 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001727 Defaultness::Final => {
1728 // nothing
1729 }
1730 }
1731 }
1732 }
1733
1734 impl ToTokens for ImplPolarity {
1735 fn to_tokens(&self, tokens: &mut Tokens) {
1736 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001737 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001738 ImplPolarity::Positive => {
1739 // nothing
1740 }
1741 }
1742 }
1743 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001744}