Commits

Anonymous committed b78e97a

Unified 'prompt' function from .Internal modules.

Comments (0)

Files changed (5)

Data/Attoparsec/ByteString/Internal.hs

 -- Non-recursive so the bounds check can be inlined:
 {-# INLINE ensure #-}
 
--- | Ask for input.  If we receive any, pass it to a success
--- continuation, otherwise to a failure continuation.
-prompt :: Input -> Added -> More
-       -> (Input -> Added -> More -> Result r)
-       -> (Input -> Added -> More -> Result r)
-       -> Result r
-prompt i0 a0 _m0 kf ks = Partial $ \s ->
-    if B.null s
-    then kf i0 a0 Complete
-    else ks (i0 <> I s) (a0 <> A s) Incomplete
-
 -- | Immediately demand more input via a 'Partial' continuation
 -- result.
 demandInput :: Parser ()

Data/Attoparsec/Internal.hs

+{-# LANGUAGE CPP #-}
 -- |
 -- Module      :  Data.Attoparsec.Internal
 -- Copyright   :  Bryan O'Sullivan 2012
       compareResults
     , get
     , put
+    , prompt
     ) where
 
+#if __GLASGOW_HASKELL__ >= 700
+import Data.ByteString (ByteString)
+import Data.Text (Text)
+#endif
 import Data.Attoparsec.Internal.Types
 
 -- | Compare two 'IResult' values for equality.
 put :: t -> Parser t ()
 put c = Parser $ \_i0 a0 m0 _kf ks -> ks (I c) a0 m0 ()
 {-# INLINE put #-}
+
+-- | Ask for input.  If we receive any, pass it to a success
+-- continuation, otherwise to a failure continuation.
+prompt :: Chunk t
+       => Input t -> Added t -> More
+       -> (Input t -> Added t -> More -> IResult t r)
+       -> (Input t -> Added t -> More -> IResult t r)
+       -> IResult t r
+prompt i0 a0 _m0 kf ks = Partial $ \s ->
+    if nullChunk s
+    then kf i0 a0 Complete
+    else ks (i0 <> I s) (a0 <> A s) Incomplete
+#if __GLASGOW_HASKELL__ >= 700
+{-# SPECIALIZE prompt :: Input ByteString -> Added ByteString -> More
+                      -> (Input ByteString -> Added ByteString -> More
+                          -> IResult ByteString r)
+                      -> (Input ByteString -> Added ByteString -> More
+                          -> IResult ByteString r)
+                      -> IResult ByteString r #-}
+{-# SPECIALIZE prompt :: Input Text -> Added Text -> More
+                      -> (Input Text -> Added Text -> More -> IResult Text r)
+                      -> (Input Text -> Added Text-> More -> IResult Text r)
+                      -> IResult Text r #-}
+#endif

Data/Attoparsec/Internal/Types.hs

 {-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving, OverloadedStrings,
-    Rank2Types, RecordWildCards #-}
+    Rank2Types, RecordWildCards, TypeFamilies #-}
 -- |
 -- Module      :  Data.Attoparsec.Internal.Types
 -- Copyright   :  Bryan O'Sullivan 2007-2011
     , More(..)
     , addS
     , (<>)
+    , Chunk(..)
     ) where
 
 import Control.Applicative (Alternative(..), Applicative(..), (<$>))
 import Control.DeepSeq (NFData(rnf))
 import Control.Monad (MonadPlus(..))
+import Data.Word (Word8)
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as BS
+import Data.Text (Text)
+import qualified Data.Text as T
 import Data.Monoid (Monoid(..))
 import Prelude hiding (getChar, take, takeWhile)
 
 (<>) :: (Monoid m) => m -> m -> m
 (<>) = mappend
 {-# INLINE (<>) #-}
+
+-- | A common interface for input chunks.
+class Monoid c => Chunk c where
+  type ChunkElem c
+  -- | Test if the chunk is empty.
+  nullChunk :: c -> Bool
+
+instance Chunk ByteString where
+  type ChunkElem ByteString = Word8
+  nullChunk = BS.null
+  {-# INLINE nullChunk #-}
+
+instance Chunk Text where
+  type ChunkElem Text = Char
+  nullChunk = T.null
+  {-# INLINE nullChunk #-}

Data/Attoparsec/Text/Internal.hs

 
 type Parser = T.Parser Text
 type Result = IResult Text
-type Input = T.Input Text
-type Added = T.Added Text
 type Failure r = T.Failure Text r
 type Success a r = T.Success Text a r
 
         else runParser (demandInput >> go n') i0 a0 m0 kf ks
 {-# INLINE ensure #-}
 
--- | Ask for input.  If we receive any, pass it to a success
--- continuation, otherwise to a failure continuation.
-prompt :: Input -> Added -> More
-       -> (Input -> Added -> More -> Result r)
-       -> (Input -> Added -> More -> Result r)
-       -> Result r
-prompt i0 a0 _m0 kf ks = Partial $ \s ->
-    if T.null s
-    then kf i0 a0 Complete
-    else ks (i0 <> I s) (a0 <> A s) Incomplete
-
 -- | Immediately demand more input via a 'Partial' continuation
 -- result.
 demandInput :: Parser ()

Data/Attoparsec/Types.hs

     (
       Parser
     , IResult(..)
+    , Chunk(..)
     ) where
 
-import Data.Attoparsec.Internal.Types (Parser(..), IResult(..))
+import Data.Attoparsec.Internal.Types (Parser(..), IResult(..), Chunk(..))