diff --git a/src/action.ml b/src/action.ml index 85ac8f53..6dc2f76f 100644 --- a/src/action.ml +++ b/src/action.ml @@ -660,7 +660,7 @@ module Promotion = struct let load_db () = if Sys.file_exists db_file then - Sexp.load ~fname:db_file ~mode:Many + Io.Sexp.load ~fname:db_file ~mode:Many |> List.map ~f:File.t else [] diff --git a/src/build_system.ml b/src/build_system.ml index 6f9e39c5..57000e14 100644 --- a/src/build_system.ml +++ b/src/build_system.ml @@ -20,7 +20,7 @@ module Promoted_to_delete = struct let load () = if Sys.file_exists fn then - Sexp.load ~fname:fn ~mode:Many + Io.Sexp.load ~fname:fn ~mode:Many |> List.map ~f:Path.t else [] @@ -1126,7 +1126,7 @@ module Trace = struct let load () = let trace = Hashtbl.create 1024 in if Sys.file_exists file then begin - let sexp = Sexp.load ~fname:file ~mode:Single in + let sexp = Io.Sexp.load ~fname:file ~mode:Single in let bindings = let open Sexp.Of_sexp in list (pair Path.t (fun s -> Digest.from_hex (string s))) sexp @@ -1207,7 +1207,7 @@ let update_universe t = let fname = Path.to_string universe_file in let n = if Sys.file_exists fname then - Sexp.Of_sexp.int (Sexp.load ~mode:Single ~fname) + 1 + Sexp.Of_sexp.int (Io.Sexp.load ~mode:Single ~fname) + 1 else 0 in diff --git a/src/config.ml b/src/config.ml index f877248e..9e5dc406 100644 --- a/src/config.ml +++ b/src/config.ml @@ -81,7 +81,7 @@ let t = let user_config_file = Filename.concat Xdg.config_dir "dune/config" let load_config_file ~fname = - t (Sexp.load_many_as_one ~fname) + t (Io.Sexp.load_many_as_one ~fname) let load_user_config_file () = if Sys.file_exists user_config_file then diff --git a/src/installed_dune_file.ml b/src/installed_dune_file.ml index 04480905..8dde3ae8 100644 --- a/src/installed_dune_file.ml +++ b/src/installed_dune_file.ml @@ -39,7 +39,7 @@ let of_sexp = (fun () l -> parse_sub_systems l) ] -let load ~fname = of_sexp (Sexp.load ~mode:Single ~fname) +let load ~fname = of_sexp (Io.Sexp.load ~mode:Single ~fname) let gen confs = let sexps = diff --git a/src/jbuild.ml b/src/jbuild.ml index d196bef7..f41befb1 100644 --- a/src/jbuild.ml +++ b/src/jbuild.ml @@ -1244,7 +1244,7 @@ module Stanzas = struct (Path.to_string_maybe_quoted file); if List.exists include_stack ~f:(fun (_, f) -> f = file) then raise (Include_loop (file, include_stack)); - let sexps = Sexp.load ~fname:(Path.to_string file) ~mode:Many in + let sexps = Io.Sexp.load ~fname:(Path.to_string file) ~mode:Many in parse pkgs sexps ~default_version:Jbuild_version.V1 ~file ~include_stack) ; cstr "documentation" (Documentation.v1 pkgs @> nil) (fun d -> [Documentation d]) diff --git a/src/jbuild_load.ml b/src/jbuild_load.ml index 6e33c073..018f776f 100644 --- a/src/jbuild_load.ml +++ b/src/jbuild_load.ml @@ -157,7 +157,7 @@ end die "@{Error:@} %s failed to produce a valid jbuild file.\n\ Did you forgot to call [Jbuild_plugin.V*.send]?" (Path.to_string file); - let sexps = Sexp.load ~fname:(Path.to_string generated_jbuild) ~mode:Many in + let sexps = Io.Sexp.load ~fname:(Path.to_string generated_jbuild) ~mode:Many in Fiber.return (dir, scope, Stanzas.parse scope sexps ~file:generated_jbuild |> filter_stanzas ~ignore_promoted_rules)) >>| fun dynamic -> @@ -184,14 +184,14 @@ module Sexp_io = struct let load_many_or_ocaml_script fname = Io.with_file_in fname ~f:(fun ic -> let state = Parser.create ~fname ~mode:Many in - let buf = Bytes.create buf_len in + let buf = Bytes.create Io.buf_len in let rec loop stack = - match input ic buf 0 buf_len with + match input ic buf 0 Io.buf_len with | 0 -> Parser.feed_eoi state stack | n -> loop (Parser.feed_subbytes state buf ~pos:0 ~len:n stack) in let rec loop0 stack i = - match input ic buf i (buf_len - i) with + match input ic buf i (Io.buf_len - i) with | 0 -> let stack = Parser.feed_subbytes state buf ~pos:0 ~len:i stack in Sexps (Parser.feed_eoi state stack) diff --git a/src/stdune/io.ml b/src/stdune/io.ml index fc39d707..74ba8cb9 100644 --- a/src/stdune/io.ml +++ b/src/stdune/io.ml @@ -76,3 +76,28 @@ let copy_file ~src ~dst = (* TODO: diml: improve this *) let compare_files fn1 fn2 = String.compare (read_file fn1) (read_file fn2) + +let buf_len = 65_536 + +module Sexp = struct + open Sexp + + let load ~fname ~mode = + with_file_in fname ~f:(fun ic -> + let state = Parser.create ~fname ~mode in + let buf = Bytes.create buf_len in + let rec loop stack = + match input ic buf 0 buf_len with + | 0 -> Parser.feed_eoi state stack + | n -> loop (Parser.feed_subbytes state buf ~pos:0 ~len:n stack) + in + loop Parser.Stack.empty) + + let load_many_as_one ~fname = + match load ~fname ~mode:Many with + | [] -> Ast.List (Loc.in_file fname, []) + | x :: l -> + let last = Option.value (List.last l) ~default:x in + let loc = { (Ast.loc x) with stop = (Ast.loc last).stop } in + Ast.List (loc, x :: l) +end diff --git a/src/stdune/io.mli b/src/stdune/io.mli index 9b621f36..cc1a604a 100644 --- a/src/stdune/io.mli +++ b/src/stdune/io.mli @@ -25,3 +25,12 @@ val copy_channels : in_channel -> out_channel -> unit val copy_file : src:string -> dst:string -> unit val read_all : in_channel -> string + +module Sexp : sig + val load : fname:string -> mode:'a Sexp.Parser.Mode.t -> 'a + val load_many_as_one : fname:string -> Sexp.Ast.t +end + +(**/**) +(* used in jbuild_load *) +val buf_len : int diff --git a/src/stdune/sexp.ml b/src/stdune/sexp.ml index 7ace78e5..9e6272d9 100644 --- a/src/stdune/sexp.ml +++ b/src/stdune/sexp.ml @@ -1,26 +1,5 @@ include Usexp -let buf_len = 65_536 - -let load ~fname ~mode = - Io.with_file_in fname ~f:(fun ic -> - let state = Parser.create ~fname ~mode in - let buf = Bytes.create buf_len in - let rec loop stack = - match input ic buf 0 buf_len with - | 0 -> Parser.feed_eoi state stack - | n -> loop (Parser.feed_subbytes state buf ~pos:0 ~len:n stack) - in - loop Parser.Stack.empty) - -let load_many_as_one ~fname = - match load ~fname ~mode:Many with - | [] -> Ast.List (Loc.in_file fname, []) - | x :: l -> - let last = Option.value (List.last l) ~default:x in - let loc = { (Ast.loc x) with stop = (Ast.loc last).stop } in - Ast.List (loc, x :: l) - module type Combinators = sig type 'a t val unit : unit t diff --git a/src/stdune/sexp.mli b/src/stdune/sexp.mli index d1a2d49c..f289664b 100644 --- a/src/stdune/sexp.mli +++ b/src/stdune/sexp.mli @@ -1,8 +1,5 @@ include module type of struct include Usexp end with module Loc := Usexp.Loc -val load : fname:string -> mode:'a Parser.Mode.t -> 'a -val load_many_as_one : fname:string -> Ast.t - module type Combinators = sig type 'a t val unit : unit t @@ -155,7 +152,3 @@ module Of_sexp : sig val enum : (string * 'a) list -> 'a t end - -(**/**) -(* used in jbuild_load *) -val buf_len : int diff --git a/src/utils.ml b/src/utils.ml index 8189cb65..257a27f9 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -204,7 +204,7 @@ module Cached_digest = struct let load () = if Sys.file_exists db_file then begin - let sexp = Sexp.load ~fname:db_file ~mode:Single in + let sexp = Io.Sexp.load ~fname:db_file ~mode:Single in let bindings = let open Sexp.Of_sexp in list diff --git a/src/vfile_kind.ml b/src/vfile_kind.ml index 3bc42f10..0c88e584 100644 --- a/src/vfile_kind.ml +++ b/src/vfile_kind.ml @@ -55,7 +55,7 @@ struct let to_string path x = To_sexp.t path x |> Sexp.to_string let load path = - Of_sexp.t path (Sexp.load ~fname:(Path.to_string path) ~mode:Single) + Of_sexp.t path (Io.Sexp.load ~fname:(Path.to_string path) ~mode:Single) end diff --git a/src/workspace.ml b/src/workspace.ml index 70448a68..338ea240 100644 --- a/src/workspace.ml +++ b/src/workspace.ml @@ -135,4 +135,4 @@ let t ?x sexps = ; contexts = List.rev contexts } -let load ?x fname = t ?x (Sexp.load ~fname ~mode:Many) +let load ?x fname = t ?x (Io.Sexp.load ~fname ~mode:Many)