Change the signature of Of_peek.peek

It is always used the same way: `peek raw`, and sometimes preceded by
a `eos`. Remove the old `peek` and add:

- `peek_exn` which is the same the old `peek raw`
- `peek` which returns `None` if the end of sequence is reached

Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
This commit is contained in:
Jeremie Dimino 2018-06-27 17:13:47 +01:00 committed by Jérémie Dimino
parent 2982567639
commit f9f5503e81
6 changed files with 35 additions and 28 deletions

View File

@ -352,7 +352,7 @@ module Unexpanded = struct
let t = let t =
let open Sexp.Of_sexp in let open Sexp.Of_sexp in
peek raw >>= function peek_exn >>= function
| Template _ | Atom _ | Quoted_string _ as sexp -> | Template _ | Atom _ | Quoted_string _ as sexp ->
of_sexp_errorf (Sexp.Ast.loc sexp) of_sexp_errorf (Sexp.Ast.loc sexp)
"if you meant for this to be executed with bash, write (bash \"...\") instead" "if you meant for this to be executed with bash, write (bash \"...\") instead"
@ -613,7 +613,7 @@ module Promotion = struct
let t = let t =
let open Sexp.Of_sexp in let open Sexp.Of_sexp in
peek raw >>= function peek_exn >>= function
| List (_, [_; Atom (_, A "as"); _]) -> | List (_, [_; Atom (_, A "as"); _]) ->
enter enter
(Path.t >>= fun src -> (Path.t >>= fun src ->

View File

@ -190,7 +190,7 @@ module Pps_and_flags = struct
Left (loc, Pp.of_string s) Left (loc, Pp.of_string s)
let item = let item =
peek raw >>= function peek_exn >>= function
| Template { loc; _ } -> | Template { loc; _ } ->
no_templates loc "in the preprocessors field" no_templates loc "in the preprocessors field"
| Atom _ | Quoted_string _ -> plain_string of_string | Atom _ | Quoted_string _ -> plain_string of_string
@ -261,7 +261,7 @@ module Dep_conf = struct
sw >>| fun x -> Source_tree x) sw >>| fun x -> Source_tree x)
] ]
in in
peek raw >>= function peek_exn >>= function
| Template _ | Atom _ | Quoted_string _ -> | Template _ | Atom _ | Quoted_string _ ->
String_with_vars.t >>| fun x -> File x String_with_vars.t >>| fun x -> File x
| List _ -> t | List _ -> t
@ -316,7 +316,7 @@ module Per_module = struct
include Per_item.Make(Module.Name) include Per_item.Make(Module.Name)
let t ~default a = let t ~default a =
peek raw >>= function peek_exn >>= function
| List (loc, Atom (_, A "per_module") :: _) -> | List (loc, Atom (_, A "per_module") :: _) ->
sum [ "per_module", sum [ "per_module",
repeat repeat
@ -407,7 +407,7 @@ module Lib_dep = struct
enter ( enter (
loc >>= fun loc -> loc >>= fun loc ->
let rec loop required forbidden = let rec loop required forbidden =
peek raw >>= function peek_exn >>= function
| Atom (_, A "->") -> | Atom (_, A "->") ->
junk >>> file >>| fun file -> junk >>> file >>| fun file ->
let common = String.Set.inter required forbidden in let common = String.Set.inter required forbidden in
@ -435,7 +435,7 @@ module Lib_dep = struct
) )
let t = let t =
peek raw >>= function peek_exn >>= function
| Atom _ | Quoted_string _ -> | Atom _ | Quoted_string _ ->
plain_string (fun ~loc s -> Direct (loc, s)) plain_string (fun ~loc s -> Direct (loc, s))
| List (loc, Atom (_, A "select") :: _ :: Atom (_, A "from") :: _) -> | List (loc, Atom (_, A "select") :: _ :: Atom (_, A "from") :: _) ->
@ -806,7 +806,7 @@ module Install_conf = struct
} }
let file = let file =
peek raw >>= function peek_exn >>= function
| Atom (_, A src) -> junk >>| fun () -> { src; dst = None } | Atom (_, A src) -> junk >>| fun () -> { src; dst = None }
| List (_, [Atom (_, A src); Atom (_, A "as"); Atom (_, A dst)]) -> | List (_, [Atom (_, A src); Atom (_, A "as"); Atom (_, A dst)]) ->
junk >>> return { src; dst = Some dst } junk >>> return { src; dst = Some dst }
@ -881,7 +881,7 @@ module Executables = struct
Sexp.Of_sexp.enum simple_representations Sexp.Of_sexp.enum simple_representations
let t = let t =
peek raw >>= function peek_exn >>= function
| List _ -> | List _ ->
enter (Mode_conf.t >>= fun mode -> enter (Mode_conf.t >>= fun mode ->
Binary_kind.t >>= fun kind -> Binary_kind.t >>= fun kind ->
@ -1148,12 +1148,12 @@ module Rule = struct
} }
let jbuild_syntax = let jbuild_syntax =
peek raw >>= function peek_exn >>= function
| List (_, (Atom _ :: _)) -> short_form | List (_, (Atom _ :: _)) -> short_form
| _ -> record long_form | _ -> record long_form
let dune_syntax = let dune_syntax =
peek raw >>= function peek_exn >>= function
| List (_, Atom (loc, A s) :: _) -> begin | List (_, Atom (loc, A s) :: _) -> begin
match String.Map.find atom_table s with match String.Map.find atom_table s with
| None -> | None ->
@ -1181,7 +1181,7 @@ module Rule = struct
} }
let ocamllex_jbuild = let ocamllex_jbuild =
peek raw >>= function peek_exn >>= function
| List (_, List (_, _) :: _) -> | List (_, List (_, _) :: _) ->
record record
(field "modules" (list string) >>= fun modules -> (field "modules" (list string) >>= fun modules ->
@ -1194,20 +1194,18 @@ module Rule = struct
} }
let ocamllex_dune = let ocamllex_dune =
eos >>= function peek >>= function
| true -> | None ->
return return
{ modules = [] { modules = []
; mode = Standard ; mode = Standard
} }
| false -> | Some (List _) ->
peek raw >>= function
| List _ ->
fields fields
(field "modules" (list string) >>= fun modules -> (field "modules" (list string) >>= fun modules ->
Mode.field >>= fun mode -> Mode.field >>= fun mode ->
return { modules; mode }) return { modules; mode })
| _ -> | Some _ ->
repeat string >>| fun modules -> repeat string >>| fun modules ->
{ modules { modules
; mode = Standard ; mode = Standard
@ -1395,7 +1393,7 @@ module Env = struct
return { flags; ocamlc_flags; ocamlopt_flags } return { flags; ocamlc_flags; ocamlopt_flags }
let rule = let rule =
peek raw >>= function peek_exn >>= function
| List (_, Atom (_, A pat) :: _) -> | List (_, Atom (_, A pat) :: _) ->
enter ( enter (
junk >>= fun () -> junk >>= fun () ->

View File

@ -589,9 +589,8 @@ let of_string ?error_loc s =
let t = let t =
Sexp.Of_sexp.( Sexp.Of_sexp.(
peek raw >>= function peek_exn >>= function
| Template _ | Template _ | Atom _ | Quoted_string _ ->
| Atom _ | Quoted_string _ ->
(* necessary for old build dirs *) (* necessary for old build dirs *)
plain_string (fun ~loc:_ s -> of_string s) plain_string (fun ~loc:_ s -> of_string s)
| List _ -> | List _ ->

View File

@ -232,9 +232,16 @@ module Of_sexp = struct
| sexp :: sexps -> (f (get_user_context ctx) sexp, sexps) | sexp :: sexps -> (f (get_user_context ctx) sexp, sexps)
[@@inline always] [@@inline always]
let peek t ctx sexps = let peek _ctx sexps =
let x, _ = t ctx sexps in match sexps with
(x, sexps) | [] -> (None, sexps)
| sexp :: _ -> (Some sexp, sexps)
[@@inline always]
let peek_exn ctx sexps =
match sexps with
| [] -> end_of_list ctx
| sexp :: _ -> (sexp, sexps)
[@@inline always] [@@inline always]
let junk = next ignore let junk = next ignore

View File

@ -155,8 +155,11 @@ module Of_sexp : sig
(** Unparsed next element of the input *) (** Unparsed next element of the input *)
val raw : ast t val raw : ast t
(** Inspect the input without consuming it *) (** Inspect the next element of the input without consuming it *)
val peek : 'a t -> 'a t val peek : ast option t
(** Same as [peek] but fail if the end of input is reached *)
val peek_exn : ast t
(** Consume and ignore the next element of the input *) (** Consume and ignore the next element of the input *)
val junk : unit t val junk : unit t

View File

@ -57,7 +57,7 @@ module Context = struct
let t ~profile = let t ~profile =
Sexp.Of_sexp.( Sexp.Of_sexp.(
peek raw >>= function peek_exn >>= function
| Atom _ | Quoted_string _ -> | Atom _ | Quoted_string _ ->
enum [ "default", enum [ "default",
Default { targets = [Native] Default { targets = [Native]