[Haskell] Parsec question: attempted 'notMatching' combinator

Andrew Pimlott andrew at pimlott.net
Wed Feb 18 10:18:26 EST 2004


On Wed, Feb 18, 2004 at 02:45:15PM +0100, Daan Leijen wrote:
> On Wed, 18 Feb 2004 01:11:31 -0500, Andrew Pimlott <andrew at pimlott.net> 
> wrote:
> >After some pondering and fiddling, a version I like:
> >
> >    notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
> >    notFollowedBy' p    = join $  do a <- try p; return (unexpected (show 
> >    a))
> >                                  <|>
> >                                  return (return ())

Argh, there is still a problem!  When notFollowedBy' fails, it will have
consumed whatever p consumed.  Stupid example:

    ab    = do  char 'a'
                (notFollowedBy' $ do char 'b'; char 'c') 
                  <|> do char 'b'; return ()

    *Main> parseTest ab "abcd"
    parse error at (line 1, column 4):
    unexpected 'c'

Last version:

    notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
    notFollowedBy' p  = try $ join $  do  a <- try p
                                          return (unexpected (show a))
                                      <|>
                                      return (return ())


Try, try again,
Andrew


More information about the Haskell mailing list