1 // Copyright 2018 Syn Developers
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.
12 /// A complete file of Rust source code.
14 /// *This type is available if Syn is built with the `"full"` feature.*
18 /// Parse a Rust source file into a `syn::File` and print out a debug
19 /// representation of the syntax tree.
25 /// use std::fs::File;
26 /// use std::io::Read;
32 /// # fn fake_main() {
33 /// let mut args = env::args();
34 /// let _ = args.next(); // executable name
36 /// let filename = match (args.next(), args.next()) {
37 /// (Some(filename), None) => filename,
39 /// eprintln!("Usage: dump-syntax path/to/filename.rs");
44 /// let mut file = File::open(&filename).expect("Unable to open file");
46 /// let mut src = String::new();
47 /// file.read_to_string(&mut src).expect("Unable to read file");
49 /// let syntax = syn::parse_file(&src).expect("Unable to parse file");
50 /// println!("{:#?}", syntax);
54 /// Running with its own source code as input, this program prints output
66 /// extern_token: Extern,
67 /// crate_token: Crate,
81 pub shebang
: Option
<String
>,
82 pub attrs
: Vec
<Attribute
>,
87 #[cfg(feature = "parsing")]
94 named
!(parse
-> Self, do_parse
!(
95 attrs
: many0
!(Attribute
::parse_inner
) >>
96 items
: many0
!(Item
::parse
) >>
104 fn description() -> Option
<&'
static str> {
110 #[cfg(feature = "printing")]
113 use attr
::FilterAttrs
;
114 use proc_macro2
::TokenStream
;
115 use quote
::{ToTokens, TokenStreamExt}
;
117 impl ToTokens
for File
{
118 fn to_tokens(&self, tokens
: &mut TokenStream
) {
119 tokens
.append_all(self.attrs
.inner());
120 tokens
.append_all(&self.items
);