extern crate wee_peg as peg;
use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::io::{stdin, stdout, stderr};
use std::path::Path;
use std::process;
fn main() {
let args = env::args_os().collect::<Vec<_>>();
let progname = &args[0];
let mut log = stderr();
let fname: &str;
let mut source = String::new();
if args.len() == 2 && &args[1] != "-h" {
fname = args[1].to_str().unwrap();
File::open(Path::new(&args[1])).unwrap().read_to_string(&mut source).unwrap();
} else if args.len() == 1 {
fname = "<stdin>";
stdin().read_to_string(&mut source).unwrap();
} else {
writeln!(log, "Usage: {} [file]", progname.to_string_lossy()).unwrap();
process::exit(0);
}
match peg::compile(fname.to_owned(), source) {
Ok(parser) => {
let mut out = stdout();
writeln!(&mut out, "// Generated by rust-peg. Do not edit.").unwrap();
write!(&mut out, "{}", parser).unwrap();
}
Err(()) => {
process::exit(1);
}
}
}