adamc@1
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@1
|
2 * All rights reserved.
|
adamc@1
|
3 *
|
adamc@1
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@1
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@1
|
6 *
|
adamc@1
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@1
|
8 * this list of conditions and the following disclaimer.
|
adamc@1
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@1
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@1
|
11 * and/or other materials provided with the distribution.
|
adamc@1
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@1
|
13 * derived from this software without specific prior written permission.
|
adamc@1
|
14 *
|
adamc@1
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@1
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@1
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@1
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@1
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@1
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@1
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@1
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@1
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@1
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@1
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@1
|
26 *)
|
adamc@1
|
27
|
adamc@1
|
28 (* Pretty-printing Laconic/Web *)
|
adamc@1
|
29
|
adamc@4
|
30 structure SourcePrint :> SOURCE_PRINT = struct
|
adamc@1
|
31
|
adamc@1
|
32 open Print.PD
|
adamc@1
|
33 open Print
|
adamc@1
|
34
|
adamc@4
|
35 open Source
|
adamc@1
|
36
|
adamc@1
|
37 fun p_kind' par (k, _) =
|
adamc@1
|
38 case k of
|
adamc@1
|
39 KType => string "Type"
|
adamc@1
|
40 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
|
adamc@1
|
41 space,
|
adamc@1
|
42 string "->",
|
adamc@1
|
43 space,
|
adamc@1
|
44 p_kind k2])
|
adamc@1
|
45 | KName => string "Name"
|
adamc@1
|
46 | KRecord k => box [string "{", p_kind k, string "}"]
|
adamc@82
|
47 | KUnit => string "Unit"
|
adamc@18
|
48 | KWild => string "_"
|
adamc@1
|
49
|
adamc@1
|
50 and p_kind k = p_kind' false k
|
adamc@1
|
51
|
adamc@1
|
52 fun p_explicitness e =
|
adamc@1
|
53 case e of
|
adamc@1
|
54 Explicit => string "::"
|
adamc@1
|
55 | Implicit => string ":::"
|
adamc@1
|
56
|
adamc@1
|
57 fun p_con' par (c, _) =
|
adamc@1
|
58 case c of
|
adamc@1
|
59 CAnnot (c, k) => box [string "(",
|
adamc@1
|
60 p_con c,
|
adamc@1
|
61 space,
|
adamc@1
|
62 string "::",
|
adamc@1
|
63 space,
|
adamc@1
|
64 p_kind k,
|
adamc@1
|
65 string ")"]
|
adamc@1
|
66
|
adamc@1
|
67 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
|
adamc@1
|
68 space,
|
adamc@1
|
69 string "->",
|
adamc@1
|
70 space,
|
adamc@1
|
71 p_con t2])
|
adamc@1
|
72 | TCFun (e, x, k, c) => parenIf par (box [string x,
|
adamc@1
|
73 space,
|
adamc@1
|
74 p_explicitness e,
|
adamc@1
|
75 space,
|
adamc@1
|
76 p_kind k,
|
adamc@1
|
77 space,
|
adamc@1
|
78 string "->",
|
adamc@1
|
79 space,
|
adamc@1
|
80 p_con c])
|
adamc@1
|
81 | TRecord (CRecord xcs, _) => box [string "{",
|
adamc@1
|
82 p_list (fn (x, c) =>
|
adamc@20
|
83 box [p_name x,
|
adamc@1
|
84 space,
|
adamc@1
|
85 string ":",
|
adamc@1
|
86 space,
|
adamc@1
|
87 p_con c]) xcs,
|
adamc@1
|
88 string "}"]
|
adamc@1
|
89 | TRecord c => box [string "$",
|
adamc@1
|
90 p_con' true c]
|
adamc@1
|
91
|
adamc@34
|
92 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
|
adamc@1
|
93 | CApp (c1, c2) => parenIf par (box [p_con c1,
|
adamc@1
|
94 space,
|
adamc@1
|
95 p_con' true c2])
|
adamc@67
|
96 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
|
adamc@67
|
97 space,
|
adamc@67
|
98 string x,
|
adamc@67
|
99 space,
|
adamc@67
|
100 string "=>",
|
adamc@67
|
101 space,
|
adamc@67
|
102 p_con c])
|
adamc@67
|
103 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
|
adamc@67
|
104 space,
|
adamc@67
|
105 string x,
|
adamc@67
|
106 space,
|
adamc@67
|
107 string "::",
|
adamc@67
|
108 space,
|
adamc@67
|
109 p_kind k,
|
adamc@67
|
110 space,
|
adamc@67
|
111 string "=>",
|
adamc@67
|
112 space,
|
adamc@67
|
113 p_con c])
|
adamc@1
|
114
|
adamc@1
|
115 | CName s => box [string "#", string s]
|
adamc@1
|
116
|
adamc@1
|
117 | CRecord xcs => box [string "[",
|
adamc@1
|
118 p_list (fn (x, c) =>
|
adamc@1
|
119 box [p_con x,
|
adamc@1
|
120 space,
|
adamc@1
|
121 string "=",
|
adamc@1
|
122 space,
|
adamc@1
|
123 p_con c]) xcs,
|
adamc@1
|
124 string "]"]
|
adamc@1
|
125 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
|
adamc@1
|
126 space,
|
adamc@1
|
127 string "++",
|
adamc@1
|
128 space,
|
adamc@1
|
129 p_con c2])
|
adamc@67
|
130 | CFold => string "fold"
|
adamc@82
|
131
|
adamc@82
|
132 | CUnit => string "()"
|
adamc@82
|
133
|
adamc@18
|
134 | CWild k => box [string "(_",
|
adamc@18
|
135 space,
|
adamc@18
|
136 string "::",
|
adamc@18
|
137 space,
|
adamc@18
|
138 p_kind k]
|
adamc@1
|
139
|
adamc@1
|
140 and p_con c = p_con' false c
|
adamc@1
|
141
|
adamc@20
|
142 and p_name (all as (c, _)) =
|
adamc@20
|
143 case c of
|
adamc@20
|
144 CName s => string s
|
adamc@20
|
145 | _ => p_con all
|
adamc@20
|
146
|
adamc@8
|
147 fun p_exp' par (e, _) =
|
adamc@8
|
148 case e of
|
adamc@8
|
149 EAnnot (e, t) => box [string "(",
|
adamc@8
|
150 p_exp e,
|
adamc@8
|
151 space,
|
adamc@8
|
152 string ":",
|
adamc@8
|
153 space,
|
adamc@8
|
154 p_con t,
|
adamc@8
|
155 string ")"]
|
adamc@8
|
156
|
adamc@14
|
157 | EPrim p => Prim.p_t p
|
adamc@34
|
158 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
|
adamc@8
|
159 | EApp (e1, e2) => parenIf par (box [p_exp e1,
|
adamc@8
|
160 space,
|
adamc@8
|
161 p_exp' true e2])
|
adamc@8
|
162 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
|
adamc@8
|
163 space,
|
adamc@8
|
164 string x,
|
adamc@8
|
165 space,
|
adamc@8
|
166 string "=>",
|
adamc@8
|
167 space,
|
adamc@8
|
168 p_exp e])
|
adamc@8
|
169 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
|
adamc@8
|
170 space,
|
adamc@8
|
171 string x,
|
adamc@8
|
172 space,
|
adamc@8
|
173 string ":",
|
adamc@8
|
174 space,
|
adamc@8
|
175 p_con t,
|
adamc@8
|
176 space,
|
adamc@8
|
177 string "=>",
|
adamc@8
|
178 space,
|
adamc@8
|
179 p_exp e])
|
adamc@8
|
180 | ECApp (e, c) => parenIf par (box [p_exp e,
|
adamc@8
|
181 space,
|
adamc@8
|
182 string "[",
|
adamc@8
|
183 p_con c,
|
adamc@8
|
184 string "]"])
|
adamc@8
|
185 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@8
|
186 space,
|
adamc@8
|
187 string x,
|
adamc@8
|
188 space,
|
adamc@8
|
189 p_explicitness exp,
|
adamc@8
|
190 space,
|
adamc@8
|
191 p_kind k,
|
adamc@8
|
192 space,
|
adamc@8
|
193 string "=>",
|
adamc@8
|
194 space,
|
adamc@8
|
195 p_exp e])
|
adamc@8
|
196
|
adamc@12
|
197 | ERecord xes => box [string "{",
|
adamc@12
|
198 p_list (fn (x, e) =>
|
adamc@21
|
199 box [p_name x,
|
adamc@12
|
200 space,
|
adamc@12
|
201 string "=",
|
adamc@12
|
202 space,
|
adamc@12
|
203 p_exp e]) xes,
|
adamc@12
|
204 string "}"]
|
adamc@12
|
205 | EField (e, c) => box [p_exp' true e,
|
adamc@12
|
206 string ".",
|
adamc@12
|
207 p_con' true c]
|
adamc@71
|
208 | EFold => string "fold"
|
adamc@12
|
209
|
adamc@8
|
210 and p_exp e = p_exp' false e
|
adamc@8
|
211
|
adamc@30
|
212 fun p_sgn_item (sgi, _) =
|
adamc@30
|
213 case sgi of
|
adamc@30
|
214 SgiConAbs (x, k) => box [string "con",
|
adamc@30
|
215 space,
|
adamc@30
|
216 string x,
|
adamc@30
|
217 space,
|
adamc@30
|
218 string "::",
|
adamc@30
|
219 space,
|
adamc@30
|
220 p_kind k]
|
adamc@30
|
221 | SgiCon (x, NONE, c) => box [string "con",
|
adamc@30
|
222 space,
|
adamc@30
|
223 string x,
|
adamc@30
|
224 space,
|
adamc@30
|
225 string "=",
|
adamc@30
|
226 space,
|
adamc@30
|
227 p_con c]
|
adamc@30
|
228 | SgiCon (x, SOME k, c) => box [string "con",
|
adamc@30
|
229 space,
|
adamc@30
|
230 string x,
|
adamc@30
|
231 space,
|
adamc@30
|
232 string "::",
|
adamc@30
|
233 space,
|
adamc@30
|
234 p_kind k,
|
adamc@30
|
235 space,
|
adamc@30
|
236 string "=",
|
adamc@30
|
237 space,
|
adamc@30
|
238 p_con c]
|
adamc@30
|
239 | SgiVal (x, c) => box [string "val",
|
adamc@30
|
240 space,
|
adamc@30
|
241 string x,
|
adamc@30
|
242 space,
|
adamc@30
|
243 string ":",
|
adamc@30
|
244 space,
|
adamc@30
|
245 p_con c]
|
adamc@30
|
246 | SgiStr (x, sgn) => box [string "structure",
|
adamc@30
|
247 space,
|
adamc@30
|
248 string x,
|
adamc@30
|
249 space,
|
adamc@30
|
250 string ":",
|
adamc@30
|
251 space,
|
adamc@30
|
252 p_sgn sgn]
|
adamc@59
|
253 | SgiSgn (x, sgn) => box [string "signature",
|
adamc@59
|
254 space,
|
adamc@59
|
255 string x,
|
adamc@59
|
256 space,
|
adamc@59
|
257 string "=",
|
adamc@59
|
258 space,
|
adamc@59
|
259 p_sgn sgn]
|
adamc@58
|
260 | SgiInclude sgn => box [string "include",
|
adamc@58
|
261 space,
|
adamc@58
|
262 p_sgn sgn]
|
adamc@30
|
263
|
adamc@30
|
264 and p_sgn (sgn, _) =
|
adamc@30
|
265 case sgn of
|
adamc@30
|
266 SgnConst sgis => box [string "sig",
|
adamc@30
|
267 newline,
|
adamc@30
|
268 p_list_sep newline p_sgn_item sgis,
|
adamc@30
|
269 newline,
|
adamc@30
|
270 string "end"]
|
adamc@30
|
271 | SgnVar x => string x
|
adamc@40
|
272 | SgnFun (x, sgn, sgn') => box [string "functor",
|
adamc@40
|
273 space,
|
adamc@40
|
274 string "(",
|
adamc@40
|
275 string x,
|
adamc@40
|
276 space,
|
adamc@40
|
277 string ":",
|
adamc@40
|
278 p_sgn sgn,
|
adamc@40
|
279 string ")",
|
adamc@40
|
280 space,
|
adamc@40
|
281 string ":",
|
adamc@40
|
282 space,
|
adamc@40
|
283 p_sgn sgn']
|
adamc@42
|
284 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
|
adamc@42
|
285 space,
|
adamc@42
|
286 string "where",
|
adamc@42
|
287 space,
|
adamc@42
|
288 string "con",
|
adamc@42
|
289 space,
|
adamc@42
|
290 string x,
|
adamc@42
|
291 space,
|
adamc@42
|
292 string "=",
|
adamc@42
|
293 space,
|
adamc@42
|
294 p_con c]
|
adamc@59
|
295 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
|
adamc@59
|
296
|
adamc@42
|
297
|
adamc@1
|
298 fun p_decl ((d, _) : decl) =
|
adamc@1
|
299 case d of
|
adamc@1
|
300 DCon (x, NONE, c) => box [string "con",
|
adamc@1
|
301 space,
|
adamc@1
|
302 string x,
|
adamc@1
|
303 space,
|
adamc@1
|
304 string "=",
|
adamc@1
|
305 space,
|
adamc@1
|
306 p_con c]
|
adamc@1
|
307 | DCon (x, SOME k, c) => box [string "con",
|
adamc@1
|
308 space,
|
adamc@1
|
309 string x,
|
adamc@1
|
310 space,
|
adamc@1
|
311 string "::",
|
adamc@1
|
312 space,
|
adamc@1
|
313 p_kind k,
|
adamc@1
|
314 space,
|
adamc@1
|
315 string "=",
|
adamc@1
|
316 space,
|
adamc@1
|
317 p_con c]
|
adamc@8
|
318 | DVal (x, NONE, e) => box [string "val",
|
adamc@8
|
319 space,
|
adamc@8
|
320 string x,
|
adamc@8
|
321 space,
|
adamc@8
|
322 string "=",
|
adamc@8
|
323 space,
|
adamc@8
|
324 p_exp e]
|
adamc@8
|
325 | DVal (x, SOME t, e) => box [string "val",
|
adamc@8
|
326 space,
|
adamc@8
|
327 string x,
|
adamc@8
|
328 space,
|
adamc@8
|
329 string ":",
|
adamc@8
|
330 space,
|
adamc@8
|
331 p_con t,
|
adamc@8
|
332 space,
|
adamc@8
|
333 string "=",
|
adamc@8
|
334 space,
|
adamc@8
|
335 p_exp e]
|
adamc@1
|
336
|
adamc@30
|
337 | DSgn (x, sgn) => box [string "signature",
|
adamc@30
|
338 space,
|
adamc@30
|
339 string x,
|
adamc@30
|
340 space,
|
adamc@30
|
341 string "=",
|
adamc@30
|
342 space,
|
adamc@30
|
343 p_sgn sgn]
|
adamc@30
|
344 | DStr (x, NONE, str) => box [string "structure",
|
adamc@30
|
345 space,
|
adamc@30
|
346 string x,
|
adamc@30
|
347 space,
|
adamc@30
|
348 string "=",
|
adamc@30
|
349 space,
|
adamc@30
|
350 p_str str]
|
adamc@30
|
351 | DStr (x, SOME sgn, str) => box [string "structure",
|
adamc@30
|
352 space,
|
adamc@30
|
353 string x,
|
adamc@30
|
354 space,
|
adamc@30
|
355 string ":",
|
adamc@30
|
356 space,
|
adamc@30
|
357 p_sgn sgn,
|
adamc@30
|
358 space,
|
adamc@30
|
359 string "=",
|
adamc@30
|
360 space,
|
adamc@30
|
361 p_str str]
|
adamc@48
|
362 | DFfiStr (x, sgn) => box [string "extern",
|
adamc@48
|
363 space,
|
adamc@48
|
364 string "structure",
|
adamc@48
|
365 space,
|
adamc@48
|
366 string x,
|
adamc@48
|
367 space,
|
adamc@48
|
368 string ":",
|
adamc@48
|
369 space,
|
adamc@48
|
370 p_sgn sgn]
|
adamc@61
|
371 | DOpen (m, ms) => box [string "open",
|
adamc@61
|
372 space,
|
adamc@61
|
373 p_list_sep (string ".") string (m :: ms)]
|
adamc@30
|
374
|
adamc@30
|
375 and p_str (str, _) =
|
adamc@30
|
376 case str of
|
adamc@30
|
377 StrConst ds => box [string "struct",
|
adamc@30
|
378 newline,
|
adamc@30
|
379 p_list_sep newline p_decl ds,
|
adamc@30
|
380 newline,
|
adamc@30
|
381 string "end"]
|
adamc@30
|
382 | StrVar x => string x
|
adamc@34
|
383 | StrProj (str, x) => box [p_str str,
|
adamc@34
|
384 string ".",
|
adamc@34
|
385 string x]
|
adamc@40
|
386 | StrFun (x, sgn, NONE, str) => box [string "functor",
|
adamc@40
|
387 space,
|
adamc@40
|
388 string "(",
|
adamc@40
|
389 string x,
|
adamc@40
|
390 space,
|
adamc@40
|
391 string ":",
|
adamc@40
|
392 p_sgn sgn,
|
adamc@40
|
393 string ")",
|
adamc@40
|
394 space,
|
adamc@40
|
395 string "=>",
|
adamc@40
|
396 space,
|
adamc@40
|
397 p_str str]
|
adamc@40
|
398 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
|
adamc@40
|
399 space,
|
adamc@40
|
400 string "(",
|
adamc@40
|
401 string x,
|
adamc@40
|
402 space,
|
adamc@40
|
403 string ":",
|
adamc@40
|
404 p_sgn sgn,
|
adamc@40
|
405 string ")",
|
adamc@40
|
406 space,
|
adamc@40
|
407 string ":",
|
adamc@40
|
408 space,
|
adamc@40
|
409 p_sgn sgn',
|
adamc@40
|
410 space,
|
adamc@40
|
411 string "=>",
|
adamc@40
|
412 space,
|
adamc@40
|
413 p_str str]
|
adamc@44
|
414 | StrApp (str1, str2) => box [p_str str1,
|
adamc@44
|
415 string "(",
|
adamc@44
|
416 p_str str2,
|
adamc@44
|
417 string ")"]
|
adamc@30
|
418
|
adamc@1
|
419 val p_file = p_list_sep newline p_decl
|
adamc@1
|
420
|
adamc@1
|
421 end
|