@@ -40,6 +40,7 @@ import qualified Control.Monad.Fail as Fail
4040#if !(MIN_VERSION_base(4,10,0))
4141import Data.Semigroup ((<>))
4242#endif
43+import Streamly.Internal.Control.Exception
4344
4445import qualified Streamly.Internal.Data.Parser.ParserD.Types as D
4546
@@ -205,6 +206,9 @@ data FromParserK b e c = FPKDone !Int !b | FPKError !e | FPKCont c
205206
206207-- | Convert a CPS style 'Parser' to a direct style 'D.Parser'.
207208--
209+-- "initial" returns a continuation which can be called one input at a time
210+-- using the "step" function.
211+--
208212-- /Internal/
209213--
210214{-# INLINE_LATE fromParserK #-}
@@ -221,7 +225,12 @@ fromParserK parser = D.Parser step initial extract
221225 Partial _ cont -> FPKCont cont -- XXX can we get this?
222226 Continue _ cont -> FPKCont cont
223227
224− step (FPKDone n b) _ = return $ D.Done (n + 1) b
228+ -- Note, we can only reach FPKDone and FPKError from "initial". FPKCont
229+ -- always transitions to only FPKCont. The input remains unconsumed in
230+ -- this case so we use "n + 1".
231+ step (FPKDone n b) _ = do
232+ assertM (n == 0)
233+ return $ D.Done (n + 1) b
225234 step (FPKError e) _ = return $ D.Error e
226235 step (FPKCont cont) a = do
227236 r <- cont (Just a)