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@18
|
47 | KWild => string "_"
|
adamc@1
|
48
|
adamc@1
|
49 and p_kind k = p_kind' false k
|
adamc@1
|
50
|
adamc@1
|
51 fun p_explicitness e =
|
adamc@1
|
52 case e of
|
adamc@1
|
53 Explicit => string "::"
|
adamc@1
|
54 | Implicit => string ":::"
|
adamc@1
|
55
|
adamc@1
|
56 fun p_con' par (c, _) =
|
adamc@1
|
57 case c of
|
adamc@1
|
58 CAnnot (c, k) => box [string "(",
|
adamc@1
|
59 p_con c,
|
adamc@1
|
60 space,
|
adamc@1
|
61 string "::",
|
adamc@1
|
62 space,
|
adamc@1
|
63 p_kind k,
|
adamc@1
|
64 string ")"]
|
adamc@1
|
65
|
adamc@1
|
66 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
|
adamc@1
|
67 space,
|
adamc@1
|
68 string "->",
|
adamc@1
|
69 space,
|
adamc@1
|
70 p_con t2])
|
adamc@1
|
71 | TCFun (e, x, k, c) => parenIf par (box [string x,
|
adamc@1
|
72 space,
|
adamc@1
|
73 p_explicitness e,
|
adamc@1
|
74 space,
|
adamc@1
|
75 p_kind k,
|
adamc@1
|
76 space,
|
adamc@1
|
77 string "->",
|
adamc@1
|
78 space,
|
adamc@1
|
79 p_con c])
|
adamc@1
|
80 | TRecord (CRecord xcs, _) => box [string "{",
|
adamc@1
|
81 p_list (fn (x, c) =>
|
adamc@20
|
82 box [p_name x,
|
adamc@1
|
83 space,
|
adamc@1
|
84 string ":",
|
adamc@1
|
85 space,
|
adamc@1
|
86 p_con c]) xcs,
|
adamc@1
|
87 string "}"]
|
adamc@1
|
88 | TRecord c => box [string "$",
|
adamc@1
|
89 p_con' true c]
|
adamc@1
|
90
|
adamc@34
|
91 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
|
adamc@1
|
92 | CApp (c1, c2) => parenIf par (box [p_con c1,
|
adamc@1
|
93 space,
|
adamc@1
|
94 p_con' true c2])
|
adamc@8
|
95 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@8
|
96 space,
|
adamc@8
|
97 string x,
|
adamc@8
|
98 space,
|
adamc@8
|
99 string "::",
|
adamc@8
|
100 space,
|
adamc@8
|
101 p_kind k,
|
adamc@8
|
102 space,
|
adamc@8
|
103 string "=>",
|
adamc@8
|
104 space,
|
adamc@8
|
105 p_con c])
|
adamc@1
|
106
|
adamc@1
|
107 | CName s => box [string "#", string s]
|
adamc@1
|
108
|
adamc@1
|
109 | CRecord xcs => box [string "[",
|
adamc@1
|
110 p_list (fn (x, c) =>
|
adamc@1
|
111 box [p_con x,
|
adamc@1
|
112 space,
|
adamc@1
|
113 string "=",
|
adamc@1
|
114 space,
|
adamc@1
|
115 p_con c]) xcs,
|
adamc@1
|
116 string "]"]
|
adamc@1
|
117 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
|
adamc@1
|
118 space,
|
adamc@1
|
119 string "++",
|
adamc@1
|
120 space,
|
adamc@1
|
121 p_con c2])
|
adamc@18
|
122 | CWild k => box [string "(_",
|
adamc@18
|
123 space,
|
adamc@18
|
124 string "::",
|
adamc@18
|
125 space,
|
adamc@18
|
126 p_kind k]
|
adamc@1
|
127
|
adamc@1
|
128 and p_con c = p_con' false c
|
adamc@1
|
129
|
adamc@20
|
130 and p_name (all as (c, _)) =
|
adamc@20
|
131 case c of
|
adamc@20
|
132 CName s => string s
|
adamc@20
|
133 | _ => p_con all
|
adamc@20
|
134
|
adamc@8
|
135 fun p_exp' par (e, _) =
|
adamc@8
|
136 case e of
|
adamc@8
|
137 EAnnot (e, t) => box [string "(",
|
adamc@8
|
138 p_exp e,
|
adamc@8
|
139 space,
|
adamc@8
|
140 string ":",
|
adamc@8
|
141 space,
|
adamc@8
|
142 p_con t,
|
adamc@8
|
143 string ")"]
|
adamc@8
|
144
|
adamc@14
|
145 | EPrim p => Prim.p_t p
|
adamc@34
|
146 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
|
adamc@8
|
147 | EApp (e1, e2) => parenIf par (box [p_exp e1,
|
adamc@8
|
148 space,
|
adamc@8
|
149 p_exp' true e2])
|
adamc@8
|
150 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
|
adamc@8
|
151 space,
|
adamc@8
|
152 string x,
|
adamc@8
|
153 space,
|
adamc@8
|
154 string "=>",
|
adamc@8
|
155 space,
|
adamc@8
|
156 p_exp e])
|
adamc@8
|
157 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
|
adamc@8
|
158 space,
|
adamc@8
|
159 string x,
|
adamc@8
|
160 space,
|
adamc@8
|
161 string ":",
|
adamc@8
|
162 space,
|
adamc@8
|
163 p_con t,
|
adamc@8
|
164 space,
|
adamc@8
|
165 string "=>",
|
adamc@8
|
166 space,
|
adamc@8
|
167 p_exp e])
|
adamc@8
|
168 | ECApp (e, c) => parenIf par (box [p_exp e,
|
adamc@8
|
169 space,
|
adamc@8
|
170 string "[",
|
adamc@8
|
171 p_con c,
|
adamc@8
|
172 string "]"])
|
adamc@8
|
173 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@8
|
174 space,
|
adamc@8
|
175 string x,
|
adamc@8
|
176 space,
|
adamc@8
|
177 p_explicitness exp,
|
adamc@8
|
178 space,
|
adamc@8
|
179 p_kind k,
|
adamc@8
|
180 space,
|
adamc@8
|
181 string "=>",
|
adamc@8
|
182 space,
|
adamc@8
|
183 p_exp e])
|
adamc@8
|
184
|
adamc@12
|
185 | ERecord xes => box [string "{",
|
adamc@12
|
186 p_list (fn (x, e) =>
|
adamc@21
|
187 box [p_name x,
|
adamc@12
|
188 space,
|
adamc@12
|
189 string "=",
|
adamc@12
|
190 space,
|
adamc@12
|
191 p_exp e]) xes,
|
adamc@12
|
192 string "}"]
|
adamc@12
|
193 | EField (e, c) => box [p_exp' true e,
|
adamc@12
|
194 string ".",
|
adamc@12
|
195 p_con' true c]
|
adamc@12
|
196
|
adamc@12
|
197
|
adamc@8
|
198 and p_exp e = p_exp' false e
|
adamc@8
|
199
|
adamc@30
|
200 fun p_sgn_item (sgi, _) =
|
adamc@30
|
201 case sgi of
|
adamc@30
|
202 SgiConAbs (x, k) => box [string "con",
|
adamc@30
|
203 space,
|
adamc@30
|
204 string x,
|
adamc@30
|
205 space,
|
adamc@30
|
206 string "::",
|
adamc@30
|
207 space,
|
adamc@30
|
208 p_kind k]
|
adamc@30
|
209 | SgiCon (x, NONE, c) => box [string "con",
|
adamc@30
|
210 space,
|
adamc@30
|
211 string x,
|
adamc@30
|
212 space,
|
adamc@30
|
213 string "=",
|
adamc@30
|
214 space,
|
adamc@30
|
215 p_con c]
|
adamc@30
|
216 | SgiCon (x, SOME k, c) => box [string "con",
|
adamc@30
|
217 space,
|
adamc@30
|
218 string x,
|
adamc@30
|
219 space,
|
adamc@30
|
220 string "::",
|
adamc@30
|
221 space,
|
adamc@30
|
222 p_kind k,
|
adamc@30
|
223 space,
|
adamc@30
|
224 string "=",
|
adamc@30
|
225 space,
|
adamc@30
|
226 p_con c]
|
adamc@30
|
227 | SgiVal (x, c) => box [string "val",
|
adamc@30
|
228 space,
|
adamc@30
|
229 string x,
|
adamc@30
|
230 space,
|
adamc@30
|
231 string ":",
|
adamc@30
|
232 space,
|
adamc@30
|
233 p_con c]
|
adamc@30
|
234 | SgiStr (x, sgn) => box [string "structure",
|
adamc@30
|
235 space,
|
adamc@30
|
236 string x,
|
adamc@30
|
237 space,
|
adamc@30
|
238 string ":",
|
adamc@30
|
239 space,
|
adamc@30
|
240 p_sgn sgn]
|
adamc@59
|
241 | SgiSgn (x, sgn) => box [string "signature",
|
adamc@59
|
242 space,
|
adamc@59
|
243 string x,
|
adamc@59
|
244 space,
|
adamc@59
|
245 string "=",
|
adamc@59
|
246 space,
|
adamc@59
|
247 p_sgn sgn]
|
adamc@58
|
248 | SgiInclude sgn => box [string "include",
|
adamc@58
|
249 space,
|
adamc@58
|
250 p_sgn sgn]
|
adamc@30
|
251
|
adamc@30
|
252 and p_sgn (sgn, _) =
|
adamc@30
|
253 case sgn of
|
adamc@30
|
254 SgnConst sgis => box [string "sig",
|
adamc@30
|
255 newline,
|
adamc@30
|
256 p_list_sep newline p_sgn_item sgis,
|
adamc@30
|
257 newline,
|
adamc@30
|
258 string "end"]
|
adamc@30
|
259 | SgnVar x => string x
|
adamc@40
|
260 | SgnFun (x, sgn, sgn') => box [string "functor",
|
adamc@40
|
261 space,
|
adamc@40
|
262 string "(",
|
adamc@40
|
263 string x,
|
adamc@40
|
264 space,
|
adamc@40
|
265 string ":",
|
adamc@40
|
266 p_sgn sgn,
|
adamc@40
|
267 string ")",
|
adamc@40
|
268 space,
|
adamc@40
|
269 string ":",
|
adamc@40
|
270 space,
|
adamc@40
|
271 p_sgn sgn']
|
adamc@42
|
272 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
|
adamc@42
|
273 space,
|
adamc@42
|
274 string "where",
|
adamc@42
|
275 space,
|
adamc@42
|
276 string "con",
|
adamc@42
|
277 space,
|
adamc@42
|
278 string x,
|
adamc@42
|
279 space,
|
adamc@42
|
280 string "=",
|
adamc@42
|
281 space,
|
adamc@42
|
282 p_con c]
|
adamc@59
|
283 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
|
adamc@59
|
284
|
adamc@42
|
285
|
adamc@1
|
286 fun p_decl ((d, _) : decl) =
|
adamc@1
|
287 case d of
|
adamc@1
|
288 DCon (x, NONE, c) => box [string "con",
|
adamc@1
|
289 space,
|
adamc@1
|
290 string x,
|
adamc@1
|
291 space,
|
adamc@1
|
292 string "=",
|
adamc@1
|
293 space,
|
adamc@1
|
294 p_con c]
|
adamc@1
|
295 | DCon (x, SOME k, c) => box [string "con",
|
adamc@1
|
296 space,
|
adamc@1
|
297 string x,
|
adamc@1
|
298 space,
|
adamc@1
|
299 string "::",
|
adamc@1
|
300 space,
|
adamc@1
|
301 p_kind k,
|
adamc@1
|
302 space,
|
adamc@1
|
303 string "=",
|
adamc@1
|
304 space,
|
adamc@1
|
305 p_con c]
|
adamc@8
|
306 | DVal (x, NONE, e) => box [string "val",
|
adamc@8
|
307 space,
|
adamc@8
|
308 string x,
|
adamc@8
|
309 space,
|
adamc@8
|
310 string "=",
|
adamc@8
|
311 space,
|
adamc@8
|
312 p_exp e]
|
adamc@8
|
313 | DVal (x, SOME t, e) => box [string "val",
|
adamc@8
|
314 space,
|
adamc@8
|
315 string x,
|
adamc@8
|
316 space,
|
adamc@8
|
317 string ":",
|
adamc@8
|
318 space,
|
adamc@8
|
319 p_con t,
|
adamc@8
|
320 space,
|
adamc@8
|
321 string "=",
|
adamc@8
|
322 space,
|
adamc@8
|
323 p_exp e]
|
adamc@1
|
324
|
adamc@30
|
325 | DSgn (x, sgn) => box [string "signature",
|
adamc@30
|
326 space,
|
adamc@30
|
327 string x,
|
adamc@30
|
328 space,
|
adamc@30
|
329 string "=",
|
adamc@30
|
330 space,
|
adamc@30
|
331 p_sgn sgn]
|
adamc@30
|
332 | DStr (x, NONE, str) => box [string "structure",
|
adamc@30
|
333 space,
|
adamc@30
|
334 string x,
|
adamc@30
|
335 space,
|
adamc@30
|
336 string "=",
|
adamc@30
|
337 space,
|
adamc@30
|
338 p_str str]
|
adamc@30
|
339 | DStr (x, SOME sgn, str) => box [string "structure",
|
adamc@30
|
340 space,
|
adamc@30
|
341 string x,
|
adamc@30
|
342 space,
|
adamc@30
|
343 string ":",
|
adamc@30
|
344 space,
|
adamc@30
|
345 p_sgn sgn,
|
adamc@30
|
346 space,
|
adamc@30
|
347 string "=",
|
adamc@30
|
348 space,
|
adamc@30
|
349 p_str str]
|
adamc@48
|
350 | DFfiStr (x, sgn) => box [string "extern",
|
adamc@48
|
351 space,
|
adamc@48
|
352 string "structure",
|
adamc@48
|
353 space,
|
adamc@48
|
354 string x,
|
adamc@48
|
355 space,
|
adamc@48
|
356 string ":",
|
adamc@48
|
357 space,
|
adamc@48
|
358 p_sgn sgn]
|
adamc@30
|
359
|
adamc@30
|
360 and p_str (str, _) =
|
adamc@30
|
361 case str of
|
adamc@30
|
362 StrConst ds => box [string "struct",
|
adamc@30
|
363 newline,
|
adamc@30
|
364 p_list_sep newline p_decl ds,
|
adamc@30
|
365 newline,
|
adamc@30
|
366 string "end"]
|
adamc@30
|
367 | StrVar x => string x
|
adamc@34
|
368 | StrProj (str, x) => box [p_str str,
|
adamc@34
|
369 string ".",
|
adamc@34
|
370 string x]
|
adamc@40
|
371 | StrFun (x, sgn, NONE, str) => box [string "functor",
|
adamc@40
|
372 space,
|
adamc@40
|
373 string "(",
|
adamc@40
|
374 string x,
|
adamc@40
|
375 space,
|
adamc@40
|
376 string ":",
|
adamc@40
|
377 p_sgn sgn,
|
adamc@40
|
378 string ")",
|
adamc@40
|
379 space,
|
adamc@40
|
380 string "=>",
|
adamc@40
|
381 space,
|
adamc@40
|
382 p_str str]
|
adamc@40
|
383 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
|
adamc@40
|
384 space,
|
adamc@40
|
385 string "(",
|
adamc@40
|
386 string x,
|
adamc@40
|
387 space,
|
adamc@40
|
388 string ":",
|
adamc@40
|
389 p_sgn sgn,
|
adamc@40
|
390 string ")",
|
adamc@40
|
391 space,
|
adamc@40
|
392 string ":",
|
adamc@40
|
393 space,
|
adamc@40
|
394 p_sgn sgn',
|
adamc@40
|
395 space,
|
adamc@40
|
396 string "=>",
|
adamc@40
|
397 space,
|
adamc@40
|
398 p_str str]
|
adamc@44
|
399 | StrApp (str1, str2) => box [p_str str1,
|
adamc@44
|
400 string "(",
|
adamc@44
|
401 p_str str2,
|
adamc@44
|
402 string ")"]
|
adamc@30
|
403
|
adamc@1
|
404 val p_file = p_list_sep newline p_decl
|
adamc@1
|
405
|
adamc@1
|
406 end
|