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 (14:41)
From: Raphael Proust <raphlalou@g...>
Subject: [Camlp4] Quotation expander with OCaml syntax
Hi all,

I'm working on a syntax extension as part of an internship in the
Ocsigen team. The aim of the syntax extension is to split the code of a web
application in two separate files: one for the client and one for the
server. A
few transformations are to take place in the process.

Quotations are to be transformed into client code while antiquotations can
to server side values transmitted to the client at runtime.

In order to avoid any XY problems, here is an abstracted and simplified
of the expected behavior:

(* Pre-parsed code: *)
let start = <:on< f $y$ >> in
let html_node =
 span ~a:[onclick start] "some text" (* a is used for (html) attributes *)

(* Server side post-parsed code: *)
let start _arg1 =
 "call_closure(some_unique_name," ^ mymarshall _arg1 ")"
let html_node = span ~a:[onclick (start y)] "some text"

(* Client side post-parsed code: *)
let _ = register_closure some_unique_name (fun _arg1 -> f _arg1)

If the example isn't clear enough I can detail it a little bit more.

I'm unsure of what is the standard way of doing such a thing in Camlp4. What
have in mind is to use the original Ocaml syntax for the quotation expander.
This would (IIUC) allow me to filter the AST to transform every
found inside the quotation itself.

I'm not sure this is the ideal way of doing such a thing because of the size
the pattern matching in the AST filter. On the other hand, because the
is supposed to contain valid OCaml code, it seems normal to reuse the

I considered an alternative solution: treating quotations as raw text (with
custom quotation expander) but that would destroy any _loc information and
compile time warnings and errors quite difficult to locate.

Is there a simpler/fitter way of doing that? (Is the Y correct wrt the X?)

How can one embed the original parser in a quotation expander? (I couldn't
a function of type string -> Ast.expr in the Camlp4 doc/mlis, but I'd be
to be pointed to one if it exists. I think it would at least require some
functor application.)

Does anyone know of any example that resemble what I'm trying to achieve?