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, Raphael Proust <raphlalou@gmail.com> wrote:
> What I basically need is to get an AST with antiquotations and quotations being
> special nodes. How is this achievable w/o reimplementing a whole grammar?

There is already a special node for antiquotations, but not
quotations; see below.

> The alternative solution is to use raw strings, to find antiquotation marks, to
> split the string and to reinject it in the different files. Is there a way to
> keep precise _loc information this way?

This is more or less what already happens with quotations /
antiquotations. The lexer reads the whole quotation into a string
(checking that nested antiquotations / quotations are balanced, so it
doesn't stop too soon, but not otherwise processing them). The string
is then expanded (for OCaml AST quotations at least) by parsing it
into an OCaml AST, then lifting the AST (see my previous response),
then filtering the resulting AST to parsed antiquotations and insert
conversions (e.g. $`int:i$ becomes ExInt (_, string_of_int i)). The
nested parsing begins with the current location, so _loc information
is kept precisely. Antiquotations are represented as special nodes in
the OCaml AST containing the string (again, the whole antiquotation,
even if there is further nesting), which are parsed during the AST
filtering phase.

I'm not sure what syntactic problems Nicolas suggests you would run
into by reusing the existing parser and quotation mechanism. But it
might be a bit hairy to implement. One issue is that you can't just
drop antiquotations anywhere; where they can appear (and with what
tag) is given by the parser. If you want to go in this direction, the
place to start would be Camlp4QuotationCommon.ml, which implements
OCaml AST quotations / antiquotations. You can see there that you can
parse starting at arbitrary non-terminals, and you can filter the
OCaml AST without a giant pattern match using the Ast.map object.

Jake