@@ -1246,23 +1246,101 @@ lookAhead (Parser step1 initial1 _) = Parser step initial extract
12461246-------------------------------------------------------------------------------
12471247-- Interleaving
12481248-------------------------------------------------------------------------------
1249−--
1249+
1250+data DeintercalateState fs sp ss =
1251+ DeintercalateL fs sp
1252+ | DeintercalateR fs ss Bool
1253+
12501254-- | See 'Streamly.Internal.Data.Parser.deintercalate'.
12511255--
1252−-- /Unimplemented/
1256+-- /Internal/
12531257--
12541258{-# INLINE deintercalate #-}
1255−deintercalate ::
1256− -- Monad m =>
1257− Fold m a y -> Parser m x a
1258− -> Fold m b z -> Parser m x b
1259− -> Parser m x (y, z)
1260−deintercalate = undefined
1259+deintercalate :: Monad m =>
1260+ Fold m (Either x y) z
1261+ -> Parser m a x
1262+ -> Parser m a y
1263+ -> Parser m a z
1264+deintercalate
1265+ (Fold fstep finitial fextract)
1266+ (Parser stepL initialL extractL)
1267+ (Parser stepR initialR extractR) = Parser step initial extract
1268+
1269+ where
1270+
1271+ errMsg p status =
1272+ error $ "sepBy: " ++ p ++ " parser cannot "
1273+ ++ status ++ " without input"
1274+
1275+ initial = do
1276+ res <- finitial
1277+ case res of
1278+ FL.Partial fs -> do
1279+ resL <- initialL
1280+ case resL of
1281+ IPartial sL -> return $ IPartial $ DeintercalateL fs sL
1282+ IDone _ -> errMsg "left" "succeed"
1283+ IError _ -> errMsg "left" "fail"
1284+ FL.Done c -> return $ IDone c
1285+
1286+ step (DeintercalateL fs sL) a = do
1287+ r <- stepL sL a
1288+ case r of
1289+ Partial n s -> return $ Partial n (DeintercalateL fs s)
1290+ Continue n s -> return $ Continue n (DeintercalateL fs s)
1291+ Done n b -> do
1292+ fres <- fstep fs (Left b)
1293+ case fres of
1294+ FL.Partial fs1 -> do
1295+ resR <- initialR
1296+ case resR of
1297+ IPartial sR ->
1298+ return
1299+ $ Partial n (DeintercalateR fs1 sR False)
1300+ IDone _ -> errMsg "right" "succeed"
1301+ IError _ -> errMsg "right" "fail"
1302+ FL.Done c -> return $ Done n c
1303+ Error err -> return $ Error err
1304+ step (DeintercalateR fs sR consumed) a = do
1305+ r <- stepR sR a
1306+ case r of
1307+ Partial n s -> return $ Partial n (DeintercalateR fs s True)
1308+ Continue n s -> return $ Continue n (DeintercalateR fs s True)
1309+ Done n b ->
1310+ if consumed
1311+ then do
1312+ fres <- fstep fs (Right b)
1313+ case fres of
1314+ FL.Partial fs1 -> do
1315+ resL <- initialL
1316+ case resL of
1317+ IPartial sL ->
1318+ return $ Partial n $ DeintercalateL fs1 sL
1319+ IDone _ -> errMsg "left" "succeed"
1320+ IError _ -> errMsg "left" "fail"
1321+ FL.Done c -> return $ Done n c
1322+ else return $ Error "sepBy: infinite loop"
1323+ Error err -> return $ Error err
1324+
1325+ extract (DeintercalateL fs sL) = do
1326+ r <- extractL sL
1327+ res <- fstep fs (Left r)
1328+ case res of
1329+ FL.Partial fs1 -> fextract fs1
1330+ FL.Done c -> return c
1331+ extract (DeintercalateR fs sR _) = do
1332+ r <- extractR sR
1333+ res <- fstep fs (Right r)
1334+ case res of
1335+ FL.Partial fs1 -> fextract fs1
1336+ FL.Done c -> return c
12611337
12621338data SepByState fs sp ss =
12631339 SepByInit fs sp
12641340 | SepBySeparator fs ss Bool
12651341
1342+-- This is a special case of deintercalate and can be easily implemented in
1343+-- terms of deintercalate.
12661344{-# INLINE sepBy #-}
12671345sepBy :: MonadCatch m =>
12681346 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c