More verbose couldn't-find-cert message
[hcoop/domtool2.git] / src / printFn.sml
CommitLineData
9b7ee2b2
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19(* Pretty-printing Domtool configuration file ASTs *)
20
21functor PrintFn (PF : PRINTFN_INPUT) :> PRINTFN_OUTPUT where type rendering = PF.rendering = struct
22
23open Ast PF
24open PD
25
26fun dBox ds = hovBox (PPS.Rel 1, ds)
27fun dvBox ds = vBox (PPS.Rel 0, ds)
28fun ivBox ds = vBox (PPS.Rel 1, ds)
29
30fun modify file =
31 let
32 val file' = #file (OS.Path.splitDirFile file)
33 val file' = #base (OS.Path.splitBaseExt file')
34 in
35 file'
36 end
37
38fun parenIf pn ds =
39 if pn then
40 dBox (punct "(" :: ds @ [punct ")"])
41 else
42 dBox ds
43
44fun p_pred' pn (p, _) =
45 case p of
46 CRoot => keyword "Root"
47 | CConst s => context s
48 | CPrefix p => dBox [punct "^", p_pred' true p]
49 | CNot p => dBox [punct "!", p_pred' true p]
50 | CAnd (p1, p2) =>
51 parenIf pn [p_pred' true p1, space 1, punct "&", space 1, p_pred' true p2]
52
53val p_pred = p_pred' false
54
55fun p_predBoxed p = dBox [punct "[", p_pred p, punct "]"]
56
57fun p_typ' pn (t, _) =
58 case t of
59 TBase s => typ s
60 | TList t => dBox [punct "[", p_typ' false t, punct "]"]
61 | TArrow (t1, t2) =>
62 parenIf pn [p_typ' true t1, space 1, punct "->", space 1, p_typ' false t2]
63 | TAction (p, r1, r2) =>
64 (case (StringMap.numItems r1, StringMap.numItems r2) of
65 (0, 0) => parenIf pn [p_predBoxed p]
66 | (_, 0) => parenIf pn [p_predBoxed p, space 1, p_record r1]
67 | _ => parenIf pn [p_predBoxed p, space 1, p_record r1, space 1,
68 punct "=>", space 1, p_record r2])
69 | TNested (p, t) =>
70 parenIf pn [p_pred' false p, space 1, punct "=>", space 1, p_typ' false t]
71
72 | TError => keyword "<error>"
73 | TUnif (_, ref (SOME t)) => p_typ' pn t
74 | TUnif (name, ref NONE) => string ("<" ^ name ^ ">")
75
76and p_record r =
77 case StringMap.foldri (fn (name, t, d) =>
78 SOME (case d of
79 NONE => dBox [field name, space 1,
80 punct ":", space 1, p_typ t]
81 | SOME d => dBox [dBox [field name, space 1,
82 punct ":", space 1, p_typ t],
83 punct ",", space 1, d]))
84 NONE r of
85 NONE => punct "{}"
86 | SOME d => dBox [punct "{", d, punct "}"]
87
88and p_typ t = p_typ' false t
89
90fun p_exp' pn (e, _) =
91 case e of
92 EInt n => lit (Int.toString n)
93 | EString s => lit (String.concat ["\"", String.toString s, "\""])
94 | EList es =>
95 (case foldr (fn (e, d) =>
96 SOME (case d of
97 NONE => p_exp e
98 | SOME d => dBox [p_exp e, punct ",", space 1, d]))
99 NONE es of
100 NONE => punct "[]"
101 | SOME d => dBox [punct "[", d, punct "]"])
102
103 | ELam (x, NONE, e) => dBox [punct "(\\", space 1, exp x, space 1,
104 punct "->", space 1, p_exp e, punct ")"]
105 | ELam (x, SOME t, e) => dBox [punct "(\\", space 1, exp x, space 1,
106 punct ":", space 1,
107 dBox [punct "(", p_typ t, punct ")"],
108 space 1, punct "->", space 1, p_exp e, punct ")"]
109 | EALam (x, p, e) => dBox [punct "(\\", space 1, exp x, space 1,
110 punct ":", space 1, p_pred p,
111 space 1, punct "->", space 1, p_exp e, punct ")"]
112
113 | EVar x => exp x
114 | EApp (e1, e2) => parenIf pn [p_exp e1, break {nsp = 1, offset = 0}, p_exp' true e2]
115
116 | ESkip => keyword "_"
117 | ESet (x, e) => parenIf pn [exp x, space 1, punct "=", space 1, p_exp e]
118 | EGet (x1, x2, e) => parenIf pn [dBox [exp x1, space 1, punct "<-",
119 space 1, exp x2, punct ";", space 1],
120 p_exp e]
121 | ESeq es => parenIf pn (valOf (foldr (fn (e, NONE) => SOME [p_exp e]
122 | (e, SOME ds) => SOME (dBox [p_exp e, punct ";", newline] :: ds))
123 NONE es))
124 | ELocal (e1, e2) => dBox [keyword "let", space 1,
125 p_exp e1, space 1,
126 keyword "in", space 1,
127 p_exp e2, space 1,
128 keyword "end"]
129 | EWith (e1, (ESkip, _)) => dBox [p_exp e1, space 1, keyword "with", space 1, keyword "end"]
130 | EWith (e1, e2) => dBox [p_exp e1, space 1, keyword "with", p_exp e2, space 1, keyword "end"]
131and p_exp e = p_exp' false e
132
133fun p_decl d =
134 case d of
135 DExternType name => anchor ("T_" ^ name,
136 dBox [keyword "extern", space 1,
137 keyword "type", space 1,
138 ident name])
139 | DExternVal (name, t) => anchor ("V_" ^ name,
140 dBox [keyword "extern", space 1,
141 keyword "val", space 1,
142 ident name, space 1,
143 string ":", space 1,
144 p_typ t])
145 | DVal (name, NONE, _) => string "Unannotated val declaration!"
146 | DVal (name, SOME t, _) => anchor ("V_" ^ name,
147 dBox [keyword "val", space 1,
148 ident name, space 1,
149 punct ":", space 1,
150 p_typ t])
151 | DContext name => anchor ("C_" ^ name,
152 dBox [keyword "context", space 1,
153 ident name])
154
155fun p_decl_fref d =
156 case d of
157 DExternType name => dBox [keyword "extern", space 1,
158 keyword "type", space 1,
159 link ("#T_" ^ name, ident name)]
160 | DExternVal (name, t) => dBox [keyword "extern", space 1,
161 keyword "val", space 1,
162 link ("#V_" ^ name, ident name),
163 space 1,
164 string ":", space 1,
165 p_typ t]
166 | DVal (name, NONE, _) => string "Unannotated val declaration!"
167 | DVal (name, SOME t, _) => dBox [keyword "val", space 1,
168 link ("#V_" ^ name, ident name),
169 space 1,
170 punct ":", space 1,
171 p_typ t]
172 | DContext name => dBox [keyword "context", space 1,
173 link ("#C_" ^ name, ident name)]
174
175fun output d =
176 let
177 val myStream = openStream ()
178 in
179 description (myStream, d);
180 PPS.flushStream myStream;
181 closeStream myStream
182 end
183
184fun preface (s, d) = output (PD.hovBox (PD.PPS.Rel 0,
185 [PD.string s, PD.space 1, d]))
186
187end