[Haskell-cafe] Template Haskell antiquotation in user-defined quasiquoters

Geoffrey Mainland mainland at apeiron.net
Sat May 26 00:31:38 CEST 2012


On 05/25/2012 21:46, Antoine Latter wrote:
> On Fri, May 25, 2012 at 2:51 PM, Sam Lindley <Sam.Lindley at ed.ac.uk> wrote:
>> Template Haskell supports antiquotation for built-in quasiquotes, e.g.:
>>
>>  [| \x -> x + $([|3 * 4|]) |]
>>
>> However, as far as I can tell, there is no way of supporting antiquotation
>> in user-defined quasiquoters, because the only way to specify a new
>> quasiquoter is through a quoteExp function of type String -> Q Exp. Of
>> course, it is perfectly possible to write a parser for some fragment of
>> Haskell inside your quoteExp function, but that seems crazy given that
>> Template Haskell or rather GHC already implements a parser for the whole
>> language.
>>
>> I know about Language.Haskell.Exts.Parser in haskell-src-exts, which
>> provides parseExp :: String -> ParseResult Exp, but that Exp is a different
>> type to the one provided by Template
>> Haskell.<http://hackage.haskell.org/packages/archive/haskell-src-exts/1.9.0/doc/html/Language-Haskell-Exts-Syntax.html#t:Exp>
>> I'm also aware of Dominic Orchard's syntax-trees package, which supports
>> converting between the two representations using a cunning hack that
>> pretty-prints the haskell-src-exts representation to a string and uses
>> Template Haskell to parse it back.
>>
>> Is there a saner way of simulating antiquotation in user-defined
>> quasiquoters?
>>
> 
> Have you looked at:
> 
> http://hackage.haskell.org/package/haskell-src-exts-qq
> http://hackage.haskell.org/package/haskell-src-meta
> 
> The might help you pull something together.
> 
> 
> Antoine
> 
>> Sam

I use haskell-src-meta in language-c-quote (also on hackage) to support
antiquotation and heartily endorse it. I have not used haskell-src-exts-qq.

Geoff




More information about the Haskell-Cafe mailing list