Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Stefano Zacchiroli <zack@b...>
Subject: [camlp4] how to parse/pretty print OCaml source files
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!)

- 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. ]

- 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. ]

- 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 ...

- 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?

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.

Many thanks in advance,
Cheers.

-- 
Stefano Zacchiroli -*- PhD in Computer Science ............... now what?
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
(15:56:48)  Zack: e la demo dema ?    /\    All one has to do is hit the
(15:57:15)  Bac: no, la demo scema    \/    right keys at the right time