Bryan O'Sullivan avatar Bryan O'Sullivan committed 5a5e29b Merge

Merge.

Comments (0)

Files changed (4)

 4e6ad4e2712c82714bb055c261adb376dae4c513 0.7
 5504cf45797458399d3f840088bd31f84ef1d56d 0.7.1
 69d4bdcb1e2b35d768ea291886b85cbadd7bf839 0.7.2
+f20aef71b438d9ab2db399479ae1836e5d98c31c 0.8.0.0

Data/Attoparsec.hs

     , parseWith
     , parseTest
 
+    -- ** Result conversion
+    , maybeResult
+    , eitherResult
+
     -- * Combinators
     , (I.<?>)
     , I.try
   where step (I.Fail st stk msg) = return $! Fail (I.input st) stk msg
         step (I.Partial k)       = (step . k) =<< refill
         step (I.Done st r)       = return $! Done (I.input st) r
+
+-- | Convert a 'Result' value to a 'Maybe' value. A 'Partial' result
+-- is treated as failure.
+maybeResult :: Result r -> Maybe r
+maybeResult (Done _ r) = Just r
+maybeResult _          = Nothing
+
+-- | Convert a 'Result' value to an 'Either' value. A 'Partial' result
+-- is treated as failure.
+eitherResult :: Result r -> Either String r
+eitherResult (Done _ r)     = Right r
+eitherResult (Fail _ _ msg) = Left msg
+eitherResult _              = Left "Result: incomplete input"

Data/Attoparsec/Lazy.hs

+-- |
+-- Module      :  Data.Attoparsec.Lazy
+-- Copyright   :  Bryan O'Sullivan 2010
+-- License     :  BSD3
+-- 
+-- Maintainer  :  bos@serpentine.com
+-- Stability   :  experimental
+-- Portability :  unknown
+--
+-- Simple, efficient combinator parsing for lazy 'ByteString'
+-- strings, loosely based on the Parsec library.
+--
+-- This is essentially the same code as in the 'Data.Attoparsec'
+-- module, only with a 'parse' function that can consume a lazy
+-- 'ByteString' incrementally, and a 'Result' type that does not allow
+-- more input to be fed in.  Think of this as suitable for use with a
+-- lazily read file, e.g. via 'L.readFile' or 'L.hGetContents'.
+--
+-- Behind the scenes, strict 'B.ByteString' values are still used
+-- internally to store parser input and manipulate it efficiently.
+-- High-performance parsers such as 'string' still expect strict
+-- 'B.ByteString' parameters.
+
+module Data.Attoparsec.Lazy
+    (
+      Result(..)
+    , module Data.Attoparsec
+    -- * Running parsers
+    , parse
+    , parseTest
+    -- ** Result conversion
+    , maybeResult
+    , eitherResult
+    ) where
+
+import Data.ByteString.Lazy.Internal (ByteString(..), chunk)
+import qualified Data.ByteString.Lazy as L
+import qualified Data.ByteString as B
+import qualified Data.Attoparsec as A
+import Data.Attoparsec hiding (Result(..), eitherResult, maybeResult,
+                               parse, parseWith, parseTest)
+
+-- | The result of a parse.
+data Result r = Fail ByteString [String] String
+              -- ^ The parse failed.  The 'ByteString' is the input
+              -- that had not yet been consumed when the failure
+              -- occurred.  The @[@'String'@]@ is a list of contexts
+              -- in which the error occurred.  The 'String' is the
+              -- message describing the error, if any.
+              | Done ByteString r
+              -- ^ The parse succeeded.  The 'ByteString' is the
+              -- input that had not yet been consumed (if any) when
+              -- the parse succeeded.
+
+instance Show r => Show (Result r) where
+    show (Fail bs stk msg) =
+        "Fail " ++ show bs ++ " " ++ show stk ++ " " ++ show msg
+    show (Done bs r)       = "Done " ++ show bs ++ " " ++ show r
+
+fmapR :: (a -> b) -> Result a -> Result b
+fmapR _ (Fail st stk msg) = Fail st stk msg
+fmapR f (Done bs r)       = Done bs (f r)
+
+instance Functor Result where
+    fmap = fmapR
+
+-- | Run a parser and return its result.
+parse :: A.Parser a -> ByteString -> Result a
+parse p s = case s of
+              Chunk x xs -> go (A.parse p x) xs
+              empty      -> go (A.parse p B.empty) empty
+  where
+    go (A.Fail x stk msg) ys      = Fail (chunk x ys) stk msg
+    go (A.Done x r) ys            = Done (chunk x ys) r
+    go (A.Partial k) (Chunk y ys) = go (k y) ys
+    go (A.Partial k) empty        = go (k B.empty) empty
+
+-- | Run a parser and print its result to standard output.
+parseTest :: (Show a) => A.Parser a -> ByteString -> IO ()
+parseTest p s = print (parse p s)
+
+-- | Convert a 'Result' value to a 'Maybe' value.
+maybeResult :: Result r -> Maybe r
+maybeResult (Done _ r) = Just r
+maybeResult _          = Nothing
+
+-- | Convert a 'Result' value to an 'Either' value.
+eitherResult :: Result r -> Either String r
+eitherResult (Done _ r)     = Right r
+eitherResult (Fail _ _ msg) = Left msg
     A fast parser combinator library, aimed particularly at dealing
     efficiently with network protocols and complicated text/binary
     file formats.
+extra-source-files:
+    benchmarks/Makefile
+    benchmarks/Tiny.hs
+    benchmarks/med.txt.bz2
+    tests/Makefile
+    tests/QC.hs
+    tests/QCSupport.hs
+    tests/TestFastSet.hs
+    examples/Makefile
+    examples/Parsec_RFC2616.hs
+    examples/RFC2616.hs
+    examples/TestRFC2616.hs
+    examples/rfc2616.c
 
 flag split-base
 flag applicative-in-base
                    Data.Attoparsec.Char8
                    Data.Attoparsec.Combinator
                    Data.Attoparsec.FastSet
+                   Data.Attoparsec.Lazy
   other-modules:   Data.Attoparsec.Internal
   ghc-options:     -Wall
   ghc-prof-options: -auto-all
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.