@@ -73,7 +73,13 @@ type non_det = Nd_output | Nd_command
7373
7474let default_non_det = Nd_output
7575
76- type block_kind = OCaml | Cram | Toplevel | Include
76+ type block_type = OCaml | Cram | Toplevel | Include
77+
78+ let pp_block_type ppf = function
79+ | OCaml -> Fmt. string ppf " ocaml"
80+ | Cram -> Fmt. string ppf " cram"
81+ | Toplevel -> Fmt. string ppf " toplevel"
82+ | Include -> Fmt. string ppf " include"
7783
7884type t =
7985 | Dir of string
@@ -86,13 +92,7 @@ type t =
8692 | Version of Relation .t * Ocaml_version .t
8793 | Set of string * string
8894 | Unset of string
89- | Block_kind of block_kind
90-
91- let pp_block_kind ppf = function
92- | OCaml -> Fmt. string ppf " ocaml"
93- | Cram -> Fmt. string ppf " cram"
94- | Toplevel -> Fmt. string ppf " toplevel"
95- | Include -> Fmt. string ppf " include"
95+ | Block_type of block_type
9696
9797let pp ppf = function
9898 | Dir d -> Fmt. pf ppf " dir=%s" d
@@ -108,7 +108,7 @@ let pp ppf = function
108108 Fmt. pf ppf " version%a%a" Relation. pp op Ocaml_version. pp v
109109 | Set (v , x ) -> Fmt. pf ppf " set-%s=%s" v x
110110 | Unset x -> Fmt. pf ppf " unset-%s" x
111- | Block_kind bk -> pp_block_kind ppf bk
111+ | Block_type bt -> Fmt. pf ppf " block-type=%a " pp_block_type bt
112112
113113let is_prefix ~prefix s =
114114 let len_prefix = String. length prefix in
@@ -142,41 +142,67 @@ let requires_value ~label ~value f =
142142
143143let requires_eq_value ~label ~value f =
144144 requires_value ~label ~value (fun op value ->
145- match op with Relation. Eq -> Ok (f value) | _ -> non_eq_op ~label )
145+ match op with Relation. Eq -> (f value) | _ -> non_eq_op ~label )
146+
147+ let version_of_string s =
148+ match Ocaml_version. of_string s with
149+ | Ok v -> Ok v
150+ | Error (`Msg e ) ->
151+ Util.Result. errorf " Invalid version: %s." e
152+
153+ let parse_non_det_value ~label s =
154+ match s with
155+ | "output" -> Ok Nd_output
156+ | "command" -> Ok Nd_command
157+ | s ->
158+ let allowed_values = [ " <none>" ; {| " command" | }; {| " output" | } ] in
159+ invalid_value ~label ~allowed_values s
160+
161+ let parse_block_type_value ~label s =
162+ match s with
163+ | "ocaml" -> Ok OCaml
164+ | "cram" -> Ok Cram
165+ | "toplevel" -> Ok Toplevel
166+ | "include" -> Ok Include
167+ | s ->
168+ let allowed_values =
169+ [{| " ocaml" | }; {| " cram" | }; {| " toplevel" | }; {| " include" | }]
170+ in
171+ invalid_value ~label ~allowed_values s
146172
147173let interpret label value =
174+ let open Util.Result.Infix in
148175 match label with
149176 | "skip" -> doesnt_accept_value ~label ~value Skip
150- | "ocaml" -> doesnt_accept_value ~label ~value (Block_kind OCaml )
151- | "cram" -> doesnt_accept_value ~label ~value (Block_kind Cram )
152- | "toplevel" -> doesnt_accept_value ~label ~value (Block_kind Toplevel )
153- | "include" -> doesnt_accept_value ~label ~value (Block_kind Include )
177+ | "ocaml" -> doesnt_accept_value ~label ~value (Block_type OCaml )
178+ | "cram" -> doesnt_accept_value ~label ~value (Block_type Cram )
179+ | "toplevel" -> doesnt_accept_value ~label ~value (Block_type Toplevel )
180+ | "include" -> doesnt_accept_value ~label ~value (Block_type Include )
154181 | v when is_prefix ~prefix: " unset-" v ->
155182 doesnt_accept_value ~label ~value
156183 (Unset (split_prefix ~prefix: " unset-" v))
157184 | "version" ->
158- requires_value ~label ~value (fun op v ->
159- match Ocaml_version. of_string v with
160- | Ok v -> Ok (Version (op, v))
161- | Error (`Msg e ) ->
162- Util.Result. errorf " Invalid `version` label value: %s." e)
185+ requires_value ~label ~value
186+ (fun op v -> version_of_string v >> = fun v -> Ok (Version (op, v)))
163187 | "non-deterministic" -> (
164188 match value with
165189 | None -> Ok (Non_det None )
166- | Some (Relation. Eq, "output" ) -> Ok (Non_det (Some Nd_output ))
167- | Some (Relation. Eq, "command" ) -> Ok (Non_det (Some Nd_command ))
168- | Some (Relation. Eq, v ) ->
169- let allowed_values = [ " <none>" ; {| " command" | }; {| " output" | } ] in
170- invalid_value ~label ~allowed_values v
190+ | Some (Relation. Eq, s ) ->
191+ parse_non_det_value ~label s >> = fun nd ->
192+ Ok (Non_det (Some nd))
171193 | Some _ -> non_eq_op ~label )
172- | "dir" -> requires_eq_value ~label ~value (fun x -> Dir x)
173- | "source-tree" -> requires_eq_value ~label ~value (fun x -> Source_tree x)
174- | "file" -> requires_eq_value ~label ~value (fun x -> File x)
175- | "part" -> requires_eq_value ~label ~value (fun x -> Part x)
176- | "env" -> requires_eq_value ~label ~value (fun x -> Env x)
194+ | "dir" -> requires_eq_value ~label ~value (fun x -> Ok (Dir x))
195+ | "source-tree" ->
196+ requires_eq_value ~label ~value (fun x -> Ok (Source_tree x))
197+ | "file" -> requires_eq_value ~label ~value (fun x -> Ok (File x))
198+ | "part" -> requires_eq_value ~label ~value (fun x -> Ok (Part x))
199+ | "env" -> requires_eq_value ~label ~value (fun x -> Ok (Env x))
200+ | "block-type" ->
201+ requires_eq_value ~label ~value
202+ (fun x -> parse_block_type_value ~label x >> = fun bt -> Ok (Block_type bt))
177203 | l when is_prefix ~prefix: " set-" l ->
178204 requires_eq_value ~label ~value (fun x ->
179- Set (split_prefix ~prefix: " set-" l, x))
205+ Ok ( Set (split_prefix ~prefix: " set-" l, x) ))
180206 | l -> Error (`Msg (Format. sprintf " `%s` is not a valid label." l))
181207
182208let of_string s =
0 commit comments