From c906801c69654a247d690d5aee0c6ed9177666ee Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 1 Jul 2018 15:20:40 +0700 Subject: [PATCH] Parse dune files in a versioned way. We read the first 3 tokens of a dune file and use it recognize if the file was generated using jbuilder or dune Signed-off-by: Rudi Grinberg --- src/installed_dune_file.ml | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/installed_dune_file.ml b/src/installed_dune_file.ml index ab7535cc..3fc1d3c2 100644 --- a/src/installed_dune_file.ml +++ b/src/installed_dune_file.ml @@ -45,7 +45,36 @@ let of_sexp = ] let load fname = - Sexp.Of_sexp.parse of_sexp Univ_map.empty (Io.Sexp.load ~mode:Single fname) + Io.with_lexbuf_from_file fname ~f:(fun lexbuf -> + let (version_loc, version) = + let bad_dune_file = "Unable to read (dune x.y ..) line file" in + let rec loop = function + | [_; _; _] as a -> List.rev a + | acc -> + begin match (Sexp.Lexer.token lexbuf : Sexp.Lexer.Token.t) with + | Eof -> + Loc.fail (Loc.in_file (Path.to_string fname)) "%s" bad_dune_file + | t -> loop (t :: acc) + end + in + match loop [] with + | [Lparen; Atom (A "dune"); Atom s] -> + (Sexp.Loc.of_lexbuf lexbuf, Sexp.Atom.to_string s) + | _ -> + Loc.fail + { start = Lexing.lexeme_start_p lexbuf + ; stop = Lexing.lexeme_end_p lexbuf + } "%s" bad_dune_file + in + match version with + | "1" -> + Sexp.Of_sexp.parse of_sexp Univ_map.empty + (Io.Sexp.load ~lexer:Sexp.Lexer.jbuild_token ~mode:Single fname) + | "2" -> + Sexp.Of_sexp.parse of_sexp Univ_map.empty + (Io.Sexp.load ~lexer:Sexp.Lexer.token ~mode:Single fname) + | _ -> + Loc.fail version_loc "unknown version %S" version) let gen confs = let sexps =