Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Camlp4] Antiquotation and unescaped strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-02-18 (14:34)
From: Gabriel Kerneis <kerneis@e...>
Subject: [Camlp4] Antiquotation and unescaped strings

I've got a problem with antiquotations and strings in camlp4. I've got a
parser (xmlp4, used in Ocsigen) written with camlp4 which has to deal
with strings. 
This parser provides a quotation mechanism to let the user input XML in
a natural way within an Ocaml program.

Basically, the problem is the following expression (in the parser's

  | PCData(s) -> [ <:expr< XHTML.EncodedPCData $str:s$ >> ]

where s is a (potentially unescaped) string. Here is a typical output:

  [XML.EncodedPCDATA " "bar \" ]

corresponding to the following input file:

  <:xml< "bar\ >>

(assuming that this quotation is parsed as PCData of course)

As you can see, it's weird and it took me some time to find out what was
happening (real-life examples being much more intricate).

Of course, I would like to get:

  [XML.EncodedPCDATA "\"bar \\" ]

instead, and of course, I could obtain it through:

  | PCData(s) -> [<:expr< XHTML.EncodedPCData $str:String.escaped s$ >>]

But I wonder whether this should be considered as a camlp4 bug (or
possible enhancement). When I write << EncodedPCData $str:s$ >>, I 
expect to get something consistent, not some kind of "let's evaluate s
and copy/paste it blindly there".

Shall I submit a bug report? Or is there something I got wrong in my
code (which is very likely)?

Anyway, if this is an intended behavior, the explanation should be put 
in BIG BOLD RED letters on the camlp4 wiki, on the quotation page. This
would help a lot (plus I would like to understand what is exactly going 
on there).

NB: I simplified the examples for the shake of clarity. You can find the
relevant code here:
You may also want to have a look at: and
I'd welcome any remark regarding this code.

Kind regards,
Gabriel Kerneis