| 1 |
|
| 2 |
|
| 3 |
extern crate proc_macro; |
| 4 |
extern crate proc_macro2; |
| 5 |
extern crate quote; |
| 6 |
|
| 7 |
use std::env; |
| 8 |
use std::fs::File; |
| 9 |
use std::io::{stderr, stdin, stdout}; |
| 10 |
use std::io::{Read, Write}; |
| 11 |
use std::path::Path; |
| 12 |
use std::process; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
extern crate peg_runtime as peg; |
| 17 |
|
| 18 |
mod analysis; |
| 19 |
mod ast; |
| 20 |
mod grammar; |
| 21 |
mod tokens; |
| 22 |
mod translate; |
| 23 |
|
| 24 |
fn main() { |
| 25 |
let args = env::args_os().collect::<Vec<_>>(); |
| 26 |
let progname = &args[0]; |
| 27 |
let mut log = stderr(); |
| 28 |
|
| 29 |
let mut source = String::new(); |
| 30 |
|
| 31 |
if args.len() == 2 && &args[1] != "-h" { |
| 32 |
File::open(Path::new(&args[1])) |
| 33 |
.unwrap() |
| 34 |
.read_to_string(&mut source) |
| 35 |
.unwrap(); |
| 36 |
} else if args.len() == 1 { |
| 37 |
stdin().read_to_string(&mut source).unwrap(); |
| 38 |
} else { |
| 39 |
writeln!(log, "Usage: {} [file]", progname.to_string_lossy()).unwrap(); |
| 40 |
process::exit(0); |
| 41 |
} |
| 42 |
|
| 43 |
let source_tokens = source.parse().expect("Error tokenizing input"); |
| 44 |
let input_tokens = tokens::FlatTokenStream::new(source_tokens); |
| 45 |
let grammar = match grammar::peg::peg_grammar(&input_tokens) { |
| 46 |
Ok(g) => g, |
| 47 |
Err(err) => { |
| 48 |
eprintln!("Failed to parse grammar: expected {}", err.expected); |
| 49 |
process::exit(1); |
| 50 |
} |
| 51 |
}; |
| 52 |
let parser_tokens = translate::compile_grammar(&grammar); |
| 53 |
let mut out = stdout(); |
| 54 |
writeln!(&mut out, "// Generated by rust-peg. Do not edit.").unwrap(); |
| 55 |
write!(&mut out, "{}", parser_tokens).unwrap(); |
| 56 |
} |