I’m learning haskell, and my current project is writing a parser to read a text file representation of a database.
At the moment, I’m setting up the code for reading individual fields of tables. In the text file, fields look either like this:
name type flags format
or this:
name type format
This gives the trouble of having to account for cases of there being a flag or not being a flag. I solved this in my main function like this:
main = case parse fieldsWithFlags "(test)" testLine of
Left err -> noFlags
Right res -> print res
where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
Left err -> print err
Right res -> print res
If I understand correctly, this says “If it’s a line that doesn’t have flags, try to parse it as such; otherwise, return an error.” It prints the correct results for any “testLine” I throw at it, and returns errors if both options fail. However, when I try to pull this out into its own function, like this:
field :: Either ParseError Field
field = case parse fieldsWithFlags "(test)" testLine of
Left err -> noFlags
Right res -> return Right res
where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
Left err -> return Left err
Right res -> return Right res
main = case field of
Left err -> print err
Right res -> print res
GHC gives me:
haskellParsing.hs:58:26:
Couldn't match expected type `Either ParseError Field'
with actual type `b0 -> Either b0 b0'
In the expression: noFlags
In a case alternative: Left err -> noFlags
In the expression:
case parse fieldsWithFlags "(test)" testLine of {
Left err -> noFlags
Right res -> return Right res }
I’ve played around with this a lot, but just can’t get it working. I’m sure there’s a much more clear-headed way of doing this, so any suggestions would be welcome – but I also want to understand why this isn’t working.
Full code is at: http://pastebin.com/ajG6BkPU
Thanks!
You don’t need the
returnin your cases. Once you wrap something inLeftorRightit is inEither; since you only need aEither ParseError Field, theLeftandRightdo not need an extrareturn.Also, you should be able to simplify your
parseFieldssignificantly. You can write a new parser that looks like this:what this does is run the first one and, if it fails, backtrack and run the second one. The
tryis important because this is what enables the backtracking behavior. You have to backtrack becausefieldsWithFlagsconsumes some of the input that you care about.Now you should be able to just use
fieldsin yourmainfunction.