Add switch_file_kind

This expresses all the cases where we dispatch based on the syntax
version.

Signed-off-by: Etienne Millon <me@emillon.org>
This commit is contained in:
Etienne Millon 2018-07-31 12:28:19 +02:00 committed by Etienne Millon
parent d9e7f54277
commit 6298d283ba
5 changed files with 48 additions and 39 deletions

View File

@ -225,11 +225,9 @@ module Pps_and_flags = struct
end
let t =
Syntax.get_exn Stanza.syntax >>= fun ver ->
if ver < (1, 0) then
Jbuild_syntax.t
else
Dune_syntax.t
switch_file_kind
~jbuild:Jbuild_syntax.t
~dune:Dune_syntax.t
end
module Bindings = struct
@ -255,10 +253,11 @@ module Bindings = struct
let singleton x = [Unnamed x]
let t elem =
Stanza.file_kind () >>= function
| Jbuild -> list (elem >>| fun x -> Unnamed x)
| Dune -> parens_removed_in_dune (
let jbuild elem =
list (elem >>| fun x -> Unnamed x)
let dune elem =
parens_removed_in_dune (
let%map l =
repeat
(if_paren_colon_form
@ -283,6 +282,11 @@ module Bindings = struct
in
loop String.Set.empty [] l)
let t elem =
switch_file_kind
~jbuild:(jbuild elem)
~dune:(dune elem)
let sexp_of_t sexp_of_a bindings =
Sexp.List (
List.map bindings ~f:(function
@ -1397,11 +1401,9 @@ module Rule = struct
"S-expression of the form (<atom> ...) expected"
let t =
Syntax.get_exn Stanza.syntax >>= fun ver ->
if ver < (1, 0) then
jbuild_syntax
else
dune_syntax
switch_file_kind
~jbuild:jbuild_syntax
~dune:dune_syntax
type lex_or_yacc =
{ modules : string list
@ -1443,11 +1445,9 @@ module Rule = struct
}))
let ocamllex =
Syntax.get_exn Stanza.syntax >>= fun ver ->
if ver < (1, 0) then
ocamllex_jbuild
else
ocamllex_dune
switch_file_kind
~jbuild:ocamllex_jbuild
~dune:ocamllex_dune
let ocamlyacc = ocamllex

View File

@ -22,8 +22,7 @@ end
let file_kind () =
let open Sexp.Of_sexp in
Syntax.get_exn syntax >>| fun ver ->
if ver < (1, 0) then File_kind.Jbuild else Dune
Syntax.get_exn syntax >>| File_kind.of_syntax
module Of_sexp = struct
include Sexp.Of_sexp
@ -45,11 +44,15 @@ module Of_sexp = struct
}
| _ -> None)
let switch_file_kind ~jbuild ~dune =
file_kind () >>= function
| Jbuild -> jbuild
| Dune -> dune
let parens_removed_in_dune_generic ~is_record t =
Syntax.get_exn syntax >>= fun ver ->
if ver < (1, 0) then
enter t
else
switch_file_kind
~jbuild:(enter t)
~dune:(
try_
t
(function
@ -70,6 +73,7 @@ module Of_sexp = struct
(function
| Parens_no_longer_necessary _ as exn -> raise exn
| _ -> raise exn))
)
let record parse =
parens_removed_in_dune_generic (fields parse) ~is_record:true

View File

@ -56,4 +56,12 @@ module Of_sexp : sig
displays a nice error messages when parentheses are used in dune
files. *)
val parens_removed_in_dune : 'a t -> 'a t
(** Use a different parser depending on the syntax in the current file.
If the syntax version is strictly less than `(1, 0)`, use `jbuild`.
Otherwise use `dune`. *)
val switch_file_kind :
jbuild:('a, 'b) parser ->
dune:('a, 'b) parser ->
('a, 'b) parser
end

View File

@ -113,13 +113,10 @@ let t =
| Quoted_string (loc, s) -> literal ~quoted:true ~loc s
| List (loc, _) -> Sexp.Of_sexp.of_sexp_error loc "Unexpected list"
in
Syntax.get_exn Stanza.syntax >>= fun syntax_version ->
let template =
match syntax_version with
| (0, _) -> jbuild
| (_, _) -> dune
let template_parser = Stanza.Of_sexp.switch_file_kind ~jbuild ~dune in
let%map syntax_version = Syntax.get_exn Stanza.syntax
and template = template_parser
in
template >>| fun template ->
{template; syntax_version}
let loc t = t.template.loc

View File

@ -117,14 +117,14 @@ module Context = struct
]
let t ~profile ~x =
Syntax.get_exn syntax >>= function
| (0, _) ->
(* jbuild-workspace files *)
(peek_exn >>= function
| List (_, List _ :: _) ->
Sexp.Of_sexp.record (Opam.t ~profile ~x) >>| fun x -> Opam x
| _ -> t ~profile ~x)
| _ -> t ~profile ~x
switch_file_kind
~jbuild:
(* jbuild-workspace files *)
(peek_exn >>= function
| List (_, List _ :: _) ->
Sexp.Of_sexp.record (Opam.t ~profile ~x) >>| fun x -> Opam x
| _ -> t ~profile ~x)
~dune:(t ~profile ~x)
let name = function
| Default _ -> "default"