From 0051aeca291e028a2e9dceefcc6a766719c7297c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 2 Jul 2018 14:42:43 +0700 Subject: [PATCH] Do not read .dune files twice Reuse the same lexer by pushing back some tokens manually Signed-off-by: Rudi Grinberg --- src/installed_dune_file.ml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/installed_dune_file.ml b/src/installed_dune_file.ml index 8e5ee1c9..089b108e 100644 --- a/src/installed_dune_file.ml +++ b/src/installed_dune_file.ml @@ -42,7 +42,7 @@ let of_sexp = let load fname = Io.with_lexbuf_from_file fname ~f:(fun lexbuf -> - let (version_loc, version) = + let (tokens, version_loc, version) = let rec loop = function | [_; _; _] as a -> List.rev a | acc -> @@ -53,7 +53,8 @@ let load fname = in let loc = Sexp.Loc.of_lexbuf lexbuf in match loop [] with - | [Lparen; Atom (A "dune"); Atom s] -> (loc, Sexp.Atom.to_string s) + | [Lparen; Atom (A "dune"); Atom s] as tokens -> + (tokens, loc, Sexp.Atom.to_string s) | _ -> Loc.fail loc "Unable to read (dune x.y ..) line file" in let (lexer, syntax) = @@ -62,9 +63,17 @@ let load fname = | "2" -> (Sexp.Lexer.token, (1, 0)) | _ -> Loc.fail version_loc "unknown version %S" version in + (* push back the tokens that we already read *) + let lexer = + let pending_tokens = ref tokens in + fun lb -> + match !pending_tokens with + | [] -> lexer lb + | x :: xs -> pending_tokens := xs; x + in Sexp.Of_sexp.parse of_sexp (Univ_map.singleton (Syntax.key Stanza.syntax) syntax) - (Io.Sexp.load ~lexer ~mode:Single fname)) + (Sexp.Parser.parse ~lexer ~mode:Single lexbuf)) let gen ~(dune_version : Syntax.Version.t) confs = let sexps =