src/Pure/General/pretty.ML
author wenzelm
Sat Dec 08 14:42:45 2001 +0100 (2001-12-08)
changeset 12421 54c06c1f88b8
parent 10952 b520e4f1313b
child 14832 6589a58f57cb
permissions -rw-r--r--
added writelns;
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@10952
     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@9730
    46
  val indent: int -> T -> T
wenzelm@6116
    47
  val string_of: T -> string
wenzelm@6116
    48
  val writeln: T -> unit
wenzelm@12421
    49
  val writelns: T list -> unit
wenzelm@6116
    50
  val str_of: T -> string
wenzelm@6116
    51
  val pprint: T -> pprint_args -> unit
wenzelm@6116
    52
  val setdepth: int -> unit
wenzelm@6116
    53
  val setmargin: int -> unit
wenzelm@6321
    54
  val setmp_margin: int -> ('a -> 'b) -> 'a -> 'b
wenzelm@6116
    55
  end;
wenzelm@6116
    56
wenzelm@6116
    57
structure Pretty : PRETTY =
wenzelm@6116
    58
struct
wenzelm@6116
    59
wenzelm@10952
    60
wenzelm@10952
    61
(** printing items: compound phrases, strings, and breaks **)
wenzelm@10952
    62
wenzelm@6116
    63
datatype T =
wenzelm@6116
    64
  Block of T list * int * int |         (*body, indentation, length*)
wenzelm@6116
    65
  String of string * int |              (*text, length*)
wenzelm@6116
    66
  Break of bool * int;                  (*mandatory flag, width if not taken*);
wenzelm@6116
    67
wenzelm@9730
    68
wenzelm@6116
    69
wenzelm@10952
    70
(** output text **)
wenzelm@10952
    71
wenzelm@10952
    72
val output_spaces = Symbol.output o Symbol.spaces;
wenzelm@10952
    73
val add_indent = Buffer.add o output_spaces;
wenzelm@10952
    74
fun set_indent wd = Buffer.empty |> add_indent wd;
wenzelm@6116
    75
wenzelm@10952
    76
val empty =
wenzelm@10952
    77
 {tx = Buffer.empty,
wenzelm@10952
    78
  ind = Buffer.empty,
wenzelm@10952
    79
  pos = 0,
wenzelm@10952
    80
  nl = 0};
wenzelm@6116
    81
wenzelm@10952
    82
fun newline {tx, ind, pos, nl} =
wenzelm@10952
    83
 {tx = Buffer.add (Symbol.output "\n") tx,
wenzelm@10952
    84
  ind = Buffer.empty,
wenzelm@10952
    85
  pos = 0,
wenzelm@10952
    86
  nl = nl + 1};
wenzelm@6116
    87
wenzelm@10952
    88
fun string (s, len) {tx, ind, pos: int, nl} =
wenzelm@10952
    89
 {tx = Buffer.add s tx,
wenzelm@10952
    90
  ind = Buffer.add s ind,
wenzelm@10952
    91
  pos = pos + len,
wenzelm@10952
    92
  nl = nl};
wenzelm@10952
    93
wenzelm@10952
    94
fun blanks wd = string (output_spaces wd, wd);
wenzelm@6116
    95
wenzelm@10952
    96
fun indentation (buf, len) {tx, ind, pos, nl} =
wenzelm@10952
    97
  let val s = Buffer.content buf in
wenzelm@10952
    98
   {tx = Buffer.add (Symbol.indent (s, len)) tx,
wenzelm@10952
    99
    ind = Buffer.add s ind,
wenzelm@10952
   100
    pos = pos + len,
wenzelm@10952
   101
    nl = nl}
wenzelm@10952
   102
  end;
wenzelm@6116
   103
wenzelm@6116
   104
wenzelm@10952
   105
wenzelm@10952
   106
(** formatting **)
wenzelm@6116
   107
wenzelm@6116
   108
(* margin *)
wenzelm@6116
   109
wenzelm@6321
   110
fun make_margin_info m =
wenzelm@6321
   111
 {margin = m,                   (*right margin, or page width*)
wenzelm@6321
   112
  breakgain = m div 20,         (*minimum added space required of a break*)
wenzelm@6321
   113
  emergencypos = m div 2};      (*position too far to right*)
wenzelm@6116
   114
wenzelm@6321
   115
val margin_info = ref (make_margin_info 76);
wenzelm@6321
   116
