| 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 | |
| 21 | functor PrintFn (PF : PRINTFN_INPUT) :> PRINTFN_OUTPUT where type rendering = PF.rendering = struct |
| 22 | |
| 23 | open Ast PF |
| 24 | open PD |
| 25 | |
| 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) |
| 29 | |
| 30 | fun 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 | |
| 38 | fun parenIf pn ds = |
| 39 | if pn then |
| 40 | dBox (punct "(" :: ds @ [punct ")"]) |
| 41 | else |
| 42 | dBox ds |
| 43 | |
| 44 | fun 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 | |
| 53 | val p_pred = p_pred' false |
| 54 | |
| 55 | fun p_predBoxed p = dBox [punct "[", p_pred p, punct "]"] |
| 56 | |
| 57 | fun 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 | |
| 76 | and 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 | |
| 88 | and p_typ t = p_typ' false t |
| 89 | |
| 90 | fun 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, NONE, x2, e) => parenIf pn [dBox [exp x1, space 1, punct "<-", |
| 119 | space 1, exp x2, punct ";", space 1], |
| 120 | p_exp e] |
| 121 | | EGet (x1, SOME t, x2, e) => parenIf pn [dBox [exp x1, space 1, punct ":", space 1, p_typ t, |
| 122 | space 1, punct "<-", |
| 123 | space 1, exp x2, punct ";", space 1], |
| 124 | p_exp e] |
| 125 | | ESeq es => parenIf pn (valOf (foldr (fn (e, NONE) => SOME [p_exp e] |
| 126 | | (e, SOME ds) => SOME (dBox [p_exp e, punct ";", newline] :: ds)) |
| 127 | NONE es)) |
| 128 | | ELocal (e1, e2) => dBox [keyword "let", space 1, |
| 129 | p_exp e1, space 1, |
| 130 | keyword "in", space 1, |
| 131 | p_exp e2, space 1, |
| 132 | keyword "end"] |
| 133 | | EWith (e1, (ESkip, _)) => dBox [p_exp e1, space 1, keyword "with", space 1, keyword "end"] |
| 134 | | EWith (e1, e2) => dBox [p_exp e1, space 1, keyword "with", p_exp e2, space 1, keyword "end"] |
| 135 | | EIf (e1, e2, e3) => dBox [keyword "if", space 1, p_exp e1, |
| 136 | space 1, keyword "then", space 1, p_exp e2, |
| 137 | space 1, keyword "else", space 1, p_exp e3] |
| 138 | and p_exp e = p_exp' false e |
| 139 | |
| 140 | fun p_decl d = |
| 141 | case d of |
| 142 | DExternType name => anchor ("T_" ^ name, |
| 143 | dBox [keyword "extern", space 1, |
| 144 | keyword "type", space 1, |
| 145 | ident name]) |
| 146 | | DExternVal (name, t) => anchor ("V_" ^ name, |
| 147 | dBox [keyword "extern", space 1, |
| 148 | keyword "val", space 1, |
| 149 | ident name, space 1, |
| 150 | string ":", space 1, |
| 151 | p_typ t]) |
| 152 | | DVal (name, NONE, _) => string "Unannotated val declaration!" |
| 153 | | DVal (name, SOME t, _) => anchor ("V_" ^ name, |
| 154 | dBox [keyword "val", space 1, |
| 155 | ident name, space 1, |
| 156 | punct ":", space 1, |
| 157 | p_typ t]) |
| 158 | | DEnv (name, NONE, _) => string "Unannotated env declaration!" |
| 159 | | DEnv (name, SOME t, _) => anchor ("D_" ^ name, |
| 160 | dBox [keyword "var", space 1, |
| 161 | ident name, space 1, |
| 162 | punct ":", space 1, |
| 163 | p_typ t]) |
| 164 | | DContext name => anchor ("C_" ^ name, |
| 165 | dBox [keyword "context", space 1, |
| 166 | ident name]) |
| 167 | |
| 168 | fun p_decl_fref d = |
| 169 | case d of |
| 170 | DExternType name => dBox [keyword "extern", space 1, |
| 171 | keyword "type", space 1, |
| 172 | link ("#T_" ^ name, ident name)] |
| 173 | | DExternVal (name, t) => dBox [keyword "extern", space 1, |
| 174 | keyword "val", space 1, |
| 175 | link ("#V_" ^ name, ident name), |
| 176 | space 1, |
| 177 | string ":", space 1, |
| 178 | p_typ t] |
| 179 | | DVal (name, NONE, _) => string "Unannotated val declaration!" |
| 180 | | DVal (name, SOME t, _) => dBox [keyword "val", space 1, |
| 181 | link ("#V_" ^ name, ident name), |
| 182 | space 1, |
| 183 | punct ":", space 1, |
| 184 | p_typ t] |
| 185 | | DEnv (name, NONE, _) => string "Unannotated var declaration!" |
| 186 | | DEnv (name, SOME t, _) => dBox [keyword "var", space 1, |
| 187 | link ("#D_" ^ name, ident name), |
| 188 | space 1, |
| 189 | punct ":", space 1, |
| 190 | p_typ t] |
| 191 | | DContext name => dBox [keyword "context", space 1, |
| 192 | link ("#C_" ^ name, ident name)] |
| 193 | |
| 194 | fun output d = |
| 195 | let |
| 196 | val myStream = openStream () |
| 197 | in |
| 198 | description (myStream, d); |
| 199 | PPS.flushStream myStream; |
| 200 | closeStream myStream |
| 201 | end |
| 202 | |
| 203 | fun preface (s, d) = output (PD.hovBox (PD.PPS.Rel 0, |
| 204 | [PD.string s, PD.space 1, d])) |
| 205 | |
| 206 | end |