diff --git a/src/jbuild_load.ml b/src/jbuild_load.ml index 67dd1070..6e33c073 100644 --- a/src/jbuild_load.ml +++ b/src/jbuild_load.ml @@ -171,9 +171,48 @@ type conf = ; scopes : Scope_info.t list } +module Sexp_io = struct + open Sexp + + let ocaml_script_prefix = "(* -*- tuareg -*- *)" + let ocaml_script_prefix_len = String.length ocaml_script_prefix + + type sexps_or_ocaml_script = + | Sexps of Ast.t list + | Ocaml_script + + 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 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 + let rec loop0 stack i = + match input ic buf i (buf_len - i) with + | 0 -> + let stack = Parser.feed_subbytes state buf ~pos:0 ~len:i stack in + Sexps (Parser.feed_eoi state stack) + | n -> + let i = i + n in + if i < ocaml_script_prefix_len then + loop0 stack i + else if Bytes.sub_string buf 0 ocaml_script_prefix_len + [@warning "-6"] + = ocaml_script_prefix then + Ocaml_script + else + let stack = Parser.feed_subbytes state buf ~pos:0 ~len:i stack in + Sexps (loop stack) + in + loop0 Parser.Stack.empty 0) +end + let load ~dir ~scope ~ignore_promoted_rules = let file = Path.relative dir "jbuild" in - match Sexp.load_many_or_ocaml_script (Path.to_string file) with + match Sexp_io.load_many_or_ocaml_script (Path.to_string file) with | Sexps sexps -> Jbuilds.Literal (dir, scope, Stanzas.parse scope sexps ~file diff --git a/src/stdune/sexp.ml b/src/stdune/sexp.ml index f30bbe52..7ace78e5 100644 --- a/src/stdune/sexp.ml +++ b/src/stdune/sexp.ml @@ -21,41 +21,6 @@ let load_many_as_one ~fname = let loc = { (Ast.loc x) with stop = (Ast.loc last).stop } in Ast.List (loc, x :: l) -let ocaml_script_prefix = "(* -*- tuareg -*- *)" -let ocaml_script_prefix_len = String.length ocaml_script_prefix - -type sexps_or_ocaml_script = - | Sexps of Ast.t list - | Ocaml_script - -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 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 - let rec loop0 stack i = - match input ic buf i (buf_len - i) with - | 0 -> - let stack = Parser.feed_subbytes state buf ~pos:0 ~len:i stack in - Sexps (Parser.feed_eoi state stack) - | n -> - let i = i + n in - if i < ocaml_script_prefix_len then - loop0 stack i - else if Bytes.sub_string buf 0 ocaml_script_prefix_len - [@warning "-6"] - = ocaml_script_prefix then - Ocaml_script - else - let stack = Parser.feed_subbytes state buf ~pos:0 ~len:i stack in - Sexps (loop stack) - in - loop0 Parser.Stack.empty 0) - module type Combinators = sig type 'a t val unit : unit t diff --git a/src/stdune/sexp.mli b/src/stdune/sexp.mli index b4cb16fb..d1a2d49c 100644 --- a/src/stdune/sexp.mli +++ b/src/stdune/sexp.mli @@ -3,12 +3,6 @@ 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 -type sexps_or_ocaml_script = - | Sexps of Ast.t list - | Ocaml_script - -val load_many_or_ocaml_script : string -> sexps_or_ocaml_script - module type Combinators = sig type 'a t val unit : unit t @@ -161,3 +155,7 @@ module Of_sexp : sig val enum : (string * 'a) list -> 'a t end + +(**/**) +(* used in jbuild_load *) +val buf_len : int