Skip to content

Commit ebc9575

Browse files
Add an error location counter in streamEqBy parser
1 parent 2dd8908 commit ebc9575

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

core/src/Streamly/Internal/Data/Parser.hs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,9 +2136,7 @@ groupByRollingEither
21362136
FL.Done b -> return $ FDone 0 (Left b)
21372137
FL.Partial s11 -> FDone 0 . Left <$> ffinal1 s11
21382138

2139-
-- XXX use an Unfold instead of a list?
2140-
-- XXX custom combinators for matching list, array and stream?
2141-
-- XXX rename to listBy?
2139+
-- XXX custom combinator for matching, array as well?
21422140

21432141
-- | Match the given sequence of elements using the given comparison function.
21442142
-- Returns the original sequence if successful.
@@ -2153,7 +2151,7 @@ groupByRollingEither
21532151
-- Right "string"
21542152
--
21552153
-- >>> Stream.parsePos (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
2156-
-- Left (ParseErrorPos 2 "listEqBy: mismatch occurred")
2154+
-- Left (ParseErrorPos 2 "listEqBy: mismatch after matching 1 elements")
21572155
--
21582156
{-# INLINE listEqBy #-}
21592157
listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser a m [a]
@@ -2196,33 +2194,37 @@ streamEqByInternal fname cmp (D.Stream sstep state) = Parser step initial extrac
21962194
initial = do
21972195
r <- sstep defState state
21982196
case r of
2199-
D.Yield x s -> return $ IPartial (Just' x, s)
2197+
D.Yield x s -> return $ IPartial (Just' x, s, 0 :: Int)
22002198
D.Stop -> return $ IDone ()
22012199
-- Need Skip/Continue in initial to loop right here
2202-
D.Skip s -> return $ IPartial (Nothing', s)
2200+
D.Skip s -> return $ IPartial (Nothing', s, 0)
22032201

2204-
step (Just' x, st) a =
2202+
step (Just' x, st, n) a =
22052203
if x `cmp` a
22062204
then do
22072205
r <- sstep defState st
22082206
return
22092207
$ case r of
2210-
D.Yield x1 s -> SContinue 1 (Just' x1, s)
2208+
D.Yield x1 s -> SContinue 1 (Just' x1, s, n + 1)
22112209
D.Stop -> SDone 1 ()
2212-
D.Skip s -> SContinue 0 (Nothing', s)
2213-
else return $ SError (fname ++ ": mismatch occurred")
2214-
step (Nothing', st) a = do
2210+
D.Skip s -> SContinue 0 (Nothing', s, n + 1)
2211+
else return $ SError
2212+
(fname ++ ": mismatch after matching " ++ show n ++ " elements")
2213+
step (Nothing', st, n) a = do
22152214
r <- sstep defState st
22162215
return
22172216
$ case r of
22182217
D.Yield x s -> do
22192218
if x `cmp` a
2220-
then SContinue 1 (Nothing', s)
2221-
else SError (fname ++ ": mismatch occurred")
2219+
then SContinue 1 (Nothing', s, n + 1)
2220+
else SError
2221+
(fname ++ ": mismatch after matching "
2222+
++ show n ++ " elements")
22222223
D.Stop -> SDone 0 ()
2223-
D.Skip s -> SContinue 0 (Nothing', s)
2224+
D.Skip s -> SContinue 0 (Nothing', s, n)
22242225

2225-
extract _ = return $ FError (fname ++ ": end of input")
2226+
extract (_, _, n) = return $ FError
2227+
(fname ++ ": end of input after matching " ++ show n ++ " elements")
22262228

22272229
-- | Like 'listEqBy' but uses a stream instead of a list and does not return
22282230
-- the stream.

0 commit comments

Comments
 (0)