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 | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 13 | /// |
| 14 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 15 | pub struct File { |
| 16 | pub shebang: Option<String>, |
| 17 | pub attrs: Vec<Attribute>, |
| 18 | pub items: Vec<Item>, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | #[cfg(feature = "parsing")] |
| 23 | pub mod parsing { |
| 24 | use super::*; |
| 25 | |
| 26 | use synom::Synom; |
| 27 | |
| 28 | impl Synom for File { |
| 29 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 30 | attrs: many0!(Attribute::parse_inner) >> |
| 31 | items: many0!(Item::parse) >> |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 32 | (File { |
| 33 | shebang: None, |
| 34 | attrs: attrs, |
| 35 | items: items, |
| 36 | }) |
| 37 | )); |
| 38 | |
| 39 | fn description() -> Option<&'static str> { |
| 40 | Some("crate") |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[cfg(feature = "printing")] |
| 46 | mod printing { |
| 47 | use super::*; |
| 48 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 49 | use quote::{ToTokens, Tokens}; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 50 | |
| 51 | impl ToTokens for File { |
| 52 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 53 | tokens.append_all(self.attrs.inner()); |
| 54 | tokens.append_all(&self.items); |
| 55 | } |
| 56 | } |
| 57 | } |