Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: zack <zack@bononia.it>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
Date: Tue, 06 Nov 2007 20:09:28 +0100	[thread overview]
Message-ID: <1194375800-sup-2076@ausone.local> (raw)
In-Reply-To: <20071106181431.GA19764@takhisis.invalid>

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


  reply	other threads:[~2007-11-06 19:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-06 18:14 Stefano Zacchiroli
2007-11-06 19:09 ` Nicolas Pouillard [this message]
2007-11-07 15:28   ` [Caml-list] " Stefano Zacchiroli
2007-11-06 20:31 ` Jon Harrop
2007-11-07  8:39   ` Andrej Bauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1194375800-sup-2076@ausone.local \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=zack@bononia.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox