1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![recursion_limit = "192"]

#[macro_use]
extern crate quote;

use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::convert::AsRef;
use std::fs::File;
use std::process::exit;
use std::env;

mod translate;
mod grammar;

/// Compile a peg grammar to Rust source
pub fn compile(input: &str) -> Result<String, String> {
    let ast_items = match grammar::items(&input) {
        Ok(g) => g,
        Err(msg) => {
            return Err(format!("Error parsing language specification: {}", msg))
        }
    };

    let grammar_def = translate::Grammar::from_ast(ast_items)?;
    let output_tokens = translate::compile_grammar(&grammar_def);
    Ok(output_tokens?.to_string())
}

/// Compile the PEG grammar in the specified filename to cargo's OUT_DIR.
/// Errors are emitted to stderr and terminate the process.
pub fn cargo_build<T: AsRef<Path> + ?Sized>(input_path: &T) {
    let mut stderr = io::stderr();
    let input_path = input_path.as_ref();

    let mut peg_source = String::new();
    if let Err(e) = File::open(input_path).and_then(|mut x| x.read_to_string(&mut peg_source)) {
        writeln!(stderr, "Could not read PEG input file `{}`: {}", input_path.display(), e).unwrap();
        exit(1);
    }

    println!("cargo:rerun-if-changed={}", input_path.display());

    let rust_source = match compile(&peg_source) {
        Ok(s) => s,
        Err(e) => {
            writeln!(stderr, "Error compiling PEG grammar `{}`:\n\t{}", input_path.display(), e).unwrap();
            exit(1);
        }
    };

    let out_dir: PathBuf = env::var_os("OUT_DIR").unwrap().into();
    let rust_path = out_dir.join(input_path.file_name().unwrap()).with_extension("rs");

    let mut output_file = File::create(&rust_path).unwrap();
    output_file.write_all(rust_source.as_bytes()).unwrap();
}