Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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:

  http://github.com/jaked/ocamljs/tree/master/src/jslib/

and a somewhat simpler one for JSON here:

  http://github.com/jaked/cufp-metaprogramming-tutorial/tree/master/ocaml/json_quot/

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.

Jake