adamc@1
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@1
|
2 * All rights reserved.
|
adamc@1
|
3 *
|
adamc@1
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@1
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@1
|
6 *
|
adamc@1
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@1
|
8 * this list of conditions and the following disclaimer.
|
adamc@1
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@1
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@1
|
11 * and/or other materials provided with the distribution.
|
adamc@1
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@1
|
13 * derived from this software without specific prior written permission.
|
adamc@1
|
14 *
|
adamc@1
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@1
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@1
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@1
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@1
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@1
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@1
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@1
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@1
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@1
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@1
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@1
|
26 *)
|
adamc@1
|
27
|
adamc@1
|
28 (* Pretty-printing Laconic/Web *)
|
adamc@1
|
29
|
adamc@1
|
30 structure Print :> PRINT = struct
|
adamc@1
|
31
|
adamc@1
|
32 structure SM = TextIOPP
|
adamc@1
|
33 structure PD = PPDescFn(SM)
|
adamc@1
|
34
|
adamc@1
|
35 type 'a printer = 'a -> PD.pp_desc
|
adamc@1
|
36
|
adamc@1
|
37 fun box ds = PD.hovBox (PD.PPS.Rel 1, ds)
|
adamc@1
|
38 fun parenIf b d =
|
adamc@1
|
39 if b then
|
adamc@1
|
40 box [PD.string "(", d, PD.string ")"]
|
adamc@1
|
41 else
|
adamc@1
|
42 d
|
adamc@1
|
43 val space = PD.space 1
|
adamc@1
|
44
|
adamc@1
|
45 val out = SM.openOut {dst = TextIO.stdOut, wid = 70}
|
adamc@1
|
46 val err = SM.openOut {dst = TextIO.stdErr, wid = 70}
|
adamc@1
|
47
|
adamc@1
|
48 fun p_list_sep sep f ls =
|
adamc@1
|
49 case ls of
|
adamc@1
|
50 [] => PD.string ""
|
adamc@1
|
51 | [x] => f x
|
adamc@1
|
52 | x :: rest =>
|
adamc@1
|
53 let
|
adamc@1
|
54 val tokens = foldr (fn (x, tokens) =>
|
adamc@1
|
55 sep :: f x :: tokens)
|
adamc@1
|
56 [] rest
|
adamc@1
|
57 in
|
adamc@1
|
58 box (f x :: tokens)
|
adamc@1
|
59 end
|
adamc@1
|
60 fun p_list f = p_list_sep (box [PD.string ",", space]) f
|
adamc@1
|
61
|
adamc@1
|
62 fun fprint f d = (PD.description (f, d);
|
adamc@1
|
63 PD.PPS.flushStream f)
|
adamc@1
|
64 val print = fprint out
|
adamc@1
|
65 val eprint = fprint err
|
adamc@1
|
66
|
adamc@1
|
67 fun fpreface f (s, d) =
|
adamc@1
|
68 fprint f (PD.hovBox (PD.PPS.Rel 0,
|
adamc@1
|
69 [PD.string s, PD.space 1, d]))
|
adamc@1
|
70
|
adamc@1
|
71 val preface = fpreface out
|
adamc@1
|
72 val epreface = fpreface err
|
adamc@1
|
73
|
adamc@1
|
74 fun fprefaces f ls =
|
adamc@1
|
75 let
|
adamc@1
|
76 val len = foldl (fn ((s, _), best) =>
|
adamc@1
|
77 Int.max (size s, best)) 0 ls
|
adamc@1
|
78 in
|
adamc@1
|
79 app (fn (s, d) =>
|
adamc@1
|
80 let
|
adamc@1
|
81 val s = CharVector.tabulate (len - size s,
|
adamc@1
|
82 fn _ => #" ")
|
adamc@1
|
83 ^ s ^ ": "
|
adamc@1
|
84 in
|
adamc@1
|
85 fpreface f (s, d)
|
adamc@1
|
86 end) ls
|
adamc@1
|
87 end
|
adamc@1
|
88
|
adamc@1
|
89 val prefaces = fprefaces out
|
adamc@1
|
90 val eprefaces = fprefaces err
|
adamc@1
|
91
|
adamc@1
|
92 end
|