From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 51205BBAF for ; Wed, 6 Aug 2008 21:59:14 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwEACefmUjAXQIm/2dsb2JhbACKcoU6nW0 X-IronPort-AV: E=Sophos;i="4.31,316,1215381600"; d="scan'208";a="15866003" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 06 Aug 2008 21:59:14 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m76JxDSq004116 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 6 Aug 2008 21:59:13 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: As8BAJ+fmUiB1xBmmmdsb2JhbACKcoU6gRABAQEBAQgFCAcRBZwr X-IronPort-AV: E=Sophos;i="4.31,316,1215381600"; d="scan'208";a="27999704" Received: from treacle.ucs.ed.ac.uk ([129.215.16.102]) by mail4-smtp-sop.national.inria.fr with ESMTP; 06 Aug 2008 21:59:13 +0200 Received: from lmtp2.ucs.ed.ac.uk (lmtp2.ucs.ed.ac.uk [129.215.149.71]) by treacle.ucs.ed.ac.uk (8.13.8/8.13.4) with ESMTP id m76JwvGw022797; Wed, 6 Aug 2008 20:58:59 +0100 (BST) Received: from smtp.sms.ed.ac.uk (mailwfe2.ucs.ed.ac.uk [129.215.149.70]) by lmtp2.ucs.ed.ac.uk (8.13.8/8.13.7) with ESMTP id m76JwvMD002422; Wed, 6 Aug 2008 20:58:57 +0100 (BST) Received: from 88-107-44-236.dynamic.dsl.as9105.com (88-107-44-236.dynamic.dsl.as9105.com [88.107.44.236]) by www.sms.ed.ac.uk (Horde MIME library) with HTTP; Wed, 06 Aug 2008 20:58:57 +0100 Message-ID: <20080806205857.z2fq5n7i0w4gww0s@www.sms.ed.ac.uk> Date: Wed, 06 Aug 2008 20:58:57 +0100 From: Jeremy Yallop To: Richard Jones Cc: caml-list@inria.fr Subject: Re: [Caml-list] [camlp4] expr_of_string, string_of_expr functions exist? References: <20080805160426.GA698@annexia.org> In-Reply-To: <20080805160426.GA698@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes"; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable X-Edinburgh-Scanned: at treacle.ucs.ed.ac.uk with MIMEDefang 2.60, Sophie, Sophos Anti-Virus, Clam AntiVirus X-Scanned-By: MIMEDefang 2.60 on 129.215.16.102 X-Scanned-By: MIMEDefang 2.52 on 129.215.149.71 X-Miltered: at discorde with ID 489A0291.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 expr:01 expr:01 camlp:01 syntax:01 ocaml:01 syntax:01 buf:01 buffer:01 buffer:01 buf:01 top-down:01 endline:01 ocamlc:01 cmo:01 Quoting Richard Jones : > 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 =20 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 =3D let module P =3D 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 = =3D let buf =3D Buffer.create 128 in let fmt =3D Format.formatter_of_buffer buf in let () =3D f fmt v in let () =3D Format.pp_print_flush fmt () in Buffer.contents buf (* Some to_string functions for particular AST types. *) let expr_of_string : Ast.expr -> string =3D format_to_string printer#expr let patt_of_string : Ast.patt -> string =3D format_to_string printer#patt (* A "filter" that prints out expressions encountered in the AST =20 (top-down). *) let print_stuff =3D object inherit Ast.map as super method expr e =3D let () =3D print_endline ("expr: "^ expr_of_string e) in super#expr e end (* Register the filter with camlp4 *) let () =3D 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. --=20 The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.