Anonymous avatar Anonymous committed b347694

Added the "capture" function

Comments (0)

Files changed (2)

Data/Attoparsec.hs

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

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.
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.