Rich.
--
Richard Jones
Red Hat
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
open Pcaml
let ast_to_strings ast =
List.map (function str_item -> string_of pr_str_item str_item) ast
--Yitzhak
-----------------------------
Yitzhak Mandelbaum
Parsing:
open Camlp4.PreCast
let loc = Loc.ghost;;
Syntax.AntiquotSyntax.parse_expr loc "x = 1";;
Syntax.AntiquotSyntax.parse_patt loc "Failure _";;
Printing: I do not know a way to print to a string, only to a file.
I guess this asymmetry is due to the fact that a string output was
never needed... (but it would be useful to me too!) Moreover, you can
only print str_item's, so you have to wrap your expr and patt. E.g.
let e = <:expr< 1 + 1 >>;;
Printers.OCaml.print_implem ~output_file:"/tmp/o.ml" <:str_item< $exp: e$ >>;;
(* will print
let _ = 1 + 1;;
*)
Printers.OCaml.print_implem ~output_file:"/tmp/o.ml"
(let _loc = Ast.loc_of_expr e in Ast.StExp(_loc, e));;
(* will print
1 + 1;;
(but I do not know whether this is a feature!) *)
Hope it helps,
ChriS
Thanks, these techniques worked perfectly for me. I too could do with
'print-to-string' functions though :-)
As some background to this, I am trying to serialize Bitstring_-
persistent[1] structures into a more durable format (currently based on
XML).
Rich.
[1] http://et.redhat.com/~rjones/bitstring/html/Bitstring_persistent.html
--
Richard Jones
Red Hat
_______________________________________________
One way to do it is to use the Camlp4.Printers module. To illustrate
the idea,
here's a complete working program that prints out all expressions found in a
file.
open Camlp4.PreCast
open Syntax
(* Instantiate the module that allows us to print AST values.
This gives us an object with methods `expr', `patt', etc.,
each with type `Format.formatter -> t -> unit'. *)
let printer = let module P = Camlp4.Printers.OCaml.Make(Syntax)
in new P.printer ()
(* Transform a formatter function into a to_string function. *)
let format_to_string (f : Format.formatter -> 'a -> unit) (v : 'a) : string =
let buf = Buffer.create 128 in
let fmt = Format.formatter_of_buffer buf in
let () = f fmt v in
let () = Format.pp_print_flush fmt () in
Buffer.contents buf
(* Some to_string functions for particular AST types. *)
let expr_of_string : Ast.expr -> string = format_to_string printer#expr
let patt_of_string : Ast.patt -> string = format_to_string printer#patt
(* A "filter" that prints out expressions encountered in the AST
(top-down). *)
let print_stuff = object
inherit Ast.map as super
method expr e =
let () = print_endline ("expr: "^ expr_of_string e) in
super#expr e
end
(* Register the filter with camlp4 *)
let () = AstFilters.register_str_item_filter print_stuff#str_item
Compile:
ocamlc -c -I +camlp4 print.ml
Run:
camlp4of print.cmo file.ml
Hope this helps,
Jeremy.
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.