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

Andrew Pimlott andrew at pimlott.net
Mon Nov 21 17:09:14 EST 2005


On Sun, Nov 20, 2005 at 09:27:53PM -0500, Sara Kenedy wrote:
> Thanks for your solution. However, when I try this,
> 
> >     str1 :: Parser String
> >    str1 = do str <- many anyToken
> >	          notFollowedBy' semi
> >                 return str
> 
> >     notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
> >     notFollowedBy' p  = try $ join $  do  a <- try p
> >                                                         return (unexpected (show a))
> >                                                  <|>
> >                                                  return (return ())
> >      run:: Show a => Parser a -> String -> IO()
> 
> >      run p input
> 
> >	= case (parse p "" input) of
> 
> >		Left err -> do {putStr "parse error at " ;print err}
> 
> >		Right x -> print
> 
> When I compile, it still displays ";" at the end of the string.
> 
>   Parser> run str1 "Hello ;"
>   "Hello ;"
> 
> The reason, as I think, because anyToken accepts any kind of token, it
> considers ";" as token of its string. Thus, it does not understand
> notFollowedBy' ???

That's right--your parser consumes and returns the whole input.  I can't
tell you what to use instead, because it depends on what kinds of
strings you want to parse.  Since you are using Token parsers, maybe you
want "symbol"?  The functions in the Char module might also be useful.

Andrew


More information about the Haskell-Cafe mailing list