git.iko.soy/typeable/attoparsec
on master help sign in
Unified 'demandInput' and 'wantInput' for ByteString and Text.
commitfcc25ceba1fc088f732fe315bf2aab17de45fbe0 authorMikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com> dateTue 11 Feb 2014, 23:33 (13 years ago) parent5186287
3 files changed+32−44
Data/Attoparsec/ByteString/Internal.hs−22 Data/Attoparsec/Internal.hs+32 Data/Attoparsec/Text/Internal.hs−22
Data/Attoparsec/ByteString/Internal.hs−22
@@ -121,28 +121,6 @@ ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
121121-- Non-recursive so the bounds check can be inlined:
122122{-# INLINE ensure #-}
123123
124−-- | Immediately demand more input via a 'Partial' continuation
125−-- result.
126−demandInput :: Parser ()
127−demandInput = T.Parser $ \i0 a0 m0 kf ks ->
128− if m0 == Complete
129− then kf i0 a0 m0 ["demandInput"] "not enough bytes"
130− else let kf' i a m = kf i a m ["demandInput"] "not enough bytes"
131− ks' i a m = ks i a m ()
132− in prompt i0 a0 m0 kf' ks'
133−
134−-- | This parser always succeeds. It returns 'True' if any input is
135−-- available either immediately or on demand, and 'False' if the end
136−-- of all input has been reached.
137−wantInput :: Parser Bool
138−wantInput = T.Parser $ \i0 a0 m0 _kf ks ->
139− case () of
140− _ | not (B.null (unI i0)) -> ks i0 a0 m0 True
141− | m0 == Complete -> ks i0 a0 m0 False
142− | otherwise -> let kf' i a m = ks i a m False
143− ks' i a m = ks i a m True
144− in prompt i0 a0 m0 kf' ks'
145−
146124-- | The parser @satisfy p@ succeeds for any byte for which the
147125-- predicate @p@ returns 'True'. Returns the byte that is actually
148126-- parsed.
Data/Attoparsec/Internal.hs+32
@@ -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
Data/Attoparsec/Text/Internal.hs−22
@@ -107,28 +107,6 @@ ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
107107 else runParser (demandInput >> go n') i0 a0 m0 kf ks
108108{-# INLINE ensure #-}
109109
110−-- | Immediately demand more input via a 'Partial' continuation
111−-- result.
112−demandInput :: Parser ()
113−demandInput = T.Parser $ \i0 a0 m0 kf ks ->
114− if m0 == Complete
115− then kf i0 a0 m0 ["demandInput"] "not enough input"
116− else let kf' i a m = kf i a m ["demandInput"] "not enough input"
117− ks' i a m = ks i a m ()
118− in prompt i0 a0 m0 kf' ks'
119−
120−-- | This parser always succeeds. It returns 'True' if any input is
121−-- available either immediately or on demand, and 'False' if the end
122−-- of all input has been reached.
123−wantInput :: Parser Bool
124−wantInput = T.Parser $ \i0 a0 m0 _kf ks ->
125− case () of
126− _ | not (T.null (unI i0)) -> ks i0 a0 m0 True
127− | m0 == Complete -> ks i0 a0 m0 False
128− | otherwise -> let kf' i a m = ks i a m False
129− ks' i a m = ks i a m True
130− in prompt i0 a0 m0 kf' ks'
131−
132110unsafeHead :: Text -> Char
133111unsafeHead = T.head
134112