From 58997d9df7a65e180cbf6005f1736265ee38d041 Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Wed, 20 Jun 2018 12:16:51 +0100 Subject: [PATCH] Present menhir as an extension Signed-off-by: Jeremie Dimino --- CHANGES.md | 3 ++ src/dune_project.ml | 2 +- src/gen_rules.ml | 4 +- src/jbuild.ml | 43 ++++++++++++++++--- src/jbuild.mli | 3 +- .../test-cases/dune-project-edition/run.t | 9 ++++ .../test-cases/menhir/dune-project | 1 + 7 files changed, 56 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b30853d4..809467a0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -81,6 +81,9 @@ next - Rename `files_recursively_in` to `source_tree` to make it clearer it doesn't include generated files (#899, fix #843, @diml) +- Present the `menhir` stanza as an extension with its own version + (#..., @diml) + 1.0+beta20 (10/04/2018) ----------------------- diff --git a/src/dune_project.ml b/src/dune_project.ml index 4695c231..d4900e46 100644 --- a/src/dune_project.ml +++ b/src/dune_project.ml @@ -259,7 +259,7 @@ module Extension = struct let parse_args p = let open Sexp.Of_sexp in let dune_project_edited = ref false in - parse p Univ_map.empty (List (Loc.none, [])) + parse (enter p) Univ_map.empty (List (Loc.of_pos __POS__, [])) |> List.map ~f:(fun (name, p) -> (name, return () >>= fun () -> diff --git a/src/gen_rules.ml b/src/gen_rules.ml index ea02f97b..0502cb4e 100644 --- a/src/gen_rules.ml +++ b/src/gen_rules.ml @@ -252,7 +252,7 @@ module Gen(P : Install_rules.Params) = struct let generated_files = List.concat_map stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with - | Menhir menhir -> + | Menhir.T menhir -> Menhir_rules.targets menhir | Rule rule -> List.map (user_rule rule ~dir ~scope) ~f:Path.basename @@ -998,7 +998,7 @@ module Gen(P : Install_rules.Params) = struct | _ -> None)); List.iter stanzas ~f:(fun stanza -> match (stanza : Stanza.t) with - | Menhir m -> + | Menhir.T m -> let cctx = match List.find_map (Menhir_rules.module_names m) diff --git a/src/jbuild.ml b/src/jbuild.ml index 4aa130d2..713755d9 100644 --- a/src/jbuild.ml +++ b/src/jbuild.ml @@ -1155,7 +1155,34 @@ module Menhir = struct ; loc : Loc.t } + let syntax = + Syntax.create + ~name:"menhir" + ~desc:"the menhir extension" + [ (1, 0) ] + let t = + record + (field_o "merge_into" string >>= fun merge_into -> + field_oslu "flags" >>= fun flags -> + field "modules" (list string) >>= fun modules -> + Rule.Mode.field >>= fun mode -> + return + { merge_into + ; flags + ; modules + ; mode + ; loc = Loc.none + }) + + type Stanza.t += T of t + + let () = + Dune_project.Extension.register syntax + (return [ "menhir", t >>| fun x -> [T x] ]) + + (* Syntax for jbuild files *) + let jbuild_syntax = record (field_o "merge_into" string >>= fun merge_into -> field_oslu "flags" >>= fun flags -> @@ -1275,7 +1302,6 @@ type Stanza.t += | Install of Install_conf.t | Alias of Alias_conf.t | Copy_files of Copy_files.t - | Menhir of Menhir.t | Documentation of Documentation.t | Env of Env.t @@ -1313,10 +1339,6 @@ module Stanzas = struct (loc >>= fun loc -> Rule.ocamlyacc >>| fun x -> rules (Rule.ocamlyacc_to_rule loc x)) - ; "menhir", - (loc >>= fun loc -> - Menhir.t >>| fun x -> - [Menhir { x with loc }]) ; "install", (Install_conf.t >>| fun x -> [Install x]) @@ -1347,6 +1369,17 @@ module Stanzas = struct ] let jbuild_parser = + (* The menhir stanza was part of the vanilla jbuild + syntax. Starting from Dune 1.0, it is presented as an + extension with its own version. *) + let stanzas = + stanzas @ + [ "menhir", + (loc >>= fun loc -> + Menhir.jbuild_syntax >>| fun x -> + [Menhir.T { x with loc }]) + ] + in Syntax.set syntax (0, 0) (sum stanzas) let () = diff --git a/src/jbuild.mli b/src/jbuild.mli index eacd8654..4bceafd4 100644 --- a/src/jbuild.mli +++ b/src/jbuild.mli @@ -303,6 +303,8 @@ module Menhir : sig ; mode : Rule.Mode.t ; loc : Loc.t } + + type Stanza.t += T of t end module Alias_conf : sig @@ -353,7 +355,6 @@ type Stanza.t += | Install of Install_conf.t | Alias of Alias_conf.t | Copy_files of Copy_files.t - | Menhir of Menhir.t | Documentation of Documentation.t | Env of Env.t diff --git a/test/blackbox-tests/test-cases/dune-project-edition/run.t b/test/blackbox-tests/test-cases/dune-project-edition/run.t index b40bafe0..abbe2aa3 100644 --- a/test/blackbox-tests/test-cases/dune-project-edition/run.t +++ b/test/blackbox-tests/test-cases/dune-project-edition/run.t @@ -7,3 +7,12 @@ Info: creating file dune-project with this contents: (lang dune 1.0) $ cat dune-project (lang dune 1.0) + +Test that using menhir automatically update the dune-project file + + $ echo '(library ((name x))) (menhir ((modules (x))))' >> src/dune + $ dune build + Info: appending this line to dune-project: (using menhir 1.0) + $ cat dune-project + (lang dune 1.0) + (using menhir 1.0) diff --git a/test/blackbox-tests/test-cases/menhir/dune-project b/test/blackbox-tests/test-cases/menhir/dune-project index de4fc209..0b09661e 100644 --- a/test/blackbox-tests/test-cases/menhir/dune-project +++ b/test/blackbox-tests/test-cases/menhir/dune-project @@ -1 +1,2 @@ (lang dune 1.0) +(using menhir 1.0)