Mercurial > urweb
comparison src/laconic_print.sml @ 1:4202f6eda946
Initial parsing and pretty-printing
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sat, 26 Jan 2008 12:35:32 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:502c6d622477 | 1:4202f6eda946 |
---|---|
1 (* Copyright (c) 2008, Adam Chlipala | |
2 * All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are met: | |
6 * | |
7 * - Redistributions of source code must retain the above copyright notice, | |
8 * this list of conditions and the following disclaimer. | |
9 * - Redistributions in binary form must reproduce the above copyright notice, | |
10 * this list of conditions and the following disclaimer in the documentation | |
11 * and/or other materials provided with the distribution. | |
12 * - The names of contributors may not be used to endorse or promote products | |
13 * derived from this software without specific prior written permission. | |
14 * | |
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 * POSSIBILITY OF SUCH DAMAGE. | |
26 *) | |
27 | |
28 (* Pretty-printing Laconic/Web *) | |
29 | |
30 structure LaconicPrint :> LACONIC_PRINT = struct | |
31 | |
32 open Print.PD | |
33 open Print | |
34 | |
35 open Laconic | |
36 | |
37 fun p_kind' par (k, _) = | |
38 case k of | |
39 KType => string "Type" | |
40 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1, | |
41 space, | |
42 string "->", | |
43 space, | |
44 p_kind k2]) | |
45 | KName => string "Name" | |
46 | KRecord k => box [string "{", p_kind k, string "}"] | |
47 | |
48 and p_kind k = p_kind' false k | |
49 | |
50 fun p_explicitness e = | |
51 case e of | |
52 Explicit => string "::" | |
53 | Implicit => string ":::" | |
54 | |
55 fun p_con' par (c, _) = | |
56 case c of | |
57 CAnnot (c, k) => box [string "(", | |
58 p_con c, | |
59 space, | |
60 string "::", | |
61 space, | |
62 p_kind k, | |
63 string ")"] | |
64 | |
65 | TFun (t1, t2) => parenIf par (box [p_con' true t1, | |
66 space, | |
67 string "->", | |
68 space, | |
69 p_con t2]) | |
70 | TCFun (e, x, k, c) => parenIf par (box [string x, | |
71 space, | |
72 p_explicitness e, | |
73 space, | |
74 p_kind k, | |
75 space, | |
76 string "->", | |
77 space, | |
78 p_con c]) | |
79 | TRecord (CRecord xcs, _) => box [string "{", | |
80 p_list (fn (x, c) => | |
81 box [p_con x, | |
82 space, | |
83 string ":", | |
84 space, | |
85 p_con c]) xcs, | |
86 string "}"] | |
87 | TRecord c => box [string "$", | |
88 p_con' true c] | |
89 | |
90 | CVar s => string s | |
91 | CApp (c1, c2) => parenIf par (box [p_con c1, | |
92 space, | |
93 p_con' true c2]) | |
94 | CAbs (e, x, k, c) => parenIf par (box [string "fn", | |
95 space, | |
96 string x, | |
97 space, | |
98 p_explicitness e, | |
99 space, | |
100 p_kind k, | |
101 space, | |
102 string "=>", | |
103 space, | |
104 p_con c]) | |
105 | |
106 | CName s => box [string "#", string s] | |
107 | |
108 | CRecord xcs => box [string "[", | |
109 p_list (fn (x, c) => | |
110 box [p_con x, | |
111 space, | |
112 string "=", | |
113 space, | |
114 p_con c]) xcs, | |
115 string "]"] | |
116 | CConcat (c1, c2) => parenIf par (box [p_con' true c1, | |
117 space, | |
118 string "++", | |
119 space, | |
120 p_con c2]) | |
121 | |
122 and p_con c = p_con' false c | |
123 | |
124 fun p_decl ((d, _) : decl) = | |
125 case d of | |
126 DCon (x, NONE, c) => box [string "con", | |
127 space, | |
128 string x, | |
129 space, | |
130 string "=", | |
131 space, | |
132 p_con c] | |
133 | DCon (x, SOME k, c) => box [string "con", | |
134 space, | |
135 string x, | |
136 space, | |
137 string "::", | |
138 space, | |
139 p_kind k, | |
140 space, | |
141 string "=", | |
142 space, | |
143 p_con c] | |
144 | |
145 val p_file = p_list_sep newline p_decl | |
146 | |
147 end |