on master help sign in
Add 'next' to Parsers
d70b57cJulian Ospald
src/Streamly/Internal/Data/Parser.hs+10
@@ -66,6 +66,7 @@ module Streamly.Internal.Data.Parser
66 , peek
66 , peek
67 , eof
67 , eof
68 , satisfy
68 , satisfy
69+ , next
69 , maybe
70 , maybe
70 , either
71 , either
71
72
@@ -342,6 +343,15 @@ eof = K.toParserK D.eof
342satisfy :: MonadCatch m => (a -> Bool) -> Parser m a a
343satisfy :: MonadCatch m => (a -> Bool) -> Parser m a a
343satisfy = K.toParserK . D.satisfy
344satisfy = K.toParserK . D.satisfy
344
345
346+-- | Return the next element of the input. Returns 'Nothing'
347+-- on end of input. Also known as 'head'.
348+--
349+-- /Pre-release/
350+--
351+{-# INLINE next #-}
352+next :: MonadCatch m => Parser m a (Maybe a)
353+next = K.toParserK D.next
354+
345-- | Map a 'Maybe' returning function on the next element in the stream. The
355-- | Map a 'Maybe' returning function on the next element in the stream. The
346-- parser fails if the function returns 'Nothing' otherwise returns the 'Just'
356-- parser fails if the function returns 'Nothing' otherwise returns the 'Just'
347-- value.
357-- value.
src/Streamly/Internal/Data/Parser/ParserD.hs+13
@@ -33,6 +33,7 @@ module Streamly.Internal.Data.Parser.ParserD
33 , peek
33 , peek
34 , eof
34 , eof
35 , satisfy
35 , satisfy
36+ , next
36 , maybe
37 , maybe
37 , either
38 , either
38
39
@@ -311,6 +312,18 @@ satisfy predicate = Parser step initial extract
311
312
312 extract _ = throwM $ ParseError "satisfy: end of input"
313 extract _ = throwM $ ParseError "satisfy: end of input"
313
314
315+-- | See 'Streamly.Internal.Data.Parser.satisfy'.
316+--
317+-- /Pre-release/
318+--
319+{-# INLINE next #-}
320+next :: MonadCatch m => Parser m a (Maybe a)
321+next = Parser step initial extract
322+ where
323+ initial = pure $ IPartial ()
324+ step _ a = pure $ Done 0 (Just a)
325+ extract _ = pure Nothing
326+
314-- | See 'Streamly.Internal.Data.Parser.maybe'.
327-- | See 'Streamly.Internal.Data.Parser.maybe'.
315--
328--
316-- /Pre-release/
329-- /Pre-release/
test/Streamly/Test/Data/Parser.hs+15
@@ -155,6 +155,19 @@ satisfy =
155 where
155 where
156 predicate = (>= mid_value)
156 predicate = (>= mid_value)
157
157
158+nextPass :: Property
159+nextPass =
160+ forAll (chooseInt (1, max_value)) $ \int ->
161+ property (case S.parse P.next (S.fromList [int]) of
162+ Right (Just i) -> i == int
163+ _ -> False)
164+
165+next :: Property
166+next =
167+ property (case S.parse P.next (S.fromList []) of
168+ Right Nothing -> True
169+ _ -> False)
170+
158-- Sequence Parsers Tests
171-- Sequence Parsers Tests
159takeBetweenPass :: Property
172takeBetweenPass :: Property
160takeBetweenPass =
173takeBetweenPass =
@@ -745,6 +758,8 @@ main =
745 prop "eof pass on []" eofPass
758 prop "eof pass on []" eofPass
746 prop "eof fail on non-empty list" eofFail
759 prop "eof fail on non-empty list" eofFail
747 prop "first element exists and >= mid_value" satisfyPass
760 prop "first element exists and >= mid_value" satisfyPass
761+ prop "next pass on [Int]" nextPass
762+ prop "next fail on []" next
748 prop "check first element exists and satisfies predicate" satisfy
763 prop "check first element exists and satisfies predicate" satisfy
749
764
750 describe "test for sequence parser" $ do
765 describe "test for sequence parser" $ do