blob: 584fe3cabfc36ac420ab52ce141324a31bf39570 [file] [log] [blame]
David Tolnay8d1ffa32018-01-01 00:17:46 -05001//! Parse a Rust source file into a `syn::File` and print out a debug
2//! representation of the syntax tree.
3//!
4//! Use the following command from this directory to test this program by
5//! running it on its own source code:
6//!
7//! cargo run -- main.rs
8//!
9//! The output will begin with:
10//!
11//! File {
12//! shebang: None,
13//! attrs: [
14//! Attribute {
15//! pound_token: Pound,
16//! style: Inner(
17//! ...
18//! }
19
20extern crate syn;
21
22use std::env;
23use std::fs::File;
24use std::io::Read;
25use std::process;
26
27fn main() {
28 let mut args = env::args();
29 let _ = args.next(); // executable name
30
31 let filename = match (args.next(), args.next()) {
32 (Some(filename), None) => filename,
33 _ => {
34 eprintln!("Usage: dump-syntax path/to/filename.rs");
35 process::exit(1);
36 }
37 };
38
39 let mut file = File::open(&filename).expect("Unable to open file");
40
41 let mut src = String::new();
42 file.read_to_string(&mut src).expect("Unable to read file");
43
44 let syntax = syn::parse_file(&src).expect("Unable to parse file");
45 println!("{:#?}", syntax);
46}