Commits

Bryan O'Sullivan committed 209da93

Add an atEnd combinator

Comments (0)

Files changed (3)

Data/Attoparsec.hs

     , I.takeWhile1
     , I.takeTill
 
+    -- ** Consume all remaining input
+    , I.takeByteString
+    , I.takeLazyByteString
+
     -- * State observation and manipulation functions
     , I.endOfInput
+    , I.atEnd
     , I.ensure
     ) where
 

Data/Attoparsec/Char8.hs

     , takeWhile1
     , takeTill
 
+    -- ** Consume all remaining input
+    , I.takeByteString
+    , I.takeLazyByteString
+
     -- * Text parsing
     , I.endOfLine
     , isEndOfLine
 
     -- * State observation and manipulation functions
     , I.endOfInput
+    , I.atEnd
     , I.ensure
     ) where
 

Data/Attoparsec/Internal.hs

 -- Module      :  Data.Attoparsec.Internal
 -- Copyright   :  Bryan O'Sullivan 2007-2010
 -- License     :  BSD3
--- 
+--
 -- Maintainer  :  bos@serpentine.com
 -- Stability   :  experimental
 -- Portability :  unknown
     , takeWhile1
     , takeTill
 
+    -- ** Consume all remaining input
+    , takeByteString
+    , takeLazyByteString
+
     -- * State observation and manipulation functions
     , endOfInput
+    , atEnd
     , ensure
 
     -- * Utilities
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Internal as B
 import qualified Data.ByteString.Unsafe as B
+import qualified Data.ByteString.Lazy as L
 
 -- | The result of a parse.
 data Result r = Fail B.ByteString [String] String
 instance Applicative Parser where
     pure   = returnP
     (<*>)  = apP
-    
+
     -- These definitions are equal to the defaults, but this
     -- way the optimizer doesn't have to work so hard to figure
     -- that out.
           else return (h:acc)
       else return acc
 
+takeRest :: Parser [B.ByteString]
+takeRest = go []
+ where
+  go acc = do
+    input <- wantInput
+    if input
+      then do
+        s <- get
+        put B.empty
+        go (s:acc)
+      else return (reverse acc)
+
+-- | Consume all remaining input and return it as a single string.
+takeByteString :: Parser B.ByteString
+takeByteString = B.concat `fmap` takeRest
+
+-- | Consume all remaining input and return it as a single string.
+takeLazyByteString :: Parser L.ByteString
+takeLazyByteString = L.fromChunks `fmap` takeRest
+
 -- | A stateful scanner.  The predicate consumes and transforms a
 -- state argument, and each transformed state is passed to successive
 -- invocations of the predicate on each byte of the input until one
                                                             "endOfInput"
                        in  runParser demandInput i0 a0 m0 kf' ks'
              else kf i0 a0 m0 [] "endOfInput"
-                                               
+
+-- | Return an indication of whether the end of input has been
+-- reached.
+atEnd :: Parser Bool
+atEnd = not <$> wantInput
+{-# INLINE atEnd #-}
+
 -- | Match either a single newline character @\'\\n\'@, or a carriage
 -- return followed by a newline character @\"\\r\\n\"@.
 endOfLine :: Parser ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.