author | wenzelm |
Mon, 18 Apr 2011 11:13:29 +0200 | |
changeset 42383 | 0ae4ad40d7b5 |
parent 42266 | f87e0be80a3f |
child 43684 | 85388f5570c4 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/pretty.ML |
8806 | 2 |
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
|
3 |
Author: Markus Wenzel, TU Munich |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
4 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
5 |
Generic pretty printing module. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
6 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
7 |
Loosely based on |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
8 |
D. C. Oppen, "Pretty Printing", |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
9 |
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
|
10 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
11 |
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
|
12 |
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
|
13 |
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
|
14 |
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
|
15 |
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
|
16 |
breaks" are provided. |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
17 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
18 |
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
|
19 |
a unit for breaking). |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
20 |
*) |
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 |
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
|
23 |
sig |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
24 |
val default_indent: string -> int -> Output.output |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
25 |
val add_mode: string -> (string -> int -> Output.output) -> unit |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
26 |
type T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
27 |
val str: string -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
28 |
val brk: int -> T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
29 |
val fbrk: T |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
30 |
val breaks: T list -> T list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
31 |
val fbreaks: T list -> T list |
23645 | 32 |
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
|
33 |
val block: T list -> T |
23645 | 34 |
val strs: string list -> T |
23617 | 35 |
val markup: Markup.T -> T list -> T |
26703 | 36 |
val mark: Markup.T -> T -> T |
42266 | 37 |
val mark_str: Markup.T * string -> T |
38 |
val marks_str: Markup.T list * string -> T |
|
23617 | 39 |
val keyword: string -> T |
40 |
val command: string -> T |
|
23638 | 41 |
val markup_chunks: Markup.T -> T list -> T |
18802 | 42 |
val chunks: T list -> T |
19266 | 43 |
val chunks2: T list -> T |
23617 | 44 |
val block_enclose: T * T -> T list -> T |
18802 | 45 |
val quote: T -> T |
46 |
val backquote: T -> T |
|
47 |
val separate: string -> T list -> T list |
|
48 |
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
|
49 |
val enclose: string -> string -> T list -> T |
18802 | 50 |
val enum: string -> string -> string -> T list -> T |
30620 | 51 |
val position: Position.T -> T |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
val big_list: string -> T list -> T |
9730 | 55 |
val indent: int -> T -> T |
23645 | 56 |
val unbreakable: T -> T |
36745 | 57 |
val margin_default: int Unsynchronized.ref |
23645 | 58 |
val symbolicN: string |
36745 | 59 |
val output_buffer: int option -> T -> Buffer.T |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
60 |
val output: int option -> T -> Output.output |
36745 | 61 |
val string_of_margin: int -> T -> string |
23645 | 62 |
val string_of: T -> string |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
63 |
val str_of: T -> string |
23645 | 64 |
val writeln: T -> unit |
36748 | 65 |
val to_ML: T -> ML_Pretty.pretty |
66 |
val from_ML: ML_Pretty.pretty -> T |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
67 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
68 |
|
23617 | 69 |
structure Pretty: PRETTY = |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
70 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
71 |
|
23617 | 72 |
(** print mode operations **) |
73 |
||
74 |
fun default_indent (_: string) = Symbol.spaces; |
|
75 |
||
76 |
local |
|
23698 | 77 |
val default = {indent = default_indent}; |
32738 | 78 |
val modes = Unsynchronized.ref (Symtab.make [("", default)]); |
23617 | 79 |
in |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23787
diff
changeset
|
80 |
fun add_mode name indent = CRITICAL (fn () => |
32738 | 81 |
Unsynchronized.change modes (Symtab.update_new (name, {indent = indent}))); |
23617 | 82 |
fun get_mode () = |
24612 | 83 |
the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ())); |
23617 | 84 |
end; |
85 |
||
86 |
fun mode_indent x y = #indent (get_mode ()) x y; |
|
23645 | 87 |
|
88 |
val output_spaces = Output.output o Symbol.spaces; |
|
89 |
val add_indent = Buffer.add o output_spaces; |
|
23617 | 90 |
|
91 |
||
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
92 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
93 |
(** printing items: compound phrases, strings, and breaks **) |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
94 |
|
37529 | 95 |
abstype T = |
40131
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
96 |
Block of (Output.output * Output.output) * T list * int * int |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
97 |
(*markup output, body, indentation, length*) |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
98 |
| String of Output.output * int (*text, length*) |
7cbebd636e79
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents:
38474
diff
changeset
|
99 |
| Break of bool * int (*mandatory flag, width if not taken*) |
37529 | 100 |
with |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
101 |
|
23645 | 102 |
fun length (Block (_, _, _, len)) = len |
103 |
| length (String (_, len)) = len |
|
104 |
| length (Break (_, wd)) = wd; |
|
105 |
||
9730 | 106 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
107 |
|
23645 | 108 |
(** derived operations to create formatting expressions **) |
109 |
||
110 |
val str = String o Output.output_width; |
|
111 |
||
112 |
fun brk wd = Break (false, wd); |
|
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
113 |
val fbrk = Break (true, 1); |
23645 | 114 |
|
115 |
fun breaks prts = Library.separate (brk 1) prts; |
|
116 |
fun fbreaks prts = Library.separate fbrk prts; |
|
117 |
||
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
118 |
fun block_markup m (indent, es) = |
23645 | 119 |
let |
120 |
fun sum [] k = k |
|
121 |
| sum (e :: es) k = sum es (length e + k); |
|
122 |
in Block (m, es, indent, sum es 0) end; |
|
123 |
||
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
124 |
fun markup_block m arg = block_markup (Markup.output m) arg; |
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
125 |
|
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
126 |
val blk = markup_block Markup.empty; |
23645 | 127 |
fun block prts = blk (2, prts); |
128 |
val strs = block o breaks o map str; |
|
129 |
||
130 |
fun markup m prts = markup_block m (0, prts); |
|
42266 | 131 |
fun mark m prt = if m = Markup.empty then prt else markup m [prt]; |
132 |
fun mark_str (m, s) = mark m (str s); |
|
133 |
fun marks_str (ms, s) = fold_rev mark ms (str s); |
|
134 |
||
135 |
fun keyword name = mark_str (Markup.keyword, name); |
|
136 |
fun command name = mark_str (Markup.command, name); |
|
23645 | 137 |
|
138 |
fun markup_chunks m prts = markup m (fbreaks prts); |
|
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
37529
diff
changeset
|
139 |
val chunks = markup_chunks Markup.empty; |
23645 | 140 |
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts))); |
141 |
||
36733 | 142 |
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2]; |
23645 | 143 |
|
144 |
fun quote prt = blk (1, [str "\"", prt, str "\""]); |
|
145 |
fun backquote prt = blk (1, [str "`", prt, str "`"]); |
|
146 |
||
147 |
fun separate sep prts = |
|
148 |
flat (Library.separate [str sep, brk 1] (map single prts)); |
|
149 |
||
150 |
val commas = separate ","; |
|
151 |
||
152 |
fun enclose lpar rpar prts = |
|
153 |
block (str lpar :: (prts @ [str rpar])); |
|
154 |
||
155 |
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
|
156 |
|
30620 | 157 |
val position = |
158 |
enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of; |
|
159 |
||
23645 | 160 |
val list = enum ","; |
161 |
fun str_list lpar rpar strs = list lpar rpar (map str strs); |
|
162 |
||
163 |
fun big_list name prts = block (fbreaks (str name :: prts)); |
|
164 |
||
165 |
fun indent 0 prt = prt |
|
166 |
| indent n prt = blk (0, [str (Symbol.spaces n), prt]); |
|
167 |
||
168 |
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd) |
|
169 |
| unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd) |
|
170 |
| unbreakable (e as String _) = e; |
|
171 |
||
172 |
||
173 |
||
174 |
(** formatting **) |
|
175 |
||
176 |
(* formatted output *) |
|
177 |
||
178 |
local |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
179 |
|
17756 | 180 |
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int}; |
181 |
||
182 |
val empty: text = |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
183 |
{tx = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
184 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
185 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
186 |
nl = 0}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
187 |
|
32784 | 188 |
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
|
189 |
{tx = Buffer.add (Output.output "\n") tx, |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
190 |
ind = Buffer.empty, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
191 |
pos = 0, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
192 |
nl = nl + 1}; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
193 |
|
23628
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
194 |
fun control s {tx, ind, pos: int, nl} : text = |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
195 |
{tx = Buffer.add s tx, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
196 |
ind = ind, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
197 |
pos = pos, |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
198 |
nl = nl}; |
41cdbfb9f77b
markup: emit as control information -- no indent text;
wenzelm
parents:
23617
diff
changeset
|
199 |
|
17756 | 200 |
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
|
201 |
{tx = Buffer.add s tx, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
202 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
203 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
204 |
nl = nl}; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
205 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
206 |
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
|
207 |
|
17756 | 208 |
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
|
209 |
let val s = Buffer.content buf in |
23617 | 210 |
{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
|
211 |
ind = Buffer.add s ind, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
212 |
pos = pos + len, |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
213 |
nl = nl} |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
214 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
215 |
|
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
216 |
(*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
|
217 |
include "after", to account for text following this block.*) |
36687 | 218 |
fun breakdist (Break _ :: _, _) = 0 |
219 |
| breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after) |
|
32784 | 220 |
| breakdist (String (_, len) :: es, after) = len + breakdist (es, after) |
10952
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
221 |
| breakdist ([], after) = after; |
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
222 |
|
b520e4f1313b
support general indentation (e.g. for non-tt latex output);
wenzelm
parents:
9730
diff
changeset
|
223 |
(*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
|
224 |
fun forcenext [] = [] |
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
225 |
| forcenext (Break _ :: es) = fbrk :: es |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
226 |
| forcenext (e :: es) = e :: forcenext es; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
227 |
|
23645 | 228 |
in |
19266 | 229 |
|
36745 | 230 |
fun formatted margin input = |
231 |
let |
|
232 |
val breakgain = margin div 20; (*minimum added space required of a break*) |
|
233 |
val emergencypos = margin div 2; (*position too far to right*) |
|
234 |
||
235 |
(*es is list of expressions to print; |
|
236 |
blockin is the indentation of the current block; |
|
237 |
after is the width of the following context until next break.*) |
|
238 |
fun format ([], _, _) text = text |
|
239 |
| format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) = |
|
240 |
(case e of |
|
241 |
Block ((bg, en), bes, indent, _) => |
|
242 |
let |
|
243 |
val pos' = pos + indent; |
|
244 |
val pos'' = pos' mod emergencypos; |
|
245 |
val block' = |
|
246 |
if pos' < emergencypos then (ind |> add_indent indent, pos') |
|
247 |
else (add_indent pos'' Buffer.empty, pos''); |
|
248 |
val btext: text = text |
|
249 |
|> control bg |
|
250 |
|> format (bes, block', breakdist (es, after)) |
|
251 |
|> control en; |
|
252 |
(*if this block was broken then force the next break*) |
|
253 |
val es' = if nl < #nl btext then forcenext es else es; |
|
254 |
in format (es', block, after) btext end |
|
255 |
| Break (force, wd) => |
|
256 |
(*no break if text to next break fits on this line |
|
257 |
or if breaking would add only breakgain to space*) |
|
258 |
format (es, block, after) |
|
259 |
(if not force andalso |
|
260 |
pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain) |
|
261 |
then text |> blanks wd (*just insert wd blanks*) |
|
262 |
else text |> newline |> indentation block) |
|
263 |
| String str => format (es, block, after) (string str text)); |
|
264 |
in |
|
36747
7361d5dde9ce
discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents:
36745
diff
changeset
|
265 |
#tx (format ([input], (Buffer.empty, 0), 0) empty) |
36745 | 266 |
end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
267 |
|
23645 | 268 |
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
|
269 |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
270 |
|
23645 | 271 |
(* special output *) |
18802 | 272 |
|
23645 | 273 |
(*symbolic markup -- no formatting*) |
274 |
fun symbolic prt = |
|
275 |
let |
|
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
276 |
fun out (Block ((bg, en), [], _, _)) = Buffer.add bg #> Buffer.add en |
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
277 |
| out (Block ((bg, en), prts, indent, _)) = |
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
278 |
Buffer.add bg #> Buffer.markup (Markup.block indent) (fold out prts) #> Buffer.add en |
23645 | 279 |
| out (String (s, _)) = Buffer.add s |
23787 | 280 |
| out (Break (false, wd)) = Buffer.markup (Markup.break wd) (Buffer.add (output_spaces wd)) |
36689
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents:
36687
diff
changeset
|
281 |
| out (Break (true, _)) = Buffer.add (Output.output "\n"); |
23645 | 282 |
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
|
283 |
|
23645 | 284 |
(*unformatted output*) |
285 |
fun unformatted prt = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
286 |
let |
30667
53fbf7c679b0
Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents:
30624
diff
changeset
|
287 |
fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en |
23617 | 288 |
| fmt (String (s, _)) = Buffer.add s |
36690
97d2780ad6f0
uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents:
36689
diff
changeset
|
289 |
| fmt (Break (_, wd)) = Buffer.add (output_spaces wd); |
36747
7361d5dde9ce
discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents:
36745
diff
changeset
|
290 |
in fmt prt Buffer.empty end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
291 |
|
30624
e755b8b76365
simplified datatype ML_Pretty.pretty: model Isabelle not Poly/ML;
wenzelm
parents:
30620
diff
changeset
|
292 |
|
36748 | 293 |
(* output interfaces *) |
30620 | 294 |
|
36748 | 295 |
val margin_default = Unsynchronized.ref 76; (*right margin, or page width*) |
23645 | 296 |
|
297 |
val symbolicN = "pretty_symbolic"; |
|
298 |
||
36745 | 299 |
fun output_buffer margin prt = |
23645 | 300 |
if print_mode_active symbolicN then symbolic prt |
36745 | 301 |
else formatted (the_default (! margin_default) margin) prt; |
23645 | 302 |
|
36745 | 303 |
val output = Buffer.content oo output_buffer; |
304 |
fun string_of_margin margin = Output.escape o output (SOME margin); |
|
305 |
val string_of = Output.escape o output NONE; |
|
23645 | 306 |
val str_of = Output.escape o Buffer.content o unformatted; |
307 |
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
|
308 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
309 |
|
14832
6589a58f57cb
pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents:
12421
diff
changeset
|
310 |
|
36748 | 311 |
(** ML toplevel pretty printing **) |
312 |
||
313 |
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind) |
|
314 |
| to_ML (String s) = ML_Pretty.String s |
|
315 |
| to_ML (Break b) = ML_Pretty.Break b; |
|
316 |
||
317 |
fun from_ML (ML_Pretty.Block (m, prts, ind)) = block_markup m (ind, map from_ML prts) |
|
318 |
| from_ML (ML_Pretty.String s) = String s |
|
319 |
| from_ML (ML_Pretty.Break b) = Break b; |
|
320 |
||
37529 | 321 |
end; |
322 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
323 |
end; |