[Haskell-cafe] Automatic derivation (TemplateHaskell?)
Stefan O'Rear
stefanor at cox.net
Thu Apr 5 11:09:28 EDT 2007
On Thu, Apr 05, 2007 at 03:19:15PM +0100, Joel Reymont wrote:
> numExpr :: GenParser Char a NumExpr
> numExpr =
> choice [ integer >>= return . Int
> , float >>= return . Num
> ]
Parsec's choice operator works by parsing the first, and only parsing
the second if the first fails immediately. So, given the input
"123.456":
- Parsec parses 'integer >>= return . Int'
- this is successful - numExpr returns (Int 123, ".456")
- we try to match . against ) and fail.
The fix is to left-factor the grammar, or just use the existing
factored choice operator:
> numExpr :: GenParser Char a NumExpr
> numExpr = do sg <- lexeme sign
> nf <- natOrFloat
> return $ either (Int . sg) (Nat . sg) nf
It seems silly that there is no signed version of natOrFloat
predefined, any Parsec experts?
Stefan
More information about the Haskell-Cafe
mailing list