on master help sign in
Add some derived utility parsers
43dda21Harendra Kumar
core/src/Streamly/Internal/Data/Parser.hs+55−2
@@ -70,9 +70,13 @@ module Streamly.Internal.Data.Parser
70 -- * Element parsers
70 -- * Element parsers
71 , peek
71 , peek
72 , one
72 , one
73+ , next
74+ , element
75+ , except
76+ , oneOf
77+ , noneOf
73 , eof
78 , eof
74 , satisfy
79 , satisfy
75− , next
76 , maybe
80 , maybe
77 , either
81 , either
78
82
@@ -396,13 +400,62 @@ satisfy = D.toParserK . D.satisfy
396-- | Consume one element from the head of the stream. Fails if it encounters
400-- | Consume one element from the head of the stream. Fails if it encounters
397-- end of input.
401-- end of input.
398--
402--
399−-- >>> one = satisfy $ const True
403+-- >>> one = Parser.satisfy $ const True
400--
404--
401-- /Pre-release/
405-- /Pre-release/
402--
406--
407+{-# INLINE one #-}
403one :: MonadCatch m => Parser m a a
408one :: MonadCatch m => Parser m a a
404one = satisfy $ const True
409one = satisfy $ const True
405
410
411+-- | Match a specific element.
412+--
413+-- >>> element x = Parser.satisfy (== x)
414+--
415+{-# INLINE element #-}
416+element :: (MonadCatch m, Eq a) => a -> Parser m a a
417+element x = satisfy (== x)
418+
419+-- | Match anything other than the supplied element.
420+--
421+-- >>> except x = Parser.satisfy (/= x)
422+--
423+{-# INLINE except #-}
424+except :: (MonadCatch m, Eq a) => a -> Parser m a a
425+except x = satisfy (/= x)
426+
427+-- | Match any one of the elements in the supplied list.
428+--
429+-- >>> oneOf xs = Parser.satisfy (`elem` xs)
430+--
431+-- When performance matters a pattern matching predicate could be more
432+-- efficient than a list:
433+--
434+-- @
435+-- let p x =
436+-- case x of
437+-- 'a' -> True
438+-- 'e' -> True
439+-- _ -> False
440+-- in satisfy p
441+-- @
442+--
443+-- GHC may use a binary search instead of linear search in the list.
444+-- Alternatively, you can also use an array instead of list for storage and
445+-- search.
446+--
447+{-# INLINE oneOf #-}
448+oneOf :: (MonadCatch m, Eq a) => [a] -> Parser m a a
449+oneOf xs = satisfy (`elem` xs)
450+
451+-- | See performance notes in 'oneOf'.
452+--
453+-- >>> noneOf xs = Parser.satisfy (`notElem` xs)
454+--
455+{-# INLINE noneOf #-}
456+noneOf :: (MonadCatch m, Eq a) => [a] -> Parser m a a
457+noneOf xs = satisfy (`notElem` xs)
458+
406-- | Return the next element of the input. Returns 'Nothing'
459-- | Return the next element of the input. Returns 'Nothing'
407-- on end of input. Also known as 'head'.
460-- on end of input. Also known as 'head'.
408--
461--