Version française
Home     About     Download     Resources     Contact us    
Browse thread
[camlp4] expr_of_string, string_of_expr functions exist?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe TROESTLER <Christophe.Troestler+ocaml@u...>
Subject: Re: [Caml-list] [camlp4] expr_of_string, string_of_expr functions exist?
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