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: Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
On Tue, Nov 06, 2007 at 08:09:28PM +0100, Nicolas Pouillard wrote:
> > - 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. ]

First things first :), so let's start with parsing ...

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

OK.

> 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

That's right, parsing one phrase at a time is definitely handier.
However the above does not work. (The argument are permuted, but that of
course is a non issue.) What does "loc" stands for? Given I'm parsing a
char stream I would have expected that the parser returns me a located
object, why should I give a location in advance? Is it some kind of
"current" location when parsing has been left last time?

In this case how should I build a location? The only way I've found is
Camlp4.PreCast.Loc.mk which takes as input a file name and returns a
location placed at the beginning of that file. I'm going for Loc.ml "",
but I don't know if I'll mess up something in the future ...

TIA, 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