Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add some Format-based container pretty printers to BatIO.Incubator #292

Merged
merged 1 commit into from

3 participants

@hcarty

These are not based on toplevel printer code but their output is pretty close. I've put them in Incubator in case we want to either move the location of the functions around or change the interfaces of the functions themselves.

@thelema thelema merged commit 3c6b01e into ocaml-batteries-team:master
@vincent-hugot
Collaborator

I get a problem with 'make test' that seems linked to this (haven't done a bisect or any other troubleshooting, sorry)

+ ocamlfind ocamlc -c -g -annot -w A -w e -w -29 -package bigarray,num,str -package oUnit -thread -I testsuite -I build -I src -I qtest -I benchsuite -I libs -I src/syntax/pa_comprehension -I src/syntax/pa_strings -I libs/estring -o testsuite/test_bounded.cmo testsuite/test_bounded.ml
File "testsuite/test_bounded.ml", line 15, characters 31-41:
Error: Signature mismatch:
       Modules do not match:
         sig
           type t = int
           val bounds : [> `c of int ] * [> `c of int ]
           val default_low : 'a option
           val default_high : 'a option
           val bounded : (int, int option) BatBounded.bounding_f
         end
       is not included in
         BatBounded.BoundedType
       The field `map2' is required but not provided
       The field `map' is required but not provided
       The field `base_of_t_exn' is required but not provided
       The field `base_of_t' is required but not provided
       The field `base_t' is required but not provided

This was caused by a problem in an earlier pull request. It should be fixed by a pull request I just submitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 166 additions and 0 deletions.
  1. +99 −0 src/batIO.ml
  2. +67 −0 src/batIO.mli
View
99 src/batIO.ml
@@ -723,3 +723,102 @@ let string_of_t_printer p x =
let to_format printer =
fun fmt t -> Format.pp_print_string fmt (to_string printer t)
+
+module Incubator = struct
+ module Array = struct
+ let pp ?(flush = false) ?(first = "[|") ?(last = "|]") ?(sep = "; ") ?(indent = String.length first) pp f a =
+ let open Format in
+ pp_open_box f indent;
+ pp_print_cut f ();
+ pp_print_string f first;
+ pp_print_cut f ();
+
+ for i = 0 to Array.length a - 2 do
+ pp_open_box f indent;
+ pp f a.(i);
+ pp_print_string f sep;
+ pp_close_box f ();
+ pp_print_cut f ();
+ done;
+
+ if Array.length a > 0 then (
+ (* Print the last element without a trailing separator *)
+ pp_open_box f indent;
+ pp f a.(Array.length a - 1);
+ pp_close_box f ();
+ );
+
+ pp_print_cut f ();
+ pp_print_string f last;
+ pp_close_box f ();
+ if flush then pp_print_flush f ()
+ end
+
+ module Enum = struct
+ let pp ?(flush = false) ?(first = "") ?(last = "") ?(sep = " ") ?(indent = String.length first) pp f e =
+ let open Format in
+ pp_open_box f indent;
+ pp_print_cut f ();
+ pp_print_string f first;
+ pp_print_cut f ();
+ match BatEnum.get e with
+ | None ->
+ pp_print_string f last;
+ pp_close_box f ();
+ if flush then pp_print_flush f ()
+ | Some x ->
+ pp_open_box f indent;
+ pp f x;
+ let rec aux () =
+ match BatEnum.get e with
+ | None ->
+ pp_close_box f ();
+ pp_print_cut f ();
+ pp_print_string f last;
+ pp_close_box f ();
+ if flush then pp_print_flush f ()
+ | Some x ->
+ pp_print_string f sep;
+ pp_close_box f ();
+ pp_print_cut f ();
+ pp_open_box f indent;
+ pp f x;
+ aux ()
+ in
+ aux ()
+ end
+
+ module List = struct
+ let pp ?(flush = false) ?(first = "[") ?(last = "]") ?(sep = "; ") ?(indent = String.length first) pp f l =
+ let open Format in
+ pp_open_box f indent;
+ pp_print_cut f ();
+ pp_print_string f first;
+ pp_print_cut f ();
+ match l with
+ | [] ->
+ pp_print_string f last;
+ pp_close_box f ();
+ if flush then pp_print_flush f ()
+ | hd :: tl ->
+ pp_open_box f indent;
+ pp f hd;
+ let rec aux rem =
+ match rem with
+ | [] ->
+ pp_close_box f ();
+ pp_print_cut f ();
+ pp_print_string f last;
+ pp_close_box f ();
+ if flush then pp_print_flush f ()
+ | hd :: tl ->
+ pp_print_string f sep;
+ pp_close_box f ();
+ pp_print_cut f ();
+ pp_open_box f indent;
+ pp f hd;
+ aux tl
+ in
+ aux tl
+ end
+end
View
67 src/batIO.mli
@@ -929,3 +929,70 @@ val make_enum : (input -> 'a) -> input -> 'a BatEnum.t
val get_output_id : _ output -> int
val get_input_id : input -> int
+
+module Incubator : sig
+ (** {6 Format-based pretty-printing} *)
+
+ module Array : sig
+ val pp :
+ ?flush:bool ->
+ ?first:string ->
+ ?last:string ->
+ ?sep:string ->
+ ?indent:int ->
+ (Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a array -> unit
+ (** Print the contents of an array, with [first] preceeding the first item
+ (default: ["\[|"]), [last] following the last item (default: ["|\]"])
+ and [sep] separating items (default: ["; "]). A printing function must
+ be provided to print the items in the array. The [flush] parameter
+ (default: [false]) should be set to [true] for the outer-most printing
+ call. Setting inner calls to [true] - for example, for nested values -
+ prevent indentation from working properly.
+
+ Example:
+ [pp ~flush:true Format.pp_print_int Format.std_formatter \[|1; 2; 3|\]]
+ *)
+ end
+
+ module Enum : sig
+ val pp :
+ ?flush:bool ->
+ ?first:string ->
+ ?last:string ->
+ ?sep:string ->
+ ?indent:int ->
+ (Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a BatEnum.t -> unit
+ (** Print the contents of an enum, with [first] preceeding the first item
+ (default: [""]), [last] following the last item (default: [""])
+ and [sep] separating items (default: [" "]). A printing function must
+ be provided to print the items in the enum. The [flush] parameter
+ (default: [false]) should be set to [true] for the outer-most printing
+ call. Setting inner calls to [true] - for example, for nested values -
+ prevent indentation from working properly.
+
+ Example:
+ [pp ~flush:true Format.pp_print_int Format.std_formatter (1 -- 3)] *)
+ end
+
+ module List : sig
+ val pp :
+ ?flush:bool ->
+ ?first:string ->
+ ?last:string ->
+ ?sep:string ->
+ ?indent:int ->
+ (Format.formatter -> 'a -> 'b) -> Format.formatter -> 'a list -> unit
+ (** Print the contents of a list, with [first] preceeding the first item
+ (default: ["\["]), [last] following the last item (default: ["\]"])
+ and [sep] separating items (default: ["; "]). A printing function must
+ be provided to print the items in the list. The [flush] parameter
+ (default: [false]) should be set to [true] for the outer-most printing
+ call. Setting inner calls to [true] - for example, for nested values -
+ prevent indentation from working properly.
+
+ Example:
+ [pp ~flush:true Format.pp_print_int Format.std_formatter \[1; 2; 3\]]
+ *)
+ end
+end
+
Something went wrong with that request. Please try again.