David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 9 | use super::*; |
| 10 | |
| 11 | ast_struct! { |
David Tolnay | 4844566 | 2018-01-07 01:06:48 -0800 | [diff] [blame^] | 12 | /// A complete file of Rust source code. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 13 | pub struct File { |
| 14 | pub shebang: Option<String>, |
| 15 | pub attrs: Vec<Attribute>, |
| 16 | pub items: Vec<Item>, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | #[cfg(feature = "parsing")] |
| 21 | pub mod parsing { |
| 22 | use super::*; |
| 23 | |
| 24 | use synom::Synom; |
| 25 | |
| 26 | impl Synom for File { |
| 27 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 28 | attrs: many0!(Attribute::parse_inner) >> |
| 29 | items: many0!(Item::parse) >> |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 30 | (File { |
| 31 | shebang: None, |
| 32 | attrs: attrs, |
| 33 | items: items, |
| 34 | }) |
| 35 | )); |
| 36 | |
| 37 | fn description() -> Option<&'static str> { |
| 38 | Some("crate") |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[cfg(feature = "printing")] |
| 44 | mod printing { |
| 45 | use super::*; |
| 46 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 47 | use quote::{ToTokens, Tokens}; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 48 | |
| 49 | impl ToTokens for File { |
| 50 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 51 | tokens.append_all(self.attrs.inner()); |
| 52 | tokens.append_all(&self.items); |
| 53 | } |
| 54 | } |
| 55 | } |