blob: 0739a677e1f7685f983265abb3f2a4ab5a3887b2 [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
David Tolnay8d1ffa32018-01-01 00:17:46 -050020use std::env;
21use std::fs::File;
22use std::io::Read;
23use std::process;
24
25fn main() {
26 let mut args = env::args();
27 let _ = args.next(); // executable name
28
29 let filename = match (args.next(), args.next()) {
30 (Some(filename), None) => filename,
31 _ => {
32 eprintln!("Usage: dump-syntax path/to/filename.rs");
33 process::exit(1);
34 }
35 };
36
37 let mut file = File::open(&filename).expect("Unable to open file");
38
39 let mut src = String::new();
40 file.read_to_string(&mut src).expect("Unable to read file");
41
42 let syntax = syn::parse_file(&src).expect("Unable to parse file");
43 println!("{:#?}", syntax);
44}