English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Camlp4] Quotation expander with OCaml syntax
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-07-26 (16:05)
From: Jake Donham <jake@d...>
Subject: Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax
On Mon, Jul 26, 2010 at 8:41 AM, Joel Reymont <joelr1@gmail.com> wrote:
> What if the quotation is not valid OCaml syntax, e.g. C#, and a Camlp4 parser for it exists?
> How would I tie it all together to parse the quotation, plug in  antiquotations and end up with the custom AST that my parser outputs?

There is an example of this in the jslib library in ocamljs:


and a somewhat simpler one for JSON here:


I hope to post a full explanation of this shortly. The magic is the
Camlp4MetaGenerator filter, which generates functions to "lift" your
custom AST into the OCaml AST, either as an expr or a patt. So e.g.
your custom constructor Foo becomes ExId (_, IdUid (_, "Foo"))
(respectively PaId). When you write <:lang< foo >> (where "foo" parses
to Foo) in an expression, you get Foo in the expanded OCaml AST.