How to make Parser explicitly "fail"?
Daan Leijen
daanleijen@xs4all.nl
Mon, 03 Mar 2003 11:05:36 +0100
> Now I am thinking of writing a Parser for variable. My
> thought is:
>
> variable::Parser String
> variable = do s<-identifier
> if not (s=="define") then return s else
> --let parser fail
You forgot to tell which parser library you use. If you
use ParseLib (or Parsec) you can use the monadic zero
function "mzero".
Now, if you are using Parsec, you can also use
"fail :: String -> Parser a" to add some error message.
> variable = do s<-identifier
> when (s=="define") (fail "define is a keyword")
> return s
Or better:
> variable = do s<-identifier when (s=="define") (unexpected "keyword")
> return s
> <?> "variable"
Or even better, use the standard "identifier" parser from the "ParsecToken" module to handle this for you automatically. The
user guide describes this in detail: http://www.cs.uu.nl/~daan/parsec.html
All the best,
Daan.
> I will be very happy if there is some mechanism to
> implement the "else" branch, for I have thought of it
> for long. :)
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Tax Center - forms, calculators, tips, more
> http://taxes.yahoo.com/
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>
>