Nika Layzell | 38b0379 | 2017-10-24 08:56:31 -0400 | [diff] [blame] | 1 | extern crate syn; |
| 2 | |
| 3 | use std::env; |
| 4 | use std::fs::File; |
| 5 | use std::io::{self, Read}; |
| 6 | |
| 7 | fn main() { |
| 8 | let mut args = env::args(); |
| 9 | let _ = args.next(); // executable name |
| 10 | let filename = args.next().unwrap_or_else(|| { |
| 11 | panic!("USAGE: dump-ast FILENAME"); |
| 12 | }); |
| 13 | if args.next().is_some() { |
| 14 | panic!("dump-ast only takes one argument"); |
| 15 | } |
| 16 | |
| 17 | let mut src = String::new(); |
| 18 | if filename != "-" { |
| 19 | let mut file = File::open(&filename).expect("Unable to open source file"); |
| 20 | file.read_to_string(&mut src).expect("Unable to read input file"); |
| 21 | } else { |
| 22 | io::stdin().read_to_string(&mut src).expect("Unable to read stdin"); |
| 23 | } |
| 24 | |
| 25 | let ast = syn::parse_file(&src).expect("Unable to parse file"); |
| 26 | println!("{:#?}", ast); |
| 27 | } |