Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmark/Streamly/Benchmark/Data/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import System.IO (Handle)
import System.Random (randomRIO)
import Streamly.Internal.Data.Fold (Fold(..))
import Streamly.Internal.Data.Parser
(ParseError(..), Parser(..), Initial(..), Step(..))
(ParseError(..), Parser(..), Initial(..), Step(..), Final(..))
import Streamly.Internal.Data.Stream (Stream)
import Prelude hiding
(any, all, take, sequence, sequence_, sequenceA, takeWhile, dropWhile, span)
Expand Down Expand Up @@ -366,9 +366,9 @@ takeWhileFail predicate (Fold fstep finitial _ ffinal) =
$ case fres of
Fold.Partial s1 -> SPartial 1 s1
Fold.Done b -> SDone 1 b
else return $ Error "fail"
else return $ SError "fail"

extract s = fmap (SDone 0) (ffinal s)
extract s = fmap (FDone 0) (ffinal s)

{-# INLINE alt2 #-}
alt2 :: Monad m
Expand Down
6 changes: 3 additions & 3 deletions benchmark/Streamly/Benchmark/Data/ParserK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import Streamly.Data.Array.Generic (Array)
import Streamly.Internal.Data.Fold (Fold(..))
import Streamly.Data.StreamK (StreamK)
import Streamly.Internal.Data.Parser
(ParseError(..), Parser(..), Initial(..), Step(..))
(ParseError(..), Parser(..), Initial(..), Step(..), Final(..))
import Streamly.Internal.Data.Stream (Stream)
import System.Random (randomRIO)
import Prelude hiding
Expand Down Expand Up @@ -236,9 +236,9 @@ takeWhileFailD predicate (Fold fstep finitial _ ffinal) =
$ case fres of
Fold.Partial s1 -> SContinue 1 s1
Fold.Done b -> SDone 1 b
else return $ Error "fail"
else return $ SError "fail"

extract s = fmap (SDone 0) (ffinal s)
extract s = fmap (FDone 0) (ffinal s)

{-# INLINE takeWhileFail #-}
takeWhileFail :: CONSTRAINT =>
Expand Down
23 changes: 11 additions & 12 deletions core/src/Streamly/Internal/Data/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ parseBreakD
-- constructors every time.
str = D.cons arr0 (D.cons arr1 (D.Stream step s))
return (b, str)
PR.Error err -> throwM $ ParseError err
PR.SError err -> throwM $ ParseError err

-- | Parse an array stream using the supplied 'Parser'. Returns the parse
-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
Expand Down Expand Up @@ -924,7 +924,7 @@ parseBreakChunksK (Parser pstep initial extract) stream = do
arr1 = Array contents next end
str = StreamK.cons arr0 (StreamK.cons arr1 st)
return (Right b, str)
Parser.Error err -> do
Parser.SError err -> do
let n = Prelude.length backBuf
arr0 = fromListN n (Prelude.reverse backBuf)
arr1 = Array contents cur end
Expand Down Expand Up @@ -975,7 +975,7 @@ parseBreakChunksK (Parser pstep initial extract) stream = do
arr1 = Array contents next end
str = StreamK.cons arr0 (StreamK.fromPure arr1)
return (Right b, str)
Parser.Error err -> do
Parser.SError err -> do
let n = Prelude.length backBuf
arr0 = fromListN n (Prelude.reverse backBuf)
arr1 = Array contents cur end
Expand Down Expand Up @@ -1008,7 +1008,7 @@ parseBreakChunksK (Parser pstep initial extract) stream = do
-- arr0 = A.fromListRevN n src0
arr0 = fromListN n (Prelude.reverse src0)
return (Right b, StreamK.fromPure arr0)
Parser.Error err -> do
Parser.SError err -> do
let n = Prelude.length backBuf
arr0 = fromListN n (Prelude.reverse backBuf)
return (Left (ParseError err), StreamK.fromPure arr0)
Expand Down Expand Up @@ -1098,7 +1098,7 @@ parseBreak parser input = do
assertM(n1 >= 0 && n1 <= sum (Prelude.map length backBuf))
let (s1, _) = backtrack n1 backBuf StreamK.nil
in return (Right b, s1)
ParserK.Error _ err -> do
ParserK.SError _ err -> do
let s1 = Prelude.foldl (flip StreamK.cons) StreamK.nil backBuf
return (Left (ParseError err), s1)

Expand Down Expand Up @@ -1143,7 +1143,7 @@ parseBreak parser input = do
assertM(n1 <= sum (Prelude.map length (arr:backBuf)))
let (s1, _) = backtrack n1 (arr:backBuf) stream
in return (Right b, s1)
ParserK.Error _ err -> do
ParserK.SError _ err -> do
let s1 = Prelude.foldl (flip StreamK.cons) stream (arr:backBuf)
return (Left (ParseError err), s1)

Expand All @@ -1167,7 +1167,7 @@ adaptCWith
:: forall m a s b r. (Monad m, Unbox a)
=> (s -> a -> m (ParserD.Step s b))
-> m (ParserD.Initial s b)
-> (s -> m (ParserD.Step s b))
-> (s -> m (ParserD.Final s b))
-> (ParseResult b -> Int -> Input (Array a) -> m (Step (Array a) m r))
-> Int
-> Int
Expand Down Expand Up @@ -1255,23 +1255,22 @@ adaptCWith pstep initial extract cont !offset0 !usedCount !input = do
go SPEC cur pst1
ParserD.SContinue n pst1 ->
onBack (move n) elemSize onContinue pst1
ParserD.Error err ->
ParserD.SError err ->
onError curOff err

{-# NOINLINE parseContNothing #-}
parseContNothing !count !pst = do
r <- extract pst
case r of
ParserD.SDone n b ->
ParserD.FDone n b ->
assert (n <= 0) (cont (Success n b) (count + n) None)
ParserD.SContinue n pst1 ->
ParserD.FContinue n pst1 ->
assert (n <= 0)
(return $ Continue n (parseCont (count + n) pst1))
ParserD.Error err ->
ParserD.FError err ->
-- XXX It is called only when there is no input arr. So using 0
-- as the position is correct?
cont (Failure 0 err) count None
ParserD.SPartial _ _ -> error "Bug: adaptCWith Partial unreachable"

-- XXX Maybe we can use two separate continuations instead of using
-- Just/Nothing cases here. That may help in avoiding the parseContJust
Expand Down
15 changes: 7 additions & 8 deletions core/src/Streamly/Internal/Data/Array/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ parseBreak parser input = do
assertM(n1 >= 0 && n1 <= sum (Prelude.map length backBuf))
let (s1, _) = backtrack n1 backBuf StreamK.nil
in return (Right b, s1)
ParserK.Error _ err ->
ParserK.SError _ err ->
let strm = Prelude.foldl (flip StreamK.cons) StreamK.nil backBuf
in return (Left (ParseError err), strm)

Expand Down Expand Up @@ -517,7 +517,7 @@ parseBreak parser input = do
assertM(n1 <= sum (Prelude.map length (arr:backBuf)))
let (s1, _) = backtrack n1 (arr:backBuf) stream
in return (Right b, s1)
ParserK.Error _ err ->
ParserK.SError _ err ->
let strm = Prelude.foldl (flip StreamK.cons) stream (arr:backBuf)
in return (Left (ParseError err), strm)

Expand Down Expand Up @@ -550,7 +550,7 @@ adaptCGWith
:: forall m a s b r. (Monad m)
=> (s -> a -> m (ParserD.Step s b))
-> m (ParserD.Initial s b)
-> (s -> m (ParserD.Step s b))
-> (s -> m (ParserD.Final s b))
-> (ParseResult b -> Int -> Input (Array a) -> m (Step (Array a) m r))
-> Int
-> Int
Expand Down Expand Up @@ -632,23 +632,22 @@ adaptCGWith pstep initial extract cont !offset0 !usedCount !input = do
go SPEC cur pst1
ParserD.SContinue n pst1 ->
onBack (move n) onContinue pst1
ParserD.Error err ->
ParserD.SError err ->
onError curOff err

{-# NOINLINE parseContNothing #-}
parseContNothing !count !pst = do
r <- extract pst
case r of
ParserD.SDone n b ->
ParserD.FDone n b ->
assert (n <= 0) (cont (Success n b) (count + n) None)
ParserD.SContinue n pst1 ->
ParserD.FContinue n pst1 ->
assert (n <= 1)
(return $ Continue n (parseCont (count + n) pst1))
ParserD.Error err ->
ParserD.FError err ->
-- XXX It is called only when there is no input arr. So using 0
-- as the position is correct?
cont (Failure 0 err) count None
ParserD.SPartial _ _ -> error "Bug: adaptCGWith Partial unreachable"

{-# INLINE parseCont #-}
parseCont !cnt !pst (Chunk arr) = parseContChunk cnt 0 pst arr
Expand Down
32 changes: 15 additions & 17 deletions core/src/Streamly/Internal/Data/Array/Stream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ runArrayParserDBreak
let src0 = takeArrayListRev n (x:getList backBuf)
src = Prelude.reverse src0 ++ xs
return (Right b, D.append (D.fromList src) (D.Stream step s))
PR.Error err -> do
PR.SError err -> do
let src0 = x:getList backBuf
src = Prelude.reverse src0 ++ x:xs
strm = D.append (D.fromList src) (D.Stream step s)
Expand Down Expand Up @@ -408,7 +408,7 @@ runArrayParserDBreak
let src0 = takeArrayListRev n (x:getList backBuf)
src = Prelude.reverse src0 ++ xs
return (Right b, D.fromList src)
PR.Error err -> do
PR.SError err -> do
let src0 = getList backBuf
src = Prelude.reverse src0 ++ x:xs
return (Left (ParseError err), D.fromList src)
Expand All @@ -418,25 +418,24 @@ runArrayParserDBreak
goStop backBuf pst = do
pRes <- extract pst
case pRes of
PR.Partial _ _ -> error "Bug: runArrayParserDBreak: Partial in extract"
PR.Continue 0 pst1 ->
PR.FContinue 0 pst1 ->
goStop backBuf pst1
PR.Continue n pst1 -> do
PR.FContinue n pst1 -> do
assert
(n <= sum (map Array.length (getList backBuf)))
(return ())
let (src0, buf1) = splitAtArrayListRev n (getList backBuf)
src = Prelude.reverse src0
goExtract SPEC src (List buf1) pst1
PR.Done 0 b -> return (Right b, D.nil)
PR.Done n b -> do
PR.FDone 0 b -> return (Right b, D.nil)
PR.FDone n b -> do
assert
(n <= sum (map Array.length (getList backBuf)))
(return ())
let src0 = takeArrayListRev n (getList backBuf)
src = Prelude.reverse src0
return (Right b, D.fromList src)
PR.Error err -> do
PR.FError err -> do
let src0 = getList backBuf
src = Prelude.reverse src0
return (Left (ParseError err), D.fromList src)
Expand Down Expand Up @@ -592,7 +591,7 @@ runArrayFoldManyD
return
$ D.Skip
$ ParseChunksYield (Right b) next
PR.Error err -> do
PR.SError err -> do
let next = ParseChunksInitLeftOver []
return
$ D.Skip
Expand Down Expand Up @@ -634,7 +633,7 @@ runArrayFoldManyD
return
$ D.Skip
$ ParseChunksYield (Right b) (ParseChunksInit src s)
PR.Error err -> do
PR.SError err -> do
let next = ParseChunksInitLeftOver []
return
$ D.Skip
Expand Down Expand Up @@ -672,7 +671,7 @@ runArrayFoldManyD
return
$ D.Skip
$ ParseChunksYield (Right b) (ParseChunksInitBuf src)
PR.Error err -> do
PR.SError err -> do
let next = ParseChunksInitLeftOver []
return
$ D.Skip
Expand All @@ -683,26 +682,25 @@ runArrayFoldManyD
stepOuter _ (ParseChunksStop backBuf pst) = do
pRes <- extract pst
case pRes of
PR.Partial _ _ -> error "runArrayFoldManyD: Partial in extract"
PR.Continue 0 pst1 ->
PR.FContinue 0 pst1 ->
return $ D.Skip $ ParseChunksStop backBuf pst1
PR.Continue n pst1 -> do
PR.FContinue n pst1 -> do
assert (n <= sum (map Array.length backBuf)) (return ())
let (src0, buf1) = splitAtArrayListRev n backBuf
src = Prelude.reverse src0
return $ D.Skip $ ParseChunksExtract src buf1 pst1
PR.Done 0 b ->
PR.FDone 0 b ->
return
$ D.Skip
$ ParseChunksYield (Right b) (ParseChunksInitLeftOver [])
PR.Done n b -> do
PR.FDone n b -> do
assert (n <= sum (map Array.length backBuf)) (return ())
let src0 = takeArrayListRev n backBuf
src = Prelude.reverse src0
return
$ D.Skip
$ ParseChunksYield (Right b) (ParseChunksInitBuf src)
PR.Error err -> do
PR.FError err -> do
let next = ParseChunksInitLeftOver []
return
$ D.Skip
Expand Down
14 changes: 7 additions & 7 deletions core/src/Streamly/Internal/Data/Binary/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import qualified Streamly.Internal.Data.Array as A
import qualified Streamly.Internal.Data.Parser as PR
(fromPure, either, satisfy, takeEQ)
import qualified Streamly.Internal.Data.Parser as PRD
(Parser(..), Initial(..), Step(..))
(Parser(..), Initial(..), Step(..), Final(..))

-- Note: The () type does not need to have an on-disk representation in theory.
-- But we use a concrete representation for it so that we count how many ()
Expand Down Expand Up @@ -153,7 +153,7 @@ word16beD = PRD.Parser step initial extract
step (Just' w) a =
return $ PRD.SDone 1 (w .|. fromIntegral a)

extract _ = return $ PRD.Error "word16be: end of input"
extract _ = return $ PRD.FError "word16be: end of input"

-- | Parse two bytes as a 'Word16', the first byte is the MSB of the Word16 and
-- second byte is the LSB (big endian representation).
Expand All @@ -178,7 +178,7 @@ word16leD = PRD.Parser step initial extract
step (Just' w) a =
return $ PRD.SDone 1 (w .|. fromIntegral a `unsafeShiftL` 8)

extract _ = return $ PRD.Error "word16le: end of input"
extract _ = return $ PRD.FError "word16le: end of input"

-- | Parse two bytes as a 'Word16', the first byte is the LSB of the Word16 and
-- second byte is the MSB (little endian representation).
Expand All @@ -205,7 +205,7 @@ word32beD = PRD.Parser step initial extract
in PRD.SContinue 1 (Tuple' w1 (sh - 8))
else PRD.SDone 1 (w .|. fromIntegral a)

extract _ = return $ PRD.Error "word32beD: end of input"
extract _ = return $ PRD.FError "word32beD: end of input"

-- | Parse four bytes as a 'Word32', the first byte is the MSB of the Word32
-- and last byte is the LSB (big endian representation).
Expand All @@ -231,7 +231,7 @@ word32leD = PRD.Parser step initial extract
then PRD.SContinue 1 (Tuple' w1 (sh + 8))
else PRD.SDone 1 w1

extract _ = return $ PRD.Error "word32leD: end of input"
extract _ = return $ PRD.FError "word32leD: end of input"

-- | Parse four bytes as a 'Word32', the first byte is the MSB of the Word32
-- and last byte is the LSB (big endian representation).
Expand All @@ -258,7 +258,7 @@ word64beD = PRD.Parser step initial extract
in PRD.SContinue 1 (Tuple' w1 (sh - 8))
else PRD.SDone 1 (w .|. fromIntegral a)

extract _ = return $ PRD.Error "word64beD: end of input"
extract _ = return $ PRD.FError "word64beD: end of input"

-- | Parse eight bytes as a 'Word64', the first byte is the MSB of the Word64
-- and last byte is the LSB (big endian representation).
Expand All @@ -284,7 +284,7 @@ word64leD = PRD.Parser step initial extract
then PRD.SContinue 1 (Tuple' w1 (sh + 8))
else PRD.SDone 1 w1

extract _ = return $ PRD.Error "word64leD: end of input"
extract _ = return $ PRD.FError "word64leD: end of input"

-- | Parse eight bytes as a 'Word64', the first byte is the MSB of the Word64
-- and last byte is the LSB (big endian representation).
Expand Down
Loading
Loading