src/Pure/General/pretty.ML
author wenzelm
Fri May 05 22:02:46 2000 +0200 (2000-05-05)
changeset 8806 a202293db3f6
parent 8456 8ccda76f07cb
child 9121 25245986e667
permissions -rw-r--r--
GPLed;
wenzelm@6118
     1
(*  Title:      Pure/General/pretty.ML
wenzelm@6116
     2
    ID:         $Id$
wenzelm@8806
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@8806
     4
    Author:	Markus Wenzel, TU Munich
wenzelm@8806
     5
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6116
     6
wenzelm@6116
     7
Generic pretty printing module.
wenzelm@6116
     8
wenzelm@6116
     9
Loosely based on
wenzelm@6116
    10
  D. C. Oppen, "Pretty Printing",
wenzelm@6116
    11
  ACM Transactions on Programming Languages and Systems (1980), 465-483.
wenzelm@6116
    12
wenzelm@6116
    13
The object to be printed is given as a tree with indentation and line
wenzelm@6116
    14
breaking information.  A "break" inserts a newline if the text until
wenzelm@6116
    15
the next break is too long to fit on the current line.  After the newline,
wenzelm@6116
    16
text is indented to the level of the enclosing block.  Normally, if a block
wenzelm@6116
    17
is broken then all enclosing blocks will also be broken.  Only "inconsistent
wenzelm@6116
    18
breaks" are provided.
wenzelm@6116
    19
wenzelm@6116
    20
The stored length of a block is used in breakdist (to treat each inner block as
wenzelm@6116
    21
a unit for breaking).
wenzelm@6116
    22
*)
wenzelm@6116
    23
wenzelm@6116
    24
type pprint_args = (string -> unit) * (int -> unit) * (int -> unit) *
wenzelm@6116
    25
  (unit -> unit) * (unit -> unit);
wenzelm@6116
    26
wenzelm@6116
    27
signature PRETTY =
wenzelm@6116
    28
  sig
wenzelm@6116
    29
  type T
wenzelm@8456
    30
  val raw_str: string * real -> T
wenzelm@6116
    31
  val str: string -> T
wenzelm@6116
    32
  val brk: int -> T
wenzelm@6116
    33
  val fbrk: T
wenzelm@6116
    34
  val blk: int * T list -> T
wenzelm@6116
    35
  val quote: T -> T
wenzelm@6116
    36
  val commas: T list -> T list
wenzelm@6116
    37
  val breaks: T list -> T list
wenzelm@6116
    38
  val fbreaks: T list -> T list
wenzelm@6116
    39
  val block: T list -> T
wenzelm@6116
    40
  val strs: string list -> T
wenzelm@6116
    41
  val enclose: string -> string -> T list -> T
wenzelm@6116
    42
  val list: string -> string -> T list -> T
wenzelm@6116
    43
  val str_list: string -> string -> string list -> T
wenzelm@6116
    44
  val big_list: string -> T list -> T
wenzelm@8456
    45
  val chunks: T list -> T
wenzelm@6116
    46
  val string_of: T -> string
wenzelm@6116
    47
  val writeln: T -> unit
wenzelm@6116
    48
  val str_of: T -> string
wenzelm@6116
    49
  val pprint: T -> pprint_args -> unit
wenzelm@6116
    50
  val setdepth: int -> unit
wenzelm@6116
    51
  val setmargin: int -> unit
wenzelm@6321
    52
  val setmp_margin: int -> ('a -> 'b) -> 'a -> 'b
wenzelm@6116
    53
  end;
wenzelm@6116
    54
wenzelm@6116
    55
structure Pretty : PRETTY =
wenzelm@6116
    56
struct
wenzelm@6116
    57
wenzelm@6116
    58
(*printing items: compound phrases, strings, and breaks*)
wenzelm@6116
    59
datatype T =
wenzelm@6116
    60
  Block of T list * int * int |         (*body, indentation, length*)
wenzelm@6116
    61
  String of string * int |              (*text, length*)
wenzelm@6116
    62
  Break of bool * int;                  (*mandatory flag, width if not taken*);
wenzelm@6116
    63
wenzelm@6116
    64
(*Add the lengths of the expressions until the next Break; if no Break then
wenzelm@6116
    65
  include "after", to account for text following this block. *)
wenzelm@6116
    66
fun breakdist (Block(_,_,len)::es, after) = len + breakdist(es, after)
wenzelm@6116
    67
  | breakdist (String (s, len) :: es, after) = len + breakdist (es, after)
wenzelm@6116
    68
  | breakdist (Break _ :: es, after) = 0
wenzelm@6116
    69
  | breakdist ([], after) = after;
wenzelm@6116
    70
wenzelm@6116
    71
fun repstring a 0 = ""
wenzelm@6116
    72
  | repstring a 1 = a
wenzelm@6116
    73
  | repstring a k =
wenzelm@6116
    74
      if k mod 2 = 0 then repstring(a^a) (k div 2)
