author | wenzelm |
Sun, 08 Jul 2007 13:10:54 +0200 | |
changeset 23645 | d220d12bd45e |
parent 23638 | 09120c2dd71f |
child 23659 | 4b702ac388d6 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/pretty.ML |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
8806 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
4 |
Author: Markus Wenzel, TU Munich |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
5 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
6 |
Generic pretty printing module. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
7 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
8 |
Loosely based on |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
9 |
D. C. Oppen, "Pretty Printing", |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
10 |
ACM Transactions on Programming Languages and Systems (1980), 465-483. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
11 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
12 |
The object to be printed is given as a tree with indentation and line |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
13 |
breaking information. A "break" inserts a newline if the text until |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
14 |
the next break is too long to fit on the current line. After the newline, |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
15 |
text is indented to the level of the enclosing block. Normally, if a block |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
16 |
is broken then all enclosing blocks will also be broken. Only "inconsistent |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
17 |
breaks" are provided. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
18 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
19 |
The stored length of a block is used in breakdist (to treat each inner block as |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
20 |
a unit for breaking). |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
21 |
*) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
22 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
23 |
type pprint_args = (string -> unit) * (int -> unit) * (int -> unit) * |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
24 |
(unit -> unit) * (unit -> unit); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
25 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
26 |
signature PRETTY = |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
27 |
sig |
23617 | 28 |
val default_indent: string -> int -> string |
29 |
val default_markup: Markup.T -> string * string |
|
23645 | 30 |
val mode_markup: Markup.T -> string * string |
23617 | 31 |
val add_mode: string -> (string -> int -> string) -> (Markup.T -> string * string) -> unit |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
32 |
type T |
23617 | 33 |
val raw_str: string * int -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
34 |
val str: string -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
35 |
val brk: int -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
36 |
val fbrk: T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
37 |
val breaks: T list -> T list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
38 |
val fbreaks: T list -> T list |
23645 | 39 |
val blk: int * T list -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
40 |
val block: T list -> T |
23645 | 41 |
val strs: string list -> T |
23617 | 42 |
val markup: Markup.T -> T list -> T |
43 |
val keyword: string -> T |
|
44 |
val command: string -> T |
|
23638 | 45 |
val markup_chunks: Markup.T -> T list -> T |
18802 | 46 |
val chunks: T list -> T |
19266 | 47 |
val chunks2: T list -> T |
23617 | 48 |
val block_enclose: T * T -> T list -> T |
18802 | 49 |
val quote: T -> T |
50 |
val backquote: T -> T |
|
51 |
val separate: string -> T list -> T list |
|
52 |
val commas: T list -> T list |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
val enclose: string -> string -> T list -> T |
18802 | 54 |
val enum: string -> string -> string -> T list -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
55 |
val list: string -> string -> T list -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
56 |
val str_list: string -> string -> string list -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
57 |
val big_list: string -> T list -> T |
9730 | 58 |
val indent: int -> T -> T |
23645 | 59 |
val unbreakable: T -> T |
60 |
val setmargin: int -> unit |
|
61 |
val setmp_margin: int -> ('a -> 'b) -> 'a -> 'b |
|
62 |
val setdepth: int -> unit |
|
63 |
val pprint: T -> pprint_args -> unit |
|
64 |
val symbolicN: string |
|
16714 | 65 |
val output_buffer: T -> Buffer.T |
14995 | 66 |
val output: T -> string |
23645 | 67 |
val string_of: T -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
68 |
val str_of: T -> string |
23645 | 69 |
val writeln: T -> unit |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
70 |
type pp |
14972 | 71 |
val pp: (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T) -> pp |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
72 |
val term: pp -> term -> T |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
73 |
val typ: pp -> typ -> T |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
74 |
val sort: pp -> sort -> T |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
75 |
val classrel: pp -> class list -> T |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
76 |
val arity: pp -> arity -> T |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
77 |
val string_of_term: pp -> term -> string |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
78 |
val string_of_typ: pp -> typ -> string |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
79 |
val string_of_sort: pp -> sort -> string |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
80 |
val string_of_classrel: pp -> class list -> string |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
81 |
val string_of_arity: pp -> arity -> string |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
82 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
83 |
|
23617 | 84 |
structure Pretty: PRETTY = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
85 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
86 |
|
23617 | 87 |
(** print mode operations **) |
88 |
||
89 |
fun default_indent (_: string) = Symbol.spaces; |
|
90 |
fun default_markup (_: Markup.T) = ("", ""); |
|
91 |
||
92 |
local |
|
93 |
val default = {indent = default_indent, markup = default_markup}; |
|
94 |
val modes = ref (Symtab.make [("", default)]); |
|
95 |
in |
|
96 |
fun add_mode name indent markup = |
|
97 |
change modes (Symtab.update_new (name, {indent = indent, markup = markup})); |
|
98 |
fun get_mode () = |
|
99 |
the_default default (Library.get_first (Symtab.lookup (! modes)) (! print_mode)); |
|
100 |
end; |
|
101 |
||
102 |
fun mode_indent x y = #indent (get_mode ()) x y; |
|
23645 | 103 |
|
104 |
fun mode_markup m = |
|
105 |
if m = Markup.none then ("", "") |
|
106 |
else #markup (get_mode ()) m; |
|
107 |
||
108 |
fun add_markup m add = |
|
109 |
let val (bg, en) = mode_markup m |
|
110 |
in Buffer.add bg #> add #> Buffer.add en end; |
|
111 |
||
112 |
val output_spaces = Output.output o Symbol.spaces; |
|
113 |
val add_indent = Buffer.add o output_spaces; |
|
23617 | 114 |
|
115 |
||
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
116 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
117 |
(** printing items: compound phrases, strings, and breaks **) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
118 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
119 |
datatype T = |
23617 | 120 |
Block of Markup.T * T list * int * int | (*markup, body, indentation, length*) |
121 |
String of string * int | (*text, length*) |
|
122 |
Break of bool * int; (*mandatory flag, width if not taken*) |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
123 |
|
23645 | 124 |
fun length (Block (_, _, _, len)) = len |
125 |
| length (String (_, len)) = len |
|
126 |
| length (Break (_, wd)) = wd; |
|
127 |
||
9730 | 128 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
129 |
|
23645 | 130 |
(** derived operations to create formatting expressions **) |
131 |
||
132 |
val raw_str = String; |
|
133 |
val str = String o Output.output_width; |
|
134 |
||
135 |
fun brk wd = Break (false, wd); |
|
136 |
val fbrk = Break (true, 2); |
|
137 |
||
138 |
fun breaks prts = Library.separate (brk 1) prts; |
|
139 |
fun fbreaks prts = Library.separate fbrk prts; |
|
140 |
||
141 |
fun markup_block m (indent, es) = |
|
142 |
let |
|
143 |
fun sum [] k = k |
|
144 |
| sum (e :: es) k = sum es (length e + k); |
|
145 |
in Block (m, es, indent, sum es 0) end; |
|
146 |
||
147 |
val blk = markup_block Markup.none; |
|
148 |
fun block prts = blk (2, prts); |
|
149 |
val strs = block o breaks o map str; |
|
150 |
||
151 |
fun markup m prts = markup_block m (0, prts); |
|
152 |
fun keyword name = markup (Markup.keyword name) [str name]; |
|
153 |
fun command name = markup (Markup.command name) [str name]; |
|
154 |
||
155 |
fun markup_chunks m prts = markup m (fbreaks prts); |
|
156 |
val chunks = markup_chunks Markup.none; |
|
157 |
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts))); |
|
158 |
||
159 |
fun block_enclose (p1, p2) ps = chunks [(block o fbreaks) (p1 :: ps), p2]; |
|
160 |
||
161 |
fun quote prt = blk (1, [str "\"", prt, str "\""]); |
|
162 |
fun backquote prt = blk (1, [str "`", prt, str "`"]); |
|
163 |
||
164 |
fun separate sep prts = |
|
165 |
flat (Library.separate [str sep, brk 1] (map single prts)); |
|
166 |
||
167 |
val commas = separate ","; |
|
168 |
||
169 |
fun enclose lpar rpar prts = |
|
170 |
block (str lpar :: (prts @ [str rpar])); |
|
171 |
||
172 |
fun enum sep lpar rpar prts = enclose lpar rpar (separate sep prts); |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
173 |
|
23645 | 174 |
val list = enum ","; |
175 |
fun str_list lpar rpar strs = list lpar rpar (map str strs); |
|
176 |
||
177 |
fun big_list name prts = block (fbreaks (str name :: prts)); |
|
178 |
||
179 |
fun indent 0 prt = prt |
|
180 |
| indent n prt = blk (0, [str (Symbol.spaces n), prt]); |
|
181 |
||
182 |
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd) |
|
183 |
| unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd) |
|
184 |
| unbreakable (e as String _) = e; |
|
185 |
||
186 |
||
187 |
||
188 |
(** formatting **) |
|
189 |
||
190 |
(* margin *) |
|
191 |
||
192 |
fun make_margin_info m = |
|
193 |
{margin = m, (*right margin, or page width*) |
|
194 |
breakgain = m div 20, (*minimum added space required of a break*) |
|
195 |
emergencypos = m div 2}; (*position too far to right*) |
|
196 |
||
197 |
val margin_info = ref (make_margin_info 76); |
|
198 |
fun setmargin m = margin_info := make_margin_info m; |
|
199 |
fun setmp_margin m f = setmp margin_info (make_margin_info m) f; |
|
200 |
||
201 |
||
202 |
(* depth limitation *) |
|
203 |
||
204 |
val depth = ref 0; (*maximum depth; 0 means no limit*) |
|
205 |
fun setdepth dp = (depth := dp); |
|
206 |
||
207 |
local |
|
208 |
fun pruning dp (Block (m, bes, indent, wd)) = |
|
209 |
if dp > 0 |
|
210 |
then markup_block m (indent, map (pruning (dp - 1)) bes) |
|
211 |
else str "..." |
|
212 |
| pruning dp e = e |
|
213 |
in |
|
214 |
fun prune e = if ! depth > 0 then pruning (! depth) e else e; |
|
215 |
end; |
|
216 |
||
217 |
||
218 |
(* formatted output *) |
|
219 |
||
220 |
local |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
221 |
|
17756 | 222 |
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int}; |
223 |
||
224 |
val empty: text = |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
225 |
{tx = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
226 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
227 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
228 |
nl = 0}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
229 |
|
17756 | 230 |
fun newline {tx, ind, pos, nl} : text = |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
231 |
{tx = Buffer.add (Output.output "\n") tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
232 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
233 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
234 |
nl = nl + 1}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
235 |
|
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
236 |
fun control s {tx, ind, pos: int, nl} : text = |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
237 |
{tx = Buffer.add s tx, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
238 |
ind = ind, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
239 |
pos = pos, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
240 |
nl = nl}; |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
241 |
|
17756 | 242 |
fun string (s, len) {tx, ind, pos: int, nl} : text = |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
243 |
{tx = Buffer.add s tx, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
244 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
245 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
246 |
nl = nl}; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
247 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
248 |
fun blanks wd = string (output_spaces wd, wd); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
249 |
|
17756 | 250 |
fun indentation (buf, len) {tx, ind, pos, nl} : text = |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
251 |
let val s = Buffer.content buf in |
23617 | 252 |
{tx = Buffer.add (mode_indent s len) tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
253 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
254 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
255 |
nl = nl} |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
256 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
257 |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
258 |
(*Add the lengths of the expressions until the next Break; if no Break then |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
259 |
include "after", to account for text following this block.*) |
23617 | 260 |
fun breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after) |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
261 |
| breakdist (String (s, len) :: es, after) = len + breakdist (es, after) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
262 |
| breakdist (Break _ :: es, after) = 0 |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
263 |
| breakdist ([], after) = after; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
264 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
265 |
(*Search for the next break (at this or higher levels) and force it to occur.*) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
266 |
fun forcenext [] = [] |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
267 |
| forcenext (Break (_, wd) :: es) = Break (true, 0) :: es |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
268 |
| forcenext (e :: es) = e :: forcenext es; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
269 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
270 |
(*es is list of expressions to print; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
271 |
blockin is the indentation of the current block; |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
272 |
after is the width of the following context until next break.*) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
273 |
fun format ([], _, _) text = text |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
274 |
| format (e :: es, block as (blockind, blockin), after) (text as {ind, pos, nl, ...}) = |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
275 |
(case e of |
23617 | 276 |
Block (markup, bes, indent, wd) => |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
277 |
let |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
278 |
val {emergencypos, ...} = ! margin_info; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
279 |
val pos' = pos + indent; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
280 |
val pos'' = pos' mod emergencypos; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
281 |
val block' = |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
282 |
if pos' < emergencypos then (ind |> add_indent indent, pos') |
23645 | 283 |
else (add_indent pos'' Buffer.empty, pos''); |
284 |
val (bg, en) = mode_markup markup; |
|
23617 | 285 |
val btext: text = text |
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
286 |
|> control bg |
23617 | 287 |
|> format (bes, block', breakdist (es, after)) |
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
288 |
|> control en; |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
289 |
(*if this block was broken then force the next break*) |
23617 | 290 |
val es' = if nl < #nl btext then forcenext es else es; |
291 |
in format (es', block, after) btext end |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
292 |
| String str => format (es, block, after) (string str text) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
293 |
| Break (force, wd) => |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
294 |
let val {margin, breakgain, ...} = ! margin_info in |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
295 |
(*no break if text to next break fits on this line |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
296 |
or if breaking would add only breakgain to space*) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
297 |
format (es, block, after) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
298 |
(if not force andalso |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
299 |
pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
300 |
then text |> blanks wd (*just insert wd blanks*) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
301 |
else text |> newline |> indentation block) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
302 |
end); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
303 |
|
23645 | 304 |
in |
19266 | 305 |
|
23645 | 306 |
fun formatted e = #tx (format ([prune e], (Buffer.empty, 0), 0) empty); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
307 |
|
23645 | 308 |
end; |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
309 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
310 |
|
23645 | 311 |
(* special output *) |
18802 | 312 |
|
23645 | 313 |
(*symbolic markup -- no formatting*) |
314 |
fun symbolic prt = |
|
315 |
let |
|
316 |
fun out (Block (m, prts, indent, _)) = |
|
317 |
add_markup m (add_markup (Markup.block indent) (fold out prts)) |
|
318 |
| out (String (s, _)) = Buffer.add s |
|
319 |
| out (Break (false, wd)) = add_markup (Markup.break wd) (Buffer.add (output_spaces wd)) |
|
320 |
| out (Break (true, _)) = add_markup Markup.fbreak I |
|
321 |
in out prt Buffer.empty end; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
322 |
|
23645 | 323 |
(*unformatted output*) |
324 |
fun unformatted prt = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
325 |
let |
23645 | 326 |
fun fmt (Block (m, prts, _, _)) = add_markup m (fold fmt prts) |
23617 | 327 |
| fmt (String (s, _)) = Buffer.add s |
328 |
| fmt (Break (false, wd)) = Buffer.add (output_spaces wd) |
|
329 |
| fmt (Break (true, _)) = Buffer.add (output_spaces 1); |
|
23645 | 330 |
in fmt (prune prt) Buffer.empty end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
331 |
|
23645 | 332 |
(*ML toplevel pretty printing*) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
333 |
fun pprint prt (put_str, begin_blk, put_brk, put_fbrk, end_blk) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
334 |
let |
23617 | 335 |
fun pp (Block (_, prts, ind, _)) = (begin_blk ind; pp_lst prts; end_blk ()) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
336 |
| pp (String (s, _)) = put_str s |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
337 |
| pp (Break (false, wd)) = put_brk wd |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
338 |
| pp (Break (true, _)) = put_fbrk () |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
339 |
and pp_lst [] = () |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
340 |
| pp_lst (prt :: prts) = (pp prt; pp_lst prts); |
23645 | 341 |
in pp (prune prt) end; |
342 |
||
343 |
||
344 |
(* output interfaces *) |
|
345 |
||
346 |
val symbolicN = "pretty_symbolic"; |
|
347 |
||
348 |
fun output_buffer prt = |
|
349 |
if print_mode_active symbolicN then symbolic prt |
|
350 |
else formatted prt; |
|
351 |
||
352 |
val output = Buffer.content o output_buffer; |
|
353 |
val string_of = Output.escape o output; |
|
354 |
val str_of = Output.escape o Buffer.content o unformatted; |
|
355 |
val writeln = Output.writeln o string_of; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
356 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
357 |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
358 |
|
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
359 |
(** abstract pretty printing context **) |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
360 |
|
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
361 |
datatype pp = |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
362 |
PP of (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T); |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
363 |
|
14972 | 364 |
val pp = PP; |
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
365 |
|
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
366 |
fun pp_fun f (PP x) = f x; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
367 |
|
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
368 |
val term = pp_fun #1; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
369 |
val typ = pp_fun #2; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
370 |
val sort = pp_fun #3; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
371 |
val classrel = pp_fun #4; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
372 |
val arity = pp_fun #5; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
373 |
|
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
374 |
val string_of_term = string_of oo term; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
375 |
val string_of_typ = string_of oo typ; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
376 |
val string_of_sort = string_of oo sort; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
377 |
val string_of_classrel = string_of oo classrel; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
378 |
val string_of_arity = string_of oo arity; |
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
379 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
380 |
end; |