1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
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
.
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
.
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
.
19 (* Pretty
-printing Domtool configuration file ASTs
*)
21 functor PrintFn (PF
: PRINTFN_INPUT
) :> PRINTFN_OUTPUT
where type rendering
= PF
.rendering
= struct
26 fun dBox ds
= hovBox (PPS
.Rel
1, ds
)
27 fun dvBox ds
= vBox (PPS
.Rel
0, ds
)
28 fun ivBox ds
= vBox (PPS
.Rel
1, ds
)
32 val file
' = #
file (OS
.Path
.splitDirFile file
)
33 val file
' = #
base (OS
.Path
.splitBaseExt file
')
40 dBox (punct
"(" :: ds @
[punct
")"])
44 fun p_pred
' pn (p
, _
) =
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
]
51 parenIf pn
[p_pred
' true p1
, space
1, punct
"&", space
1, p_pred
' true p2
]
53 val p_pred
= p_pred
' false
55 fun p_predBoxed p
= dBox
[punct
"[", p_pred p
, punct
"]"]
57 fun p_typ
' pn (t
, _
) =
60 | TList t
=> dBox
[punct
"[", p_typ
' false t
, punct
"]"]
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
])
70 parenIf pn
[p_pred
' false p
, space
1, punct
"=>", space
1, p_typ
' false t
]
72 | TError
=> keyword
"<error>"
73 |
TUnif (_
, ref (SOME t
)) => p_typ
' pn t
74 |
TUnif (name
, ref NONE
) => string ("<" ^ name ^
">")
77 case StringMap
.foldri (fn (name
, t
, d
) =>
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
]))
86 | SOME d
=> dBox
[punct
"{", d
, punct
"}"]
88 and p_typ t
= p_typ
' false t
90 fun p_exp
' pn (e
, _
) =
92 EInt n
=> lit (Int.toString n
)
93 | EString s
=> lit (String.concat
["\"", String.toString s
, "\""])
95 (case foldr (fn (e
, d
) =>
98 | SOME d
=> dBox
[p_exp e
, punct
",", space
1, d
]))
101 | SOME d
=> dBox
[punct
"[", d
, punct
"]"])
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,
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
")"]
114 |
EApp (e1
, e2
) => parenIf pn
[p_exp e1
, break
{nsp
= 1, offset
= 0}, p_exp
' true e2
]
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],
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
))
124 |
ELocal (e1
, e2
) => dBox
[keyword
"let", space
1,
126 keyword
"in", space
1,
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"]
131 and p_exp e
= p_exp
' false e
135 DExternType name
=> anchor ("T_" ^ name
,
136 dBox
[keyword
"extern", space
1,
137 keyword
"type", space
1,
139 |
DExternVal (name
, t
) => anchor ("V_" ^ name
,
140 dBox
[keyword
"extern", space
1,
141 keyword
"val", space
1,
145 |
DVal (name
, NONE
, _
) => string "Unannotated val declaration!"
146 |
DVal (name
, SOME t
, _
) => anchor ("V_" ^ name
,
147 dBox
[keyword
"val", space
1,
151 | DContext name
=> anchor ("C_" ^ name
,
152 dBox
[keyword
"context", space
1,
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
),
166 |
DVal (name
, NONE
, _
) => string "Unannotated val declaration!"
167 |
DVal (name
, SOME t
, _
) => dBox
[keyword
"val", space
1,
168 link ("#V_" ^ name
, ident name
),
172 | DContext name
=> dBox
[keyword
"context", space
1,
173 link ("#C_" ^ name
, ident name
)]
177 val myStream
= openStream ()
179 description (myStream
, d
);
180 PPS
.flushStream myStream
;
184 fun preface (s
, d
) = output (PD
.hovBox (PD
.PPS
.Rel
0,
185 [PD
.string s
, PD
.space
1, d
]))