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