annotate src/print.sml @ 1579:e1f4ac9ca34b

Note a parsing restriction in the manual
author Adam Chlipala <adam@chlipala.net>
date Fri, 21 Oct 2011 08:36:10 -0400
parents fd34210bc3e5
children
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
adam@1362 35 val openOut = SM.openOut
adam@1362 36
adamc@1 37 type 'a printer = 'a -> PD.pp_desc
adamc@1 38
adamc@1 39 fun box ds = PD.hovBox (PD.PPS.Rel 1, ds)
adamc@1 40 fun parenIf b d =
adamc@1 41 if b then
adamc@1 42 box [PD.string "(", d, PD.string ")"]
adamc@1 43 else
adamc@1 44 d
adamc@1 45 val space = PD.space 1
adamc@1 46
adamc@1 47 val out = SM.openOut {dst = TextIO.stdOut, wid = 70}
adamc@1 48 val err = SM.openOut {dst = TextIO.stdErr, wid = 70}
adamc@1 49
adamc@1 50 fun p_list_sep sep f ls =
adamc@1 51 case ls of
adamc@1 52 [] => PD.string ""
adamc@1 53 | [x] => f x
adamc@1 54 | x :: rest =>
adamc@1 55 let
adamc@1 56 val tokens = foldr (fn (x, tokens) =>
adamc@223 57 sep :: PD.cut :: f x :: tokens)
adamc@1 58 [] rest
adamc@1 59 in
adamc@1 60 box (f x :: tokens)
adamc@1 61 end
adamc@1 62 fun p_list f = p_list_sep (box [PD.string ",", space]) f
adamc@1 63
adamc@275 64 fun p_list_sepi sep f ls =
adamc@275 65 case ls of
adamc@275 66 [] => PD.string ""
adamc@275 67 | [x] => f 0 x
adamc@275 68 | x :: rest =>
adamc@275 69 let
adamc@275 70 val tokens = ListUtil.foldri (fn (n, x, tokens) =>
adamc@275 71 sep :: PD.cut :: f (n + 1) x :: tokens)
adamc@275 72 [] rest
adamc@275 73 in
adamc@275 74 box (f 0 x :: tokens)
adamc@275 75 end
adamc@275 76
adamc@1 77 fun fprint f d = (PD.description (f, d);
adamc@1 78 PD.PPS.flushStream f)
adamc@1 79 val print = fprint out
adamc@1 80 val eprint = fprint err
adamc@1 81
adamc@1 82 fun fpreface f (s, d) =
adamc@1 83 fprint f (PD.hovBox (PD.PPS.Rel 0,
adamc@5 84 [PD.string s, PD.space 1, d, PD.newline]))
adamc@1 85
adamc@1 86 val preface = fpreface out
adamc@1 87 val epreface = fpreface err
adamc@1 88
adamc@3 89 fun fprefaces f s ls =
adamc@3 90 let
adamc@3 91 val len = foldl (fn ((s, _), best) =>
adamc@3 92 Int.max (size s, best)) 0 ls
adamc@3 93 in
adamc@3 94 fprint f (PD.string s);
adamc@3 95 fprint f PD.newline;
adamc@3 96 app (fn (s, d) =>
adamc@3 97 let
adamc@3 98 val s = CharVector.tabulate (len - size s,
adamc@3 99 fn _ => #" ")
adamc@3 100 ^ s ^ ": "
adamc@3 101 in
adamc@3 102 fpreface f (s, d)
adamc@3 103 end) ls
adamc@3 104 end
adamc@3 105
adamc@3 106 val prefaces = fprefaces out
adamc@3 107 val eprefaces = fprefaces err
adamc@3 108
adamc@3 109 fun fprefaces' f ls =
adamc@1 110 let
adamc@1 111 val len = foldl (fn ((s, _), best) =>
adamc@1 112 Int.max (size s, best)) 0 ls
adamc@1 113 in
adamc@1 114 app (fn (s, d) =>
adamc@1 115 let
adamc@1 116 val s = CharVector.tabulate (len - size s,
adamc@1 117 fn _ => #" ")
adamc@1 118 ^ s ^ ": "
adamc@1 119 in
adamc@1 120 fpreface f (s, d)
adamc@1 121 end) ls
adamc@1 122 end
adamc@1 123
adamc@3 124 val prefaces' = fprefaces' out
adamc@3 125 val eprefaces' = fprefaces' err
adamc@1 126
adamc@1 127 end