fun setmargin m = margin_info := make_margin_info m;
wenzelm@6321
   117
fun setmp_margin m f = setmp margin_info (make_margin_info m) f;
wenzelm@6116
   118
wenzelm@10952
   119
wenzelm@10952
   120
(* format *)
wenzelm@10952
   121
wenzelm@10952
   122
(*Add the lengths of the expressions until the next Break; if no Break then
wenzelm@10952
   123
  include "after", to account for text following this block.*)
wenzelm@10952
   124
fun breakdist (Block (_, _, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   125
  | breakdist (String (s, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   126
  | breakdist (Break _ :: es, after) = 0
wenzelm@10952
   127
  | breakdist ([], after) = after;
wenzelm@10952
   128
wenzelm@10952
   129
(*Search for the next break (at this or higher levels) and force it to occur.*)
wenzelm@6116
   130
fun forcenext [] = []
wenzelm@10952
   131
  | forcenext (Break (_, wd) :: es) = Break (true, 0) :: es
wenzelm@6116
   132
  | forcenext (e :: es) = e :: forcenext es;
wenzelm@6116
   133
wenzelm@6116
   134
(*es is list of expressions to print;
wenzelm@6116
   135
  blockin is the indentation of the current block;
wenzelm@10952
   136
  after is the width of the following context until next break.*)
wenzelm@6116
   137
fun format ([], _, _) text = text
wenzelm@10952
   138
  | format (e :: es, block as (blockind, blockin), after) (text as {ind, pos, nl, ...}) =
wenzelm@10952
   139
      (case e of
wenzelm@10952
   140
        Block (bes, indent, wd) =>
wenzelm@10952
   141
          let
wenzelm@10952
   142
            val {emergencypos, ...} = ! margin_info;
wenzelm@10952
   143
            val pos' = pos + indent;
wenzelm@10952
   144
            val pos'' = pos' mod emergencypos;
wenzelm@10952
   145
            val block' =
wenzelm@10952
   146
              if pos' < emergencypos then (ind |> add_indent indent, pos')
wenzelm@10952
   147
              else (set_indent pos'', pos'');
wenzelm@10952
   148
            val btext = format (bes, block', breakdist (es, after)) text;
wenzelm@10952
   149
            (*if this block was broken then force the next break*)
wenzelm@10952
   150
            val es2 = if nl < #nl btext then forcenext es else es;
wenzelm@10952
   151
          in format (es2, block, after) btext end
wenzelm@10952
   152
      | String str => format (es, block, after) (string str text)
wenzelm@10952
   153
      | Break (force, wd) =>
wenzelm@10952
   154
          let val {margin, breakgain, ...} = ! margin_info in
wenzelm@10952
   155
            (*no break if text to next break fits on this line
wenzelm@10952
   156
              or if breaking would add only breakgain to space*)
wenzelm@10952
   157
            format (es, block, after)
wenzelm@10952
   158
              (if not force andalso
wenzelm@10952
   159
                  pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain)
wenzelm@10952
   160
                then text |> blanks wd  (*just insert wd blanks*)
wenzelm@10952
   161
                else text |> newline |> indentation block)
wenzelm@10952
   162
          end);
wenzelm@6116
   163
wenzelm@6116
   164
wenzelm@6116
   165
(*** Exported functions to create formatting expressions ***)
wenzelm@6116
   166
wenzelm@6116
   167
fun length (Block (_, _, len)) = len
wenzelm@6116
   168
  | length (String (_, len)) = len
wenzelm@6116
   169
  | length (Break(_, wd)) = wd;
wenzelm@6116
   170
wenzelm@8456
   171
fun raw_str (s, len) = String (s, Real.round len);
wenzelm@8456
   172
val str = String o apsnd Real.round o Symbol.output_width;
wenzelm@6116
   173
wenzelm@6116
   174
fun brk wd = Break (false, wd);
wenzelm@9121
   175
val fbrk = Break (true, 2);
wenzelm@6116
   176
wenzelm@6116
   177
fun blk (indent, es) =
wenzelm@6116
   178
  let
wenzelm@6116
   179
    fun sum([], k) = k
wenzelm@6116
   180
      | sum(e :: es, k) = sum (es, length e + k);
wenzelm@6116
   181
  in Block (es, indent, sum (es, 0)) end;
wenzelm@6116
   182
wenzelm@6116
   183
wenzelm@6116
   184
(* utils *)
wenzelm@6116
   185
wenzelm@6116
   186
fun quote prt =
wenzelm@6116
   187
  blk (1, [str "\"", prt, str "\""]);
wenzelm@6116
   188
wenzelm@6116
   189
fun commas prts =
wenzelm@6116
   190
  flat (separate [str ",", brk 1] (map (fn x => [x]) prts));
wenzelm@6116
   191
wenzelm@6116
   192
fun breaks prts = separate (brk 1) prts;
wenzelm@6116
   193
fun fbreaks prts = separate fbrk prts;
wenzelm@6116
   194
wenzelm@6116
   195
fun block prts = blk (2, prts);
wenzelm@6116
   196
wenzelm@9730
   197
val strs = block o breaks o map str;
wenzelm@6116
   198
wenzelm@6116
   199
fun enclose lpar rpar prts =
wenzelm@6116
   200
  block (str lpar :: (prts @ [str rpar]));
wenzelm@6116
   201
wenzelm@8456
   202
fun list lpar rpar prts = enclose lpar rpar (commas prts);
wenzelm@8456
   203
fun str_list lpar rpar strs = list lpar rpar (map str strs);
wenzelm@8456
   204
fun big_list name prts = block (fbreaks (str name :: prts));
wenzelm@8456
   205
fun chunks prts = blk (0, fbreaks prts);
wenzelm@6116
   206
wenzelm@9730
   207
fun indent 0 prt = prt
wenzelm@10952
   208
  | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
wenzelm@9730
   209
wenzelm@6116
   210
wenzelm@6116
   211
(*** Pretty printing with depth limitation ***)
wenzelm@6116
   212
wenzelm@6116
   213
val depth       = ref 0;        (*maximum depth; 0 means no limit*)
wenzelm@6116
   214
wenzelm@6116
   215
fun setdepth dp = (depth := dp);
wenzelm@6116
   216
wenzelm@6116
   217
(*Recursively prune blocks, discarding all text exceeding depth dp*)
wenzelm@6116
   218
fun pruning dp (Block(bes,indent,wd)) =
wenzelm@6116
   219
      if dp>0 then blk(indent, map (pruning(dp-1)) bes)
wenzelm@6116
   220
              else str "..."
wenzelm@6116
   221
  | pruning dp e = e;
wenzelm@6116
   222
wenzelm@10952
   223
fun prune dp e = if dp > 0 then pruning dp e else e;
wenzelm@6116
   224
wenzelm@10952
   225
fun string_of e = Buffer.content (#tx (format ([prune (! depth) e], (Buffer.empty, 0), 0) empty));
wenzelm@6116
   226
val writeln = writeln o string_of;
wenzelm@12421
   227
fun writelns [] = () | writelns es = writeln (chunks es);
wenzelm@6116
   228
wenzelm@6116
   229
wenzelm@6116
   230
(*Create a single flat string: no line breaking*)
wenzelm@6116
   231
fun str_of prt =
wenzelm@6116
   232
  let
wenzelm@6116
   233
    fun s_of (Block (prts, _, _)) = implode (map s_of prts)
wenzelm@6116
   234
      | s_of (String (s, _)) = s
wenzelm@10952
   235
      | s_of (Break (false, wd)) = output_spaces wd
wenzelm@10952
   236
      | s_of (Break (true, _)) = output_spaces 1;
wenzelm@6321
   237
  in s_of (prune (! depth) prt) end;
wenzelm@6116
   238
wenzelm@8456
   239
(*part of the interface to the ML toplevel pretty printers*)
wenzelm@6116
   240
fun pprint prt (put_str, begin_blk, put_brk, put_fbrk, end_blk) =
wenzelm@6116
   241
  let
wenzelm@6116
   242
    fun pp (Block (prts, ind, _)) = (begin_blk ind; pp_lst prts; end_blk ())
wenzelm@6116
   243
      | pp (String (s, _)) = put_str s
wenzelm@6116
   244
      | pp (Break (false, wd)) = put_brk wd
wenzelm@6116
   245
      | pp (Break (true, _)) = put_fbrk ()
wenzelm@6116
   246
    and pp_lst [] = ()
wenzelm@6116
   247
      | pp_lst (prt :: prts) = (pp prt; pp_lst prts);
wenzelm@6116
   248
  in
wenzelm@6116
   249
    pp (prune (! depth) prt)
wenzelm@6116
   250
  end;
wenzelm@6116
   251
wenzelm@6116
   252
wenzelm@6116
   253
end;