wenzelm@6116
    75
                     else repstring(a^a) (k div 2) ^ a;
wenzelm@6116
    76
wenzelm@6116
    77
(*** Type for lines of text: string, # of lines, position on line ***)
wenzelm@6116
    78
wenzelm@8456
    79
type text = {tx: Buffer.T, nl: int, pos: int};
wenzelm@6116
    80
wenzelm@8456
    81
val emptytext = {tx = Buffer.empty, nl = 0, pos = 0};
wenzelm@6116
    82
wenzelm@8456
    83
fun blanks wd {tx, nl, pos} =
wenzelm@8456
    84
    {tx  = Buffer.add (Symbol.output (repstring " " wd)) tx,
wenzelm@6116
    85
     nl  = nl,
wenzelm@6116
    86
     pos = pos+wd};
wenzelm@6116
    87
wenzelm@8456
    88
fun newline {tx, nl, pos} =
wenzelm@8456
    89
    {tx  = Buffer.add (Symbol.output "\n") tx,
wenzelm@8456
    90
     nl  = nl + 1,
wenzelm@6116
    91
     pos = 0};
wenzelm@6116
    92
wenzelm@8456
    93
fun string s len {tx, nl, pos:int} =
wenzelm@8456
    94
    {tx  = Buffer.add s tx,
wenzelm@6116
    95
     nl  = nl,
wenzelm@6116
    96
     pos = pos + len};
wenzelm@6116
    97
wenzelm@6116
    98
wenzelm@6116
    99
(*** Formatting ***)
wenzelm@6116
   100
wenzelm@6116
   101
(* margin *)
wenzelm@6116
   102
wenzelm@6321
   103
fun make_margin_info m =
wenzelm@6321
   104
 {margin = m,                   (*right margin, or page width*)
wenzelm@6321
   105
  breakgain = m div 20,         (*minimum added space required of a break*)
wenzelm@6321
   106
  emergencypos = m div 2};      (*position too far to right*)
wenzelm@6116
   107
wenzelm@6321
   108
val margin_info = ref (make_margin_info 76);
wenzelm@6321
   109
fun setmargin m = margin_info := make_margin_info m;
wenzelm@6321
   110
fun setmp_margin m f = setmp margin_info (make_margin_info m) f;
wenzelm@6116
   111
wenzelm@6116
   112
(*Search for the next break (at this or higher levels) and force it to occur*)
wenzelm@6116
   113
fun forcenext [] = []
wenzelm@6116
   114
  | forcenext (Break(_,wd) :: es) = Break(true,0) :: es
wenzelm@6116
   115
  | forcenext (e :: es) = e :: forcenext es;
wenzelm@6116
   116
wenzelm@6116
   117
(*es is list of expressions to print;
wenzelm@6116
   118
  blockin is the indentation of the current block;
wenzelm@6116
   119
  after is the width of the following context until next break. *)
wenzelm@6116
   120
fun format ([], _, _) text = text
wenzelm@6116
   121
  | format (e::es, blockin, after) (text as {pos,nl,...}) =
wenzelm@6116
   122
    (case e of
wenzelm@6116
   123
       Block(bes,indent,wd) =>
wenzelm@6321
   124
         let val blockin' = (pos + indent) mod #emergencypos (! margin_info)
wenzelm@6116
   125
             val btext = format(bes, blockin', breakdist(es,after)) text
wenzelm@6116
   126
             (*If this block was broken then force the next break.*)
wenzelm@6116
   127
             val es2 = if nl < #nl(btext) then forcenext es else es
wenzelm@6116
   128
         in  format (es2,blockin,after) btext  end
wenzelm@6116
   129
     | String (s, len) => format (es,blockin,after) (string s len text)
wenzelm@6116
   130
     | Break(force,wd) => (*no break if text to next break fits on this line
wenzelm@6116
   131
                            or if breaking would add only breakgain to space *)
wenzelm@6116
   132
           format (es,blockin,after)
wenzelm@6116
   133
               (if not force andalso
wenzelm@6321
   134
                   pos+wd <= Int.max(#margin (! margin_info) - breakdist(es,after),
wenzelm@6321
   135
                                     blockin + #breakgain (! margin_info))
wenzelm@6116
   136
                then blanks wd text  (*just insert wd blanks*)
wenzelm@6116
   137
                else blanks blockin (newline text)));
wenzelm@6116
   138
wenzelm@6116
   139
wenzelm@6116
   140
(*** Exported functions to create formatting expressions ***)
wenzelm@6116
   141
wenzelm@6116
   142
fun length (Block (_, _, len)) = len
wenzelm@6116
   143
  | length (String (_, len)) = len
wenzelm@6116
   144
  | length (Break(_, wd)) = wd;
wenzelm@6116
   145
wenzelm@8456
   146
fun raw_str (s, len) = String (s, Real.round len);
wenzelm@8456
   147
val str = String o apsnd Real.round o Symbol.output_width;
wenzelm@6116
   148
wenzelm@6116
   149
fun brk wd = Break (false, wd);
wenzelm@6116
   150
val fbrk = Break (true, 0);
wenzelm@6116
   151
wenzelm@6116
   152
fun blk (indent, es) =
wenzelm@6116
   153
  let
wenzelm@6116
   154
    fun sum([], k) = k
wenzelm@6116
   155
      | sum(e :: es, k) = sum (es, length e + k);
wenzelm@6116
   156
  in Block (es, indent, sum (es, 0)) end;
wenzelm@6116
   157
wenzelm@6116
   158
wenzelm@6116
   159
(* utils *)
wenzelm@6116
   160
wenzelm@6116
   161
fun quote prt =
wenzelm@6116
   162
  blk (1, [str "\"", prt, str "\""]);
wenzelm@6116
   163
wenzelm@6116
   164
fun commas prts =
wenzelm@6116
   165
  flat (separate [str ",", brk 1] (map (fn x => [x]) prts));
wenzelm@6116
   166
wenzelm@6116
   167
fun breaks prts = separate (brk 1) prts;
wenzelm@6116
   168
fun fbreaks prts = separate fbrk prts;
wenzelm@6116
   169
wenzelm@6116
   170
fun block prts = blk (2, prts);
wenzelm@6116
   171
wenzelm@6116
   172
val strs = block o breaks o (map str);
wenzelm@6116
   173
wenzelm@6116
   174
fun enclose lpar rpar prts =
wenzelm@6116
   175
  block (str lpar :: (prts @ [str rpar]));
wenzelm@6116
   176
wenzelm@8456
   177
fun list lpar rpar prts = enclose lpar rpar (commas prts);
wenzelm@8456
   178
fun str_list lpar rpar strs = list lpar rpar (map str strs);
wenzelm@8456
   179
fun big_list name prts = block (fbreaks (str name :: prts));
wenzelm@8456
   180
fun chunks prts = blk (0, fbreaks prts);
wenzelm@6116
   181
wenzelm@6116
   182
wenzelm@6116
   183
(*** Pretty printing with depth limitation ***)
wenzelm@6116
   184
wenzelm@6116
   185
val depth       = ref 0;        (*maximum depth; 0 means no limit*)
wenzelm@6116
   186
wenzelm@6116
   187
fun setdepth dp = (depth := dp);
wenzelm@6116
   188
wenzelm@6116
   189
(*Recursively prune blocks, discarding all text exceeding depth dp*)
wenzelm@6116
   190
fun pruning dp (Block(bes,indent,wd)) =
wenzelm@6116
   191
      if dp>0 then blk(indent, map (pruning(dp-1)) bes)
wenzelm@6116
   192
              else str "..."
wenzelm@6116
   193
  | pruning dp e = e;
wenzelm@6116
   194
wenzelm@6116
   195
fun prune dp e = if dp>0 then pruning dp e else e;
wenzelm@6116
   196
wenzelm@6116
   197
wenzelm@8456
   198
fun string_of e = Buffer.content (#tx (format ([prune (! depth) e], 0, 0) emptytext));
wenzelm@6116
   199
wenzelm@6116
   200
val writeln = writeln o string_of;
wenzelm@6116
   201
wenzelm@6116
   202
wenzelm@6116
   203
(*Create a single flat string: no line breaking*)
wenzelm@6116
   204
fun str_of prt =
wenzelm@6116
   205
  let
wenzelm@6116
   206
    fun s_of (Block (prts, _, _)) = implode (map s_of prts)
wenzelm@6116
   207
      | s_of (String (s, _)) = s
wenzelm@8456
   208
      | s_of (Break (false, wd)) = Symbol.output (repstring " " wd)
wenzelm@8456
   209
      | s_of (Break (true, _)) = Symbol.output " ";
wenzelm@6321
   210
  in s_of (prune (! depth) prt) end;
wenzelm@6116
   211
wenzelm@8456
   212
(*part of the interface to the ML toplevel pretty printers*)
wenzelm@6116
   213
fun pprint prt (put_str, begin_blk, put_brk, put_fbrk, end_blk) =
wenzelm@6116
   214
  let
wenzelm@6116
   215
    fun pp (Block (prts, ind, _)) = (begin_blk ind; pp_lst prts; end_blk ())
wenzelm@6116
   216
      | pp (String (s, _)) = put_str s
wenzelm@6116
   217
      | pp (Break (false, wd)) = put_brk wd
wenzelm@6116
   218
      | pp (Break (true, _)) = put_fbrk ()
wenzelm@6116
   219
    and pp_lst [] = ()
wenzelm@6116
   220
      | pp_lst (prt :: prts) = (pp prt; pp_lst prts);
wenzelm@6116
   221
  in
wenzelm@6116
   222
    pp (prune (! depth) prt)
wenzelm@6116
   223
  end;
wenzelm@6116
   224
wenzelm@6116
   225
wenzelm@6116
   226
end;