git.iko.soy/typeable/attoparsec
on master help sign in
Add an atEnd combinator
commite873991bfaf776a17323c093d49b9d1b2b7b8968 authorBryan O'Sullivan <bos@serpentine.com> dateFri 11 Feb 2011, 20:20 (16 years ago) parentb221be1
3 files changed+45−3
Data/Attoparsec.hs+5 Data/Attoparsec/Char8.hs+5 Data/Attoparsec/Internal.hs+35−3
Data/Attoparsec.hs+5
@@ -61,8 +61,13 @@ module Data.Attoparsec
6161 , I.takeWhile1
6262 , I.takeTill
6363
64+ -- ** Consume all remaining input
65+ , I.takeByteString
66+ , I.takeLazyByteString
67+
6468 -- * State observation and manipulation functions
6569 , I.endOfInput
70+ , I.atEnd
6671 , I.ensure
6772 ) where
6873
Data/Attoparsec/Char8.hs+5
@@ -69,6 +69,10 @@ module Data.Attoparsec.Char8
6969 , takeWhile1
7070 , takeTill
7171
72+ -- ** Consume all remaining input
73+ , I.takeByteString
74+ , I.takeLazyByteString
75+
7276 -- * Text parsing
7377 , I.endOfLine
7478 , isEndOfLine
@@ -83,6 +87,7 @@ module Data.Attoparsec.Char8
8387
8488 -- * State observation and manipulation functions
8589 , I.endOfInput
90+ , I.atEnd
8691 , I.ensure
8792 ) where
8893
Data/Attoparsec/Internal.hs+35−3
@@ -3,7 +3,7 @@
33-- Module : Data.Attoparsec.Internal
44-- Copyright : Bryan O'Sullivan 2007-2010
55-- License : BSD3
6−--
6+--
77-- Maintainer : bos@serpentine.com
88-- Stability : experimental
99-- Portability : unknown
@@ -50,8 +50,13 @@ module Data.Attoparsec.Internal
5050 , takeWhile1
5151 , takeTill
5252
53+ -- ** Consume all remaining input
54+ , takeByteString
55+ , takeLazyByteString
56+
5357 -- * State observation and manipulation functions
5458 , endOfInput
59+ , atEnd
5560 , ensure
5661
5762 -- * Utilities
@@ -73,6 +78,7 @@ import qualified Data.ByteString as B8
7378import qualified Data.ByteString.Char8 as B
7479import qualified Data.ByteString.Internal as B
7580import qualified Data.ByteString.Unsafe as B
81+import qualified Data.ByteString.Lazy as L
7682
7783-- | The result of a parse.
7884data Result r = Fail B.ByteString [String] String
@@ -183,7 +189,7 @@ apP d e = do
183189instance Applicative Parser where
184190 pure = returnP
185191 (<*>) = apP
186−
192+
187193 -- These definitions are equal to the defaults, but this
188194 -- way the optimizer doesn't have to work so hard to figure
189195 -- that out.
@@ -406,6 +412,26 @@ takeWhile p = (B.concat . reverse) `fmap` go []
406412 else return (h:acc)
407413 else return acc
408414
415+takeRest :: Parser [B.ByteString]
416+takeRest = go []
417+ where
418+ go acc = do
419+ input <- wantInput
420+ if input
421+ then do
422+ s <- get
423+ put B.empty
424+ go (s:acc)
425+ else return (reverse acc)
426+
427+-- | Consume all remaining input and return it as a single string.
428+takeByteString :: Parser B.ByteString
429+takeByteString = B.concat `fmap` takeRest
430+
431+-- | Consume all remaining input and return it as a single string.
432+takeLazyByteString :: Parser L.ByteString
433+takeLazyByteString = L.fromChunks `fmap` takeRest
434+
409435-- | A stateful scanner. The predicate consumes and transforms a
410436-- state argument, and each transformed state is passed to successive
411437-- invocations of the predicate on each byte of the input until one
@@ -510,7 +536,13 @@ endOfInput = Parser $ \i0 a0 m0 kf ks ->
510536 "endOfInput"
511537 in runParser demandInput i0 a0 m0 kf' ks'
512538 else kf i0 a0 m0 [] "endOfInput"
513−
539+
540+-- | Return an indication of whether the end of input has been
541+-- reached.
542+atEnd :: Parser Bool
543+atEnd = not <$> wantInput
544+{-# INLINE atEnd #-}
545+
514546-- | Match either a single newline character @\'\\n\'@, or a carriage
515547-- return followed by a newline character @\"\\r\\n\"@.
516548endOfLine :: Parser ()