Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] [camlp4] expr_of_string, string_of_expr functions exist?

18 views
Skip to first unread message

Richard Jones

unread,
Aug 5, 2008, 12:04:39 PM8/5/08
to caml...@inria.fr

Maybe a simple question, but does camlp4 have functions to turn
expr and patt AST structures to and from strings?

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

Yitzhak Mandelbaum

unread,
Aug 5, 2008, 1:43:51 PM8/5/08
to Richard Jones, caml...@inria.fr
I don't know about the new camlp4, but in the old one the code looked
something like this (where my AST is a list of str_item-s):

open Pcaml

let ast_to_strings ast =
List.map (function str_item -> string_of pr_str_item str_item) ast

--Yitzhak

-----------------------------
Yitzhak Mandelbaum

Christophe TROESTLER

unread,
Aug 5, 2008, 1:53:24 PM8/5/08
to ri...@annexia.org, caml...@inria.fr
On Tue, 5 Aug 2008 17:04:26 +0100, Richard Jones wrote:
>
> Maybe a simple question, but does camlp4 have functions to turn
> expr and patt AST structures to and from strings?

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

Richard Jones

unread,
Aug 6, 2008, 9:41:05 AM8/6/08
to caml...@inria.fr
On Tue, Aug 05, 2008 at 07:53:08PM +0200, Christophe TROESTLER wrote:
> On Tue, 5 Aug 2008 17:04:26 +0100, Richard Jones wrote:
> >
> > Maybe a simple question, but does camlp4 have functions to turn
> > expr and patt AST structures to and from strings?
>
> Parsing:
> Printing:
[..]

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

_______________________________________________

Jeremy Yallop

unread,
Aug 6, 2008, 3:59:27 PM8/6/08
to Richard Jones, caml...@inria.fr
Quoting Richard Jones <ri...@annexia.org>:
> Maybe a simple question, but does camlp4 have functions to turn
> expr and patt AST structures to and from strings?

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.

0 new messages