1use crate::attr::Attribute;
2use crate::item::Item;
3
4#[doc = r" A complete file of Rust source code."]
#[doc = r""]
#[doc = r" Typically `File` objects are created with [`parse_file`]."]
#[doc = r""]
#[doc = r" [`parse_file`]: crate::parse_file"]
#[doc = r""]
#[doc = r" # Example"]
#[doc = r""]
#[doc = r" Parse a Rust source file into a `syn::File` and print out a debug"]
#[doc = r" representation of the syntax tree."]
#[doc = r""]
#[doc = r" ```"]
#[doc = r" use std::env;"]
#[doc = r" use std::fs;"]
#[doc = r" use std::process;"]
#[doc = r""]
#[doc = r" fn main() {"]
#[doc = r" # }"]
#[doc = r" #"]
#[doc = r" # fn fake_main() {"]
#[doc = r" let mut args = env::args();"]
#[doc = r" let _ = args.next(); // executable name"]
#[doc = r""]
#[doc = r" let filename = match (args.next(), args.next()) {"]
#[doc = r" (Some(filename), None) => filename,"]
#[doc = r" _ => {"]
#[doc =
r#" eprintln!("Usage: dump-syntax path/to/filename.rs");"#]
#[doc = r" process::exit(1);"]
#[doc = r" }"]
#[doc = r" };"]
#[doc = r""]
#[doc =
r#" let src = fs::read_to_string(&filename).expect("unable to read file");"#]
#[doc =
r#" let syntax = syn::parse_file(&src).expect("unable to parse file");"#]
#[doc = r""]
#[doc =
r#" // Debug impl is available if Syn is built with "extra-traits" feature."#]
#[doc = r#" println!("{:#?}", syntax);"#]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc =
r" Running with its own source code as input, this program prints output"]
#[doc = r" that begins with:"]
#[doc = r""]
#[doc = r" ```text"]
#[doc = r" File {"]
#[doc = r" shebang: None,"]
#[doc = r" attrs: [],"]
#[doc = r" items: ["]
#[doc = r" Use("]
#[doc = r" ItemUse {"]
#[doc = r" attrs: [],"]
#[doc = r" vis: Inherited,"]
#[doc = r" use_token: Use,"]
#[doc = r" leading_colon: None,"]
#[doc = r" tree: Path("]
#[doc = r" UsePath {"]
#[doc = r" ident: Ident("]
#[doc = r" std,"]
#[doc = r" ),"]
#[doc = r" colon2_token: Colon2,"]
#[doc = r" tree: Name("]
#[doc = r" UseName {"]
#[doc = r" ident: Ident("]
#[doc = r" env,"]
#[doc = r" ),"]
#[doc = r" },"]
#[doc = r" ),"]
#[doc = r" },"]
#[doc = r" ),"]
#[doc = r" semi_token: Semi,"]
#[doc = r" },"]
#[doc = r" ),"]
#[doc = r" ..."]
#[doc = r" ```"]
pub struct File {
pub shebang: Option<String>,
pub attrs: Vec<Attribute>,
pub items: Vec<Item>,
}ast_struct! {
5 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
79 pub struct File {
80 pub shebang: Option<String>,
81 pub attrs: Vec<Attribute>,
82 pub items: Vec<Item>,
83 }
84}
85
86#[cfg(feature = "parsing")]
87pub(crate) mod parsing {
88 use crate::attr::Attribute;
89 use crate::error::Result;
90 use crate::file::File;
91 use crate::parse::{Parse, ParseStream};
92
93 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
94 impl Parse for File {
95 fn parse(input: ParseStream) -> Result<Self> {
96 Ok(File {
97 shebang: None,
98 attrs: input.call(Attribute::parse_inner)?,
99 items: {
100 let mut items = Vec::new();
101 while !input.is_empty() {
102 items.push(input.parse()?);
103 }
104 items
105 },
106 })
107 }
108 }
109}
110
111#[cfg(feature = "printing")]
112mod printing {
113 use crate::attr::FilterAttrs;
114 use crate::file::File;
115 use proc_macro2::TokenStream;
116 use quote::{ToTokens, TokenStreamExt};
117
118 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
119 impl ToTokens for File {
120 fn to_tokens(&self, tokens: &mut TokenStream) {
121 tokens.append_all(self.attrs.inner());
122 tokens.append_all(&self.items);
123 }
124 }
125}