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.4 required=5.0 tests=AWL 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 B71BCBBCA for ; Fri, 21 Mar 2008 16:48:16 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsEAI5140fAXQIn/2dsb2JhbACKTZ9E X-IronPort-AV: E=Sophos;i="4.25,536,1199660400"; d="asc'?ml'?scan'208";a="9794362" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 21 Mar 2008 16:48:16 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m2LFmEH5006349 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 21 Mar 2008 16:48:16 +0100 X-IronPort-AV: E=Sophos;i="4.25,536,1199660400"; d="asc'?ml'?scan'208";a="9794361" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail1-relais-roc.national.inria.fr with SMTP; 21 Mar 2008 16:48:16 +0100 Received: by ausone.inria.fr (sSMTP sendmail emulation); Fri, _d Mar 2008 16:47:01 +0100 From: "Nicolas Pouillard" Cc: OCaml Mailing List Subject: Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold To: "Richard W.M. Jones" References: <20080321150639.GA29482@annexia.org> In-Reply-To: <20080321150639.GA29482@annexia.org> Date: Fri, 21 Mar 2008 16:46:56 +0100 Message-Id: <1206113824-sup-4922@port-ext16.ensta.fr> User-Agent: Sup/git Content-Type: multipart/signed; protocol="application/pgp-signature"; boundary="=-1206114421-317119-647-2271-6-="; micalg="pgp-sha1" MIME-Version: 1.0 X-Miltered: at concorde with ID 47E3D8BE.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 gettext:01 camlp:01 ocaml:01 struct:01 gettext:01 syntax:01 sig:01 syntax:01 sig:01 constructors:01 struct:01 val:01 expr:01 expr:01 X-Attachments: cset="UTF-8" type="application/octet-stream" name="pr_gettext.ml" name="pr_gettext.ml" type="application/octet-stream" name="pr_gettext_simple.ml" name="pr_gettext_simple.ml" type="application/octet-stream" name="pr_gettext_simple2.ml" name="pr_gettext_simple2.ml" type="application/pgp-signature" name="signature.asc" name="signature.asc" --=-1206114421-317119-647-2271-6-= Content-Type: multipart/mixed; boundary="=-1206114416-602057-647-3489-5-=" MIME-Version: 1.0 --=-1206114416-602057-647-3489-5-= Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Excerpts from Richard W.M. Jones's message of Fri Mar 21 16:06:40 +0100 2008: > I'm trying to translate Sylvain Le Gall's gettext module to use camlp4 > from ocaml 3.10.0. The module is a printer which folds over the AST > looking for certain types of function call by name. A simplified > version is shown below. > > This program is supposed to look for all instances of a function named > f applied to a string. > > I cannot for the life of me work out how to get this to compile. I've > tried about a dozen different variations of the module names, 'open', > 'include' etc. and got a dozen different errors. You where very close to something working... Essentially three errors: > ---------------------------------------------------------------------- > module Id = struct > let name = "pr_gettext" > let version = "$Id$" > end > > module Make (Syntax : Camlp4.Sig.Syntax) Here it's Camlp4.Sig.Camlp4Syntax otherwise types are abstract and you don't have access to constructors. > : Camlp4.Sig.Printer(Syntax.Ast).S = > struct > module Loc = Syntax.Loc > module Ast = Syntax.Ast > > class visitor = object > inherit Ast.fold as super > > val t = [] > method t = t > > method expr = function > | <:expr@loc< f $str:singular$ >> -> Here you bind loc but don't use it, however that's just a warning. > let t = str :: t in Here the variable is singular not str. > {< t = t >} > > | e -> super#expr e > end > > let print_interf ?input_file ?output_file _ = () > > let print_implem ?input_file ?output_file ast = > let visitor = (new visitor)#str_item in > let t = (visitor ast)#t in > List.iter prerr_endline t > end > > (* Register the new printer. *) > module M = Camlp4.Register.Printer(Id)(Make) And here it's Camlp4.Register.OCamlPrinter because Camlp4.Register.Printer is too abstract. I attach three working versions of it. To compile them I use ocamlbuild -tags camlp4of,use_camlp4 pr_gettext.cmo The first [1] is very closer to yours. However you don't really define a proper printer here prerr_endline, so if the goal is just to iterate over the AST [2] is a simpler version. But why make an effect while one can easily go one step further and produce an OCaml output that define the list of strings to be translated [3]. Cheers, [1]: pr_gettext.ml [2]: pr_gettext_simple.ml [3]: pr_gettext_simple2.ml -- Nicolas Pouillard aka Ertai --=-1206114416-602057-647-3489-5-= Content-Disposition: attachment; filename="pr_gettext.ml" Content-Type: application/octet-stream; name="pr_gettext.ml" Content-Transfer-Encoding: base64 bW9kdWxlIElkID0gc3RydWN0CiAgbGV0IG5hbWUgPSAicHJfZ2V0dGV4dCIK ICBsZXQgdmVyc2lvbiA9ICIkSWQkIgplbmQKCm1vZHVsZSBNYWtlIChTeW50 YXggOiBDYW1scDQuU2lnLkNhbWxwNFN5bnRheCkKICA6IENhbWxwNC5TaWcu UHJpbnRlcihTeW50YXguQXN0KS5TID0Kc3RydWN0CiAgbW9kdWxlIExvYyA9 IFN5bnRheC5Mb2MKICBtb2R1bGUgQXN0ID0gU3ludGF4LkFzdAogIGNsYXNz IHZpc2l0b3IgPSBvYmplY3QKICAgIGluaGVyaXQgQXN0LmZvbGQgYXMgc3Vw ZXIKCiAgICB2YWwgdCA9IFtdCiAgICBtZXRob2QgdCA9IHQKICAgIG1ldGhv ZCBleHByID0gZnVuY3Rpb24KICAgIHwgPDpleHByPCBmICRzdHI6c2luZ3Vs YXIkID4+IC0+CiAgICAgIGxldCB0ID0gc2luZ3VsYXIgOjogdCBpbgogICAg ICB7PCB0ID0gdCA+fQogICAgfCBlIC0+IHN1cGVyI2V4cHIgZQogIGVuZAoK ICBsZXQgcHJpbnRfaW50ZXJmID9pbnB1dF9maWxlOihfKSA/b3V0cHV0X2Zp bGU6KF8pIF8gPSAoKQoKICBsZXQgcHJpbnRfaW1wbGVtID9pbnB1dF9maWxl OihfKSA/b3V0cHV0X2ZpbGU6KF8pIGFzdCA9CiAgICBsZXQgdmlzaXRvciA9 IChuZXcgdmlzaXRvcikjc3RyX2l0ZW0gaW4KICAgIGxldCB0ID0gKHZpc2l0 b3IgYXN0KSN0IGluCiAgICBMaXN0Lml0ZXIgcHJlcnJfZW5kbGluZSB0CmVu ZAooKiBSZWdpc3RlciB0aGUgbmV3IHByaW50ZXIuICopCm1vZHVsZSBNID0g Q2FtbHA0LlJlZ2lzdGVyLk9DYW1sUHJpbnRlcihJZCkoTWFrZSkKCgo= --=-1206114416-602057-647-3489-5-= Content-Disposition: attachment; filename="pr_gettext_simple.ml" Content-Type: application/octet-stream; name="pr_gettext_simple.ml" Content-Transfer-Encoding: base64 b3BlbiBDYW1scDQuUHJlQ2FzdDs7CgpjbGFzcyB2aXNpdG9yID0gb2JqZWN0 CiAgaW5oZXJpdCBBc3QuZm9sZCBhcyBzdXBlcgoKICB2YWwgdCA9IFtdCiAg bWV0aG9kIHQgPSB0CiAgbWV0aG9kIGV4cHIgPSBmdW5jdGlvbgogIHwgPDpl eHByPCBmICRzdHI6c2luZ3VsYXIkID4+IC0+CiAgICBsZXQgdCA9IHNpbmd1 bGFyIDo6IHQgaW4KICAgIHs8IHQgPSB0ID59CiAgfCBlIC0+IHN1cGVyI2V4 cHIgZQplbmQ7OwoKbGV0IGZpbHRlciBhc3QgPQogIGxldCB2aXNpdG9yID0g KG5ldyB2aXNpdG9yKSNzdHJfaXRlbSBpbgogIGxldCB0ID0gKHZpc2l0b3Ig YXN0KSN0IGluCiAgTGlzdC5pdGVyIHByZXJyX2VuZGxpbmUgdDsKICBhc3QK OzsKCkFzdEZpbHRlcnMucmVnaXN0ZXJfc3RyX2l0ZW1fZmlsdGVyIGZpbHRl cjs7Cgo= --=-1206114416-602057-647-3489-5-= Content-Disposition: attachment; filename="pr_gettext_simple2.ml" Content-Type: application/octet-stream; name="pr_gettext_simple2.ml" Content-Transfer-Encoding: base64 b3BlbiBDYW1scDQuUHJlQ2FzdDs7CgpjbGFzcyB2aXNpdG9yID0gb2JqZWN0 CiAgaW5oZXJpdCBBc3QuZm9sZCBhcyBzdXBlcgoKICB2YWwgdCA9IFtdCiAg bWV0aG9kIHQgPSB0CiAgbWV0aG9kIGV4cHIgPSBmdW5jdGlvbgogIHwgPDpl eHByPCBmICRzdHI6c2luZ3VsYXIkID4+IC0+CiAgICBsZXQgdCA9IHNpbmd1 bGFyIDo6IHQgaW4KICAgIHs8IHQgPSB0ID59CiAgfCBlIC0+IHN1cGVyI2V4 cHIgZQplbmQ7OwoKbGV0IGZpbHRlciBhc3QgPQogIGxldCB2aXNpdG9yID0g KG5ldyB2aXNpdG9yKSNzdHJfaXRlbSBpbgogIGxldCB0ID0gKHZpc2l0b3Ig YXN0KSN0IGluCiAgbGV0IGdob3N0ID0gTG9jLmdob3N0IGluCiAgbGV0IGV4 cHIgPQogICAgTGlzdC5mb2xkX3JpZ2h0IGJlZ2luIGZ1biBzIGFjYyAtPgog ICAgICA8OmV4cHJAZ2hvc3Q8ICRzdHI6cyQgOjogJGFjYyQgPj4KICAgIGVu ZCB0IDw6ZXhwckBnaG9zdDwgW10gPj4KICBpbiA8OnN0cl9pdGVtQGdob3N0 PCBsZXQgc3RyaW5nc190b190cmFuc2xhdGUgPSAkZXhwOmV4cHIkID4+Cjs7 CgpBc3RGaWx0ZXJzLnJlZ2lzdGVyX3N0cl9pdGVtX2ZpbHRlciBmaWx0ZXI7 OwoK --=-1206114416-602057-647-3489-5-=-- --=-1206114421-317119-647-2271-6-= Content-Disposition: attachment; filename="signature.asc" Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkfj2HAACgkQj+FCNw9dwLn1ogCggyPReDvoT140vRNPploRRx/V oJUAn0zK0JpRMwkE4X55eObOKv15zCm+ =N3G8 -----END PGP SIGNATURE----- --=-1206114421-317119-647-2271-6-=--