Handle failure in Alternative when extracting

Fixes #604
This commit is contained in:
Harendra Kumar 2020-07-26 14:47:30 +05:30
parent 7408867312
commit 0eb4c88335

View File

@ -392,7 +392,7 @@ data AltParseState sl sr = AltParseL Int sl | AltParseR sr
-- /Internal/
--
{-# INLINE alt #-}
alt :: Monad m => Parser m x a -> Parser m x a -> Parser m x a
alt :: MonadCatch m => Parser m x a -> Parser m x a -> Parser m x a
alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
Parser step initial extract
@ -426,7 +426,11 @@ alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
Error err -> Error err
extract (AltParseR sR) = extractR sR
extract (AltParseL _ sL) = extractL sL
extract (AltParseL _ sL) = do
r <- try $ extractL sL
case r of
Left (_ :: ParseError) -> initialR >>= extractR
Right b -> return b
-- | See documentation of 'Streamly.Internal.Data.Parser.many'.
--