[Haskell-cafe] How to use notFollowedBy function in Parsec

Andrew Pimlott andrew at pimlott.net
Sun Nov 20 00:32:55 EST 2005


On Sat, Nov 19, 2005 at 07:58:31PM -0800, Andrew Pimlott wrote:
> Here is a version (which came out of that thread) with a nicer type,
> that probably also works more reliably (though I won't guarantee it):
> 
>     notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
>     notFollowedBy' p  = try $ join $  do  a <- try p
>                                           return (unexpected (show a))
>                                       <|>
>                                       return (return ())

Ok, I almost guarantee it works reliably.  I know of no way to make it
obvious, but by considering the four cases for p (succeeds consuming 0
tokens, succeeds consuming >0 tokens, fails having consumed 0 tokens,
fails having consumerd >0 tokens) should be convincing.

When this came up before, Daan suggested committing the change[1], but
it seems to have gotten lost.  There is also the small issue that
changing the type changes the error reporting slighly[2].  Thoughts?

Andrew

[1] http://www.haskell.org/pipermail/haskell/2004-February/013630.html
[2] http://www.haskell.org/pipermail/haskell/2004-February/013631.html


More information about the Haskell-Cafe mailing list