aboutsummaryrefslogtreecommitdiff
path: root/lib/util.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util.ml')
-rw-r--r--lib/util.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/util.ml b/lib/util.ml
index 549b79e786..94e812ff65 100644
--- a/lib/util.ml
+++ b/lib/util.ml
@@ -1210,6 +1210,8 @@ let pr_opt_no_spc pr = function None -> mt () | Some x -> pr x
let nth n = str (ordinal n)
+(* [prlist pr [a ; ... ; c]] outputs [pr a ++ ... ++ pr c] *)
+
let rec prlist elem l = match l with
| [] -> mt ()
| h::t -> Stream.lapp (fun () -> elem h) (prlist elem t)
@@ -1221,6 +1223,9 @@ let rec prlist_strict elem l = match l with
| [] -> mt ()
| h::t -> (elem h)++(prlist_strict elem t)
+(* [prlist_with_sep sep pr [a ; ... ; c]] outputs
+ [pr a ++ sep() ++ ... ++ sep() ++ pr c] *)
+
let rec prlist_with_sep sep elem l = match l with
| [] -> mt ()
| [h] -> elem h
@@ -1228,6 +1233,23 @@ let rec prlist_with_sep sep elem l = match l with
let e = elem h and s = sep() and r = prlist_with_sep sep elem t in
e ++ s ++ r
+(* Print sequence of objects separated by space (unless an element is empty) *)
+
+let rec pr_sequence elem = function
+ | [] -> mt ()
+ | [h] -> elem h
+ | h::t ->
+ let e = elem h and r = pr_sequence elem t in
+ if e = mt () then r else e ++ spc () ++ r
+
+(* [pr_enum pr [a ; b ; ... ; c]] outputs
+ [pr a ++ str "," ++ pr b ++ str "," ++ ... ++ str "and" ++ pr c] *)
+
+let pr_enum pr l =
+ let c,l' = list_sep_last l in
+ prlist_with_sep pr_coma pr l' ++
+ (if l'<>[] then str " and" ++ spc () else mt()) ++ pr c
+
let pr_vertical_list pr = function
| [] -> str "none" ++ fnl ()
| l -> fnl () ++ str " " ++ hov 0 (prlist_with_sep pr_fnl pr l) ++ fnl ()
@@ -1242,6 +1264,9 @@ let prvecti elem v =
in
if n = 0 then mt () else pr (n - 1)
+(* [prvect_with_sep sep pr [|a ; ... ; c|]] outputs
+ [pr a ++ sep() ++ ... ++ sep() ++ pr c] *)
+
let prvect_with_sep sep elem v =
let rec pr n =
if n = 0 then
@@ -1253,8 +1278,16 @@ let prvect_with_sep sep elem v =
let n = Array.length v in
if n = 0 then mt () else pr (n - 1)
+(* [prvect pr [|a ; ... ; c|]] outputs [pr a ++ ... ++ pr c] *)
+
let prvect elem v = prvect_with_sep mt elem v
+let pr_located pr (loc,x) =
+ if Flags.do_translate() && loc<>dummy_loc then
+ let (b,e) = unloc loc in
+ comment b ++ pr x ++ comment e
+ else pr x
+
let surround p = hov 1 (str"(" ++ p ++ str")")
(*s Size of ocaml values. *)