Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[camlp4] how to parse/pretty print OCaml source files
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-11-06 (19:10)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
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/ 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/

> 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