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
use std::error::Error;
use std::path::Path;
use std::process::Command;

pub fn process_bison_file(filepath: &Path) -> Result<(), Box<dyn Error>> {
    let input = filepath;
    let output = filepath.with_extension("rs");

    println!("cargo:rerun-if-changed={}", input.to_str().unwrap());
    let bison_root_dir = Path::new(file!())
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("bison");

    println!(
        "cargo:rerun-if-changed={}",
        bison_root_dir.join("c-like.m4").to_str().unwrap()
    );
    println!(
        "cargo:rerun-if-changed={}",
        bison_root_dir.join("lalr1-rust.m4").to_str().unwrap()
    );
    println!(
        "cargo:rerun-if-changed={}",
        bison_root_dir.join("main.m4").to_str().unwrap()
    );
    println!(
        "cargo:rerun-if-changed={}",
        bison_root_dir.join("rust.m4").to_str().unwrap()
    );

    let bison_root_file = bison_root_dir.join("main.m4");

    let args = &[
        "-S",
        bison_root_file.to_str().unwrap(),
        "-o",
        output.to_str().unwrap(),
        input.to_str().unwrap(),
    ];

    let output = Command::new("bison").args(args).output()?;
    println!("output = {:#?}", output);

    Ok(())
}