Commit 6919c00a authored by POTTIER Francois's avatar POTTIER Francois

New/updated printing functions in [Misc]. Used in a few places.

parent c58bf1e1
......@@ -323,11 +323,10 @@ module Terminal = struct
elements 0 (String.length w) w
let print w =
let b = Buffer.create 128 in
String.iter (fun c ->
Printf.bprintf b "%s " (print (Char.code c));
) w;
Buffer.contents b
Misc.separated_iter_to_string
(fun c -> print (Char.code c))
" "
(fun f -> String.iter f w)
(* [Pervasives.compare] implements a lexicographic ordering on strings. *)
let compare = Pervasives.compare
......@@ -344,15 +343,7 @@ module TerminalSet = struct
include CompressedBitSet
let print toks =
let _, accu =
fold (fun tok (first, accu) ->
false,
if first then
accu ^ (Terminal.print tok)
else
accu ^ " " ^ (Terminal.print tok)
) toks (true, "") in
accu
Misc.separated_iter_to_string Terminal.print " " (fun f -> iter f toks)
let universe =
remove Terminal.sharp (
......@@ -468,15 +459,7 @@ module SymbolSet = struct
include Set.Make(Symbol)
let print symbols =
let _, accu =
fold (fun symbol (first, accu) ->
false,
if first then
accu ^ (Symbol.print symbol)
else
accu ^ " " ^ (Symbol.print symbol)
) symbols (true, "") in
accu
Symbol.printl (elements symbols)
(* The following definitions are used in the computation of symbolic FOLLOW
sets below. They are not exported outside of this file. *)
......
......@@ -67,23 +67,36 @@ module IntSet = Set.Make (struct
let compare = ( - )
end)
let separated_list_to_string printer separator list =
let rec loop x = function
| [] ->
printer x
| y :: xs ->
printer x
^ separator
^ loop y xs
in
match list with
| [] ->
""
| x :: xs ->
loop x xs
type 'a iter = ('a -> unit) -> unit
let separated_iter_to_string printer separator iter =
let b = Buffer.create 32 in
let first = ref true in
iter (fun x ->
if !first then begin
Buffer.add_string b (printer x);
first := false
end
else begin
Buffer.add_string b separator;
Buffer.add_string b (printer x)
end
);
Buffer.contents b
let separated_list_to_string printer separator xs =
separated_iter_to_string printer separator (fun f -> List.iter f xs)
let terminated_iter_to_string printer terminator iter =
let b = Buffer.create 32 in
iter (fun x ->
Buffer.add_string b (printer x);
Buffer.add_string b terminator
);
Buffer.contents b
let terminated_list_to_string printer terminator xs =
terminated_iter_to_string printer terminator (fun f -> List.iter f xs)
let index_map string_map =
let n = StringMap.cardinal string_map in
......
......@@ -60,10 +60,20 @@ val ( $$ ) : 'a -> ('a -> 'b) -> 'b
module IntSet : Set.S with type elt = int
(* [separated_list_to_string printer sep l] converts [l] into a string
representation built using [printer] on each element and [sep] as
representation built by using [printer] on each element and [sep] as
a separator. *)
val separated_list_to_string: ('a -> string) -> string -> 'a list -> string
type 'a iter = ('a -> unit) -> unit
val separated_iter_to_string: ('a -> string) -> string -> 'a iter -> string
val separated_list_to_string: ('a -> string) -> string -> 'a list -> string
(* [terminated_list_to_string printer term l] converts [l] into a string
representation built by using [printer] on each element and [term] as
a terminator. *)
val terminated_list_to_string: ('a -> string) -> string -> 'a list -> string
val terminated_iter_to_string: ('a -> string) -> string -> 'a iter -> string
(* [index_map f] returns a triple (indexed_f, domain_indexation, domain_array).
[indexed_f] is a mapping from [0..n-1] to the elements of the map [f]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment