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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id D74C9BC69 for ; Wed, 17 Oct 2007 00:56:31 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAE/eFEfAXQImh2dsb2JhbACOTgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,284,1188770400"; d="scan'208";a="18108988" Received: from discorde.inria.fr ([192.93.2.38]) by mail4-smtp-sop.national.inria.fr with ESMTP; 17 Oct 2007 00:56:31 +0200 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l9GMuVwZ005737 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 17 Oct 2007 00:56:31 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACPeFEdA6aazkmdsb2JhbACOTgEBAQEHBAQTFg X-IronPort-AV: E=Sophos;i="4.21,284,1188770400"; d="scan'208";a="3122593" Received: from py-out-1112.google.com ([64.233.166.179]) by mail2-smtp-roc.national.inria.fr with ESMTP; 17 Oct 2007 00:56:30 +0200 Received: by py-out-1112.google.com with SMTP id u52so3949582pyb for ; Tue, 16 Oct 2007 15:56:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:received:date:to:subject:message-id:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; bh=k7Jx9s4Ib/OoeYrGQ2yxUKmh7CsJ4nHvdQ44yMn/o9k=; b=OaoHkvV1GBl6DTXiYBZwIkS+oye+emFEflKM2DMCqFUJGbnE3fVCF1ty/7OVR5J8epYSLv1OZ34asOOnwYJaU6nj5rPgf2UTmR4feL6vfVrgCnjvWGCMSDXW0IW77tIE8RmXntcsnwIl+nJ7vivrIGo6ZwTAgJbGSoWrIhr62ys= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:date:to:subject:message-id:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; b=OBlej3Hj9+2I4XCE5foI/fTfX/L/V+sztHTnD9+HgQKYbN8MHvnSWq29AEZG/OBZSewAEYGXoIOgR2M1R0CoysiVNH5w4Lz8NFCIbJGig+6NnQsW9smmj5UW1rv2j9cQigKgxWANKNTG1aMG1Oyj2er02MuL8lgGFt2m6DoX5v4= Received: by 10.65.59.11 with SMTP id m11mr14896704qbk.1192575388966; Tue, 16 Oct 2007 15:56:28 -0700 (PDT) Received: from localhost ( [83.201.28.205]) by mx.google.com with ESMTPS id d27sm7539837nfh.2007.10.16.15.56.24 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 16 Oct 2007 15:56:26 -0700 (PDT) Received: by localhost (sSMTP sendmail emulation); Wed, 17 Oct 2007 00:57:48 +0200 Date: Wed, 17 Oct 2007 00:57:47 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] Camlp4 3.10: printing OCaml expression within extension: how? Message-ID: <20071016225747.GA31813@localhost> Mail-Followup-To: Julien Moutinho , caml-list@inria.fr References: <471562FB.3000808@tepkom.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <471562FB.3000808@tepkom.ru> User-Agent: Mutt/1.5.16 (2007-06-11) From: Julien Moutinho X-Miltered: at discorde with ID 4715419F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 ocaml:01 ocaml:01 syntax:01 struct:01 syntax:01 camlp:01 sig:01 struct:01 interf:01 expr:01 expr:01 buf:01 buffer:01 buf:01 On Wed, Oct 17, 2007 at 01:18:51AM +0000, Dmitri Boulytchev wrote: > is it possible to convert OCaml expression into string within syntax > extension? In past version > it could be done by string_of function. Maybe you can do like this: % cat pa_soe.ml4 module Id = struct let name = "Pa_soe" let version = "$Id: Pa_soe mer 17 oct 2007 00:19:12 CEST $" end module Make (Syntax : Camlp4.Sig.Camlp4Syntax) = struct include Syntax let print_interf ?input_file ?output_file ast = () let print_implem ?input_file ?output_file ast = () module PP = Camlp4.Printers.OCaml.Make(Syntax) let pp = new PP.printer ~comments: false () let string_of_expr expr = let buf = Buffer.create 42 in let () = Format.bprintf buf "%a@?" pp#expr expr in let str = Buffer.contents buf in if str = "" then assert false else str EXTEND Gram GLOBAL: expr; expr: LEVEL "." (* A macro building a 2-tuple string * expr. * Example: `(name) gives ("name", name) *) [ [ "`"; "("; e = SELF; ")" -> <:expr< $str:string_of_expr e$,$exp:e$ >> ] ]; END end module M = Camlp4.Register.OCamlSyntaxExtension(Id)(Make) % camlp4of -o pa_soe.ml -loc _loc -printer o -impl pa_soe.ml4 % ocamlc -c -I +camlp4 pa_soe.ml % cat test.ml4 prerr_endline (fst `((=) (6 * 9) 42)) % camlp4of -o test.ml -loc _loc pa_soe.cmo -printer o -impl test.ml4 % ocamlc -c test.ml % ocamlc -o test.tpo test.cmo % ./test.tpo (6 * 9) = 42 Bonus, how I have found this: % cd /usr/local/src/ocaml/_cvs/ocaml/camlp4 % grep -R string_of * > /tmp/out % vim /tmp/out There is a line: boot/camlp4boot.ml: let string_of_patt patt = % vim boot/camlp4boot.ml There is a line: let () = Format.bprintf buf "%a@?" pp#patt patt in Match against "method patt" 2nd match: just above (with vim's line folding) there is a "method expr" % grep -R "method expr" * > /tmp/out2 % vim /tmp/out2 Encouraging lines: Camlp4/Printers/OCaml.ml: method expr_list f = Camlp4/Printers/OCaml.ml: method expr_list_cons simple f e = Camlp4/Printers/OCaml.ml: method expr f e = % vim Camlp4/Printers/OCaml.ml Seems like the holy grail. pa_soe.ml4 is derivated from the module Make holding string_of_patt (boot/camlp4boot.ml)