Version française
Home     About     Download     Resources     Contact us    
Browse thread
OCaml IDE (Camlp4 for code formatting)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Hendrik Tews <tews@t...>
Subject: Re: [Caml-list] OCaml IDE (Camlp4 for code formatting)
"Nathaniel J. Gaylinn" <ngaylinn@cs.brown.edu> writes:

   One feature I want my program to have is autoformatting, that is, the
   ability to go through and indent source code for you in some standard way.
   At first glance, OCamlp4 seemed like the way to go for this, but under
   further inspection I'm not sure this is true.
   
   My main problem with OCamlp4 is that it changes the input source code!
   Expressions like "5;;" that are just used for side effect are replaced
   with "let _ = 5;;" which is equivalent, but different code. 

If you are sensitive to these kind of changes then you cannot use
camlp4 to format source code. There are many cases where camlp4
uses the same internal representation for different input. For
example 

  let f a b = ...      and      let f = fun a -> fun b ->
  5 + 6                and      (+) 5 6
  module A(B:S) = ...  and      module A = functor(B:S) -> ...
  begin end            and      ()

Further, superfluous parenthesis and comments are not present in
the ast (i.e. (1,2) is the same as 1,2). (Toplevel comments are
preserved if the printer is able to reopen the input file but
they are lost if you feed camlp4 from a pipe.)

>From that it is clear that camlp4's printer must change the input
in many places.

   I understand why it makes this change, 

Could you explain? (Because I don't understand. "let _ = 5" and
"5" are different internally.)

   On a similar note, is there any reference to the format in which Camlp4
   outputs its syntax tree? 

I don't think so. It is defined in the compiler sources and
probably not for the general public. However, you can define your
own camlp4 printers: Write a module that overwrites
Pcaml.print_implem and load this module into camlp4. The function
that you plug into Pcaml.print_implem has to pattern match on
camlp4's ast, which is defined in mLast.mli. You can use the
constructors from mLast.mli or the quotations from q_MLast. See
the source of pr_o or other pretty printers (like in my ocamlp4
package). 

Bye,

Hendrik