Hi all,
Apologies in advance for what may be a very obvious question!
I’m writing a PNG parser using megaparsec
, and am having some difficulty with the many
combinator. My understanding in the context of parsing is that it applies a given parser until that parser fails, and then returns a list of the successfully parsed results. Because of this, it seems reasonable that many
should never fail; it can just return an empty list.
In my code, I have the following:
pPNGBytestream :: Parser PNGImage
pPNGBytestream = do
pngSignature
header <- pImageHeader
_ <- many pUnsupported -- problem here
palette <- optional $ pPalette header
pure $ PNGImage{..}
where
pngSignature = string (B.pack [137, 80, 78, 71, 13, 10, 26, 10]) <?> "png signature"
My problem is that when there’s a parsing failure in pUnsupported
, many
also fails and that cascades up to pPNGBytestream
. Can anyone explain why that behaviour might occur?
The full code is available here. Running the main executable should induce the behaviour I’m describing. If you feel like making comments on any of the other code, please feel free. I’m sure I’m not writing the most idiomatic code.
Thanks very much for any help in advance!