David Tolnay | 8d1ffa3 | 2018-01-01 00:17:46 -0500 | [diff] [blame^] | 1 | //! 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 | |
| 20 | extern crate syn; |
| 21 | |
| 22 | use std::env; |
| 23 | use std::fs::File; |
| 24 | use std::io::Read; |
| 25 | use std::process; |
| 26 | |
| 27 | fn 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 | } |