Quoting a quasi-quote

Simon Marlow marlowsd at gmail.com
Mon Jul 11 14:43:18 CEST 2011

On 30/06/2011 14:52, Yitzchak Gale wrote:
> It was pointed out by Ben Millwood on the Cafe
> that there is an undocumented way to escape the
> closing oxford bracket of a quasi-quote using
> a backslash:
> [s|This quasi-quote contains this, \|], an escaped
> closing oxford bracket.|]
> The backslash itself cannot be escaped in this
> way:
> [s|Also contains an escaped bracket \\|] |]
> Thus there is a fairly strong limitation on the
> contents of a quasi-quote: it can never end
> in a backslash.
> This behavior is not mentioned in the GHC docs.
> Is it a mistake, or is it meant to be a supported
> feature?
> This behavior is a bit surprising to me. Since the
> whole point of a quasi-quoter is to allow the user
> to define syntax, you would think that the syntax
> for the quasi-quote itself would be as quiet as
> possible and stay out of the way. People who
> need to be able to escape the closing bracket
> can easily define their own syntax to do so.
> In any case, if this is indeed a feature, it certainly
> should be documented.

It looks intentional to me:

lex_quasiquote :: String -> P String
lex_quasiquote s = do
   i <- getInput
   case alexGetChar' i of
     Nothing -> lit_error i

     Just ('\\',i)
	| Just ('|',i) <- next -> do
		setInput i; lex_quasiquote ('|' : s)
	| Just (']',i) <- next -> do
		setInput i; lex_quasiquote (']' : s)
	where next = alexGetChar' i

     Just ('|',i)
	| Just (']',i) <- next -> do
		setInput i; return s
	where next = alexGetChar' i

     Just (c, i) -> do
	 setInput i; lex_quasiquote (c : s)

Indeed, it also seems strange that "\\]" is interpreted as "]".

That's all I know.  I agree we should either document or remove the feature.


More information about the Glasgow-haskell-users mailing list