Move sexp's io functions to Io.Sexp

to avoid circular dependencies when adding Path.t to Io
This commit is contained in:
Rudi Grinberg 2018-04-24 23:21:42 +07:00
parent 0d76abca91
commit 79e434c658
13 changed files with 48 additions and 42 deletions

View File

@ -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
[]

View File

@ -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

View File

@ -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

View File

@ -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 =

View File

@ -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])

View File

@ -157,7 +157,7 @@ end
die "@{<error>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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)