@@ -17,6 +17,8 @@ module Data.Attoparsec.Internal
1717 , get
1818 , put
1919 , prompt
20+ , demandInput
21+ , wantInput
2022 ) where
2123
2224#if __GLASGOW_HASKELL__ >= 700
@@ -69,3 +71,33 @@ prompt i0 a0 _m0 kf ks = Partial $ \s ->
6971 -> (Input Text -> Added Text-> More -> IResult Text r)
7072 -> IResult Text r #-}
7173#endif
74+
75+-- | Immediately demand more input via a 'Partial' continuation
76+-- result.
77+demandInput :: Chunk t => Parser t ()
78+demandInput = Parser $ \i0 a0 m0 kf ks ->
79+ if m0 == Complete
80+ then kf i0 a0 m0 ["demandInput"] "not enough input"
81+ else let kf' i a m = kf i a m ["demandInput"] "not enough input"
82+ ks' i a m = ks i a m ()
83+ in prompt i0 a0 m0 kf' ks'
84+#if __GLASGOW_HASKELL__ >= 700
85+{-# SPECIALIZE demandInput :: Parser ByteString () #-}
86+{-# SPECIALIZE demandInput :: Parser Text () #-}
87+#endif
88+
89+-- | This parser always succeeds. It returns 'True' if any input is
90+-- available either immediately or on demand, and 'False' if the end
91+-- of all input has been reached.
92+wantInput :: Chunk t => Parser t Bool
93+wantInput = Parser $ \i0 a0 m0 _kf ks ->
94+ case () of
95+ _ | not (nullChunk (unI i0)) -> ks i0 a0 m0 True
96+ | m0 == Complete -> ks i0 a0 m0 False
97+ | otherwise -> let kf' i a m = ks i a m False
98+ ks' i a m = ks i a m True
99+ in prompt i0 a0 m0 kf' ks'
100+#if __GLASGOW_HASKELL__ >= 700
101+{-# SPECIALIZE wantInput :: Parser ByteString Bool #-}
102+{-# SPECIALIZE wantInput :: Parser Text Bool #-}
103+#endif