on master help sign in
Rename some parsers (#1976)
1f376cfHarendra Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+6−6
@@ -153,13 +153,13 @@ takeFramedByEsc_ _ = Stream.parse parser
153
153
154 parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
154 parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
155
155
156+{-# INLINE listEqBy #-}
157+listEqBy :: Int -> Stream IO Int -> IO [Int]
158+listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
159+
156{-# INLINE eqBy #-}
160{-# INLINE eqBy #-}
157eqBy :: Int -> Stream IO Int -> IO ()
161eqBy :: Int -> Stream IO Int -> IO ()
158−eqBy len = Stream.parse (PR.eqBy (==) [1 .. len])
162+eqBy len = Stream.parse (PR.eqBy (==) (Stream.enumerateFromTo 1 len))
159−
160−{-# INLINE matchBy #-}
161−matchBy :: Int -> Stream IO Int -> IO ()
162−matchBy len = Stream.parse (PR.matchBy (==) (Stream.enumerateFromTo 1 len))
163
163
164{-# INLINE takeWhile #-}
164{-# INLINE takeWhile #-}
165takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
165takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
@@ -481,8 +481,8 @@ o_1_space_serial value =
481 , benchIOSink value "concatSequence" concatSequence
481 , benchIOSink value "concatSequence" concatSequence
482 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
482 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
483 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
483 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
484+ , benchIOSink value "listEqBy" (listEqBy value)
484 , benchIOSink value "eqBy" (eqBy value)
485 , benchIOSink value "eqBy" (eqBy value)
485− , benchIOSink value "matchBy" (matchBy value)
486 ]
486 ]
487
487
488o_1_space_filesystem :: BenchEnv -> [Benchmark]
488o_1_space_filesystem :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+6−6
@@ -66,13 +66,13 @@ benchIOSink value name f =
66-- Parsers
66-- Parsers
67-------------------------------------------------------------------------------
67-------------------------------------------------------------------------------
68
68
69+{-# INLINE listEqBy #-}
70+listEqBy :: Int -> Stream IO Int -> IO [Int]
71+listEqBy len = Stream.parseD (PR.listEqBy (==) [1 .. len])
72+
69{-# INLINE eqBy #-}
73{-# INLINE eqBy #-}
70eqBy :: Int -> Stream IO Int -> IO ()
74eqBy :: Int -> Stream IO Int -> IO ()
71−eqBy len = Stream.parseD (PR.eqBy (==) [1 .. len])
75+eqBy len = Stream.parseD (PR.eqBy (==) (D.enumerateFromToIntegral 1 len))
72−
73−{-# INLINE matchBy #-}
74−matchBy :: Int -> Stream IO Int -> IO ()
75−matchBy len = Stream.parseD (PR.matchBy (==) (D.enumerateFromToIntegral 1 len))
76
76
77{-# INLINE drainWhile #-}
77{-# INLINE drainWhile #-}
78drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
78drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
@@ -359,8 +359,8 @@ o_1_space_serial value =
359 , benchIOSink value "longest (all,any)" $ longestAllAny value
359 , benchIOSink value "longest (all,any)" $ longestAllAny value
360 -}
360 -}
361 , benchIOSink value "sequenceParser" sequenceParser
361 , benchIOSink value "sequenceParser" sequenceParser
362+ , benchIOSink value "listEqBy" (listEqBy value)
362 , benchIOSink value "eqBy" (eqBy value)
363 , benchIOSink value "eqBy" (eqBy value)
363− , benchIOSink value "matchBy" (matchBy value)
364 ]
364 ]
365
365
366o_1_space_serial_spanning :: Int -> [Benchmark]
366o_1_space_serial_spanning :: Int -> [Benchmark]
core/src/Streamly/Data/Parser.hs+4−3
@@ -38,8 +38,8 @@ module Streamly.Data.Parser
38
38
39 -- All of these can be expressed in terms of either
39 -- All of these can be expressed in terms of either
40 , one
40 , one
41− , element
41+ -- , oneEq
42− , except
42+ -- , oneNotEq
43 , oneOf
43 , oneOf
44 , noneOf
44 , noneOf
45 , satisfy
45 , satisfy
@@ -49,7 +49,8 @@ module Streamly.Data.Parser
49
49
50 -- ** Sequences
50 -- ** Sequences
51 , eqBy
51 , eqBy
52− , list
52+ , listEqBy
53+ , listEq
53
54
54 -- * Combinators
55 -- * Combinators
55 -- Mapping on output
56 -- Mapping on output
core/src/Streamly/Internal/Data/Parser.hs+36−30
@@ -76,8 +76,8 @@ module Streamly.Internal.Data.Parser
76
76
77 -- All of these can be expressed in terms of either
77 -- All of these can be expressed in terms of either
78 , one
78 , one
79− , element
79+ , oneEq
80− , except
80+ , oneNotEq
81 , oneOf
81 , oneOf
82 , noneOf
82 , noneOf
83 , eof
83 , eof
@@ -102,9 +102,9 @@ module Streamly.Internal.Data.Parser
102
102
103 -- Grab a sequence of input elements by inspecting them
103 -- Grab a sequence of input elements by inspecting them
104 -- ** Exact match
104 -- ** Exact match
105+ , listEq
106+ , listEqBy
105 , eqBy
107 , eqBy
106− , matchBy
107− , list
108
108
109 -- ** By predicate
109 -- ** By predicate
110 , takeWhileP
110 , takeWhileP
@@ -248,11 +248,6 @@ module Streamly.Internal.Data.Parser
248 )
248 )
249where
249where
250
250
251−import Data.Functor (($>))
252−import Prelude hiding
253− ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
254− , filter )
255−
256import Streamly.Internal.Data.Fold.Type (Fold(..))
251import Streamly.Internal.Data.Fold.Type (Fold(..))
257import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
252import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
258import Streamly.Internal.Data.Stream.Type (Stream)
253import Streamly.Internal.Data.Stream.Type (Stream)
@@ -262,6 +257,10 @@ import qualified Streamly.Internal.Data.Parser.ParserD as D
262import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
257import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
263import qualified Streamly.Internal.Data.Stream.Type as Stream
258import qualified Streamly.Internal.Data.Stream.Type as Stream
264
259
260+import Prelude hiding
261+ ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
262+ , filter )
263+
265--
264--
266-- $setup
265-- $setup
267-- >>> :m
266-- >>> :m
@@ -463,19 +462,19 @@ one = satisfy $ const True
463
462
464-- | Match a specific element.
463-- | Match a specific element.
465--
464--
466−-- >>> element x = Parser.satisfy (== x)
465+-- >>> oneEq x = Parser.satisfy (== x)
467--
466--
468−{-# INLINE element #-}
467+{-# INLINE oneEq #-}
469−element :: (Monad m, Eq a) => a -> Parser m a a
468+oneEq :: (Monad m, Eq a) => a -> Parser m a a
470−element x = satisfy (== x)
469+oneEq x = satisfy (== x)
471
470
472-- | Match anything other than the supplied element.
471-- | Match anything other than the supplied element.
473--
472--
474−-- >>> except x = Parser.satisfy (/= x)
473+-- >>> oneNotEq x = Parser.satisfy (/= x)
475--
474--
476−{-# INLINE except #-}
475+{-# INLINE oneNotEq #-}
477−except :: (Monad m, Eq a) => a -> Parser m a a
476+oneNotEq :: (Monad m, Eq a) => a -> Parser m a a
478−except x = satisfy (/= x)
477+oneNotEq x = satisfy (/= x)
479
478
480-- | Match any one of the elements in the supplied list.
479-- | Match any one of the elements in the supplied list.
481--
480--
@@ -1026,30 +1025,37 @@ groupByRollingEither :: Monad m =>
1026groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1025groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1027
1026
1028-- | Match the given sequence of elements using the given comparison function.
1027-- | Match the given sequence of elements using the given comparison function.
1028+-- Returns the original sequence if successful.
1029--
1029--
1030−-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
1030+-- >>> Stream.parse (Parser.listEqBy (==) "string") $ Stream.fromList "string"
1031+-- "string"
1031--
1032--
1032−-- >>> Stream.parse (Parser.eqBy (==) "mismatch") $ Stream.fromList "match"
1033+-- >>> Stream.parse (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
1033−-- *** Exception: ParseError "eqBy: failed, yet to match 7 elements"
1034+-- *** Exception: ParseError "listEqBy: failed, yet to match 7 elements"
1034--
1035--
1035-- /Pre-release/
1036-- /Pre-release/
1036--
1037--
1037−{-# INLINE eqBy #-}
1038+{-# INLINE listEqBy #-}
1038−eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1039+listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a [a]
1039−eqBy cmp = D.toParserK . D.eqBy cmp
1040+listEqBy cmp xs = D.toParserK (D.listEqBy cmp xs)
1040
1041
1041−-- | Like eqBy but uses a stream instead of a list
1042+-- | Like 'listEqBy' but uses a stream instead of a list and does not return
1043+-- the stream.
1044+--
1045+-- /Pre-release/
1042−{-# INLINE matchBy #-}
1046+{-# INLINE eqBy #-}
1043−matchBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
1047+eqBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
1044−matchBy cmp = D.toParserK . D.matchBy cmp . Stream.toStreamD
1048+eqBy cmp = D.toParserK . D.eqBy cmp . Stream.toStreamD
1045
1049
1046-- | Match the input sequence with the supplied list and return it if
1050-- | Match the input sequence with the supplied list and return it if
1047-- successful.
1051-- successful.
1048--
1052--
1053+-- >>> listEq = Parser.listEqBy (==)
1054+--
1049-- /Pre-release/
1055-- /Pre-release/
1050−{-# INLINE list #-}
1056+{-# INLINE listEq #-}
1051−list :: (Monad m, Eq a) => [a] -> Parser m a [a]
1057+listEq :: (Monad m, Eq a) => [a] -> Parser m a [a]
1052−list xs = eqBy (==) xs $> xs
1058+listEq = listEqBy (==)
1053
1059
1054-------------------------------------------------------------------------------
1060-------------------------------------------------------------------------------
1055-- nested parsers
1061-- nested parsers
core/src/Streamly/Internal/Data/Parser/ParserD.hs+16−17
@@ -91,8 +91,8 @@ module Streamly.Internal.Data.Parser.ParserD
91 , groupByRollingEither
91 , groupByRollingEither
92
92
93 -- Matching strings
93 -- Matching strings
94+ , listEqBy
94 , eqBy
95 , eqBy
95− , matchBy
96 -- , prefixOf -- match any prefix of a given string
96 -- , prefixOf -- match any prefix of a given string
97 -- , suffixOf -- match any suffix of a given string
97 -- , suffixOf -- match any suffix of a given string
98 -- , infixOf -- match any substring of a given string
98 -- , infixOf -- match any substring of a given string
@@ -1474,40 +1474,39 @@ groupByRollingEither
1474--
1474--
1475-- /Pre-release/
1475-- /Pre-release/
1476--
1476--
1477−{-# INLINE eqBy #-}
1477+{-# INLINE listEqBy #-}
1478−eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1478+listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a [a]
1479−eqBy cmp str = Parser step initial extract
1479+listEqBy cmp str = Parser step initial extract
1480
1480
1481 where
1481 where
1482
1482
1483 -- XXX Should return IDone in initial for [] case
1483 -- XXX Should return IDone in initial for [] case
1484 initial = return $ IPartial str
1484 initial = return $ IPartial str
1485
1485
1486− step [] _ = return $ Done 0 ()
1486+ step [] _ = return $ Done 0 str
1487 step [x] a =
1487 step [x] a =
1488 return
1488 return
1489 $ if x `cmp` a
1489 $ if x `cmp` a
1490− then Done 0 ()
1490+ then Done 0 str
1491− else Error "eqBy: failed, yet to match the last element"
1491+ else Error "listEqBy: failed, yet to match the last element"
1492 step (x:xs) a =
1492 step (x:xs) a =
1493 return
1493 return
1494 $ if x `cmp` a
1494 $ if x `cmp` a
1495 then Continue 0 xs
1495 then Continue 0 xs
1496 else Error
1496 else Error
1497− $ "eqBy: failed, yet to match "
1497+ $ "listEqBy: failed, yet to match "
1498 ++ show (length xs + 1) ++ " elements"
1498 ++ show (length xs + 1) ++ " elements"
1499
1499
1500 extract xs =
1500 extract xs =
1501 return
1501 return
1502 $ Error
1502 $ Error
1503− $ "eqBy: end of input, yet to match "
1503+ $ "listEqBy: end of input, yet to match "
1504 ++ show (length xs) ++ " elements"
1504 ++ show (length xs) ++ " elements"
1505
1505
1506−-- XXX rename to streamBy?
1507−-- | Like eqBy but uses a stream instead of a list
1506+-- | Like 'listEqBy' but uses a stream instead of a list
1508−{-# INLINE matchBy #-}
1507+{-# INLINE eqBy #-}
1509−matchBy :: Monad m => (a -> a -> Bool) -> D.Stream m a -> Parser m a ()
1508+eqBy :: Monad m => (a -> a -> Bool) -> D.Stream m a -> Parser m a ()
1510−matchBy cmp (D.Stream sstep state) = Parser step initial extract
1509+eqBy cmp (D.Stream sstep state) = Parser step initial extract
1511
1510
1512 where
1511 where
1513
1512
@@ -1528,7 +1527,7 @@ matchBy cmp (D.Stream sstep state) = Parser step initial extract
1528 D.Yield x1 s -> Continue 0 (Just' x1, s)
1527 D.Yield x1 s -> Continue 0 (Just' x1, s)
1529 D.Stop -> Done 0 ()
1528 D.Stop -> Done 0 ()
1530 D.Skip s -> Continue 1 (Nothing', s)
1529 D.Skip s -> Continue 1 (Nothing', s)
1531− else return $ Error "match: mismtach occurred"
1530+ else return $ Error "eqBy: mismtach occurred"
1532 step (Nothing', st) a = do
1531 step (Nothing', st) a = do
1533 r <- sstep defState st
1532 r <- sstep defState st
1534 return
1533 return
@@ -1536,11 +1535,11 @@ matchBy cmp (D.Stream sstep state) = Parser step initial extract
1536 D.Yield x s -> do
1535 D.Yield x s -> do
1537 if x `cmp` a
1536 if x `cmp` a
1538 then Continue 0 (Nothing', s)
1537 then Continue 0 (Nothing', s)
1539− else Error "match: mismatch occurred"
1538+ else Error "eqBy: mismatch occurred"
1540 D.Stop -> Done 1 ()
1539 D.Stop -> Done 1 ()
1541 D.Skip s -> Continue 1 (Nothing', s)
1540 D.Skip s -> Continue 1 (Nothing', s)
1542
1541
1543− extract _ = return $ Error "match: end of input"
1542+ extract _ = return $ Error "eqBy: end of input"
1544
1543
1545-------------------------------------------------------------------------------
1544-------------------------------------------------------------------------------
1546-- Transformations on input
1545-- Transformations on input
core/src/Streamly/Internal/Unicode/Char/Parser.hs+4−3
@@ -65,7 +65,7 @@ import qualified Streamly.Internal.Data.Parser as Parser
65 (
65 (
66 lmap
66 lmap
67 , satisfy
67 , satisfy
68− , list
68+ , listEq
69 , takeWhile1
69 , takeWhile1
70 , dropWhile
70 , dropWhile
71 )
71 )
@@ -183,12 +183,13 @@ charIgnoreCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
183
183
184-- | Match the input with the supplied string and return it if successful.
184-- | Match the input with the supplied string and return it if successful.
185string :: MonadCatch m => String -> Parser m Char String
185string :: MonadCatch m => String -> Parser m Char String
186−string = Parser.list
186+string = Parser.listEq
187
187
188-- XXX Not accurate unicode case conversion
188-- XXX Not accurate unicode case conversion
189-- | Match the input with the supplied string and return it if successful.
189-- | Match the input with the supplied string and return it if successful.
190stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
190stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
191+stringIgnoreCase s =
191−stringIgnoreCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
192+ Parser.lmap Char.toLower (Parser.listEq (map Char.toLower s))
192
193
193-- | Drop /zero/ or more white space characters.
194-- | Drop /zero/ or more white space characters.
194dropSpace :: MonadCatch m => Parser m Char ()
195dropSpace :: MonadCatch m => Parser m Char ()