Fix user domtool directory resolution
[hcoop/domtool2.git] / src / htmlPrint.sml
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 for HTML *)
20
21 structure HtmlPrint :> HTML_PRINT = struct
22
23 open Ast Order
24
25 val prov : providers option ref = ref NONE
26 fun setProviders p = prov := SOME p
27
28 structure TextToken = struct
29 type token = string
30 type style = HTMLDev.style
31 fun string t = t
32 fun style t = HTMLDev.styleTT
33 fun size t = String.size t
34 end
35
36 structure SM = PPStreamFn(structure Token = TextToken
37 structure Device = HTMLDev)
38
39 structure PD = PPDescFn(SM)
40 open PD
41
42 fun dBox ds = hovBox (PPS.Rel 1, ds)
43 fun dvBox ds = vBox (PPS.Rel 0, ds)
44 fun ivBox ds = vBox (PPS.Rel 1, ds)
45
46 fun keyword s = style (HTMLDev.styleB, [string s])
47 val punct = string
48
49 fun modify file =
50 let
51 val file' = #file (OS.Path.splitDirFile file)
52 val file' = #base (OS.Path.splitBaseExt file')
53 in
54 file'
55 end
56
57 fun context s =
58 case providesContext (valOf (!prov), s) of
59 NONE => string s
60 | SOME m => style (HTMLDev.link (modify m ^ ".html#C_" ^ s), [string s])
61 fun typ s =
62 case providesType (valOf (!prov), s) of
63 NONE => string s
64 | SOME m => style (HTMLDev.link (modify m ^ ".html#T_" ^ s), [string s])
65 fun exp s =
66 case providesValue (valOf (!prov), s) of
67 NONE => string s
68 | SOME m => style (HTMLDev.link (modify m ^ ".html#V_" ^ s), [string s])
69
70 val field = string
71 val lit = string
72 val ident = string
73
74 fun parenIf pn ds =
75 if pn then
76 dBox (punct "(" :: ds @ [punct ")"])
77 else
78 dBox ds
79
80 fun p_pred' pn (p, _) =
81 case p of
82 CRoot => keyword "Root"
83 | CConst s => context s
84 | CPrefix p => dBox [punct "^", p_pred' true p]
85 | CNot p => dBox [punct "!", p_pred' true p]
86 | CAnd (p1, p2) =>
87 parenIf pn [p_pred' true p1, space 1, punct "&", space 1, p_pred' true p2]
88
89 val p_pred = p_pred' false
90
91 fun p_predBoxed p = dBox [punct "[", p_pred p, punct "]"]
92
93 fun p_typ' pn (t, _) =
94 case t of
95 TBase s => typ s
96 | TList t => dBox [punct "[", p_typ' false t, punct "]"]
97 | TArrow (t1, t2) =>
98 parenIf pn [p_typ' true t1, space 1, punct "->", space 1, p_typ' true t2]
99 | TAction (p, r1, r2) =>
100 parenIf pn [p_predBoxed p, space 1, p_record r1, space 1,
101 punct "=>", space 1, p_record r2]
102 | TNested (p, t) =>
103 parenIf pn [p_pred' false p, space 1, punct "=>", space 1, p_typ' false t]
104
105 | TError => keyword "<error>"
106 | TUnif (_, ref (SOME t)) => p_typ' pn t
107 | TUnif (name, ref NONE) => string ("<" ^ name ^ ">")
108
109 and p_record r =
110 case StringMap.foldri (fn (name, t, d) =>
111 SOME (case d of
112 NONE => dBox [field name, space 1,
113 punct ":", space 1, p_typ t]
114 | SOME d => dBox [dBox [field name, space 1,
115 punct ":", space 1, p_typ t],
116 punct ",", space 1, d]))
117 NONE r of
118 NONE => punct "{}"
119 | SOME d => dBox [punct "{", d, punct "}"]
120
121 and p_typ t = p_typ' false t
122
123 fun p_exp (e, _) =
124 case e of
125 EInt n => lit (Int.toString n)
126 | EString s => lit (String.concat ["\"", String.toString s, "\""])
127 | EList es =>
128 (case foldr (fn (e, d) =>
129 SOME (case d of
130 NONE => p_exp e
131 | SOME d => dBox [p_exp e, punct ",", space 1, d]))
132 NONE es of
133 NONE => punct "[]"
134 | SOME d => dBox [punct "[", d, punct "]"])
135
136 | ELam (x, NONE, e) => dBox [punct "(\\", space 1, exp x, space 1,
137 punct "->", space 1, p_exp e, punct ")"]
138 | ELam (x, SOME t, e) => dBox [punct "(\\", space 1, exp x, space 1,
139 punct ":", space 1,
140 dBox [punct "(", p_typ t, punct ")"],
141 space 1, punct "->", space 1, p_exp e, punct ")"]
142 | EALam (x, p, e) => dBox [punct "(\\", space 1, exp x, space 1,
143 punct ":", space 1, p_pred p,
144 space 1, punct "->", space 1, p_exp e, punct ")"]
145
146 | EVar x => exp x
147 | EApp (e1, e2) => dBox [punct "(", p_exp e1, break {nsp = 1, offset = 0}, p_exp e2, punct ")"]
148
149 | ESkip => keyword "_"
150 | ESet (x, e) => dBox [exp x, space 1, punct "=", space 1, p_exp e]
151 | EGet (x1, x2, e) => dBox [dBox [exp x1, space 1, punct "<-",
152 space 1, exp x2, punct ";", space 1],
153 p_exp e]
154 | ESeq es => dBox (valOf (foldr (fn (e, NONE) => SOME [p_exp e]
155 | (e, SOME ds) => SOME (dBox [p_exp e, punct ";", newline] :: ds))
156 NONE es))
157 | ELocal (e1, e2) => dBox [keyword "let", space 1,
158 p_exp e1, space 1,
159 keyword "in", space 1,
160 p_exp e2, space 1,
161 keyword "end"]
162 | EWith (e1, (ESkip, _)) => dBox [p_exp e1, space 1, keyword "with", space 1, keyword "end"]
163 | EWith (e1, e2) => dBox [p_exp e1, space 1, keyword "with", p_exp e2, space 1, keyword "end"]
164
165 fun p_decl d =
166 case d of
167 DExternType name => style (HTMLDev.anchor ("T_" ^ name),
168 [dBox [keyword "extern", space 1,
169 keyword "type", space 1,
170 ident name]])
171 | DExternVal (name, t) => style (HTMLDev.anchor ("V_" ^ name),
172 [dBox [keyword "extern", space 1,
173 keyword "val", space 1,
174 ident name, space 1,
175 string ":", space 1,
176 p_typ t]])
177 | DVal (name, NONE, _) => string "Unannotated val declaration!"
178 | DVal (name, SOME t, _) => style (HTMLDev.anchor ("V_" ^ name),
179 [dBox [keyword "val", space 1,
180 ident name, space 1,
181 punct ":", space 1,
182 p_typ t]])
183 | DContext name => style (HTMLDev.anchor ("C_" ^ name),
184 [dBox [keyword "context", space 1,
185 ident name]])
186
187 fun p_decl_fref d =
188 case d of
189 DExternType name => dBox [keyword "extern", space 1,
190 keyword "type", space 1,
191 style (HTMLDev.link ("#T_" ^ name), [ident name])]
192 | DExternVal (name, t) => dBox [keyword "extern", space 1,
193 keyword "val", space 1,
194 style (HTMLDev.link ("#V_" ^ name), [ident name]),
195 space 1,
196 string ":", space 1,
197 p_typ t]
198 | DVal (name, NONE, _) => string "Unannotated val declaration!"
199 | DVal (name, SOME t, _) => dBox [keyword "val", space 1,
200 style (HTMLDev.link ("#V_" ^ name), [ident name]),
201 space 1,
202 punct ":", space 1,
203 p_typ t]
204 | DContext name => dBox [keyword "context", space 1,
205 style (HTMLDev.link ("#C_" ^ name), [ident name])]
206
207 fun output d =
208 let
209 val dev = HTMLDev.openDev {wid = 80,
210 textWid = NONE}
211 val myStream = SM.openStream dev
212 in
213 description (myStream, d);
214 SM.flushStream myStream;
215 HTMLDev.done dev
216 end
217
218 end