attoparsec-enumerator-0.3.1/0000755000000000000000000000000012012603051014213 5ustar0000000000000000attoparsec-enumerator-0.3.1/Setup.hs0000644000000000000000000000005612012603051015650 0ustar0000000000000000import Distribution.Simple main = defaultMain attoparsec-enumerator-0.3.1/license.txt0000644000000000000000000000204112012603051016373 0ustar0000000000000000Copyright (c) 2010 John Millikin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. attoparsec-enumerator-0.3.1/attoparsec-enumerator.cabal0000644000000000000000000000317112012603051021525 0ustar0000000000000000name: attoparsec-enumerator version: 0.3.1 license: MIT license-file: license.txt author: John Millikin maintainer: John Millikin build-type: Simple cabal-version: >= 1.6 category: Text, Parsing, Enumerator stability: experimental homepage: https://john-millikin.com/software/attoparsec-enumerator/ bug-reports: mailto:jmillikin@gmail.com synopsis: Pass input from an enumerator to an Attoparsec parser. description: This library allows an Attoparsec parser to receive input incrementally from an enumerator. This could be used for parsing large files, or implementing binary network protocols. . > (-# LANGUAGE OverloadedStrings #-) > > import Control.Applicative > import Data.Attoparsec > import Data.Attoparsec.Enumerator > import Data.Enumerator > import Data.Enumerator.Binary (enumHandle) > import Data.Enumerator.List > import System.IO > > parser = string "foo" <|> string "bar" > > main = do > xy <- run_ (enumHandle 1 stdin $$ do > x <- iterParser parser > y <- iterParser parser > return (x, y)) > print xy source-repository head type: bazaar location: https://john-millikin.com/branches/attoparsec-enumerator/0.3/ source-repository this type: bazaar location: https://john-millikin.com/branches/attoparsec-enumerator/0.3/ tag: attoparsec-enumerator_0.3.1 library hs-source-dirs: lib ghc-options: -Wall -O2 build-depends: base >= 4.0 && < 5.0 , attoparsec >= 0.10 && < 0.11 , bytestring >= 0.9 , enumerator >= 0.4 && < 0.5 , text exposed-modules: Data.Attoparsec.Enumerator attoparsec-enumerator-0.3.1/lib/0000755000000000000000000000000012012603051014761 5ustar0000000000000000attoparsec-enumerator-0.3.1/lib/Data/0000755000000000000000000000000012012603051015632 5ustar0000000000000000attoparsec-enumerator-0.3.1/lib/Data/Attoparsec/0000755000000000000000000000000012012603051017737 5ustar0000000000000000attoparsec-enumerator-0.3.1/lib/Data/Attoparsec/Enumerator.hs0000644000000000000000000000450612012603051022421 0ustar0000000000000000{-# LANGUAGE DeriveDataTypeable #-} -- | -- Module: Data.Attoparsec.Enumerator -- Copyright: 2010 John Millikin -- License: MIT -- -- Maintainer: jmillikin@gmail.com -- 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.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. data ParseError = ParseError { errorContexts :: [String] , errorMessage :: String } deriving (Show, Typeable) 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 :: (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 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 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)