caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [camlp4] expr_of_string, string_of_expr functions exist?
@ 2008-08-05 16:04 Richard Jones
  2008-08-05 17:43 ` [Caml-list] " Yitzhak Mandelbaum
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Richard Jones @ 2008-08-05 16:04 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list]  [camlp4] expr_of_string, string_of_expr functions exist?
  2008-08-05 16:04 [camlp4] expr_of_string, string_of_expr functions exist? Richard Jones
@ 2008-08-05 17:43 ` Yitzhak Mandelbaum
  2008-08-05 17:53 ` Christophe TROESTLER
  2008-08-06 19:58 ` Jeremy Yallop
  2 siblings, 0 replies; 5+ messages in thread
From: Yitzhak Mandelbaum @ 2008-08-05 17:43 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

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

On Aug 5, 2008, at 12:04 PM, Richard Jones wrote:

>
> 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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] expr_of_string, string_of_expr functions exist?
  2008-08-05 16:04 [camlp4] expr_of_string, string_of_expr functions exist? Richard Jones
  2008-08-05 17:43 ` [Caml-list] " Yitzhak Mandelbaum
@ 2008-08-05 17:53 ` Christophe TROESTLER
  2008-08-06 13:40   ` Richard Jones
  2008-08-06 19:58 ` Jeremy Yallop
  2 siblings, 1 reply; 5+ messages in thread
From: Christophe TROESTLER @ 2008-08-05 17:53 UTC (permalink / raw)
  To: rich; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] expr_of_string, string_of_expr functions exist?
  2008-08-05 17:53 ` Christophe TROESTLER
@ 2008-08-06 13:40   ` Richard Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2008-08-06 13:40 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list]  [camlp4] expr_of_string, string_of_expr functions exist?
  2008-08-05 16:04 [camlp4] expr_of_string, string_of_expr functions exist? Richard Jones
  2008-08-05 17:43 ` [Caml-list] " Yitzhak Mandelbaum
  2008-08-05 17:53 ` Christophe TROESTLER
@ 2008-08-06 19:58 ` Jeremy Yallop
  2 siblings, 0 replies; 5+ messages in thread
From: Jeremy Yallop @ 2008-08-06 19:58 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

Quoting Richard Jones <rich@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.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-08-06 19:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-05 16:04 [camlp4] expr_of_string, string_of_expr functions exist? Richard Jones
2008-08-05 17:43 ` [Caml-list] " Yitzhak Mandelbaum
2008-08-05 17:53 ` Christophe TROESTLER
2008-08-06 13:40   ` Richard Jones
2008-08-06 19:58 ` Jeremy Yallop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).