on master help sign in
Rename some parsers (#1976)

Rename: element -> oneEq except -> oneNotEq eqBy -> listEqBy matchBy -> eqBy Change the signature of eqBy to return the list.

commit1f376cfd9bbd135fbb622efc0aca34c050ef98d6 authorHarendra Kumar <harendra@composewell.com> dateTue 18 Oct 2022, 16:40 (4 years ago) parent6603681
6 files changed+72−65
benchmark/Streamly/Benchmark/Data/Parser.hs+6−6 benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+6−6 core/src/Streamly/Data/Parser.hs+4−3 core/src/Streamly/Internal/Data/Parser.hs+36−30 core/src/Streamly/Internal/Data/Parser/ParserD.hs+16−17 core/src/Streamly/Internal/Unicode/Char/Parser.hs+4−3
benchmark/Streamly/Benchmark/Data/Parser.hs+6−6
@@ -153,13 +153,13 @@ takeFramedByEsc_ _ = Stream.parse parser
153153
154154 parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
155155
156+{-# INLINE listEqBy #-}
157+listEqBy :: Int -> Stream IO Int -> IO [Int]
158+listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
159+
156160{-# INLINE eqBy #-}
157161eqBy :: Int -> Stream IO Int -> IO ()
158−eqBy len = Stream.parse (PR.eqBy (==) [1 .. len])
159−
160−{-# INLINE matchBy #-}
161−matchBy :: Int -> Stream IO Int -> IO ()
162−matchBy len = Stream.parse (PR.matchBy (==) (Stream.enumerateFromTo 1 len))
162+eqBy len = Stream.parse (PR.eqBy (==) (Stream.enumerateFromTo 1 len))
163163
164164{-# INLINE takeWhile #-}
165165takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
@@ -481,8 +481,8 @@ o_1_space_serial value =
481481 , benchIOSink value "concatSequence" concatSequence
482482 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
483483 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
484+ , benchIOSink value "listEqBy" (listEqBy value)
484485 , benchIOSink value "eqBy" (eqBy value)
485− , benchIOSink value "matchBy" (matchBy value)
486486 ]
487487
488488o_1_space_filesystem :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+6−6
@@ -66,13 +66,13 @@ benchIOSink value name f =
6666-- Parsers
6767-------------------------------------------------------------------------------
6868
69+{-# INLINE listEqBy #-}
70+listEqBy :: Int -> Stream IO Int -> IO [Int]
71+listEqBy len = Stream.parseD (PR.listEqBy (==) [1 .. len])
72+
6973{-# INLINE eqBy #-}
7074eqBy :: Int -> Stream IO Int -> IO ()
71−eqBy len = Stream.parseD (PR.eqBy (==) [1 .. len])
72−
73−{-# INLINE matchBy #-}
74−matchBy :: Int -> Stream IO Int -> IO ()
75−matchBy len = Stream.parseD (PR.matchBy (==) (D.enumerateFromToIntegral 1 len))
75+eqBy len = Stream.parseD (PR.eqBy (==) (D.enumerateFromToIntegral 1 len))
7676
7777{-# INLINE drainWhile #-}
7878drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
@@ -359,8 +359,8 @@ o_1_space_serial value =
359359 , benchIOSink value "longest (all,any)" $ longestAllAny value
360360 -}
361361 , benchIOSink value "sequenceParser" sequenceParser
362+ , benchIOSink value "listEqBy" (listEqBy value)
362363 , benchIOSink value "eqBy" (eqBy value)
363− , benchIOSink value "matchBy" (matchBy value)
364364 ]
365365
366366o_1_space_serial_spanning :: Int -> [Benchmark]
core/src/Streamly/Data/Parser.hs+4−3
@@ -38,8 +38,8 @@ module Streamly.Data.Parser
3838
3939 -- All of these can be expressed in terms of either
4040 , one
41− , element
42− , except
41+ -- , oneEq
42+ -- , oneNotEq
4343 , oneOf
4444 , noneOf
4545 , satisfy
@@ -49,7 +49,8 @@ module Streamly.Data.Parser
4949
5050 -- ** Sequences
5151 , eqBy
52− , list
52+ , listEqBy
53+ , listEq
5354
5455 -- * Combinators
5556 -- Mapping on output
core/src/Streamly/Internal/Data/Parser.hs+36−30
@@ -76,8 +76,8 @@ module Streamly.Internal.Data.Parser
7676
7777 -- All of these can be expressed in terms of either
7878 , one
79− , element
80− , except
79+ , oneEq
80+ , oneNotEq
8181 , oneOf
8282 , noneOf
8383 , eof
@@ -102,9 +102,9 @@ module Streamly.Internal.Data.Parser
102102
103103 -- Grab a sequence of input elements by inspecting them
104104 -- ** Exact match
105+ , listEq
106+ , listEqBy
105107 , eqBy
106− , matchBy
107− , list
108108
109109 -- ** By predicate
110110 , takeWhileP
@@ -248,11 +248,6 @@ module Streamly.Internal.Data.Parser
248248 )
249249where
250250
251−import Data.Functor (($>))
252−import Prelude hiding
253− ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
254− , filter )
255−
256251import Streamly.Internal.Data.Fold.Type (Fold(..))
257252import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
258253import Streamly.Internal.Data.Stream.Type (Stream)
@@ -262,6 +257,10 @@ import qualified Streamly.Internal.Data.Parser.ParserD as D
262257import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
263258import qualified Streamly.Internal.Data.Stream.Type as Stream
264259
260+import Prelude hiding
261+ ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
262+ , filter )
263+
265264--
266265-- $setup
267266-- >>> :m
@@ -463,19 +462,19 @@ one = satisfy $ const True
463462
464463-- | Match a specific element.
465464--
466−-- >>> element x = Parser.satisfy (== x)
465+-- >>> oneEq x = Parser.satisfy (== x)
467466--
468−{-# INLINE element #-}
469−element :: (Monad m, Eq a) => a -> Parser m a a
470−element x = satisfy (== x)
467+{-# INLINE oneEq #-}
468+oneEq :: (Monad m, Eq a) => a -> Parser m a a
469+oneEq x = satisfy (== x)
471470
472471-- | Match anything other than the supplied element.
473472--
474−-- >>> except x = Parser.satisfy (/= x)
473+-- >>> oneNotEq x = Parser.satisfy (/= x)
475474--
476−{-# INLINE except #-}
477−except :: (Monad m, Eq a) => a -> Parser m a a
478−except x = satisfy (/= x)
475+{-# INLINE oneNotEq #-}
476+oneNotEq :: (Monad m, Eq a) => a -> Parser m a a
477+oneNotEq x = satisfy (/= x)
479478
480479-- | Match any one of the elements in the supplied list.
481480--
@@ -1026,30 +1025,37 @@ groupByRollingEither :: Monad m =>
10261025groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
10271026
10281027-- | Match the given sequence of elements using the given comparison function.
1028+-- Returns the original sequence if successful.
10291029--
1030−-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
1030+-- >>> Stream.parse (Parser.listEqBy (==) "string") $ Stream.fromList "string"
1031+-- "string"
10311032--
1032−-- >>> Stream.parse (Parser.eqBy (==) "mismatch") $ Stream.fromList "match"
1033−-- *** Exception: ParseError "eqBy: failed, yet to match 7 elements"
1033+-- >>> Stream.parse (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
1034+-- *** Exception: ParseError "listEqBy: failed, yet to match 7 elements"
10341035--
10351036-- /Pre-release/
10361037--
1037−{-# INLINE eqBy #-}
1038−eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1039−eqBy cmp = D.toParserK . D.eqBy cmp
1038+{-# INLINE listEqBy #-}
1039+listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a [a]
1040+listEqBy cmp xs = D.toParserK (D.listEqBy cmp xs)
10401041
1041−-- | Like eqBy but uses a stream instead of a list
1042−{-# INLINE matchBy #-}
1043−matchBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
1044−matchBy cmp = D.toParserK . D.matchBy cmp . Stream.toStreamD
1042+-- | Like 'listEqBy' but uses a stream instead of a list and does not return
1043+-- the stream.
1044+--
1045+-- /Pre-release/
1046+{-# INLINE eqBy #-}
1047+eqBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
1048+eqBy cmp = D.toParserK . D.eqBy cmp . Stream.toStreamD
10451049
10461050-- | Match the input sequence with the supplied list and return it if
10471051-- successful.
10481052--
1053+-- >>> listEq = Parser.listEqBy (==)
1054+--
10491055-- /Pre-release/
1050−{-# INLINE list #-}
1051−list :: (Monad m, Eq a) => [a] -> Parser m a [a]
1052−list xs = eqBy (==) xs $> xs
1056+{-# INLINE listEq #-}
1057+listEq :: (Monad m, Eq a) => [a] -> Parser m a [a]
1058+listEq = listEqBy (==)
10531059
10541060-------------------------------------------------------------------------------
10551061-- nested parsers
core/src/Streamly/Internal/Data/Parser/ParserD.hs+16−17
@@ -91,8 +91,8 @@ module Streamly.Internal.Data.Parser.ParserD
9191 , groupByRollingEither
9292
9393 -- Matching strings
94+ , listEqBy
9495 , eqBy
95− , matchBy
9696 -- , prefixOf -- match any prefix of a given string
9797 -- , suffixOf -- match any suffix of a given string
9898 -- , infixOf -- match any substring of a given string
@@ -1474,40 +1474,39 @@ groupByRollingEither
14741474--
14751475-- /Pre-release/
14761476--
1477−{-# INLINE eqBy #-}
1478−eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1479−eqBy cmp str = Parser step initial extract
1477+{-# INLINE listEqBy #-}
1478+listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a [a]
1479+listEqBy cmp str = Parser step initial extract
14801480
14811481 where
14821482
14831483 -- XXX Should return IDone in initial for [] case
14841484 initial = return $ IPartial str
14851485
1486− step [] _ = return $ Done 0 ()
1486+ step [] _ = return $ Done 0 str
14871487 step [x] a =
14881488 return
14891489 $ if x `cmp` a
1490− then Done 0 ()
1491− else Error "eqBy: failed, yet to match the last element"
1490+ then Done 0 str
1491+ else Error "listEqBy: failed, yet to match the last element"
14921492 step (x:xs) a =
14931493 return
14941494 $ if x `cmp` a
14951495 then Continue 0 xs
14961496 else Error
1497− $ "eqBy: failed, yet to match "
1497+ $ "listEqBy: failed, yet to match "
14981498 ++ show (length xs + 1) ++ " elements"
14991499
15001500 extract xs =
15011501 return
15021502 $ Error
1503− $ "eqBy: end of input, yet to match "
1503+ $ "listEqBy: end of input, yet to match "
15041504 ++ show (length xs) ++ " elements"
15051505
1506−-- XXX rename to streamBy?
1507−-- | Like eqBy but uses a stream instead of a list
1508−{-# INLINE matchBy #-}
1509−matchBy :: Monad m => (a -> a -> Bool) -> D.Stream m a -> Parser m a ()
1510−matchBy cmp (D.Stream sstep state) = Parser step initial extract
1506+-- | Like 'listEqBy' but uses a stream instead of a list
1507+{-# INLINE eqBy #-}
1508+eqBy :: Monad m => (a -> a -> Bool) -> D.Stream m a -> Parser m a ()
1509+eqBy cmp (D.Stream sstep state) = Parser step initial extract
15111510
15121511 where
15131512
@@ -1528,7 +1527,7 @@ matchBy cmp (D.Stream sstep state) = Parser step initial extract
15281527 D.Yield x1 s -> Continue 0 (Just' x1, s)
15291528 D.Stop -> Done 0 ()
15301529 D.Skip s -> Continue 1 (Nothing', s)
1531− else return $ Error "match: mismtach occurred"
1530+ else return $ Error "eqBy: mismtach occurred"
15321531 step (Nothing', st) a = do
15331532 r <- sstep defState st
15341533 return
@@ -1536,11 +1535,11 @@ matchBy cmp (D.Stream sstep state) = Parser step initial extract
15361535 D.Yield x s -> do
15371536 if x `cmp` a
15381537 then Continue 0 (Nothing', s)
1539− else Error "match: mismatch occurred"
1538+ else Error "eqBy: mismatch occurred"
15401539 D.Stop -> Done 1 ()
15411540 D.Skip s -> Continue 1 (Nothing', s)
15421541
1543− extract _ = return $ Error "match: end of input"
1542+ extract _ = return $ Error "eqBy: end of input"
15441543
15451544-------------------------------------------------------------------------------
15461545-- 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
6565 (
6666 lmap
6767 , satisfy
68− , list
68+ , listEq
6969 , takeWhile1
7070 , dropWhile
7171 )
@@ -183,12 +183,13 @@ charIgnoreCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
183183
184184-- | Match the input with the supplied string and return it if successful.
185185string :: MonadCatch m => String -> Parser m Char String
186−string = Parser.list
186+string = Parser.listEq
187187
188188-- XXX Not accurate unicode case conversion
189189-- | Match the input with the supplied string and return it if successful.
190190stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
191−stringIgnoreCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
191+stringIgnoreCase s =
192+ Parser.lmap Char.toLower (Parser.listEq (map Char.toLower s))
192193
193194-- | Drop /zero/ or more white space characters.
194195dropSpace :: MonadCatch m => Parser m Char ()