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

View File

@ -22,8 +22,7 @@ end
let file_kind () = let file_kind () =
let open Sexp.Of_sexp in let open Sexp.Of_sexp in
Syntax.get_exn syntax >>| fun ver -> Syntax.get_exn syntax >>| File_kind.of_syntax
if ver < (1, 0) then File_kind.Jbuild else Dune
module Of_sexp = struct module Of_sexp = struct
include Sexp.Of_sexp include Sexp.Of_sexp
@ -45,11 +44,15 @@ module Of_sexp = struct
} }
| _ -> None) | _ -> None)
let switch_file_kind ~jbuild ~dune =
file_kind () >>= function
| Jbuild -> jbuild
| Dune -> dune
let parens_removed_in_dune_generic ~is_record t = let parens_removed_in_dune_generic ~is_record t =
Syntax.get_exn syntax >>= fun ver -> switch_file_kind
if ver < (1, 0) then ~jbuild:(enter t)
enter t ~dune:(
else
try_ try_
t t
(function (function
@ -70,6 +73,7 @@ module Of_sexp = struct
(function (function
| Parens_no_longer_necessary _ as exn -> raise exn | Parens_no_longer_necessary _ as exn -> raise exn
| _ -> raise exn)) | _ -> raise exn))
)
let record parse = let record parse =
parens_removed_in_dune_generic (fields parse) ~is_record:true 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 displays a nice error messages when parentheses are used in dune
files. *) files. *)
val parens_removed_in_dune : 'a t -> 'a t 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 end

View File

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

View File

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