1 (* Pasted from Pottier's PP compiler *)
11 (* ------------------------------------------------------------------------- *)
13 (* Newlines and indentation. *)
19 String.make maxindent ' '
25 "\n" ^ String.sub whitespace 0 !indentation
27 let indent ofs producer () x =
28 let old_indentation = !indentation in
29 let new_indentation = old_indentation + ofs in
30 if new_indentation <= maxindent then
31 indentation := new_indentation;
32 let result = sprintf "%t%a" nl producer x in
33 indentation := old_indentation;
36 (* ------------------------------------------------------------------------- *)
40 let rec list elem () xs =
45 sprintf "%a%a" elem x (list elem) xs
47 let rec preclist delim elem () xs =
52 sprintf "%t%a%a" delim elem x (preclist delim elem) xs
54 let rec termlist delim elem () xs =
59 sprintf "%a%t%a" elem x delim (termlist delim elem) xs
61 let seplist sep elem () xs =
66 sprintf "%a%a" elem x (preclist sep elem) xs
68 let annlist announcement list () xs =
73 sprintf "%t%a" announcement list xs
75 (* ------------------------------------------------------------------------- *)
92 sprintf "%t%t" semicolon nl
95 sprintf "%t%s" nl (String.make k ' ')
100 (* ------------------------------------------------------------------------- *)
102 (* [atmost n delimiter stop] normally prints a [delimiter], except that,
103 every [n] calls, it prints a [stop] in addition. *)
105 let atmost n (delimiter : punctuation) (stop : punctuation) : punctuation =
119 (* ------------------------------------------------------------------------- *)
124 List.fold_left (fun width x ->
125 max width (String.length x)
129 let y = String.make width ' ' in
130 String.blit x 0 y 0 (String.length x);
134 List.map (pad (width column)) column
136 let rec zipcat column1 column2 =
137 List.fold_right2 (fun x1 x2 column ->
141 let catenate columns =
145 | column :: columns ->
146 List.fold_left (fun table column ->
147 zipcat table (pad column)
148 ) (pad column) columns
150 let transposerev lines =
155 List.fold_left (fun columns line ->
156 List.fold_right2 (fun x column columns ->
157 (x :: column) :: columns
159 ) (List.map (fun x -> [ x ]) line) lines
161 (* ------------------------------------------------------------------------- *)
165 let showif flag printer x =
167 Printf.fprintf stdout "%s%!" (sprintf "%a" printer x);