annotate src/print.sml @ 247:5c50b17f5e4a

Corify tables
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 09:00:28 -0400
parents 71bafe66dbe1
children 73456bfde988
rev   line source
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@244 28 (* Generic printing support code *)
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@223 55 sep :: PD.cut :: 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@5 69 [PD.string s, PD.space 1, d, PD.newline]))
adamc@1 70
adamc@1 71 val preface = fpreface out
adamc@1 72 val epreface = fpreface err
adamc@1 73
adamc@3 74 fun fprefaces f s ls =
adamc@3 75 let
adamc@3 76 val len = foldl (fn ((s, _), best) =>
adamc@3 77 Int.max (size s, best)) 0 ls
adamc@3 78 in
adamc@3 79 fprint f (PD.string s);
adamc@3 80 fprint f PD.newline;
adamc@3 81 app (fn (s, d) =>
adamc@3 82 let
adamc@3 83 val s = CharVector.tabulate (len - size s,
adamc@3 84 fn _ => #" ")
adamc@3 85 ^ s ^ ": "
adamc@3 86 in
adamc@3 87 fpreface f (s, d)
adamc@3 88 end) ls
adamc@3 89 end
adamc@3 90
adamc@3 91 val prefaces = fprefaces out
adamc@3 92 val eprefaces = fprefaces err
adamc@3 93
adamc@3 94 fun fprefaces' f ls =
adamc@1 95 let
adamc@1 96 val len = foldl (fn ((s, _), best) =>
adamc@1 97 Int.max (size s, best)) 0 ls
adamc@1 98 in
adamc@1 99 app (fn (s, d) =>
adamc@1 100 let
adamc@1 101 val s = CharVector.tabulate (len - size s,
adamc@1 102 fn _ => #" ")
adamc@1 103 ^ s ^ ": "
adamc@1 104 in
adamc@1 105 fpreface f (s, d)
adamc@1 106 end) ls
adamc@1 107 end
adamc@1 108
adamc@3 109 val prefaces' = fprefaces' out
adamc@3 110 val eprefaces' = fprefaces' err
adamc@1 111
adamc@1 112 end