-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
42 lines (32 loc) · 1.33 KB
/
build.rs
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
#![deny(rust_2018_idioms)]
use std::{env, fs, path::PathBuf};
use walkdir::WalkDir;
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// FIXME(eddyb) streamline this process in `gll`.
// Find all the `.lyg` grammar fragments in `grammar/`.
let fragments = WalkDir::new("grammar")
.contents_first(true)
.into_iter()
.map(|entry| entry.unwrap())
.filter(|entry| entry.path().extension().map_or(false, |ext| ext == "lyg"));
// Start with the builtin rules for proc-macro grammars.
let mut cx = gll::grammer::proc_macro::Context::new();
let cx = &mut cx;
let mut grammar = gll::grammer::proc_macro::builtin(cx);
// Add in each grammar fragment to the grammar.
for fragment in fragments {
let path = fragment.into_path();
// Inform Cargo about our dependency on the fragment files.
println!("cargo:rerun-if-changed={}", path.display());
let src = fs::read_to_string(&path).unwrap();
let fragment = gll::parse_grammar(cx, src.parse().unwrap()).unwrap();
grammar.extend(fragment);
}
// Generate a Rust parser from the combined grammar and write it out.
fs::write(
&out_dir.join("parse.rs"),
gll::generate::rust::generate(cx, &grammar).to_rustfmt_or_pretty_string(),
)
.unwrap();
}