blob: d6d152114264b1775a84982e5d75fc172fe14099 [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 Crichton954046c2017-05-30 21:49:42 -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 Crichton954046c2017-05-30 21:49:42 -0700383 pub fn_token: tokens::Fn_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700384 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::*;
David Tolnayedf2b992016-09-23 20:43:45 -0700420
Alex Crichton954046c2017-05-30 21:49:42 -0700421 use proc_macro2::TokenTree;
422 use synom::{IResult, Synom};
423 use synom::tokens::*;
424 use synom::tokens;
David Tolnay84aa0752016-10-02 23:01:13 -0700425
Alex Crichton954046c2017-05-30 21:49:42 -0700426 impl Synom for Item {
427 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
428 alt! {
429 input,
430 item_extern_crate
431 |
432 item_use
433 |
434 item_static
435 |
436 item_const
437 |
438 item_fn
439 |
440 item_mod
441 |
442 item_foreign_mod
443 |
444 item_ty
445 |
446 item_struct_or_enum
447 |
448 item_union
449 |
450 item_trait
451 |
452 item_default_impl
453 |
454 item_impl
455 |
456 item_mac
457 }
458 }
459
460 fn description() -> Option<&'static str> {
461 Some("item")
462 }
463 }
David Tolnay453cfd12016-10-23 11:00:14 -0700464
David Tolnay84aa0752016-10-02 23:01:13 -0700465 named!(item_mac -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700466 attrs: many0!(call!(Attribute::parse_outer)) >>
467 what: syn!(Path) >>
468 bang: syn!(Bang) >>
469 name: option!(syn!(Ident)) >>
470 body: call!(::TokenTree::parse_delimited) >>
471 cond!(!body.is_braced(), syn!(Semi)) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700472 (Item {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700473 ident: name.unwrap_or_else(|| Ident::from("")),
474 vis: VisInherited {}.into(),
David Tolnay84aa0752016-10-02 23:01:13 -0700475 attrs: attrs,
476 node: ItemKind::Mac(Mac {
Alex Crichton954046c2017-05-30 21:49:42 -0700477 bang_token: bang,
David Tolnay5d55ef72016-12-21 20:20:04 -0500478 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700479 tokens: vec![body],
David Tolnay84aa0752016-10-02 23:01:13 -0700480 }),
481 })
David Tolnayedf2b992016-09-23 20:43:45 -0700482 ));
483
David Tolnaya96a3fa2016-09-24 07:17:42 -0700484 named!(item_extern_crate -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700485 attrs: many0!(call!(Attribute::parse_outer)) >>
486 vis: syn!(Visibility) >>
487 extern_: syn!(Extern) >>
488 crate_: syn!(tokens::Crate) >>
489 id: syn!(Ident) >>
490 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
491 semi: syn!(Semi) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700492 ({
Alex Crichton954046c2017-05-30 21:49:42 -0700493 let (name, original_name, as_) = match rename {
494 Some((as_, rename)) => (rename, Some(id), Some(as_)),
495 None => (id, None, None),
David Tolnayedf2b992016-09-23 20:43:45 -0700496 };
497 Item {
498 ident: name,
499 vis: vis,
500 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 node: ItemExternCrate {
Alex Crichton954046c2017-05-30 21:49:42 -0700502 as_token: as_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700503 original: original_name,
Alex Crichton954046c2017-05-30 21:49:42 -0700504 extern_token: extern_,
505 crate_token: crate_,
506 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 }.into(),
David Tolnayedf2b992016-09-23 20:43:45 -0700508 }
509 })
510 ));
511
David Tolnay4a057422016-10-08 00:02:31 -0700512 named!(item_use -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700513 attrs: many0!(call!(Attribute::parse_outer)) >>
514 vis: syn!(Visibility) >>
515 use_: syn!(Use) >>
516 what: syn!(ViewPath) >>
517 semi: syn!(Semi) >>
David Tolnay4a057422016-10-08 00:02:31 -0700518 (Item {
519 ident: "".into(),
520 vis: vis,
521 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700522 node: ItemUse {
523 path: Box::new(what),
Alex Crichton954046c2017-05-30 21:49:42 -0700524 use_token: use_,
525 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700526 }.into(),
David Tolnay4a057422016-10-08 00:02:31 -0700527 })
528 ));
529
Alex Crichton954046c2017-05-30 21:49:42 -0700530 impl Synom for ViewPath {
531 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
532 alt! {
533 input,
534 syn!(PathGlob) => { ViewPath::Glob }
535 |
536 syn!(PathList) => { ViewPath::List }
537 |
538 syn!(PathSimple) => { ViewPath::Simple } // must be last
539 }
540 }
541 }
David Tolnay4a057422016-10-08 00:02:31 -0700542
Alex Crichton954046c2017-05-30 21:49:42 -0700543 impl Synom for PathSimple {
544 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
545 do_parse! {
546 input,
547 path: syn!(Path) >>
548 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
549 (PathSimple {
550 path: path,
551 as_token: rename.as_ref().map(|p| As((p.0).0)),
552 rename: rename.map(|p| p.1),
553 })
554 }
555 }
556 }
David Tolnay4a057422016-10-08 00:02:31 -0700557
Alex Crichton954046c2017-05-30 21:49:42 -0700558 impl Synom for PathGlob {
559 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
560 do_parse! {
561 input,
562 path: syn!(Path) >>
563 colon2: syn!(Colon2) >>
564 star: syn!(Star) >>
565 (PathGlob {
566 path: path,
567 colon2_token: colon2,
568 star_token: star,
569 })
570 }
571 }
572 }
David Tolnay4a057422016-10-08 00:02:31 -0700573
Alex Crichton954046c2017-05-30 21:49:42 -0700574 impl Synom for PathList {
575 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
576 alt! {
577 input,
578 do_parse!(
579 path: syn!(Path) >>
580 colon2: syn!(Colon2) >>
581 items: braces!(call!(Delimited::parse_terminated)) >>
582 (PathList {
583 path: path,
584 items: items.0,
585 brace_token: items.1,
586 colon2_token: colon2,
587 })
588 )
589 |
590 do_parse!(
591 global: option!(syn!(Colon2)) >>
592 items: braces!(call!(Delimited::parse_terminated)) >>
593 (PathList {
594 path: Path {
595 global: global.is_some(),
596 segments: Delimited::new(),
597 leading_colon: None,
598 },
599 colon2_token: global.unwrap_or_default(),
600 brace_token: items.1,
601 items: items.0,
602 })
603 )
604 }
605 }
606 }
David Tolnay4a057422016-10-08 00:02:31 -0700607
Alex Crichton954046c2017-05-30 21:49:42 -0700608 impl Synom for PathListItem {
609 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
610 do_parse! {
611 input,
612 name: alt!(
613 syn!(Ident)
614 |
615 map!(syn!(Self_), Into::into)
616 ) >>
617 rename: option!(tuple!(syn!(As), syn!(Ident))) >>
618 (PathListItem {
619 name: name,
620 as_token: rename.as_ref().map(|p| As((p.0).0)),
621 rename: rename.map(|p| p.1),
622 })
623 }
624 }
625 }
David Tolnay4a057422016-10-08 00:02:31 -0700626
David Tolnay47a877c2016-10-01 16:50:55 -0700627 named!(item_static -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700628 attrs: many0!(call!(Attribute::parse_outer)) >>
629 vis: syn!(Visibility) >>
630 static_: syn!(Static) >>
631 mutability: syn!(Mutability) >>
632 id: syn!(Ident) >>
633 colon: syn!(Colon) >>
634 ty: syn!(Ty) >>
635 eq: syn!(Eq) >>
636 value: syn!(Expr) >>
637 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700638 (Item {
639 ident: id,
640 vis: vis,
641 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700642 node: ItemStatic {
643 ty: Box::new(ty),
644 mutbl: mutability,
645 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700646 static_token: static_,
647 colon_token: colon,
648 eq_token: eq,
649 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700651 })
652 ));
653
654 named!(item_const -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700655 attrs: many0!(call!(Attribute::parse_outer)) >>
656 vis: syn!(Visibility) >>
657 const_: syn!(Const) >>
658 id: syn!(Ident) >>
659 colon: syn!(Colon) >>
660 ty: syn!(Ty) >>
661 eq: syn!(Eq) >>
662 value: syn!(Expr) >>
663 semi: syn!(Semi) >>
David Tolnay47a877c2016-10-01 16:50:55 -0700664 (Item {
665 ident: id,
666 vis: vis,
667 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700668 node: ItemConst {
669 ty: Box::new(ty),
670 expr: Box::new(value),
Alex Crichton954046c2017-05-30 21:49:42 -0700671 const_token: const_,
672 colon_token: colon,
673 eq_token: eq,
674 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700675 }.into(),
David Tolnay47a877c2016-10-01 16:50:55 -0700676 })
677 ));
678
David Tolnay42602292016-10-01 22:25:45 -0700679 named!(item_fn -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700680 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
681 vis: syn!(Visibility) >>
682 constness: syn!(Constness) >>
683 unsafety: syn!(Unsafety) >>
684 abi: option!(syn!(Abi)) >>
685 fn_: syn!(Fn_) >>
686 name: syn!(Ident) >>
687 generics: syn!(Generics) >>
688 inputs: parens!(Delimited::parse_terminated) >>
689 ret: syn!(FunctionRetTy) >>
690 where_clause: syn!(WhereClause) >>
691 inner_attrs_stmts: braces!(tuple!(
692 many0!(call!(Attribute::parse_inner)),
693 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -0400694 )) >>
David Tolnay42602292016-10-01 22:25:45 -0700695 (Item {
696 ident: name,
697 vis: vis,
David Tolnay3b9783a2016-10-29 22:37:09 -0700698 attrs: {
699 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -0700700 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -0700701 attrs
702 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 node: ItemFn {
704 decl: Box::new(FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700705 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700706 fn_token: fn_,
707 paren_token: inputs.1,
708 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700709 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -0700710 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700711 generics: Generics {
712 where_clause: where_clause,
713 .. generics
714 },
David Tolnay42602292016-10-01 22:25:45 -0700715 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700716 unsafety: unsafety,
717 constness: constness,
718 abi: abi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700719 block: Box::new(Block {
Alex Crichton954046c2017-05-30 21:49:42 -0700720 brace_token: inner_attrs_stmts.1,
721 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -0700722 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 }.into(),
David Tolnay42602292016-10-01 22:25:45 -0700724 })
725 ));
726
Alex Crichton954046c2017-05-30 21:49:42 -0700727 impl Synom for FnArg {
728 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
729 alt! {
730 input,
731 do_parse!(
732 and: syn!(And) >>
733 lt: option!(syn!(Lifetime)) >>
734 mutability: syn!(Mutability) >>
735 self_: syn!(Self_) >>
736 not!(syn!(Colon)) >>
737 (ArgSelfRef {
738 lifetime: lt,
739 mutbl: mutability,
740 and_token: and,
741 self_token: self_,
742 }.into())
743 )
744 |
745 do_parse!(
746 mutability: syn!(Mutability) >>
747 self_: syn!(Self_) >>
748 not!(syn!(Colon)) >>
749 (ArgSelf {
750 mutbl: mutability,
751 self_token: self_,
752 }.into())
753 )
754 |
755 do_parse!(
756 pat: syn!(Pat) >>
757 colon: syn!(Colon) >>
758 ty: syn!(Ty) >>
759 (ArgCaptured {
760 pat: pat,
761 ty: ty,
762 colon_token: colon,
763 }.into())
764 )
765 |
766 syn!(Ty) => { FnArg::Ignored }
767 }
768 }
769 }
David Tolnay62f374c2016-10-02 13:37:00 -0700770
David Tolnay35902302016-10-06 01:11:08 -0700771 named!(item_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700772 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
773 vis: syn!(Visibility) >>
774 mod_: syn!(Mod) >>
775 id: syn!(Ident) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700776 content: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700777 syn!(Semi) => { Ok }
David Tolnay37d10332016-10-13 20:51:04 -0700778 |
Alex Crichton954046c2017-05-30 21:49:42 -0700779 braces!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700780 tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700781 many0!(call!(Attribute::parse_inner)),
782 many0!(syn!(Item))
Michael Layzell416724e2017-05-24 21:12:34 -0400783 )
Alex Crichton954046c2017-05-30 21:49:42 -0700784 ) => { Err }
David Tolnay37d10332016-10-13 20:51:04 -0700785 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700786 (match content {
Alex Crichton954046c2017-05-30 21:49:42 -0700787 Err(((inner_attrs, items), braces)) => Item {
David Tolnay7b8009b2016-10-25 22:36:00 -0700788 ident: id,
789 vis: vis,
790 attrs: {
791 let mut attrs = outer_attrs;
792 attrs.extend(inner_attrs);
793 attrs
794 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 node: ItemMod {
Alex Crichton954046c2017-05-30 21:49:42 -0700796 mod_token: mod_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700797 semi_token: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700798 items: Some((items, braces)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700799 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700800 },
Alex Crichton954046c2017-05-30 21:49:42 -0700801 Ok(semi) => Item {
David Tolnay7b8009b2016-10-25 22:36:00 -0700802 ident: id,
803 vis: vis,
804 attrs: outer_attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 node: ItemMod {
806 items: None,
Alex Crichton954046c2017-05-30 21:49:42 -0700807 mod_token: mod_,
808 semi_token: Some(semi),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700809 }.into(),
David Tolnay7b8009b2016-10-25 22:36:00 -0700810 },
David Tolnay35902302016-10-06 01:11:08 -0700811 })
812 ));
813
814 named!(item_foreign_mod -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700815 attrs: many0!(call!(Attribute::parse_outer)) >>
816 abi: syn!(Abi) >>
817 items: braces!(many0!(syn!(ForeignItem))) >>
David Tolnay35902302016-10-06 01:11:08 -0700818 (Item {
819 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700820 vis: VisInherited {}.into(),
David Tolnay35902302016-10-06 01:11:08 -0700821 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700822 node: ItemForeignMod {
Alex Crichton954046c2017-05-30 21:49:42 -0700823 brace_token: items.1,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700824 abi: abi,
Alex Crichton954046c2017-05-30 21:49:42 -0700825 items: items.0,
Alex Crichton62a0a592017-05-22 13:58:53 -0700826 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700827 })
828 ));
829
Alex Crichton954046c2017-05-30 21:49:42 -0700830 impl Synom for ForeignItem {
831 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
832 alt! {
833 input,
834 foreign_fn
835 |
836 foreign_static
837 }
838 }
839 }
David Tolnay35902302016-10-06 01:11:08 -0700840
841 named!(foreign_fn -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700842 attrs: many0!(call!(Attribute::parse_outer)) >>
843 vis: syn!(Visibility) >>
844 fn_: syn!(Fn_) >>
845 name: syn!(Ident) >>
846 generics: syn!(Generics) >>
847 inputs: parens!(do_parse!(
848 args: call!(Delimited::parse_terminated) >>
849 variadic: cond!(args.is_empty() || args.trailing_delim(),
850 option!(syn!(Dot3))) >>
851 (args, variadic)
852 )) >>
853 ret: syn!(FunctionRetTy) >>
854 where_clause: syn!(WhereClause) >>
855 semi: syn!(Semi) >>
856 ({
857 let ((inputs, variadic), parens) = inputs;
858 let variadic = variadic.and_then(|v| v);
859 ForeignItem {
860 ident: name,
861 attrs: attrs,
862 semi_token: semi,
863 node: ForeignItemFn {
864 decl: Box::new(FnDecl {
865 fn_token: fn_,
866 paren_token: parens,
867 inputs: inputs,
868 variadic: variadic.is_some(),
869 dot_tokens: variadic,
870 output: ret,
871 generics: Generics {
872 where_clause: where_clause,
873 .. generics
874 },
875 }),
876 }.into(),
877 vis: vis,
878 }
David Tolnay35902302016-10-06 01:11:08 -0700879 })
880 ));
881
882 named!(foreign_static -> ForeignItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700883 attrs: many0!(call!(Attribute::parse_outer)) >>
884 vis: syn!(Visibility) >>
885 static_: syn!(Static) >>
886 mutability: syn!(Mutability) >>
887 id: syn!(Ident) >>
888 colon: syn!(Colon) >>
889 ty: syn!(Ty) >>
890 semi: syn!(Semi) >>
David Tolnay35902302016-10-06 01:11:08 -0700891 (ForeignItem {
892 ident: id,
893 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -0700894 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700895 node: ForeignItemStatic {
896 ty: Box::new(ty),
897 mutbl: mutability,
Alex Crichton954046c2017-05-30 21:49:42 -0700898 static_token: static_,
899 colon_token: colon,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 }.into(),
David Tolnay35902302016-10-06 01:11:08 -0700901 vis: vis,
902 })
903 ));
904
David Tolnay3cf52982016-10-01 17:11:37 -0700905 named!(item_ty -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700906 attrs: many0!(call!(Attribute::parse_outer)) >>
907 vis: syn!(Visibility) >>
908 type_: syn!(Type) >>
909 id: syn!(Ident) >>
910 generics: syn!(Generics) >>
911 where_clause: syn!(WhereClause) >>
912 eq: syn!(Eq) >>
913 ty: syn!(Ty) >>
914 semi: syn!(Semi) >>
David Tolnay3cf52982016-10-01 17:11:37 -0700915 (Item {
916 ident: id,
917 vis: vis,
918 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700919 node: ItemTy {
Alex Crichton954046c2017-05-30 21:49:42 -0700920 type_token: type_,
921 eq_token: eq,
922 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700923 ty: Box::new(ty),
924 generics: Generics {
David Tolnay04bb3ad2016-10-30 10:59:01 -0700925 where_clause: where_clause,
926 ..generics
927 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700928 }.into(),
David Tolnay3cf52982016-10-01 17:11:37 -0700929 })
930 ));
931
David Tolnaya96a3fa2016-09-24 07:17:42 -0700932 named!(item_struct_or_enum -> Item, map!(
Alex Crichton954046c2017-05-30 21:49:42 -0700933 syn!(DeriveInput),
David Tolnay0e837402016-12-22 17:25:55 -0500934 |def: DeriveInput| Item {
David Tolnayedf2b992016-09-23 20:43:45 -0700935 ident: def.ident,
936 vis: def.vis,
937 attrs: def.attrs,
938 node: match def.body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700939 Body::Enum(data) => {
940 ItemEnum {
941 variants: data.variants,
942 brace_token: data.brace_token,
943 enum_token: data.enum_token,
944 generics: def.generics,
945 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700946 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700947 Body::Struct(data) => {
948 ItemStruct {
949 data: data.data,
950 struct_token: data.struct_token,
951 semi_token: data.semi_token,
952 generics: def.generics,
953 }.into()
David Tolnayedf2b992016-09-23 20:43:45 -0700954 }
955 }
956 }
957 ));
David Tolnay42602292016-10-01 22:25:45 -0700958
David Tolnay2f9fa632016-10-03 22:08:48 -0700959 named!(item_union -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700960 attrs: many0!(call!(Attribute::parse_outer)) >>
961 vis: syn!(Visibility) >>
962 union_: syn!(Union) >>
963 id: syn!(Ident) >>
964 generics: syn!(Generics) >>
965 where_clause: syn!(WhereClause) >>
966 fields: braces!(call!(Delimited::parse_terminated_with,
967 Field::parse_struct)) >>
David Tolnay2f9fa632016-10-03 22:08:48 -0700968 (Item {
969 ident: id,
970 vis: vis,
971 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 node: ItemUnion {
Alex Crichton954046c2017-05-30 21:49:42 -0700973 union_token: union_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700974 data: VariantData::Struct(fields.0, fields.1),
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 generics: Generics {
David Tolnay2f9fa632016-10-03 22:08:48 -0700976 where_clause: where_clause,
977 .. generics
978 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700979 }.into(),
David Tolnay2f9fa632016-10-03 22:08:48 -0700980 })
981 ));
982
David Tolnay0aecb732016-10-03 23:03:50 -0700983 named!(item_trait -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700984 attrs: many0!(call!(Attribute::parse_outer)) >>
985 vis: syn!(Visibility) >>
986 unsafety: syn!(Unsafety) >>
987 trait_: syn!(Trait) >>
988 id: syn!(Ident) >>
989 generics: syn!(Generics) >>
990 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -0700992 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700993 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700994 where_clause: syn!(WhereClause) >>
995 body: braces!(many0!(syn!(TraitItem))) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700996 (Item {
997 ident: id,
998 vis: vis,
999 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 node: ItemTrait {
Alex Crichton954046c2017-05-30 21:49:42 -07001001 trait_token: trait_,
1002 brace_token: body.1,
1003 colon_token: colon,
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 unsafety: unsafety,
1005 generics: Generics {
David Tolnay0aecb732016-10-03 23:03:50 -07001006 where_clause: where_clause,
1007 .. generics
1008 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001009 supertraits: bounds.unwrap_or_default(),
Alex Crichton954046c2017-05-30 21:49:42 -07001010 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001011 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001012 })
1013 ));
1014
David Tolnayf94e2362016-10-04 00:29:51 -07001015 named!(item_default_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001016 attrs: many0!(call!(Attribute::parse_outer)) >>
1017 unsafety: syn!(Unsafety) >>
1018 impl_: syn!(Impl) >>
1019 path: syn!(Path) >>
1020 for_: syn!(For) >>
1021 dot2: syn!(Dot2) >>
1022 braces: braces!(epsilon!()) >>
David Tolnayf94e2362016-10-04 00:29:51 -07001023 (Item {
1024 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001025 vis: VisInherited {}.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001026 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001027 node: ItemDefaultImpl {
1028 unsafety: unsafety,
1029 path: path,
Alex Crichton954046c2017-05-30 21:49:42 -07001030 impl_token: impl_,
1031 for_token: for_,
1032 dot2_token: dot2,
1033 brace_token: braces.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001034 }.into(),
David Tolnayf94e2362016-10-04 00:29:51 -07001035 })
1036 ));
1037
Alex Crichton954046c2017-05-30 21:49:42 -07001038 impl Synom for TraitItem {
1039 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1040 alt! {
1041 input,
1042 trait_item_const
1043 |
1044 trait_item_method
1045 |
1046 trait_item_type
1047 |
1048 trait_item_mac
1049 }
1050 }
1051 }
David Tolnay0aecb732016-10-03 23:03:50 -07001052
1053 named!(trait_item_const -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001054 attrs: many0!(call!(Attribute::parse_outer)) >>
1055 const_: syn!(Const) >>
1056 id: syn!(Ident) >>
1057 colon: syn!(Colon) >>
1058 ty: syn!(Ty) >>
1059 value: option!(tuple!(syn!(Eq), syn!(Expr))) >>
1060 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001061 (TraitItem {
1062 ident: id,
1063 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001064 node: TraitItemConst {
1065 ty: ty,
Alex Crichton954046c2017-05-30 21:49:42 -07001066 const_token: const_,
1067 colon_token: colon,
1068 eq_token: value.as_ref().map(|p| Eq((p.0).0)),
1069 default: value.map(|p| p.1),
1070 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001072 })
1073 ));
1074
1075 named!(trait_item_method -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001076 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1077 constness: syn!(Constness) >>
1078 unsafety: syn!(Unsafety) >>
1079 abi: option!(syn!(Abi)) >>
1080 fn_: syn!(Fn_) >>
1081 name: syn!(Ident) >>
1082 generics: syn!(Generics) >>
1083 inputs: parens!(call!(Delimited::parse_terminated)) >>
1084 ret: syn!(FunctionRetTy) >>
1085 where_clause: syn!(WhereClause) >>
1086 body: option!(braces!(
1087 tuple!(many0!(call!(Attribute::parse_inner)),
1088 call!(Block::parse_within))
David Tolnay5859df12016-10-29 22:49:54 -07001089 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001090 semi: cond!(body.is_none(), syn!(Semi)) >>
David Tolnay5859df12016-10-29 22:49:54 -07001091 ({
1092 let (inner_attrs, stmts) = match body {
Alex Crichton954046c2017-05-30 21:49:42 -07001093 Some(((inner_attrs, stmts), b)) => (inner_attrs, Some((stmts, b))),
David Tolnay5859df12016-10-29 22:49:54 -07001094 None => (Vec::new(), None),
1095 };
1096 TraitItem {
1097 ident: name,
1098 attrs: {
1099 let mut attrs = outer_attrs;
1100 attrs.extend(inner_attrs);
1101 attrs
David Tolnay0aecb732016-10-03 23:03:50 -07001102 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001103 node: TraitItemMethod {
Alex Crichton954046c2017-05-30 21:49:42 -07001104 semi_token: semi,
Alex Crichton62a0a592017-05-22 13:58:53 -07001105 sig: MethodSig {
David Tolnay5859df12016-10-29 22:49:54 -07001106 unsafety: unsafety,
1107 constness: constness,
1108 abi: abi,
1109 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001110 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001111 output: ret,
David Tolnay5859df12016-10-29 22:49:54 -07001112 variadic: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001113 fn_token: fn_,
1114 paren_token: inputs.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001115 dot_tokens: None,
1116 generics: Generics {
1117 where_clause: where_clause,
1118 .. generics
1119 },
David Tolnay5859df12016-10-29 22:49:54 -07001120 },
1121 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001122 default: stmts.map(|stmts| {
1123 Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001124 stmts: stmts.0,
1125 brace_token: stmts.1,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001126 }
1127 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001128 }.into(),
David Tolnay5859df12016-10-29 22:49:54 -07001129 }
David Tolnay0aecb732016-10-03 23:03:50 -07001130 })
1131 ));
1132
1133 named!(trait_item_type -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001134 attrs: many0!(call!(Attribute::parse_outer)) >>
1135 type_: syn!(Type) >>
1136 id: syn!(Ident) >>
1137 colon: option!(syn!(Colon)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001138 bounds: cond!(colon.is_some(),
Alex Crichton954046c2017-05-30 21:49:42 -07001139 call!(Delimited::parse_separated_nonempty)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001140 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001141 default: option!(tuple!(syn!(Eq), syn!(Ty))) >>
1142 semi: syn!(Semi) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001143 (TraitItem {
1144 ident: id,
1145 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001146 node: TraitItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001147 type_token: type_,
1148 colon_token: colon,
1149 eq_token: default.as_ref().map(|p| Eq((p.0).0)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001150 bounds: bounds.unwrap_or_default(),
Alex Crichton954046c2017-05-30 21:49:42 -07001151 semi_token: semi,
1152 default: default.map(|p| p.1),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001153 }.into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001154 })
1155 ));
1156
1157 named!(trait_item_mac -> TraitItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001158 attrs: many0!(call!(Attribute::parse_outer)) >>
1159 mac: syn!(Mac) >>
1160 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay0aecb732016-10-03 23:03:50 -07001161 (TraitItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001162 ident: "".into(),
David Tolnay0aecb732016-10-03 23:03:50 -07001163 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001164 node: TraitItemKind::Macro(mac),
David Tolnay0aecb732016-10-03 23:03:50 -07001165 })
1166 ));
1167
David Tolnay4c9be372016-10-06 00:47:37 -07001168 named!(item_impl -> Item, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001169 attrs: many0!(call!(Attribute::parse_outer)) >>
1170 unsafety: syn!(Unsafety) >>
1171 impl_: syn!(Impl) >>
1172 generics: syn!(Generics) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001173 polarity_path: alt!(
1174 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001175 polarity: syn!(ImplPolarity) >>
1176 path: syn!(Path) >>
1177 for_: syn!(For) >>
1178 (polarity, Some(path), Some(for_))
David Tolnay4c9be372016-10-06 00:47:37 -07001179 )
1180 |
Alex Crichton954046c2017-05-30 21:49:42 -07001181 epsilon!() => { |_| (ImplPolarity::Positive, None, None) }
David Tolnay4c9be372016-10-06 00:47:37 -07001182 ) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001183 self_ty: syn!(Ty) >>
1184 where_clause: syn!(WhereClause) >>
1185 body: braces!(many0!(syn!(ImplItem))) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001186 (Item {
1187 ident: "".into(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001188 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001189 attrs: attrs,
Alex Crichton62a0a592017-05-22 13:58:53 -07001190 node: ItemImpl {
Alex Crichton954046c2017-05-30 21:49:42 -07001191 impl_token: impl_,
1192 brace_token: body.1,
1193 for_token: polarity_path.2,
Alex Crichton62a0a592017-05-22 13:58:53 -07001194 unsafety: unsafety,
1195 polarity: polarity_path.0,
1196 generics: Generics {
David Tolnay4c9be372016-10-06 00:47:37 -07001197 where_clause: where_clause,
1198 .. generics
1199 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001200 trait_: polarity_path.1,
1201 self_ty: Box::new(self_ty),
Alex Crichton954046c2017-05-30 21:49:42 -07001202 items: body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001203 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001204 })
1205 ));
1206
Alex Crichton954046c2017-05-30 21:49:42 -07001207 impl Synom for ImplItem {
1208 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1209 alt! {
1210 input,
1211 impl_item_const
1212 |
1213 impl_item_method
1214 |
1215 impl_item_type
1216 |
1217 impl_item_macro
1218 }
1219 }
1220 }
David Tolnay4c9be372016-10-06 00:47:37 -07001221
1222 named!(impl_item_const -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001223 attrs: many0!(call!(Attribute::parse_outer)) >>
1224 vis: syn!(Visibility) >>
1225 defaultness: syn!(Defaultness) >>
1226 const_: syn!(Const) >>
1227 id: syn!(Ident) >>
1228 colon: syn!(Colon) >>
1229 ty: syn!(Ty) >>
1230 eq: syn!(Eq) >>
1231 value: syn!(Expr) >>
1232 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001233 (ImplItem {
1234 ident: id,
1235 vis: vis,
1236 defaultness: defaultness,
1237 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001238 node: ImplItemConst {
1239 ty: ty,
1240 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001241 const_token: const_,
1242 colon_token: colon,
1243 eq_token: eq,
1244 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001245 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001246 })
1247 ));
1248
1249 named!(impl_item_method -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001250 outer_attrs: many0!(call!(Attribute::parse_outer)) >>
1251 vis: syn!(Visibility) >>
1252 defaultness: syn!(Defaultness) >>
1253 constness: syn!(Constness) >>
1254 unsafety: syn!(Unsafety) >>
1255 abi: option!(syn!(Abi)) >>
1256 fn_: syn!(Fn_) >>
1257 name: syn!(Ident) >>
1258 generics: syn!(Generics) >>
1259 inputs: parens!(call!(Delimited::parse_terminated)) >>
1260 ret: syn!(FunctionRetTy) >>
1261 where_clause: syn!(WhereClause) >>
1262 inner_attrs_stmts: braces!(tuple!(
1263 many0!(call!(Attribute::parse_inner)),
1264 call!(Block::parse_within)
Michael Layzell416724e2017-05-24 21:12:34 -04001265 )) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001266 (ImplItem {
1267 ident: name,
1268 vis: vis,
1269 defaultness: defaultness,
David Tolnay3b9783a2016-10-29 22:37:09 -07001270 attrs: {
1271 let mut attrs = outer_attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001272 attrs.extend((inner_attrs_stmts.0).0);
David Tolnay3b9783a2016-10-29 22:37:09 -07001273 attrs
1274 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001275 node: ImplItemMethod {
1276 sig: MethodSig {
David Tolnay4c9be372016-10-06 00:47:37 -07001277 unsafety: unsafety,
1278 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -07001279 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -07001280 decl: FnDecl {
Alex Crichton954046c2017-05-30 21:49:42 -07001281 fn_token: fn_,
1282 paren_token: inputs.1,
1283 inputs: inputs.0,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001284 output: ret,
David Tolnay292e6002016-10-29 22:03:51 -07001285 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001286 generics: Generics {
1287 where_clause: where_clause,
1288 .. generics
1289 },
1290 dot_tokens: None,
David Tolnay4c9be372016-10-06 00:47:37 -07001291 },
1292 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001293 block: Block {
Alex Crichton954046c2017-05-30 21:49:42 -07001294 brace_token: inner_attrs_stmts.1,
1295 stmts: (inner_attrs_stmts.0).1,
David Tolnay3b9783a2016-10-29 22:37:09 -07001296 },
Alex Crichton62a0a592017-05-22 13:58:53 -07001297 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001298 })
1299 ));
1300
1301 named!(impl_item_type -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001302 attrs: many0!(call!(Attribute::parse_outer)) >>
1303 vis: syn!(Visibility) >>
1304 defaultness: syn!(Defaultness) >>
1305 type_: syn!(Type) >>
1306 id: syn!(Ident) >>
1307 eq: syn!(Eq) >>
1308 ty: syn!(Ty) >>
1309 semi: syn!(Semi) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001310 (ImplItem {
1311 ident: id,
1312 vis: vis,
1313 defaultness: defaultness,
1314 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001315 node: ImplItemType {
Alex Crichton954046c2017-05-30 21:49:42 -07001316 type_token: type_,
1317 eq_token: eq,
1318 semi_token: semi,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001319 ty: ty,
1320 }.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001321 })
1322 ));
1323
1324 named!(impl_item_macro -> ImplItem, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001325 attrs: many0!(call!(Attribute::parse_outer)) >>
1326 mac: syn!(Mac) >>
1327 cond!(!mac.is_braced(), syn!(Semi)) >>
David Tolnay4c9be372016-10-06 00:47:37 -07001328 (ImplItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001329 ident: "".into(),
1330 vis: VisInherited {}.into(),
David Tolnay4c9be372016-10-06 00:47:37 -07001331 defaultness: Defaultness::Final,
1332 attrs: attrs,
Alex Crichton954046c2017-05-30 21:49:42 -07001333 node: ImplItemKind::Macro(mac),
David Tolnay4c9be372016-10-06 00:47:37 -07001334 })
1335 ));
1336
Alex Crichton954046c2017-05-30 21:49:42 -07001337 impl Synom for ImplPolarity {
1338 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1339 alt! {
1340 input,
1341 syn!(Bang) => { ImplPolarity::Negative }
1342 |
1343 epsilon!() => { |_| ImplPolarity::Positive }
1344 }
1345 }
1346 }
David Tolnay4c9be372016-10-06 00:47:37 -07001347
Alex Crichton954046c2017-05-30 21:49:42 -07001348 impl Synom for Constness {
1349 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1350 alt! {
1351 input,
1352 syn!(Const) => { Constness::Const }
1353 |
1354 epsilon!() => { |_| Constness::NotConst }
1355 }
1356 }
1357 }
David Tolnay42602292016-10-01 22:25:45 -07001358
Alex Crichton954046c2017-05-30 21:49:42 -07001359 impl Synom for Defaultness {
1360 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1361 alt! {
1362 input,
1363 syn!(Default_) => { Defaultness::Default }
1364 |
1365 epsilon!() => { |_| Defaultness::Final }
1366 }
1367 }
1368 }
David Tolnayedf2b992016-09-23 20:43:45 -07001369}
David Tolnay4a51dc72016-10-01 00:40:31 -07001370
1371#[cfg(feature = "printing")]
1372mod printing {
1373 use super::*;
1374 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -07001375 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -07001376 use quote::{Tokens, ToTokens};
1377
1378 impl ToTokens for Item {
1379 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001380 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -07001381 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001382 ItemKind::ExternCrate(ref item) => {
David Tolnaycbd9f7d2016-10-30 00:26:29 -07001383 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001384 item.extern_token.to_tokens(tokens);
1385 item.crate_token.to_tokens(tokens);
1386 item.original.to_tokens(tokens);
1387 item.as_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001388 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001389 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001390 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001391 ItemKind::Use(ref item) => {
David Tolnay4a057422016-10-08 00:02:31 -07001392 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001393 item.use_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001394 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 item.semi_token.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001396 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001397 ItemKind::Static(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001398 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001399 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001400 item.mutbl.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001401 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001402 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001403 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001404 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001405 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001406 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001407 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001408 ItemKind::Const(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001409 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001410 item.const_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001411 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001412 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001413 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001415 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416 item.semi_token.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -07001417 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001418 ItemKind::Fn(ref item) => {
David Tolnay42602292016-10-01 22:25:45 -07001419 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001420 item.constness.to_tokens(tokens);
1421 item.unsafety.to_tokens(tokens);
1422 item.abi.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001423 NamedDecl(&item.decl, &self.ident).to_tokens(tokens);
1424 item.block.brace_token.surround(tokens, |tokens| {
1425 tokens.append_all(self.attrs.inner());
1426 tokens.append_all(&item.block.stmts);
1427 });
David Tolnay42602292016-10-01 22:25:45 -07001428 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001429 ItemKind::Mod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001430 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001431 item.mod_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001432 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001433 if let Some((ref items, ref brace)) = item.items {
1434 brace.surround(tokens, |tokens| {
David Tolnay7b8009b2016-10-25 22:36:00 -07001435 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001436 tokens.append_all(items);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001437 });
David Tolnay37d10332016-10-13 20:51:04 -07001438 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001439 item.semi_token.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001440 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001441 ItemKind::ForeignMod(ref item) => {
David Tolnay35902302016-10-06 01:11:08 -07001442 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001443 item.abi.to_tokens(tokens);
1444 item.brace_token.surround(tokens, |tokens| {
1445 tokens.append_all(&item.items);
1446 });
David Tolnay35902302016-10-06 01:11:08 -07001447 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001448 ItemKind::Ty(ref item) => {
David Tolnay3cf52982016-10-01 17:11:37 -07001449 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001450 item.type_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001451 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001452 item.generics.to_tokens(tokens);
1453 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001454 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001455 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001456 item.semi_token.to_tokens(tokens);
David Tolnay3cf52982016-10-01 17:11:37 -07001457 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001458 ItemKind::Enum(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001459 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001460 item.enum_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001461 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001462 item.generics.to_tokens(tokens);
1463 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001464 item.brace_token.surround(tokens, |tokens| {
1465 item.variants.to_tokens(tokens);
1466 });
David Tolnay4a51dc72016-10-01 00:40:31 -07001467 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001468 ItemKind::Struct(ref item) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001469 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001470 item.struct_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001471 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001472 item.generics.to_tokens(tokens);
1473 match item.data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001474 VariantData::Struct(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001475 item.generics.where_clause.to_tokens(tokens);
1476 item.data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001477 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001478 VariantData::Tuple(..) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001479 item.data.to_tokens(tokens);
1480 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001481 }
1482 VariantData::Unit => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001483 item.generics.where_clause.to_tokens(tokens);
David Tolnay28c1db62016-10-27 22:48:18 -07001484 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001485 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001486 item.semi_token.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001487 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001488 ItemKind::Union(ref item) => {
David Tolnay2f9fa632016-10-03 22:08:48 -07001489 self.vis.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001490 item.union_token.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001491 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001492 item.generics.to_tokens(tokens);
1493 item.generics.where_clause.to_tokens(tokens);
1494 item.data.to_tokens(tokens);
David Tolnay2f9fa632016-10-03 22:08:48 -07001495 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001496 ItemKind::Trait(ref item) => {
David Tolnayca085422016-10-04 00:12:38 -07001497 self.vis.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001498 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001499 item.trait_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001500 self.ident.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001501 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001502 item.colon_token.to_tokens(tokens);
1503 item.supertraits.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001504 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 Tolnayca085422016-10-04 00:12:38 -07001508 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001509 ItemKind::DefaultImpl(ref item) => {
1510 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001511 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001512 item.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001513 item.for_token.to_tokens(tokens);
1514 item.dot2_token.to_tokens(tokens);
1515 item.brace_token.surround(tokens, |_tokens| {});
David Tolnayf94e2362016-10-04 00:29:51 -07001516 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001517 ItemKind::Impl(ref item) => {
1518 item.unsafety.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001519 item.impl_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001520 item.generics.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001521 item.polarity.to_tokens(tokens);
1522 item.trait_.to_tokens(tokens);
1523 item.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001524 item.self_ty.to_tokens(tokens);
1525 item.generics.where_clause.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001526 item.brace_token.surround(tokens, |tokens| {
1527 tokens.append_all(&item.items);
1528 });
David Tolnay4c9be372016-10-06 00:47:37 -07001529 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001530 ItemKind::Mac(ref mac) => {
1531 mac.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001532 mac.bang_token.to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001533 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001534 tokens.append_all(&mac.tokens);
1535 if !mac.is_braced() {
1536 tokens::Semi::default().to_tokens(tokens);
David Tolnaycc3d66e2016-10-02 23:36:05 -07001537 }
1538 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001539 }
1540 }
1541 }
David Tolnay42602292016-10-01 22:25:45 -07001542
Alex Crichton62a0a592017-05-22 13:58:53 -07001543 impl ToTokens for PathSimple {
David Tolnay4a057422016-10-08 00:02:31 -07001544 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07001545 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001546 self.as_token.to_tokens(tokens);
1547 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001548 }
1549 }
1550
Alex Crichton62a0a592017-05-22 13:58:53 -07001551 impl ToTokens for PathGlob {
1552 fn to_tokens(&self, tokens: &mut Tokens) {
1553 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001554 self.colon2_token.to_tokens(tokens);
1555 self.star_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001556 }
1557 }
1558
1559 impl ToTokens for PathList {
1560 fn to_tokens(&self, tokens: &mut Tokens) {
1561 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001562 self.colon2_token.to_tokens(tokens);
1563 self.brace_token.surround(tokens, |tokens| {
1564 self.items.to_tokens(tokens);
1565 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001566 }
1567 }
1568
David Tolnay4a057422016-10-08 00:02:31 -07001569 impl ToTokens for PathListItem {
1570 fn to_tokens(&self, tokens: &mut Tokens) {
1571 self.name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001572 self.as_token.to_tokens(tokens);
1573 self.rename.to_tokens(tokens);
David Tolnay4a057422016-10-08 00:02:31 -07001574 }
1575 }
1576
David Tolnayca085422016-10-04 00:12:38 -07001577 impl ToTokens for TraitItem {
1578 fn to_tokens(&self, tokens: &mut Tokens) {
1579 tokens.append_all(self.attrs.outer());
1580 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001581 TraitItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001582 item.const_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001583 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001584 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001585 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001586 item.eq_token.to_tokens(tokens);
1587 item.default.to_tokens(tokens);
1588 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001589 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001590 TraitItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001591 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001592 match item.default {
David Tolnay3b9783a2016-10-29 22:37:09 -07001593 Some(ref block) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001594 block.brace_token.surround(tokens, |tokens| {
1595 tokens.append_all(self.attrs.inner());
1596 tokens.append_all(&block.stmts);
1597 });
David Tolnay3b9783a2016-10-29 22:37:09 -07001598 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001599 None => {
1600 item.semi_token.to_tokens(tokens);
1601 }
David Tolnayca085422016-10-04 00:12:38 -07001602 }
1603 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001604 TraitItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001605 item.type_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001606 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001607 item.colon_token.to_tokens(tokens);
1608 item.bounds.to_tokens(tokens);
1609 item.eq_token.to_tokens(tokens);
1610 item.default.to_tokens(tokens);
1611 item.semi_token.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001612 }
1613 TraitItemKind::Macro(ref mac) => {
1614 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001615 if !mac.is_braced() {
1616 tokens::Semi::default().to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001617 }
David Tolnayca085422016-10-04 00:12:38 -07001618 }
1619 }
1620 }
1621 }
1622
David Tolnay4c9be372016-10-06 00:47:37 -07001623 impl ToTokens for ImplItem {
1624 fn to_tokens(&self, tokens: &mut Tokens) {
1625 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001626 self.vis.to_tokens(tokens);
1627 self.defaultness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001628 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001629 ImplItemKind::Const(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001630 item.const_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001631 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001632 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001633 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001634 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001635 item.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001636 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001637 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001638 ImplItemKind::Method(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001639 NamedMethod(&item.sig, &self.ident).to_tokens(tokens);
1640 item.block.brace_token.surround(tokens, |tokens| {
1641 tokens.append_all(self.attrs.inner());
1642 tokens.append_all(&item.block.stmts);
1643 });
David Tolnay4c9be372016-10-06 00:47:37 -07001644 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 ImplItemKind::Type(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001646 item.type_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001647 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001648 item.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 item.ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001650 item.semi_token.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001651 }
1652 ImplItemKind::Macro(ref mac) => {
1653 mac.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001654 if !mac.is_braced() {
1655 tokens::Semi::default().to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001656 }
1657 }
1658 }
1659 }
1660 }
1661
David Tolnay35902302016-10-06 01:11:08 -07001662 impl ToTokens for ForeignItem {
1663 fn to_tokens(&self, tokens: &mut Tokens) {
1664 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001665 self.vis.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001666 match self.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001667 ForeignItemKind::Fn(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001668 NamedDecl(&item.decl, &self.ident).to_tokens(tokens)
David Tolnay35902302016-10-06 01:11:08 -07001669 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001670 ForeignItemKind::Static(ref item) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001671 item.static_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001672 item.mutbl.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001673 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001674 item.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001675 item.ty.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001676 }
1677 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001678 self.semi_token.to_tokens(tokens);
1679 }
1680 }
1681
1682 struct NamedMethod<'a>(&'a MethodSig, &'a Ident);
1683
1684 impl<'a> ToTokens for NamedMethod<'a> {
1685 fn to_tokens(&self, tokens: &mut Tokens) {
1686 self.0.constness.to_tokens(tokens);
1687 self.0.unsafety.to_tokens(tokens);
1688 self.0.abi.to_tokens(tokens);
1689 NamedDecl(&self.0.decl, self.1).to_tokens(tokens);
1690 }
1691 }
1692
1693 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
1694
1695 impl<'a> ToTokens for NamedDecl<'a> {
1696 fn to_tokens(&self, tokens: &mut Tokens) {
1697 self.0.fn_token.to_tokens(tokens);
1698 self.1.to_tokens(tokens);
1699 self.0.generics.to_tokens(tokens);
1700 self.0.paren_token.surround(tokens, |tokens| {
1701 self.0.inputs.to_tokens(tokens);
1702 self.0.dot_tokens.to_tokens(tokens);
1703 });
1704 self.0.output.to_tokens(tokens);
1705 self.0.generics.where_clause.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001706 }
1707 }
1708
Alex Crichton62a0a592017-05-22 13:58:53 -07001709 impl ToTokens for ArgSelfRef {
David Tolnay62f374c2016-10-02 13:37:00 -07001710 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001712 self.lifetime.to_tokens(tokens);
1713 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001714 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001715 }
1716 }
1717
1718 impl ToTokens for ArgSelf {
1719 fn to_tokens(&self, tokens: &mut Tokens) {
1720 self.mutbl.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001721 self.self_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001722 }
1723 }
1724
1725 impl ToTokens for ArgCaptured {
1726 fn to_tokens(&self, tokens: &mut Tokens) {
1727 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001728 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001729 self.ty.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001730 }
1731 }
1732
David Tolnay42602292016-10-01 22:25:45 -07001733 impl ToTokens for Constness {
1734 fn to_tokens(&self, tokens: &mut Tokens) {
1735 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001736 Constness::Const(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07001737 Constness::NotConst => {
1738 // nothing
1739 }
David Tolnay42602292016-10-01 22:25:45 -07001740 }
1741 }
1742 }
1743
David Tolnay4c9be372016-10-06 00:47:37 -07001744 impl ToTokens for Defaultness {
1745 fn to_tokens(&self, tokens: &mut Tokens) {
1746 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 Defaultness::Default(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001748 Defaultness::Final => {
1749 // nothing
1750 }
1751 }
1752 }
1753 }
1754
1755 impl ToTokens for ImplPolarity {
1756 fn to_tokens(&self, tokens: &mut Tokens) {
1757 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001758 ImplPolarity::Negative(ref t) => t.to_tokens(tokens),
David Tolnay4c9be372016-10-06 00:47:37 -07001759 ImplPolarity::Positive => {
1760 // nothing
1761 }
1762 }
1763 }
1764 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001765}