on master help sign in
Make applicative code in ParserD.serialWith monadic
commit87c4f9602dd4d8f1c51ee7db9895733cda748a04 authorAdithya Kumar <adithya@composewell.com> dateFri 4 Feb 2022, 06:27 (5 years ago) parentfe478a4
1 file changed+15−11
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+15−11
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+15−11
@@ -426,28 +426,33 @@ serialWith func (Parser stepL initialL extractL)
426426
427427 -- Note: For the composed parse to terminate, the left parser has to be
428428 -- a terminating parser returning a Done at some point.
429− step (SeqParseL st) a =
430− (\resL initR ->
429+ step (SeqParseL st) a = do
430+ -- Important: Please do not use Applicative here. See
431+ -- https://github.com/composewell/streamly/issues/1033 and the problem
432+ -- defined in split_ for more info.
433+ resL <- stepL st a
431434 case resL of
432435 -- Note: We need to buffer the input for a possible Alternative
433436 -- e.g. in ((,) <$> p1 <*> p2) <|> p3, if p2 fails we have to
434437 -- backtrack and start running p3. So we need to keep the input
435438 -- buffered until we know that the applicative cannot fail.
436− Partial n s -> Continue n (SeqParseL s)
437− Continue n s -> Continue n (SeqParseL s)
438− Done n b ->
439− case initR of
439+ Partial n s -> return $ Continue n (SeqParseL s)
440+ Continue n s -> return $ Continue n (SeqParseL s)
441+ Done n b -> do
442+ initR <- initialR
443+ return $ case initR of
440444 IPartial sr -> Continue n $ SeqParseR (func b) sr
441445 IDone br -> Done n (func b br)
442446 IError err -> Error err
443− Error err -> Error err) <$> stepL st a <*> initialR
447+ Error err -> return $ Error err
444448
445− step (SeqParseR f st) a =
446− (\case
449+ step (SeqParseR f st) a = do
450+ resR <- stepR st a
451+ return $ case resR of
447452 Partial n s -> Partial n (SeqParseR f s)
448453 Continue n s -> Continue n (SeqParseR f s)
449454 Done n b -> Done n (f b)
450− Error err -> Error err) <$> stepR st a
455+ Error err -> Error err
451456
452457 extract (SeqParseR f sR) = fmap f (extractR sR)
453458 extract (SeqParseL sL) = do
@@ -1147,4 +1152,3 @@ instance (MonadThrow m, MonadState s m) => MonadState s (Parser m a) where
11471152instance (MonadThrow m, MonadIO m) => MonadIO (Parser m a) where
11481153 {-# INLINE liftIO #-}
11491154 liftIO = fromEffect . liftIO
1150−