Pretty-printing
[hcoop/domtool2.git] / src / print.sml
diff --git a/src/print.sml b/src/print.sml
new file mode 100644 (file)
index 0000000..7f182b9
--- /dev/null
@@ -0,0 +1,120 @@
+(* HCoop Domtool (http://hcoop.sourceforge.net/)
+ * Copyright (c) 2006, Adam Chlipala
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*)
+
+(* Pretty-printing Domtool configuration file ASTs *)
+
+structure Print :> PRINT = struct
+
+open Ast
+
+structure SM = TextIOPP
+
+structure PD = PPDescFn(SM)
+open PD
+
+fun dBox ds = hovBox (PPS.Rel 1, ds)
+fun dvBox ds = vBox (PPS.Rel 0, ds)
+fun ivBox ds = vBox (PPS.Rel 1, ds)
+
+fun parenIf pn ds =
+    if pn then
+       dBox (string "(" :: ds @ [string ")"])
+    else
+       dBox ds
+
+fun p_pred' pn (p, _) =
+    case p of
+       CRoot => string "Root"
+      | CConst s => string s
+      | CPrefix p => dBox [string "^", p_pred' true p]
+      | CNot p => dBox [string "!", p_pred' true p]
+      | CAnd (p1, p2) =>
+       parenIf pn [p_pred' true p1, space 1, string "&", space 1, p_pred' true p2]
+
+val p_pred = p_pred' false
+
+fun p_predBoxed p = dBox [string "[", p_pred p, string "]"]
+
+fun p_typ' pn (t, _) =
+    case t of
+       TBase s => string s
+      | TList t => dBox [string "[", p_typ' false t, string "]"]
+      | TArrow (t1, t2) =>
+       parenIf pn [p_typ' true t1, space 1, string "->", space 1, p_typ' true t2]
+      | TAction (p, r1, r2) =>
+       parenIf pn [p_predBoxed p, space 1, p_record r1, space 1,
+                   string "->", space 1, p_record r2]
+and p_record r =
+    case StringMap.foldri (fn (name, t, d) =>
+                             SOME (case d of
+                                       NONE => dBox [string name, space 1,
+                                                     string ":", space 1, p_typ t]
+                                     | SOME d => dBox [dBox [string name, space 1,
+                                                             string ":", space 1, p_typ t],
+                                                       string ",", space 1, d]))
+                         NONE r of
+       NONE => string "{}"
+      | SOME d => dBox [string "{", d, string "}"]
+
+and p_typ t = p_typ' false t
+
+fun p_exp (e, _) =
+    case e of
+       EInt n => string (Int.toString n)
+      | EString s => string (String.concat ["\"", String.toString s, "\""])
+      | EList es =>
+       (case foldr (fn (e, d) =>
+                       SOME (case d of
+                                 NONE => p_exp e
+                               | SOME d => dBox [p_exp e, string ",", space 1, d]))
+                   NONE es of
+            NONE => string "[]"
+          | SOME d => dBox [string "[", d, string "]"])
+
+      | ELam (x, t, e) => dBox [string "(\\", space 1, string x, space 1,
+                               string ":", space 1,
+                               dBox [string "(", p_typ t, string ")"],
+                               space 1, string "->", space 1, p_exp e, string ")"]
+      | EVar x => string x
+      | EApp (e1, e2) => dBox [string "(", p_exp e1, break {nsp = 1, offset = 0}, p_exp e2, string ")"]
+
+      | ESet (x, e) => dBox [string x, space 1, string "=", space 1, p_exp e]
+      | EGet (x1, x2, e) => dBox [dBox [string x1, space 1, string "<-",
+                                       space 1, string x2, string ";", space 1],
+                                 p_exp e]
+      | ESeq es => dBox (valOf (foldr (fn (e, NONE) => SOME [p_exp e]
+                                       | (e, SOME ds) => SOME (dBox [p_exp e, string ";", space 1] :: ds))
+                                     NONE es))
+      | ELocal (ESeq [e1, e2], _) => dBox [string "let", space 1,
+                                          p_exp e1, space 1,
+                                          string "in", space 1,
+                                          p_exp e2, space 1,
+                                          string "end"]
+      | ELocal _ => raise Fail "Unexpected ELocal form"
+
+fun print d =
+    let
+       val myStream = SM.openOut {dst = TextIO.stdOut,
+                                  wid = 80}
+    in
+       description (myStream, d);
+       SM.newline myStream;
+       SM.closeStream myStream
+    end
+
+end