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.1 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 6C927BC6B for ; Tue, 6 Nov 2007 20:10:33 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAE9LMEfAXQImh2dsb2JhbACOfwEICik X-IronPort-AV: E=Sophos;i="4.21,380,1188770400"; d="scan'208";a="5520364" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 06 Nov 2007 20:10:29 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id lA6JASn2006460 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 6 Nov 2007 20:10:29 +0100 X-IronPort-AV: E=Sophos;i="4.21,380,1188770400"; d="scan'208";a="5520355" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail3-relais-sop.national.inria.fr with SMTP; 06 Nov 2007 20:10:07 +0100 Received: by ausone.inria.fr (sSMTP sendmail emulation); Tue, _d Nov 2007 20:09:28 +0100 From: "Nicolas Pouillard" Cc: caml-list Subject: Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files To: zack References: <20071106181431.GA19764@takhisis.invalid> In-Reply-To: <20071106181431.GA19764@takhisis.invalid> Date: Tue, 06 Nov 2007 20:09:28 +0100 Message-Id: <1194375800-sup-2076@ausone.local> User-Agent: Sup/0.2 Content-Type: text/plain; charset=UTF-8 X-Miltered: at discorde with ID 4730BC24.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 ocaml:01 ocaml:01 patching:01 syntax:01 camlp:01 wiki:01 wiki:01 pcaml:01 syntax:01 sig:01 parser:01 occurences:01 mli:01 toplevel:01 Excerpts from zack's message of Tue Nov 06 19:14:31 +0100 2007: > I need to write a simple wrapper around the OCaml top-level. Besides > executing top-level phrases as usual, the wrapper should "patch" the > pretty-printing of both input phrases and of top-level output. The kind > of patching I would need ranges from substituting ascii arts like "->" > for suitable unicode symbols to reworking abstract syntax trees of OCaml > values. > > I would have an idea of where to start with camlp5, but not the faintest > with (the new) camlp4. The stuff currently on the wiki does not help. So > here they go some questions (I'll be happy to write the corresponding > articles in the camlp4 wiki ... assuming I'll manage to find the needed > info here!) Deal. :) > - what's the corresponding camlp4 idiom for the old "Pcaml.parse_implem" > (which used to parse str_items starting from a "char Stream.t")? > [ Note that I need to parse the revised syntax, but I doubt this will > make a sensible difference. ] Looking for parse_implem in Camlp4/Sig.ml that some module have the signature Parser(Ast).S, searching for occurences of this signature leads me to two signatures Syntax and Camlp4Syntax. Then I go to Camlp4/PreCast.mli where all precasted modules lives. Here I found that Camlp4.PreCast.Syntax have the Camlp4Syntax type. So Camlp4.PreCast.Syntax.parse_implem is what you searched. However you perhaps need a function more precise that reading an implementation. You perhaps prefer just reading a toplevel phrase for this you can use: open Camlp4.PreCast;; let strm = ... in Syntax.Gram.parse loc Syntax.top_phrase strm > - what's the camlp4 idiom for the old "Pcaml.print_implem" (which used > to print str_items on a given formatter) > [ Same remark as above for the revised syntax. ] Camlp4.PreCast.Syntax.print_implem or more to print something in particular: module PP = Camlp4.Printers.OCamlr.Make(Syntax) let pp = new PP.printer ();; ... pp#ctyp ... > - what's the camlp4 idiom for "Ast2pt.phrase" (which used to convert > str_items to "Parsetree.toplevel_phrase", the type required by the > Toploop module for executing phrases), assuming it is still needed of > course ... open Camlp4.PreCast;; module Ast2pt = Camlp4.Struct.Camlp4Ast2OCamlAst.Make(Ast);; ... Ast2pt.phrase ... > - how can I install with camlp4 a custom printer for OCaml ASTs so that > Toploop.execute_phrase uses it to print on the given formatter? You should look at Camlp4Top/Rprint.ml > Note that I would also be fine in using camlp5 for the task, but AFAIU > the code, overriding tiny bits of the default pretty printers for OCaml > ASTs is easier with the new camlp4 since it has OO printers. This way I > can override methods in derived classes and be done with that. Right, except for ocaml values that are printed by Oprint or Rprint. -- Nicolas Pouillard aka Ertai