Skip to content

Commit

Permalink
Try including the generated grammar in-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed Jan 22, 2025
1 parent e7b8d53 commit 5bb1aeb
Show file tree
Hide file tree
Showing 2 changed files with 442,206 additions and 4 deletions.
28 changes: 24 additions & 4 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
use std::path::{Path, PathBuf};

fn main() {
lalrpop::Configuration::new()
.use_cargo_dir_conventions()
.process_file("src/parser/grammar.lalrpop")
.unwrap();
// Lalrpop is slow to generate grammars, so to make things easier for
// downstream users we check in a generated grammar.rs file. This is a bit
// awkward when we want to work on the grammar, though, so the build script
// will autogenerate a new grammar (in $OUT_DIR, so as not to mess up the
// source tree) whenever the generated grammar is missing from the source
// tree. Thus, when working on the grammar you just delete
// `src/parser/generated/grammar.rs` and the build script will rebuild it
// for you whenever necessary. When you're done working on the grammar,
// copy the generated file back into `src/parser/generated/grammar.rs`.
let checked_in_grammar_path = Path::new("src/parser/generated/grammar.rs");
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").expect("missing OUT_DIR variable"));
let out_parser_dir = out_dir.join("parser");
std::fs::create_dir_all(&out_parser_dir).expect("failed to create $OUT_DIR/parser");

// If we fail to link the generated grammar into $OUT_DIR, it probably didn't exist.
// We'll just pretend that's what happened, and try to generate a fresh grammar.
if std::fs::hard_link(checked_in_grammar_path, out_parser_dir.join("grammar.rs")).is_err() {
lalrpop::Configuration::new()
.use_cargo_dir_conventions()
.process_file("src/parser/grammar.lalrpop")
.unwrap();
}

#[cfg(feature = "nix-experimental")]
{
Expand Down
Loading

0 comments on commit 5bb1aeb

Please sign in to comment.