39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* PRETTY-PRINTING *)
|
|
3 |
(* Copyright (c) 2001-2008 Joe Hurd, distributed under the GNU GPL version 2 *)
|
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
structure Print :> Print =
|
|
7 |
struct
|
|
8 |
|
|
9 |
open Useful;
|
|
10 |
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
(* Constants. *)
|
|
13 |
(* ------------------------------------------------------------------------- *)
|
|
14 |
|
|
15 |
val initialLineLength = 75;
|
|
16 |
|
|
17 |
(* ------------------------------------------------------------------------- *)
|
|
18 |
(* Helper functions. *)
|
|
19 |
(* ------------------------------------------------------------------------- *)
|
|
20 |
|
|
21 |
fun revAppend xs s =
|
|
22 |
case xs of
|
|
23 |
[] => s ()
|
|
24 |
| x :: xs => revAppend xs (K (Stream.Cons (x,s)));
|
|
25 |
|
|
26 |
fun revConcat strm =
|
|
27 |
case strm of
|
|
28 |
Stream.Nil => Stream.Nil
|
|
29 |
| Stream.Cons (h,t) => revAppend h (revConcat o t);
|
|
30 |
|
|
31 |
local
|
|
32 |
fun join current prev = (prev ^ "\n", current);
|
|
33 |
in
|
|
34 |
fun joinNewline strm =
|
|
35 |
case strm of
|
|
36 |
Stream.Nil => Stream.Nil
|
|
37 |
| Stream.Cons (h,t) => Stream.maps join Stream.singleton h (t ());
|
|
38 |
end;
|
|
39 |
|
|
40 |
local
|
|
41 |
fun calcSpaces n = nChars #" " n;
|
|
42 |
|
|
43 |
val cachedSpaces = Vector.tabulate (initialLineLength,calcSpaces);
|
|
44 |
in
|
|
45 |
fun nSpaces n =
|
|
46 |
if n < initialLineLength then Vector.sub (cachedSpaces,n)
|
|
47 |
else calcSpaces n;
|
|
48 |
end;
|
|
49 |
|
|
50 |
(* ------------------------------------------------------------------------- *)
|
|
51 |
(* A type of pretty-printers. *)
|
|
52 |
(* ------------------------------------------------------------------------- *)
|
|
53 |
|
|
54 |
datatype breakStyle = Consistent | Inconsistent;
|
|
55 |
|
|
56 |
datatype ppStep =
|
|
57 |
BeginBlock of breakStyle * int
|
|
58 |
| EndBlock
|
|
59 |
| AddString of string
|
|
60 |
| AddBreak of int
|
|
61 |
| AddNewline;
|
|
62 |
|
|
63 |
type ppstream = ppStep Stream.stream;
|
|
64 |
|
|
65 |
type 'a pp = 'a -> ppstream;
|
|
66 |
|
|
67 |
fun breakStyleToString style =
|
|
68 |
case style of
|
|
69 |
Consistent => "Consistent"
|
|
70 |
| Inconsistent => "Inconsistent";
|
|
71 |
|
|
72 |
fun ppStepToString step =
|
|
73 |
case step of
|
|
74 |
BeginBlock _ => "BeginBlock"
|
|
75 |
| EndBlock => "EndBlock"
|
|
76 |
| AddString _ => "AddString"
|
|
77 |
| AddBreak _ => "AddBreak"
|
|
78 |
| AddNewline => "AddNewline";
|
|
79 |
|
|
80 |
(* ------------------------------------------------------------------------- *)
|
|
81 |
(* Pretty-printer primitives. *)
|
|
82 |
(* ------------------------------------------------------------------------- *)
|
|
83 |
|
|
84 |
fun beginBlock style indent = Stream.singleton (BeginBlock (style,indent));
|
|
85 |
|
|
86 |
val endBlock = Stream.singleton EndBlock;
|
|
87 |
|
|
88 |
fun addString s = Stream.singleton (AddString s);
|
|
89 |
|
|
90 |
fun addBreak spaces = Stream.singleton (AddBreak spaces);
|
|
91 |
|
|
92 |
val addNewline = Stream.singleton AddNewline;
|
|
93 |
|
|
94 |
val skip : ppstream = Stream.Nil;
|
|
95 |
|
|
96 |
fun sequence pp1 pp2 : ppstream = Stream.append pp1 (K pp2);
|
|
97 |
|
|
98 |
local
|
|
99 |
fun dup pp n () = if n = 1 then pp else Stream.append pp (dup pp (n - 1));
|
|
100 |
in
|
|
101 |
fun duplicate n pp = if n = 0 then skip else dup pp n ();
|
|
102 |
end;
|
|
103 |
|
|
104 |
val program : ppstream list -> ppstream = Stream.concatList;
|
|
105 |
|
|
106 |
val stream : ppstream Stream.stream -> ppstream = Stream.concat;
|
|
107 |
|
|
108 |
fun block style indent pp = program [beginBlock style indent, pp, endBlock];
|
|
109 |
|
|
110 |
fun blockProgram style indent pps = block style indent (program pps);
|
|
111 |
|
|
112 |
fun bracket l r pp =
|
|
113 |
blockProgram Inconsistent (size l)
|
|
114 |
[addString l,
|
|
115 |
pp,
|
|
116 |
addString r];
|
|
117 |
|
|
118 |
fun field f pp =
|
|
119 |
blockProgram Inconsistent 2
|
|
120 |
[addString (f ^ " ="),
|
|
121 |
addBreak 1,
|
|
122 |
pp];
|
|
123 |
|
|
124 |
val record =
|
|
125 |
let
|
|
126 |
val sep = sequence (addString ",") (addBreak 1)
|
|
127 |
|
|
128 |
fun recordField (f,pp) = field f pp
|
|
129 |
|
|
130 |
fun sepField f = sequence sep (recordField f)
|
|
131 |
|
|
132 |
fun fields [] = []
|
|
133 |
| fields (f :: fs) = recordField f :: map sepField fs
|
|
134 |
in
|
|
135 |
bracket "{" "}" o blockProgram Consistent 0 o fields
|
|
136 |
end;
|
|
137 |
|
|
138 |
(* ------------------------------------------------------------------------- *)
|
|
139 |
(* Pretty-printer combinators. *)
|
|
140 |
(* ------------------------------------------------------------------------- *)
|
|
141 |
|
|
142 |
fun ppMap f ppB a : ppstream = ppB (f a);
|
|
143 |
|
|
144 |
fun ppBracket l r ppA a = bracket l r (ppA a);
|
|
145 |
|
|
146 |
fun ppOp s = sequence (if s = "" then skip else addString s) (addBreak 1);
|
|
147 |
|
|
148 |
fun ppOp2 ab ppA ppB (a,b) =
|
|
149 |
blockProgram Inconsistent 0
|
|
150 |
[ppA a,
|
|
151 |
ppOp ab,
|
|
152 |
ppB b];
|
|
153 |
|
|
154 |
fun ppOp3 ab bc ppA ppB ppC (a,b,c) =
|
|
155 |
blockProgram Inconsistent 0
|
|
156 |
[ppA a,
|
|
157 |
ppOp ab,
|
|
158 |
ppB b,
|
|
159 |
ppOp bc,
|
|
160 |
ppC c];
|
|
161 |
|
|
162 |
fun ppOpList s ppA =
|
|
163 |
let
|
|
164 |
fun ppOpA a = sequence (ppOp s) (ppA a)
|
|
165 |
in
|
|
166 |
fn [] => skip
|
|
167 |
| h :: t => blockProgram Inconsistent 0 (ppA h :: map ppOpA t)
|
|
168 |
end;
|
|
169 |
|
|
170 |
fun ppOpStream s ppA =
|
|
171 |
let
|
|
172 |
fun ppOpA a = sequence (ppOp s) (ppA a)
|
|
173 |
in
|
|
174 |
fn Stream.Nil => skip
|
|
175 |
| Stream.Cons (h,t) =>
|
|
176 |
blockProgram Inconsistent 0
|
|
177 |
[ppA h,
|
|
178 |
Stream.concat (Stream.map ppOpA (t ()))]
|
|
179 |
end;
|
|
180 |
|
|
181 |
(* ------------------------------------------------------------------------- *)
|
|
182 |
(* Pretty-printers for common types. *)
|
|
183 |
(* ------------------------------------------------------------------------- *)
|
|
184 |
|
|
185 |
fun ppChar c = addString (str c);
|
|
186 |
|
|
187 |
val ppString = addString;
|
|
188 |
|
|
189 |
fun ppEscapeString {escape} =
|
|
190 |
let
|
|
191 |
val escapeMap = map (fn c => (c, "\\" ^ str c)) escape
|
|
192 |
|
|
193 |
fun escapeChar c =
|
|
194 |
case c of
|
|
195 |
#"\\" => "\\\\"
|
|
196 |
| #"\n" => "\\n"
|
|
197 |
| #"\t" => "\\t"
|
|
198 |
| _ =>
|
|
199 |
case List.find (equal c o fst) escapeMap of
|
|
200 |
SOME (_,s) => s
|
|
201 |
| NONE => str c
|
|
202 |
in
|
|
203 |
fn s => addString (String.translate escapeChar s)
|
|
204 |
end;
|
|
205 |
|
|
206 |
val ppUnit : unit pp = K (addString "()");
|
|
207 |
|
|
208 |
fun ppBool b = addString (if b then "true" else "false");
|
|
209 |
|
|
210 |
fun ppInt i = addString (Int.toString i);
|
|
211 |
|
|
212 |
local
|
|
213 |
val ppNeg = addString "~"
|
|
214 |
and ppSep = addString ","
|
|
215 |
and ppZero = addString "0"
|
|
216 |
and ppZeroZero = addString "00";
|
|
217 |
|
|
218 |
fun ppIntBlock i =
|
|
219 |
if i < 10 then sequence ppZeroZero (ppInt i)
|
|
220 |
else if i < 100 then sequence ppZero (ppInt i)
|
|
221 |
else ppInt i;
|
|
222 |
|
|
223 |
fun ppIntBlocks i =
|
|
224 |
if i < 1000 then ppInt i
|
|
225 |
else sequence (ppIntBlocks (i div 1000))
|
|
226 |
(sequence ppSep (ppIntBlock (i mod 1000)));
|
|
227 |
in
|
|
228 |
fun ppPrettyInt i =
|
|
229 |
if i < 0 then sequence ppNeg (ppIntBlocks (~i))
|
|
230 |
else ppIntBlocks i;
|
|
231 |
end;
|
|
232 |
|
|
233 |
fun ppReal r = addString (Real.toString r);
|
|
234 |
|
|
235 |
fun ppPercent p = addString (percentToString p);
|
|
236 |
|
|
237 |
fun ppOrder x =
|
|
238 |
addString
|
|
239 |
(case x of
|
|
240 |
LESS => "Less"
|
|
241 |
| EQUAL => "Equal"
|
|
242 |
| GREATER => "Greater");
|
|
243 |
|
|
244 |
fun ppList ppA = ppBracket "[" "]" (ppOpList "," ppA);
|
|
245 |
|
|
246 |
fun ppStream ppA = ppBracket "[" "]" (ppOpStream "," ppA);
|
|
247 |
|
|
248 |
fun ppOption ppA ao =
|
|
249 |
case ao of
|
|
250 |
SOME a => ppA a
|
|
251 |
| NONE => addString "-";
|
|
252 |
|
|
253 |
fun ppPair ppA ppB = ppBracket "(" ")" (ppOp2 "," ppA ppB);
|
|
254 |
|
|
255 |
fun ppTriple ppA ppB ppC = ppBracket "(" ")" (ppOp3 "," "," ppA ppB ppC);
|
|
256 |
|
|
257 |
fun ppBreakStyle style = addString (breakStyleToString style);
|
|
258 |
|
|
259 |
fun ppPpStep step =
|
|
260 |
let
|
|
261 |
val cmd = ppStepToString step
|
|
262 |
in
|
|
263 |
blockProgram Inconsistent 2
|
|
264 |
(addString cmd ::
|
|
265 |
(case step of
|
|
266 |
BeginBlock style_indent =>
|
|
267 |
[addBreak 1,
|
|
268 |
ppPair ppBreakStyle ppInt style_indent]
|
|
269 |
| EndBlock => []
|
|
270 |
| AddString s =>
|
|
271 |
[addBreak 1,
|
|
272 |
addString ("\"" ^ s ^ "\"")]
|
|
273 |
| AddBreak n =>
|
|
274 |
[addBreak 1,
|
|
275 |
ppInt n]
|
|
276 |
| AddNewline => []))
|
|
277 |
end;
|
|
278 |
|
|
279 |
val ppPpstream = ppStream ppPpStep;
|
|
280 |
|
|
281 |
(* ------------------------------------------------------------------------- *)
|
|
282 |
(* Pretty-printing infix operators. *)
|
|
283 |
(* ------------------------------------------------------------------------- *)
|
|
284 |
|
|
285 |
datatype infixes =
|
|
286 |
Infixes of
|
|
287 |
{token : string,
|
|
288 |
precedence : int,
|
|
289 |
leftAssoc : bool} list;
|
|
290 |
|
|
291 |
local
|
|
292 |
fun chop l =
|
|
293 |
case l of
|
|
294 |
#" " :: l => let val (n,l) = chop l in (n + 1, l) end
|
|
295 |
| _ => (0,l);
|
|
296 |
in
|
|
297 |
fun opSpaces tok =
|
|
298 |
let
|
|
299 |
val tok = explode tok
|
|
300 |
val (r,tok) = chop (rev tok)
|
|
301 |
val (l,tok) = chop (rev tok)
|
|
302 |
val tok = implode tok
|
|
303 |
in
|
|
304 |
{leftSpaces = l, token = tok, rightSpaces = r}
|
|
305 |
end;
|
|
306 |
end;
|
|
307 |
|
|
308 |
fun ppOpSpaces {leftSpaces,token,rightSpaces} =
|
|
309 |
let
|
|
310 |
val leftSpacesToken =
|
|
311 |
if leftSpaces = 0 then token else nSpaces leftSpaces ^ token
|
|
312 |
in
|
|
313 |
sequence
|
|
314 |
(addString leftSpacesToken)
|
|
315 |
(addBreak rightSpaces)
|
|
316 |
end;
|
|
317 |
|
|
318 |
local
|
|
319 |
fun new t l acc = {tokens = [opSpaces t], leftAssoc = l} :: acc;
|
|
320 |
|
|
321 |
fun add t l acc =
|
|
322 |
case acc of
|
|
323 |
[] => raise Bug "Print.layerInfixOps.layer"
|
|
324 |
| {tokens = ts, leftAssoc = l'} :: acc =>
|
|
325 |
if l = l' then {tokens = opSpaces t :: ts, leftAssoc = l} :: acc
|
|
326 |
else raise Bug "Print.layerInfixOps: mixed assocs";
|
|
327 |
|
|
328 |
fun layer ({token = t, precedence = p, leftAssoc = l}, (acc,p')) =
|
|
329 |
let
|
|
330 |
val acc = if p = p' then add t l acc else new t l acc
|
|
331 |
in
|
|
332 |
(acc,p)
|
|
333 |
end;
|
|
334 |
in
|
|
335 |
fun layerInfixes (Infixes i) =
|
|
336 |
case sortMap #precedence Int.compare i of
|
|
337 |
[] => []
|
|
338 |
| {token = t, precedence = p, leftAssoc = l} :: i =>
|
|
339 |
let
|
|
340 |
val acc = new t l []
|
|
341 |
|
|
342 |
val (acc,_) = List.foldl layer (acc,p) i
|
|
343 |
in
|
|
344 |
acc
|
|
345 |
end;
|
|
346 |
end;
|
|
347 |
|
|
348 |
val tokensLayeredInfixes =
|
|
349 |
let
|
|
350 |
fun addToken ({leftSpaces = _, token = t, rightSpaces = _}, s) =
|
|
351 |
StringSet.add s t
|
|
352 |
|
|
353 |
fun addTokens ({tokens = t, leftAssoc = _}, s) =
|
|
354 |
List.foldl addToken s t
|
|
355 |
in
|
|
356 |
List.foldl addTokens StringSet.empty
|
|
357 |
end;
|
|
358 |
|
|
359 |
val tokensInfixes = tokensLayeredInfixes o layerInfixes;
|
|
360 |
|
|
361 |
local
|
|
362 |
val mkTokenMap =
|
|
363 |
let
|
|
364 |
fun add (token,m) =
|
|
365 |
let
|
|
366 |
val {leftSpaces = _, token = t, rightSpaces = _} = token
|
|
367 |
in
|
|
368 |
StringMap.insert m (t, ppOpSpaces token)
|
|
369 |
end
|
|
370 |
in
|
|
371 |
List.foldl add (StringMap.new ())
|
|
372 |
end;
|
|
373 |
in
|
|
374 |
fun ppGenInfix {tokens,leftAssoc} =
|
|
375 |
let
|
|
376 |
val tokenMap = mkTokenMap tokens
|
|
377 |
in
|
|
378 |
fn dest => fn ppSub =>
|
|
379 |
let
|
|
380 |
fun dest' tm =
|
|
381 |
case dest tm of
|
|
382 |
NONE => NONE
|
|
383 |
| SOME (t,a,b) =>
|
|
384 |
case StringMap.peek tokenMap t of
|
|
385 |
NONE => NONE
|
|
386 |
| SOME p => SOME (p,a,b)
|
|
387 |
|
|
388 |
fun ppGo (tmr as (tm,r)) =
|
|
389 |
case dest' tm of
|
|
390 |
NONE => ppSub tmr
|
|
391 |
| SOME (p,a,b) =>
|
|
392 |
program
|
|
393 |
[(if leftAssoc then ppGo else ppSub) (a,true),
|
|
394 |
p,
|
|
395 |
(if leftAssoc then ppSub else ppGo) (b,r)]
|
|
396 |
in
|
|
397 |
fn tmr as (tm,_) =>
|
|
398 |
if Option.isSome (dest' tm) then
|
|
399 |
block Inconsistent 0 (ppGo tmr)
|
|
400 |
else
|
|
401 |
ppSub tmr
|
|
402 |
end
|
|
403 |
end;
|
|
404 |
end
|
|
405 |
|
|
406 |
fun ppInfixes ops =
|
|
407 |
let
|
|
408 |
val layeredOps = layerInfixes ops
|
|
409 |
|
|
410 |
val toks = tokensLayeredInfixes layeredOps
|
|
411 |
|
|
412 |
val iprinters = List.map ppGenInfix layeredOps
|
|
413 |
in
|
|
414 |
fn dest => fn ppSub =>
|
|
415 |
let
|
|
416 |
fun printer sub = foldl (fn (ip,p) => ip dest p) sub iprinters
|
|
417 |
|
|
418 |
fun isOp t =
|
|
419 |
case dest t of
|
|
420 |
SOME (x,_,_) => StringSet.member x toks
|
|
421 |
| NONE => false
|
|
422 |
|
|
423 |
fun subpr (tmr as (tm,_)) =
|
|
424 |
if isOp tm then
|
|
425 |
blockProgram Inconsistent 1
|
|
426 |
[addString "(",
|
|
427 |
printer subpr (tm,false),
|
|
428 |
addString ")"]
|
|
429 |
else
|
|
430 |
ppSub tmr
|
|
431 |
in
|
|
432 |
fn tmr => block Inconsistent 0 (printer subpr tmr)
|
|
433 |
end
|
|
434 |
end;
|
|
435 |
|
|
436 |
(* ------------------------------------------------------------------------- *)
|
|
437 |
(* Executing pretty-printers to generate lines. *)
|
|
438 |
(* ------------------------------------------------------------------------- *)
|
|
439 |
|
|
440 |
datatype blockBreakStyle =
|
|
441 |
InconsistentBlock
|
|
442 |
| ConsistentBlock
|
|
443 |
| BreakingBlock;
|
|
444 |
|
|
445 |
datatype block =
|
|
446 |
Block of
|
|
447 |
{indent : int,
|
|
448 |
style : blockBreakStyle,
|
|
449 |
size : int,
|
|
450 |
chunks : chunk list}
|
|
451 |
|
|
452 |
and chunk =
|
|
453 |
StringChunk of {size : int, string : string}
|
|
454 |
| BreakChunk of int
|
|
455 |
| BlockChunk of block;
|
|
456 |
|
|
457 |
datatype state =
|
|
458 |
State of
|
|
459 |
{blocks : block list,
|
|
460 |
lineIndent : int,
|
|
461 |
lineSize : int};
|
|
462 |
|
|
463 |
val initialIndent = 0;
|
|
464 |
|
|
465 |
val initialStyle = Inconsistent;
|
|
466 |
|
|
467 |
fun liftStyle style =
|
|
468 |
case style of
|
|
469 |
Inconsistent => InconsistentBlock
|
|
470 |
| Consistent => ConsistentBlock;
|
|
471 |
|
|
472 |
fun breakStyle style =
|
|
473 |
case style of
|
|
474 |
ConsistentBlock => BreakingBlock
|
|
475 |
| _ => style;
|
|
476 |
|
|
477 |
fun sizeBlock (Block {size,...}) = size;
|
|
478 |
|
|
479 |
fun sizeChunk chunk =
|
|
480 |
case chunk of
|
|
481 |
StringChunk {size,...} => size
|
|
482 |
| BreakChunk spaces => spaces
|
|
483 |
| BlockChunk block => sizeBlock block;
|
|
484 |
|
|
485 |
val splitChunks =
|
|
486 |
let
|
|
487 |
fun split _ [] = NONE
|
|
488 |
| split acc (chunk :: chunks) =
|
|
489 |
case chunk of
|
|
490 |
BreakChunk _ => SOME (rev acc, chunks)
|
|
491 |
| _ => split (chunk :: acc) chunks
|
|
492 |
in
|
|
493 |
split []
|
|
494 |
end;
|
|
495 |
|
|
496 |
val sizeChunks = List.foldl (fn (c,z) => sizeChunk c + z) 0;
|
|
497 |
|
|
498 |
local
|
|
499 |
fun render acc [] = acc
|
|
500 |
| render acc (chunk :: chunks) =
|
|
501 |
case chunk of
|
|
502 |
StringChunk {string = s, ...} => render (acc ^ s) chunks
|
|
503 |
| BreakChunk n => render (acc ^ nSpaces n) chunks
|
|
504 |
| BlockChunk (Block {chunks = l, ...}) =>
|
|
505 |
render acc (List.revAppend (l,chunks));
|
|
506 |
in
|
|
507 |
fun renderChunks indent chunks = render (nSpaces indent) (rev chunks);
|
|
508 |
|
|
509 |
fun renderChunk indent chunk = renderChunks indent [chunk];
|
|
510 |
end;
|
|
511 |
|
|
512 |
fun isEmptyBlock block =
|
|
513 |
let
|
|
514 |
val Block {indent = _, style = _, size, chunks} = block
|
|
515 |
|
|
516 |
val empty = null chunks
|
|
517 |
|
|
518 |
(*BasicDebug
|
|
519 |
val _ = not empty orelse size = 0 orelse
|
|
520 |
raise Bug "Print.isEmptyBlock: bad size"
|
|
521 |
*)
|
|
522 |
in
|
|
523 |
empty
|
|
524 |
end;
|
|
525 |
|
|
526 |
fun checkBlock ind block =
|
|
527 |
let
|
|
528 |
val Block {indent, style = _, size, chunks} = block
|
|
529 |
val _ = indent >= ind orelse raise Bug "Print.checkBlock: bad indents"
|
|
530 |
val size' = checkChunks indent chunks
|
|
531 |
val _ = size = size' orelse raise Bug "Print.checkBlock: wrong size"
|
|
532 |
in
|
|
533 |
size
|
|
534 |
end
|
|
535 |
|
|
536 |
and checkChunks ind chunks =
|
|
537 |
case chunks of
|
|
538 |
[] => 0
|
|
539 |
| chunk :: chunks => checkChunk ind chunk + checkChunks ind chunks
|
|
540 |
|
|
541 |
and checkChunk ind chunk =
|
|
542 |
case chunk of
|
|
543 |
StringChunk {size,...} => size
|
|
544 |
| BreakChunk spaces => spaces
|
|
545 |
| BlockChunk block => checkBlock ind block;
|
|
546 |
|
|
547 |
val checkBlocks =
|
|
548 |
let
|
|
549 |
fun check ind blocks =
|
|
550 |
case blocks of
|
|
551 |
[] => 0
|
|
552 |
| block :: blocks =>
|
|
553 |
let
|
|
554 |
val Block {indent,...} = block
|
|
555 |
in
|
|
556 |
checkBlock ind block + check indent blocks
|
|
557 |
end
|
|
558 |
in
|
|
559 |
check initialIndent o rev
|
|
560 |
end;
|
|
561 |
|
|
562 |
val initialBlock =
|
|
563 |
let
|
|
564 |
val indent = initialIndent
|
|
565 |
val style = liftStyle initialStyle
|
|
566 |
val size = 0
|
|
567 |
val chunks = []
|
|
568 |
in
|
|
569 |
Block
|
|
570 |
{indent = indent,
|
|
571 |
style = style,
|
|
572 |
size = size,
|
|
573 |
chunks = chunks}
|
|
574 |
end;
|
|
575 |
|
|
576 |
val initialState =
|
|
577 |
let
|
|
578 |
val blocks = [initialBlock]
|
|
579 |
val lineIndent = initialIndent
|
|
580 |
val lineSize = 0
|
|
581 |
in
|
|
582 |
State
|
|
583 |
{blocks = blocks,
|
|
584 |
lineIndent = lineIndent,
|
|
585 |
lineSize = lineSize}
|
|
586 |
end;
|
|
587 |
|
|
588 |
(*BasicDebug
|
|
589 |
fun checkState state =
|
|
590 |
(let
|
|
591 |
val State {blocks, lineIndent = _, lineSize} = state
|
|
592 |
val lineSize' = checkBlocks blocks
|
|
593 |
val _ = lineSize = lineSize' orelse
|
|
594 |
raise Error "wrong lineSize"
|
|
595 |
in
|
|
596 |
()
|
|
597 |
end
|
|
598 |
handle Error err => raise Bug err)
|
|
599 |
handle Bug bug => raise Bug ("Print.checkState: " ^ bug);
|
|
600 |
*)
|
|
601 |
|
|
602 |
fun isFinalState state =
|
|
603 |
let
|
|
604 |
val State {blocks,lineIndent,lineSize} = state
|
|
605 |
in
|
|
606 |
case blocks of
|
|
607 |
[] => raise Bug "Print.isFinalState: no block"
|
|
608 |
| [block] => isEmptyBlock block
|
|
609 |
| _ :: _ :: _ => false
|
|
610 |
end;
|
|
611 |
|
|
612 |
local
|
|
613 |
fun renderBreak lineIndent (chunks,lines) =
|
|
614 |
let
|
|
615 |
val line = renderChunks lineIndent chunks
|
|
616 |
|
|
617 |
val lines = line :: lines
|
|
618 |
in
|
|
619 |
lines
|
|
620 |
end;
|
|
621 |
|
|
622 |
fun renderBreaks lineIndent lineIndent' breaks lines =
|
|
623 |
case rev breaks of
|
|
624 |
[] => raise Bug "Print.renderBreaks"
|
|
625 |
| c :: cs =>
|
|
626 |
let
|
|
627 |
val lines = renderBreak lineIndent (c,lines)
|
|
628 |
in
|
|
629 |
List.foldl (renderBreak lineIndent') lines cs
|
|
630 |
end;
|
|
631 |
|
|
632 |
fun splitAllChunks cumulatingChunks =
|
|
633 |
let
|
|
634 |
fun split chunks =
|
|
635 |
case splitChunks chunks of
|
|
636 |
SOME (prefix,chunks) => prefix :: split chunks
|
|
637 |
| NONE => [List.concat (chunks :: cumulatingChunks)]
|
|
638 |
in
|
|
639 |
split
|
|
640 |
end;
|
|
641 |
|
|
642 |
fun mkBreak style cumulatingChunks chunks =
|
|
643 |
case splitChunks chunks of
|
|
644 |
NONE => NONE
|
|
645 |
| SOME (chunks,broken) =>
|
|
646 |
let
|
|
647 |
val breaks =
|
|
648 |
case style of
|
|
649 |
InconsistentBlock =>
|
|
650 |
[List.concat (broken :: cumulatingChunks)]
|
|
651 |
| _ => splitAllChunks cumulatingChunks broken
|
|
652 |
in
|
|
653 |
SOME (breaks,chunks)
|
|
654 |
end;
|
|
655 |
|
|
656 |
fun naturalBreak blocks =
|
|
657 |
case blocks of
|
|
658 |
[] => Right ([],[])
|
|
659 |
| block :: blocks =>
|
|
660 |
case naturalBreak blocks of
|
|
661 |
Left (breaks,blocks,lineIndent,lineSize) =>
|
|
662 |
let
|
|
663 |
val Block {size,...} = block
|
|
664 |
|
|
665 |
val blocks = block :: blocks
|
|
666 |
|
|
667 |
val lineSize = lineSize + size
|
|
668 |
in
|
|
669 |
Left (breaks,blocks,lineIndent,lineSize)
|
|
670 |
end
|
|
671 |
| Right (cumulatingChunks,blocks) =>
|
|
672 |
let
|
|
673 |
val Block {indent,style,size,chunks} = block
|
|
674 |
|
|
675 |
val style = breakStyle style
|
|
676 |
in
|
|
677 |
case mkBreak style cumulatingChunks chunks of
|
|
678 |
SOME (breaks,chunks) =>
|
|
679 |
let
|
|
680 |
val size = sizeChunks chunks
|
|
681 |
|
|
682 |
val block =
|
|
683 |
Block
|
|
684 |
{indent = indent,
|
|
685 |
style = style,
|
|
686 |
size = size,
|
|
687 |
chunks = chunks}
|
|
688 |
|
|
689 |
val blocks = block :: blocks
|
|
690 |
|
|
691 |
val lineIndent = indent
|
|
692 |
|
|
693 |
val lineSize = size
|
|
694 |
in
|
|
695 |
Left (breaks,blocks,lineIndent,lineSize)
|
|
696 |
end
|
|
697 |
| NONE =>
|
|
698 |
let
|
|
699 |
val cumulatingChunks = chunks :: cumulatingChunks
|
|
700 |
|
|
701 |
val size = 0
|
|
702 |
|
|
703 |
val chunks = []
|
|
704 |
|
|
705 |
val block =
|
|
706 |
Block
|
|
707 |
{indent = indent,
|
|
708 |
style = style,
|
|
709 |
size = size,
|
|
710 |
chunks = chunks}
|
|
711 |
|
|
712 |
val blocks = block :: blocks
|
|
713 |
in
|
|
714 |
Right (cumulatingChunks,blocks)
|
|
715 |
end
|
|
716 |
end;
|
|
717 |
|
|
718 |
fun forceBreakBlock cumulatingChunks block =
|
|
719 |
let
|
|
720 |
val Block {indent, style, size = _, chunks} = block
|
|
721 |
|
|
722 |
val style = breakStyle style
|
|
723 |
|
|
724 |
val break =
|
|
725 |
case mkBreak style cumulatingChunks chunks of
|
|
726 |
SOME (breaks,chunks) =>
|
|
727 |
let
|
|
728 |
val lineSize = sizeChunks chunks
|
|
729 |
val lineIndent = indent
|
|
730 |
in
|
|
731 |
SOME (breaks,chunks,lineIndent,lineSize)
|
|
732 |
end
|
|
733 |
| NONE => forceBreakChunks cumulatingChunks chunks
|
|
734 |
in
|
|
735 |
case break of
|
|
736 |
SOME (breaks,chunks,lineIndent,lineSize) =>
|
|
737 |
let
|
|
738 |
val size = lineSize
|
|
739 |
|
|
740 |
val block =
|
|
741 |
Block
|
|
742 |
{indent = indent,
|
|
743 |
style = style,
|
|
744 |
size = size,
|
|
745 |
chunks = chunks}
|
|
746 |
in
|
|
747 |
SOME (breaks,block,lineIndent,lineSize)
|
|
748 |
end
|
|
749 |
| NONE => NONE
|
|
750 |
end
|
|
751 |
|
|
752 |
and forceBreakChunks cumulatingChunks chunks =
|
|
753 |
case chunks of
|
|
754 |
[] => NONE
|
|
755 |
| chunk :: chunks =>
|
|
756 |
case forceBreakChunk (chunks :: cumulatingChunks) chunk of
|
|
757 |
SOME (breaks,chunk,lineIndent,lineSize) =>
|
|
758 |
let
|
|
759 |
val chunks = [chunk]
|
|
760 |
in
|
|
761 |
SOME (breaks,chunks,lineIndent,lineSize)
|
|
762 |
end
|
|
763 |
| NONE =>
|
|
764 |
case forceBreakChunks cumulatingChunks chunks of
|
|
765 |
SOME (breaks,chunks,lineIndent,lineSize) =>
|
|
766 |
let
|
|
767 |
val chunks = chunk :: chunks
|
|
768 |
|
|
769 |
val lineSize = lineSize + sizeChunk chunk
|
|
770 |
in
|
|
771 |
SOME (breaks,chunks,lineIndent,lineSize)
|
|
772 |
end
|
|
773 |
| NONE => NONE
|
|
774 |
|
|
775 |
and forceBreakChunk cumulatingChunks chunk =
|
|
776 |
case chunk of
|
|
777 |
StringChunk _ => NONE
|
|
778 |
| BreakChunk _ => raise Bug "Print.forceBreakChunk: BreakChunk"
|
|
779 |
| BlockChunk block =>
|
|
780 |
case forceBreakBlock cumulatingChunks block of
|
|
781 |
SOME (breaks,block,lineIndent,lineSize) =>
|
|
782 |
let
|
|
783 |
val chunk = BlockChunk block
|
|
784 |
in
|
|
785 |
SOME (breaks,chunk,lineIndent,lineSize)
|
|
786 |
end
|
|
787 |
| NONE => NONE;
|
|
788 |
|
|
789 |
fun forceBreak cumulatingChunks blocks' blocks =
|
|
790 |
case blocks of
|
|
791 |
[] => NONE
|
|
792 |
| block :: blocks =>
|
|
793 |
let
|
|
794 |
val cumulatingChunks =
|
|
795 |
case cumulatingChunks of
|
|
796 |
[] => raise Bug "Print.forceBreak: null cumulatingChunks"
|
|
797 |
| _ :: cumulatingChunks => cumulatingChunks
|
|
798 |
|
|
799 |
val blocks' =
|
|
800 |
case blocks' of
|
|
801 |
[] => raise Bug "Print.forceBreak: null blocks'"
|
|
802 |
| _ :: blocks' => blocks'
|
|
803 |
in
|
|
804 |
case forceBreakBlock cumulatingChunks block of
|
|
805 |
SOME (breaks,block,lineIndent,lineSize) =>
|
|
806 |
let
|
|
807 |
val blocks = block :: blocks'
|
|
808 |
in
|
|
809 |
SOME (breaks,blocks,lineIndent,lineSize)
|
|
810 |
end
|
|
811 |
| NONE =>
|
|
812 |
case forceBreak cumulatingChunks blocks' blocks of
|
|
813 |
SOME (breaks,blocks,lineIndent,lineSize) =>
|
|
814 |
let
|
|
815 |
val blocks = block :: blocks
|
|
816 |
|
|
817 |
val Block {size,...} = block
|
|
818 |
|
|
819 |
val lineSize = lineSize + size
|
|
820 |
in
|
|
821 |
SOME (breaks,blocks,lineIndent,lineSize)
|
|
822 |
end
|
|
823 |
| NONE => NONE
|
|
824 |
end;
|
|
825 |
|
|
826 |
fun normalize lineLength lines state =
|
|
827 |
let
|
|
828 |
val State {blocks,lineIndent,lineSize} = state
|
|
829 |
in
|
|
830 |
if lineIndent + lineSize <= lineLength then (lines,state)
|
|
831 |
else
|
|
832 |
let
|
|
833 |
val break =
|
|
834 |
case naturalBreak blocks of
|
|
835 |
Left break => SOME break
|
|
836 |
| Right (c,b) => forceBreak c b blocks
|
|
837 |
in
|
|
838 |
case break of
|
|
839 |
SOME (breaks,blocks,lineIndent',lineSize) =>
|
|
840 |
let
|
|
841 |
val lines = renderBreaks lineIndent lineIndent' breaks lines
|
|
842 |
|
|
843 |
val state =
|
|
844 |
State
|
|
845 |
{blocks = blocks,
|
|
846 |
lineIndent = lineIndent',
|
|
847 |
lineSize = lineSize}
|
|
848 |
in
|
|
849 |
normalize lineLength lines state
|
|
850 |
end
|
|
851 |
| NONE => (lines,state)
|
|
852 |
end
|
|
853 |
end;
|
|
854 |
|
|
855 |
(*BasicDebug
|
|
856 |
val normalize = fn lineLength => fn lines => fn state =>
|
|
857 |
let
|
|
858 |
val () = checkState state
|
|
859 |
in
|
|
860 |
normalize lineLength lines state
|
|
861 |
end
|
|
862 |
handle Bug bug =>
|
|
863 |
raise Bug ("Print.normalize: before normalize:\n" ^ bug)
|
|
864 |
*)
|
|
865 |
|
|
866 |
fun executeBeginBlock (style,ind) lines state =
|
|
867 |
let
|
|
868 |
val State {blocks,lineIndent,lineSize} = state
|
|
869 |
|
|
870 |
val Block {indent,...} =
|
|
871 |
case blocks of
|
|
872 |
[] => raise Bug "Print.executeBeginBlock: no block"
|
|
873 |
| block :: _ => block
|
|
874 |
|
|
875 |
val indent = indent + ind
|
|
876 |
|
|
877 |
val style = liftStyle style
|
|
878 |
|
|
879 |
val size = 0
|
|
880 |
|
|
881 |
val chunks = []
|
|
882 |
|
|
883 |
val block =
|
|
884 |
Block
|
|
885 |
{indent = indent,
|
|
886 |
style = style,
|
|
887 |
size = size,
|
|
888 |
chunks = chunks}
|
|
889 |
|
|
890 |
val blocks = block :: blocks
|
|
891 |
|
|
892 |
val state =
|
|
893 |
State
|
|
894 |
{blocks = blocks,
|
|
895 |
lineIndent = lineIndent,
|
|
896 |
lineSize = lineSize}
|
|
897 |
in
|
|
898 |
(lines,state)
|
|
899 |
end;
|
|
900 |
|
|
901 |
fun executeEndBlock lines state =
|
|
902 |
let
|
|
903 |
val State {blocks,lineIndent,lineSize} = state
|
|
904 |
|
|
905 |
val (lineSize,blocks) =
|
|
906 |
case blocks of
|
|
907 |
[] => raise Bug "Print.executeEndBlock: no block"
|
|
908 |
| topBlock :: blocks =>
|
|
909 |
let
|
|
910 |
val Block
|
|
911 |
{indent = topIndent,
|
|
912 |
style = topStyle,
|
|
913 |
size = topSize,
|
|
914 |
chunks = topChunks} = topBlock
|
|
915 |
in
|
|
916 |
case topChunks of
|
|
917 |
[] => (lineSize,blocks)
|
|
918 |
| headTopChunks :: tailTopChunks =>
|
|
919 |
let
|
|
920 |
val (lineSize,topSize,topChunks) =
|
|
921 |
case headTopChunks of
|
|
922 |
BreakChunk spaces =>
|
|
923 |
let
|
|
924 |
val lineSize = lineSize - spaces
|
|
925 |
and topSize = topSize - spaces
|
|
926 |
and topChunks = tailTopChunks
|
|
927 |
in
|
|
928 |
(lineSize,topSize,topChunks)
|
|
929 |
end
|
|
930 |
| _ => (lineSize,topSize,topChunks)
|
|
931 |
|
|
932 |
val topBlock =
|
|
933 |
Block
|
|
934 |
{indent = topIndent,
|
|
935 |
style = topStyle,
|
|
936 |
size = topSize,
|
|
937 |
chunks = topChunks}
|
|
938 |
in
|
|
939 |
case blocks of
|
|
940 |
[] => raise Error "Print.executeEndBlock: no block"
|
|
941 |
| block :: blocks =>
|
|
942 |
let
|
|
943 |
val Block {indent,style,size,chunks} = block
|
|
944 |
|
|
945 |
val size = size + topSize
|
|
946 |
|
|
947 |
val chunks = BlockChunk topBlock :: chunks
|
|
948 |
|
|
949 |
val block =
|
|
950 |
Block
|
|
951 |
{indent = indent,
|
|
952 |
style = style,
|
|
953 |
size = size,
|
|
954 |
chunks = chunks}
|
|
955 |
|
|
956 |
val blocks = block :: blocks
|
|
957 |
in
|
|
958 |
(lineSize,blocks)
|
|
959 |
end
|
|
960 |
end
|
|
961 |
end
|
|
962 |
|
|
963 |
val state =
|
|
964 |
State
|
|
965 |
{blocks = blocks,
|
|
966 |
lineIndent = lineIndent,
|
|
967 |
lineSize = lineSize}
|
|
968 |
in
|
|
969 |
(lines,state)
|
|
970 |
end;
|
|
971 |
|
|
972 |
fun executeAddString lineLength s lines state =
|
|
973 |
let
|
|
974 |
val State {blocks,lineIndent,lineSize} = state
|
|
975 |
|
|
976 |
val n = size s
|
|
977 |
|
|
978 |
val blocks =
|
|
979 |
case blocks of
|
|
980 |
[] => raise Bug "Print.executeAddString: no block"
|
|
981 |
| Block {indent,style,size,chunks} :: blocks =>
|
|
982 |
let
|
|
983 |
val size = size + n
|
|
984 |
|
|
985 |
val chunk = StringChunk {size = n, string = s}
|
|
986 |
|
|
987 |
val chunks = chunk :: chunks
|
|
988 |
|
|
989 |
val block =
|
|
990 |
Block
|
|
991 |
{indent = indent,
|
|
992 |
style = style,
|
|
993 |
size = size,
|
|
994 |
chunks = chunks}
|
|
995 |
|
|
996 |
val blocks = block :: blocks
|
|
997 |
in
|
|
998 |
blocks
|
|
999 |
end
|
|
1000 |
|
|
1001 |
val lineSize = lineSize + n
|
|
1002 |
|
|
1003 |
val state =
|
|
1004 |
State
|
|
1005 |
{blocks = blocks,
|
|
1006 |
lineIndent = lineIndent,
|
|
1007 |
lineSize = lineSize}
|
|
1008 |
in
|
|
1009 |
normalize lineLength lines state
|
|
1010 |
end;
|
|
1011 |
|
|
1012 |
fun executeAddBreak lineLength spaces lines state =
|
|
1013 |
let
|
|
1014 |
val State {blocks,lineIndent,lineSize} = state
|
|
1015 |
|
|
1016 |
val (blocks,lineSize) =
|
|
1017 |
case blocks of
|
|
1018 |
[] => raise Bug "Print.executeAddBreak: no block"
|
|
1019 |
| Block {indent,style,size,chunks} :: blocks' =>
|
|
1020 |
case chunks of
|
|
1021 |
[] => (blocks,lineSize)
|
|
1022 |
| chunk :: chunks' =>
|
|
1023 |
let
|
|
1024 |
val spaces =
|
|
1025 |
case style of
|
|
1026 |
BreakingBlock => lineLength + 1
|
|
1027 |
| _ => spaces
|
|
1028 |
|
|
1029 |
val size = size + spaces
|
|
1030 |
|
|
1031 |
val chunks =
|
|
1032 |
case chunk of
|
|
1033 |
BreakChunk k => BreakChunk (k + spaces) :: chunks'
|
|
1034 |
| _ => BreakChunk spaces :: chunks
|
|
1035 |
|
|
1036 |
val block =
|
|
1037 |
Block
|
|
1038 |
{indent = indent,
|
|
1039 |
style = style,
|
|
1040 |
size = size,
|
|
1041 |
chunks = chunks}
|
|
1042 |
|
|
1043 |
val blocks = block :: blocks'
|
|
1044 |
|
|
1045 |
val lineSize = lineSize + spaces
|
|
1046 |
in
|
|
1047 |
(blocks,lineSize)
|
|
1048 |
end
|
|
1049 |
|
|
1050 |
val state =
|
|
1051 |
State
|
|
1052 |
{blocks = blocks,
|
|
1053 |
lineIndent = lineIndent,
|
|
1054 |
lineSize = lineSize}
|
|
1055 |
in
|
|
1056 |
normalize lineLength lines state
|
|
1057 |
end;
|
|
1058 |
|
|
1059 |
fun executeBigBreak lineLength lines state =
|
|
1060 |
executeAddBreak lineLength (lineLength + 1) lines state;
|
|
1061 |
|
|
1062 |
fun executeAddNewline lineLength lines state =
|
|
1063 |
let
|
|
1064 |
val (lines,state) = executeAddString lineLength "" lines state
|
|
1065 |
val (lines,state) = executeBigBreak lineLength lines state
|
|
1066 |
in
|
|
1067 |
executeAddString lineLength "" lines state
|
|
1068 |
end;
|
|
1069 |
|
|
1070 |
fun final lineLength lines state =
|
|
1071 |
let
|
|
1072 |
val lines =
|
|
1073 |
if isFinalState state then lines
|
|
1074 |
else
|
|
1075 |
let
|
|
1076 |
val (lines,state) = executeBigBreak lineLength lines state
|
|
1077 |
|
|
1078 |
(*BasicDebug
|
|
1079 |
val _ = isFinalState state orelse raise Bug "Print.final"
|
|
1080 |
*)
|
|
1081 |
in
|
|
1082 |
lines
|
|
1083 |
end
|
|
1084 |
in
|
|
1085 |
if null lines then Stream.Nil else Stream.singleton lines
|
|
1086 |
end;
|
|
1087 |
in
|
|
1088 |
fun execute {lineLength} =
|
|
1089 |
let
|
|
1090 |
fun advance step state =
|
|
1091 |
let
|
|
1092 |
val lines = []
|
|
1093 |
in
|
|
1094 |
case step of
|
|
1095 |
BeginBlock style_ind => executeBeginBlock style_ind lines state
|
|
1096 |
| EndBlock => executeEndBlock lines state
|
|
1097 |
| AddString s => executeAddString lineLength s lines state
|
|
1098 |
| AddBreak spaces => executeAddBreak lineLength spaces lines state
|
|
1099 |
| AddNewline => executeAddNewline lineLength lines state
|
|
1100 |
end
|
|
1101 |
|
|
1102 |
(*BasicDebug
|
|
1103 |
val advance = fn step => fn state =>
|
|
1104 |
let
|
|
1105 |
val (lines,state) = advance step state
|
|
1106 |
val () = checkState state
|
|
1107 |
in
|
|
1108 |
(lines,state)
|
|
1109 |
end
|
|
1110 |
handle Bug bug =>
|
|
1111 |
raise Bug ("Print.advance: after " ^ ppStepToString step ^
|
|
1112 |
" command:\n" ^ bug)
|
|
1113 |
*)
|
|
1114 |
in
|
|
1115 |
revConcat o Stream.maps advance (final lineLength []) initialState
|
|
1116 |
end;
|
|
1117 |
end;
|
|
1118 |
|
|
1119 |
(* ------------------------------------------------------------------------- *)
|
|
1120 |
(* Executing pretty-printers with a global line length. *)
|
|
1121 |
(* ------------------------------------------------------------------------- *)
|
|
1122 |
|
|
1123 |
val lineLength = ref initialLineLength;
|
|
1124 |
|
|
1125 |
fun toStream ppA a =
|
|
1126 |
Stream.map (fn s => s ^ "\n")
|
|
1127 |
(execute {lineLength = !lineLength} (ppA a));
|
|
1128 |
|
|
1129 |
fun toString ppA a =
|
|
1130 |
case execute {lineLength = !lineLength} (ppA a) of
|
|
1131 |
Stream.Nil => ""
|
|
1132 |
| Stream.Cons (h,t) => Stream.foldl (fn (s,z) => z ^ "\n" ^ s) h (t ());
|
|
1133 |
|
|
1134 |
fun trace ppX nameX x =
|
|
1135 |
Useful.trace (toString (ppOp2 " =" ppString ppX) (nameX,x) ^ "\n");
|
|
1136 |
|
|
1137 |
end
|