Move ocaml-syntax related stuff to jbuild_load

This stuff is too specific to parsing jbuild files to be in stdune
This commit is contained in:
Rudi Grinberg 2018-04-24 23:17:53 +07:00
parent 239ff0054e
commit 0d76abca91
3 changed files with 44 additions and 42 deletions

View File

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

View File

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

View File

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