camlspotter avatar camlspotter committed 997077f

use of ocaml_conv for dump

Comments (0)

Files changed (3)

 OCAMLFLAGS    += -annot -w Ae
 
-OCAMLPACKS[]= spotlib
+OCAMLPACKS[]= spotlib ocaml_conv
+
+OCAMLDEPFLAGS= -syntax camlp4o -package meta_conv.syntax
+OCAMLPPFLAGS = -syntax camlp4o -package meta_conv.syntax 
 
 LIBFILES[] =
    printer
 open Spotlib.Spot
+open Ocaml_conv
 
 (** Display machine with Format like capability *)
 
     | Flush           (** "@." *)
     | Seq of t list
     | NOP
+  with conv(ocaml)
+  
+  let dump = Ocaml.format_with ocaml_of_t
 
   open Format
   let rec format ppf = function
     | String s -> string ppf s
-    | Box (n, tk) -> box ppf n; format ppf tk; close_box ppf ()
-    | VBox (n ,tk) -> vbox ppf n; format ppf tk; close_box ppf ()
+    | Box (n, tk) -> 
+        box ppf n; format ppf tk; close_box ppf ()
+    | VBox (n ,tk) -> 
+        vbox ppf n; format ppf tk; close_box ppf ()
     | Cut -> cut ppf
     | Space -> space ppf 
     | Flush -> flush ppf; newline ppf
 let vbox : int -> ppr -> ppr = 
   fun offset t a l -> VBox (offset, t a l)
 
+let do_Seq xs =
+  Seq (List.concat_map (function 
+    | Seq ys -> ys
+    | y -> [y]) xs)
+
 let (++) : ppr -> ppr -> ppr = 
-  fun p1 p2 a l -> Seq [p1 a l; p2 a l]
+  fun p1 p2 a l -> do_Seq [p1 a l; p2 a l]
 
 let cut : ppr = 
   fun _out_ops _out_lev -> Cut
   fun _out_pos _out_lev -> Flush
 
 let seq : ppr list -> ppr
-  = fun ps a l -> Seq (List.map (fun p -> p a l) ps)
+  = fun ps a l -> do_Seq (List.map (fun p -> p a l) ps)
 
 let string : string -> ppr
   = fun s _out_pos _out_lev -> String s
 
 let binop : assoc -> level -> op:ppr -> ppr -> ppr -> ppr =
   fun assoc lev ~op:sep l r ->
-    parenbox assoc lev (level lev (left l++ sep ++ right r))
+    parenbox assoc lev (level lev (left l ++ sep ++ right r))
 
 let list : level -> ppr -> ppr list -> ppr = 
   fun lev sep f_elems ->
       | `Same -> t ++ space
       | `Weaker | `Stronger -> t
 
+let parens left right ppr = string left ++ level (-1.0) ppr ++ string right
+
 module OCaml = struct
   let mbinop assoc lev sep = binop assoc lev ~op:(space ++ string sep ++ space)
   let (+) = mbinop Left 1.0 "+"
 end) = struct
   open M
   let format ppf v = Token.format ppf (ppr v Noassoc 0.0)
+  let dump ppf v = Token.dump ppf (ppr v Noassoc 0.0)
   let buffer buf ?(assoc=Noassoc) ?(level=0.0) v = Token.buffer buf (ppr v assoc level)
   let show ?(assoc=Noassoc) ?(level=0.0) v = Token.show (ppr v assoc level)
 end
   let test t answer = 
     let str = Token.show (t Noassoc 0.0) in
     Format.eprintf "%s =?= %s@." str answer;
-    if str <> answer then failwith "FAILED"
+    if str <> answer then failwith "FAILED";
+    Format.eprintf "%a@.@." Token.format  (t Noassoc 0.0);
+    Format.eprintf "%a@.@." Token.dump  (t Noassoc 0.0)
 
   let test () =
     test (num 0) "0";
 module Token : sig
   type t
   val format : Format.t -> t -> unit
+  val dump : Format.t -> t -> unit
   val buffer : Buffer.t -> t -> unit
   val show : t -> string
 end
 (** Put the arg in the specified level *)
 val reset : 'a t -> 'a t          
 (** Put the arg in "noassoc" env and 0.0 level *)
+(* CR jfuruse: Should be -1.0 ? *)
 
 val check_against_current_level : level -> [ `Same | `Stronger | `Weaker ] t
 (** Compare the argument against the current level:
 val prefix   : level -> op:ppr -> ppr -> ppr
 val postfix  : level -> op:ppr -> ppr -> ppr
 
+val parens : string -> string -> ppr -> ppr
+(** Surround the given ppr with the left and right strings.
+    The level for ppr is reset to -1.0. *)
+
 (** OCaml like printers *)
 module OCaml : sig
   val ( + )        : ppr -> ppr -> ppr
 module MakeDrivers(M : sig type t val ppr : t -> ppr end) : sig
   open M
   val format : Format.t -> t -> unit
+  val dump : Format.t -> t -> unit
   val buffer : Buffer.t -> ?assoc:assoc -> ?level:level -> t -> unit
   val show : ?assoc:assoc -> ?level:level -> t -> string
 end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.