attoparsec-enumerator 0.2.0.5 → 0.3.4
raw patch · 2 files changed
Files
attoparsec-enumerator.cabal view
@@ -1,5 +1,5 @@ name: attoparsec-enumerator-version: 0.2.0.5+version: 0.3.4 license: MIT license-file: license.txt author: John Millikin <jmillikin@gmail.com>@@ -38,13 +38,13 @@ source-repository head- type: bazaar- location: https://john-millikin.com/branches/attoparsec-enumerator/0.2/+ type: git+ location: https://john-millikin.com/code/attoparsec-enumerator/ source-repository this- type: bazaar- location: https://john-millikin.com/branches/attoparsec-enumerator/0.2/- tag: attoparsec-enumerator_0.2.0.5+ type: git+ location: https://john-millikin.com/code/attoparsec-enumerator/+ tag: attoparsec-enumerator_0.3.4 library hs-source-dirs: lib@@ -52,9 +52,10 @@ build-depends: base >= 4.0 && < 5.0- , attoparsec >= 0.8 && < 0.11- , bytestring >= 0.9 && < 0.10+ , attoparsec >= 0.10 && < 0.14+ , bytestring >= 0.9 , enumerator >= 0.4 && < 0.5+ , text exposed-modules: Data.Attoparsec.Enumerator
lib/Data/Attoparsec/Enumerator.hs view
@@ -9,14 +9,18 @@ -- Portability: portable module Data.Attoparsec.Enumerator ( ParseError (..)+ , AttoparsecInput , iterParser ) where import Control.Exception (Exception) import Data.Typeable (Typeable) import qualified Data.ByteString as B+import qualified Data.Text as T -import qualified Data.Attoparsec as A+import qualified Data.Attoparsec.ByteString+import qualified Data.Attoparsec.Text+import qualified Data.Attoparsec.Types as A import qualified Data.Enumerator as E -- | The context and message from a 'A.Fail' value.@@ -28,26 +32,49 @@ instance Exception ParseError +-- | A class of types which may be consumed by an Attoparsec parser.+--+-- Since: 0.3+class AttoparsecInput a where+ parseA :: A.Parser a b -> a -> A.IResult a b+ feedA :: A.IResult a b -> a -> A.IResult a b+ empty :: a+ isNull :: a -> Bool+ notEmpty :: [a] -> [a]++instance AttoparsecInput B.ByteString where+ parseA = Data.Attoparsec.ByteString.parse+ feedA = Data.Attoparsec.ByteString.feed+ empty = B.empty+ isNull = B.null+ notEmpty = filter (not . B.null)++instance AttoparsecInput T.Text where+ parseA = Data.Attoparsec.Text.parse+ feedA = Data.Attoparsec.Text.feed+ empty = T.empty+ isNull = T.null+ notEmpty = filter (not . T.null)+ -- | Convert an Attoparsec 'A.Parser' into an 'E.Iteratee'. The parser will -- be streamed bytes until it returns 'A.Done' or 'A.Fail'. -- -- If parsing fails, a 'ParseError' will be thrown with 'E.throwError'. Use -- 'E.catchError' to catch it.-iterParser :: Monad m => A.Parser a -> E.Iteratee B.ByteString m a-iterParser p = E.continue (step (A.parse p)) where+iterParser :: (AttoparsecInput a, Monad m) => A.Parser a b -> E.Iteratee a m b+iterParser p = E.continue (step (parseA p)) where step parse (E.Chunks xs) = parseLoop parse (notEmpty xs)- step parse E.EOF = case A.feed (parse B.empty) B.empty of- A.Done _ a -> E.yield a E.EOF+ step parse E.EOF = case feedA (parse empty) empty of+ A.Done _ b -> E.yield b E.EOF A.Partial _ -> err [] "iterParser: divergent parser" A.Fail _ ctx msg -> err ctx msg parseLoop parse [] = E.continue (step parse) parseLoop parse (x:xs) = case parse x of- A.Done extra a -> E.yield a $ if B.null extra+ A.Done extra a -> E.yield a $ if isNull extra then E.Chunks xs else E.Chunks (extra:xs) A.Partial parse' -> parseLoop parse' xs A.Fail _ ctx msg -> err ctx msg err ctx msg = E.throwError (ParseError ctx msg)- notEmpty = filter (not . B.null)