]>
Commit | Line | Data |
---|---|---|
83c7162d XL |
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 | ||
9 | use super::*; | |
10 | ||
11 | ast_struct! { | |
12 | /// A complete file of Rust source code. | |
13 | /// | |
14 | /// *This type is available if Syn is built with the `"full"` feature.* | |
15 | /// | |
16 | /// # Example | |
17 | /// | |
18 | /// Parse a Rust source file into a `syn::File` and print out a debug | |
19 | /// representation of the syntax tree. | |
20 | /// | |
21 | /// ``` | |
22 | /// extern crate syn; | |
23 | /// | |
24 | /// use std::env; | |
25 | /// use std::fs::File; | |
26 | /// use std::io::Read; | |
27 | /// use std::process; | |
28 | /// | |
29 | /// fn main() { | |
30 | /// # } | |
31 | /// # | |
32 | /// # fn fake_main() { | |
33 | /// let mut args = env::args(); | |
34 | /// let _ = args.next(); // executable name | |
35 | /// | |
36 | /// let filename = match (args.next(), args.next()) { | |
37 | /// (Some(filename), None) => filename, | |
38 | /// _ => { | |
39 | /// eprintln!("Usage: dump-syntax path/to/filename.rs"); | |
40 | /// process::exit(1); | |
41 | /// } | |
42 | /// }; | |
43 | /// | |
44 | /// let mut file = File::open(&filename).expect("Unable to open file"); | |
45 | /// | |
46 | /// let mut src = String::new(); | |
47 | /// file.read_to_string(&mut src).expect("Unable to read file"); | |
48 | /// | |
49 | /// let syntax = syn::parse_file(&src).expect("Unable to parse file"); | |
50 | /// println!("{:#?}", syntax); | |
51 | /// } | |
52 | /// ``` | |
53 | /// | |
54 | /// Running with its own source code as input, this program prints output | |
55 | /// that begins with: | |
56 | /// | |
57 | /// ```text | |
58 | /// File { | |
59 | /// shebang: None, | |
60 | /// attrs: [], | |
61 | /// items: [ | |
62 | /// ExternCrate( | |
63 | /// ItemExternCrate { | |
64 | /// attrs: [], | |
65 | /// vis: Inherited, | |
66 | /// extern_token: Extern, | |
67 | /// crate_token: Crate, | |
68 | /// ident: Ident { | |
69 | /// term: Term( | |
70 | /// "syn" | |
71 | /// ), | |
72 | /// span: Span | |
73 | /// }, | |
74 | /// rename: None, | |
75 | /// semi_token: Semi | |
76 | /// } | |
77 | /// ), | |
78 | /// ... | |
79 | /// ``` | |
80 | pub struct File { | |
81 | pub shebang: Option<String>, | |
82 | pub attrs: Vec<Attribute>, | |
83 | pub items: Vec<Item>, | |
84 | } | |
85 | } | |
86 | ||
87 | #[cfg(feature = "parsing")] | |
88 | pub mod parsing { | |
89 | use super::*; | |
90 | ||
91 | use synom::Synom; | |
92 | ||
93 | impl Synom for File { | |
94 | named!(parse -> Self, do_parse!( | |
95 | attrs: many0!(Attribute::parse_inner) >> | |
96 | items: many0!(Item::parse) >> | |
97 | (File { | |
98 | shebang: None, | |
99 | attrs: attrs, | |
100 | items: items, | |
101 | }) | |
102 | )); | |
103 | ||
104 | fn description() -> Option<&'static str> { | |
105 | Some("crate") | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | #[cfg(feature = "printing")] | |
111 | mod printing { | |
112 | use super::*; | |
113 | use attr::FilterAttrs; | |
114 | use quote::{ToTokens, Tokens}; | |
115 | ||
116 | impl ToTokens for File { | |
117 | fn to_tokens(&self, tokens: &mut Tokens) { | |
118 | tokens.append_all(self.attrs.inner()); | |
119 | tokens.append_all(&self.items); | |
120 | } | |
121 | } | |
122 | } |