summaryrefslogtreecommitdiff
path: root/build.rs
blob: 0c3f1da51bf3574f034d8c7a9d8fe0442a53f7d1 (plain) (blame)
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
use std::fs::{self, create_dir_all, read_dir, read_to_string};
use std::ffi::OsStr;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    create_dir_all("tests/cache")?;

    // Make sure the script reruns if this file changes or files are
    // added/deleted in the parsing folder.
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=tests/cache/parse");
    println!("cargo:rerun-if-changed=tests/parsing");

    // Compile all parser tests into a single giant vector.
    let mut code = "vec![".to_string();

    for entry in read_dir("tests/parsing")? {
        let path = entry?.path();
        if path.extension() != Some(OsStr::new("rs")) {
            continue;
        }

        let name = path
            .file_stem().ok_or("expected file stem")?
            .to_string_lossy();

        // Make sure this also reruns if the contents of a file in parsing
        // change. This is not ensured by rerunning only on the folder.
        println!("cargo:rerun-if-changed=tests/parsing/{}.rs", name);

        code.push_str(&format!("(\"{}\", tokens!{{", name));

        // Replace the `=>` arrows with a double arrow indicating the line
        // number in the middle, such that the tester can tell which line number
        // a test originated from.
        let file = read_to_string(&path)?;
        for (index, line) in file.lines().enumerate() {
            let line = line.replace("=>", &format!("=>({})=>", index + 1));
            code.push_str(&line);
            code.push('\n');
        }

        code.push_str("}),");
    }

    code.push(']');

    fs::write("tests/cache/parse", code)?;

    Ok(())
}