comparison src/print.sml @ 1:4202f6eda946

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