Skip to content

Commit

Permalink
ctx util: move syntax into sub-module, and use Util.exn_info directly
Browse files Browse the repository at this point in the history
  • Loading branch information
just-max committed Jul 15, 2024
1 parent d4b8931 commit 7368741
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/ast-check/ast_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ let file_violations ?prohibited ?limit path =
expose the functionality we need to do this ourselves) *)
Ocaml_common.Pparse.parse_implementation ~tool_name:"lp-ast-check" path |> ignore;

let open Common.Ctx_util in
let open Common.Ctx_util.Syntax in
let structure =
let< ch = In_channel.with_open_text path in
let lexbuf = Lexing.from_channel ch in
Expand Down
4 changes: 0 additions & 4 deletions src/common/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ module Pp_util = Pp_util
module Path_util = Path_util
module Ctx_util = Ctx_util
module P_run = P_run

(* module Syntax = Syntax *)

(* {!modules: Util Pp_util Path_util Ctx_util P_run} *)
12 changes: 6 additions & 6 deletions src/common/ctx_util.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(** Context managers, loosely inspired by Python *)

type exn_info = Util.exn_info

(** A context manager of type [('a, 'b, 'c) t] takes a continuation, and
should feed the continuation a value of type ['a]. Once the continuation
returns with either a [Ok of 'b] or an {!exn_info}, the continuation should
Expand All @@ -10,14 +8,16 @@ type exn_info = Util.exn_info
This representation has the advantage that some existing functions library
already implement this type (e.g. {!In_channel.with_open_text}). *)
type ('a, 'b, 'c) t = ('a -> ('b, exn_info) result) -> ('c, exn_info) result
type ('a, 'b, 'c) t = ('a -> ('b, Util.exn_info) result) -> ('c, Util.exn_info) result

(** [with_context cm f] runs [f] in the context manager [cm] *)
let with_context (cm : _ t) f =
cm (Util.try_to_result f) |> Util.unresult_exn

(** Let-syntax for {!with_context} *)
let ( let< ) = with_context
module Syntax = struct
(** Let-syntax for {!with_context} *)
let ( let< ) = with_context
end

(* {1 Context managers} *)

Expand Down Expand Up @@ -62,7 +62,7 @@ let timed : _ t = fun k ->
let capture_exceptions ?(filter = Fun.const true) () : _ t = fun k ->
match k () with
| Ok x -> Ok (Ok x)
| Error exn_info when filter exn_info.exn -> Ok (Error exn_info)
| Error exn_info when filter exn_info.Util.exn -> Ok (Error exn_info)
| Error exn_info -> Error exn_info

let empty_context x f : _ t = fun k -> Result.map f (k x)
Expand Down
2 changes: 2 additions & 0 deletions src/common/p_run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(** Waits for the given process or times out. *)
let wait_pid_timeout t pid =
let open Ctx_util in
let open Ctx_util.Syntax in
let< () = sigprocmask Unix.SIG_BLOCK [Sys.sigchld] in

let exception Sigchld in
Expand Down Expand Up @@ -98,6 +99,7 @@ let build_env =
Note: uses temporary files provided by {!Filename}. *)
let p_run ?timeout:timeout_desc ?input ?(args = []) ?(env = []) command =
let open Ctx_util in
let open Ctx_util.Syntax in

let env =
Array.append
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let loc = Location.none (* default location for metaquot *)
module B = Ast_builder_Make (struct let loc = Location.none end)

let parse_interface path =
let open Common.Ctx_util in
let open Common.Ctx_util.Syntax in
let< ch = In_channel.with_open_text path in
let lexbuf = Lexing.from_channel ch in
Lexing.set_filename lexbuf path;
Expand Down
4 changes: 2 additions & 2 deletions src/test-lib/grading.ml
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ let fold_tree_down f = fst (fold_down f)
let fold_forest_down f = snd (fold_down f)
let read_tree path =
let open Common.Ctx_util in
let open Common.Ctx_util.Syntax in
let< ch = In_channel.with_open_bin path in
let xml_in = Xmlm.make_input (`Channel ch) in
in_tree xml_in
let write_tree dtd tree path =
let open Common.Ctx_util in
let open Common.Ctx_util.Syntax in
let< ch = Out_channel.with_open_bin path in
let xml_out = Xmlm.make_output (`Channel ch) in
out_tree xml_out (dtd, tree)
Expand Down
1 change: 1 addition & 0 deletions src/test-lib/qCheck_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let assert_equal
expected_f actual_f =
let open Result in
let open Ctx_util in
let open Ctx_util.Syntax in

let report msg =
match context with None -> msg | Some c -> c ^ "\n" ^ msg
Expand Down
2 changes: 1 addition & 1 deletion src/test-runner/std_task.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

open Task
open Common
open Ctx_util
open Ctx_util.Syntax
open Util

let f = Printf.sprintf
Expand Down
1 change: 1 addition & 0 deletions src/test-runner/task.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ and run_tree : type a b. a st_in -> (a, b) tree -> b st_out * summary =
in

let open Ctx_util in
let open Ctx_util.Syntax in

let result, t_elapsed =
let< () = timed in
Expand Down

0 comments on commit 7368741

Please sign in to comment.