diff --git a/lib/fe/dune b/lib/fe/dune index 9771b1ac..ad596b62 100644 --- a/lib/fe/dune +++ b/lib/fe/dune @@ -45,3 +45,26 @@ (flags (:standard -open BasilIR)) (libraries bincaml.basilir)) + +(rule + (target has-bnfc-tree-sitter) + (enabled_if + (not %{bin-available:bnfc})) + (action + (with-stdout-to + %{target} + (echo "false")))) + +(rule + (target has-bnfc-tree-sitter) + (enabled_if %{bin-available:bnfc}) + (action + (with-stdout-to + %{target} + (pipe-stdout + (run bnfc --help) + (bash + "if grep -q -- --tree-sitter-word; then printf true; else printf false; fi"))))) + +; if `bnfc` exits with an error, we report that error and fail. +; "false" is only written if bnfc succeeds but tree-sitter was not found. diff --git a/tree-sitter/.gitignore b/tree-sitter/.gitignore index b73cc51b..869c50dd 100644 --- a/tree-sitter/.gitignore +++ b/tree-sitter/.gitignore @@ -1,3 +1,5 @@ +grammar.js.new + # Rust artifacts target/ diff --git a/tree-sitter/dune b/tree-sitter/dune index ca13dd0c..e7ee71c3 100644 --- a/tree-sitter/dune +++ b/tree-sitter/dune @@ -10,10 +10,14 @@ ; check that grammar.js is up to date. ; requires `bnfc-treesitter` from pac-nix. +; bnfc tree-sitter backend is only run if the fixed tree-sitter fork is +; detected in the output of bnfc --help. this lets us use upstream +; bnfc for the main ocaml parser. + (rule (alias runtest) (deps %{project_root}/lib/fe/BasilIR.cf) - (enabled_if %{bin-available:bnfc}) + (enabled_if %{read:../lib/fe/has-bnfc-tree-sitter}) (action (progn (copy %{deps} basilir.cf) @@ -25,3 +29,13 @@ basilir.cf --makefile) (diff? grammar.js tree-sitter-basilir/grammar.js)))) + +(rule + (alias runtest) + (deps (universe)) + (enabled_if + (not %{read:../lib/fe/has-bnfc-tree-sitter})) + (action + (echo + "tree-sitter/dune: bnfc tree-sitter generation skipped," + "because the installed bnfc doesn't support it.\n")))