Commits

Emil Melnikov  committed b347694

Added the "capture" function

  • Participants
  • Parent commits 070be19

Comments (0)

Files changed (2)

File Data/Attoparsec.hs

     -- * Combinators
     , (I.<?>)
     , I.try
+    , I.capture
     , module Data.Attoparsec.Combinator
 
     -- * Parsing individual bytes

File Data/Attoparsec/Internal.hs

     -- * Combinators
     , (<?>)
     , try
+    , capture
     , module Data.Attoparsec.Combinator
 
     -- * Parsing individual bytes
 getPosition :: Parser Int64
 getPosition = Parser (\st0 _kf ks -> ks st0 (position st0))
 
--- get' :: Parser (B.ByteString, Int64)
--- get' = Parser (\st0 _kf ks -> ks st0 (input st0, position st0))
+get' :: Parser (B.ByteString, Int64)
+get' = Parser (\st0 _kf ks -> ks st0 (input st0, position st0))
 
 -- put :: B.ByteString -> Parser ()
 -- put s = Parser (\(S _s0 a0 c0 i0) _kf ks -> ks (S s a0 c0 i0) ())
 try p = Parser $ \st0 kf ks ->
         runParser p (noAdds st0) (kf . mappend st0) ks
 
+-- | Add a chunk of consumed input to the parser's result.
+capture :: Parser a -> Parser (a, B.ByteString)
+capture p = do
+  (s, i0) <- get'
+  a <- p
+  i <- getPosition
+  let di = fromIntegral (i - i0)
+  return (a, B.take di s)
+
 -- | The parser @satisfy p@ succeeds for any byte for which the
 -- predicate @p@ returns 'True'. Returns the byte that is actually
 -- parsed.