caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [camlp4] how to parse/pretty print OCaml source files
@ 2007-11-06 18:14 Stefano Zacchiroli
  2007-11-06 19:09 ` [Caml-list] " Nicolas Pouillard
  2007-11-06 20:31 ` Jon Harrop
  0 siblings, 2 replies; 5+ messages in thread
From: Stefano Zacchiroli @ 2007-11-06 18:14 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
  2007-11-06 18:14 [camlp4] how to parse/pretty print OCaml source files Stefano Zacchiroli
@ 2007-11-06 19:09 ` Nicolas Pouillard
  2007-11-07 15:28   ` Stefano Zacchiroli
  2007-11-06 20:31 ` Jon Harrop
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Pouillard @ 2007-11-06 19:09 UTC (permalink / raw)
  To: zack; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
  2007-11-06 18:14 [camlp4] how to parse/pretty print OCaml source files Stefano Zacchiroli
  2007-11-06 19:09 ` [Caml-list] " Nicolas Pouillard
@ 2007-11-06 20:31 ` Jon Harrop
  2007-11-07  8:39   ` Andrej Bauer
  1 sibling, 1 reply; 5+ messages in thread
From: Jon Harrop @ 2007-11-06 20:31 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

On Tuesday 06 November 2007 18:14, Stefano Zacchiroli wrote:
> 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 very much like to provide a replacement OCaml top-level that renders 
to Smoke and allows arbitrary graphical entities (everything from colors to 
geometries) to be visualized interactively from the top-level. Let me know 
how it goes!

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
  2007-11-06 20:31 ` Jon Harrop
@ 2007-11-07  8:39   ` Andrej Bauer
  0 siblings, 0 replies; 5+ messages in thread
From: Andrej Bauer @ 2007-11-07  8:39 UTC (permalink / raw)
  To: Caml

Jon Harrop wrote:
> I would very much like to provide a replacement OCaml top-level that renders 
> to Smoke and allows arbitrary graphical entities (everything from colors to 
> geometries) to be visualized interactively from the top-level. Let me know 
> how it goes!

I join to the crowd of people who would very much like to have a
toplevel on steroids, with line editing, graphics, facilities for
pretty-printing everything (including polymorphic types, right now there
is no way to install a pretty printer for a polymorphic type). Even
better, there should be a library that allows one to construct custom
toplevels. I would be willing to join the effort.

It's just silly that people are writing wrappers around the current
toplevel.

Andrej


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [camlp4] how to parse/pretty print OCaml source files
  2007-11-06 19:09 ` [Caml-list] " Nicolas Pouillard
@ 2007-11-07 15:28   ` Stefano Zacchiroli
  0 siblings, 0 replies; 5+ messages in thread
From: Stefano Zacchiroli @ 2007-11-07 15:28 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-11-07 15:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-06 18:14 [camlp4] how to parse/pretty print OCaml source files Stefano Zacchiroli
2007-11-06 19:09 ` [Caml-list] " Nicolas Pouillard
2007-11-07 15:28   ` Stefano Zacchiroli
2007-11-06 20:31 ` Jon Harrop
2007-11-07  8:39   ` Andrej Bauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).