Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First commit

  • Loading branch information...
commit 3adce71f79dc76202e64df53eda1beefab081445 0 parents
Gregory Collins authored
13 .gitignore
... ... @@ -0,0 +1,13 @@
  1 +*~
  2 +dist/
  3 +*.tix
  4 +.hpc
  5 +*.prof
  6 +*.hi
  7 +*.o
  8 +*.swp
  9 +#*#
  10 +.#*
  11 +.DS_Store
  12 +**/.DS_Store
  13 +docs/templates/out
27 LICENSE
... ... @@ -0,0 +1,27 @@
  1 +Copyright (c) 2010, Gregory Collins
  2 +All rights reserved.
  3 +
  4 +Redistribution and use in source and binary forms, with or without
  5 +modification, are permitted provided that the following conditions are met:
  6 +
  7 + * Redistributions of source code must retain the above copyright notice, this
  8 + list of conditions and the following disclaimer.
  9 +
  10 + * Redistributions in binary form must reproduce the above copyright notice,
  11 + this list of conditions and the following disclaimer in the documentation
  12 + and/or other materials provided with the distribution.
  13 +
  14 + * Neither the name of Gregory Collins nor the names of its contributors may be
  15 + used to endorse or promote products derived from this software without
  16 + specific prior written permission.
  17 +
  18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22 +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24 +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25 +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26 +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 attoparsec-iteratee.cabal
... ... @@ -0,0 +1,35 @@
  1 +name: attoparsec-iteratee
  2 +version: 0.1
  3 +synopsis: An adapter to convert attoparsec Parsers into blazing-fast Iteratees
  4 +description:
  5 + An adapter to convert attoparsec Parsers into blazing-fast Iteratees
  6 +license: BSD3
  7 +license-file: LICENSE
  8 +author: Gregory Collins
  9 +maintainer: greg@gregorycollins.net
  10 +build-type: Simple
  11 +cabal-version: >= 1.6
  12 +homepage: http://github.com/gregorycollins
  13 +category: Data
  14 +
  15 +
  16 +Library
  17 + hs-source-dirs: src
  18 +
  19 + exposed-modules:
  20 + Data.Attoparsec.Iteratee
  21 +
  22 + build-depends:
  23 + attoparsec >= 0.8 && < 0.9,
  24 + base >= 4 && < 5,
  25 + bytestring,
  26 + iteratee >= 0.3.1 && <0.4,
  27 + monads-fd,
  28 + transformers
  29 +
  30 + ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2
  31 +
  32 +
  33 +source-repository head
  34 + type: git
  35 + location: git://github.com/gregorycollins/attoparsec-iteratee.git
73 src/Data/Attoparsec/Iteratee.hs
... ... @@ -0,0 +1,73 @@
  1 +module Data.Attoparsec.Iteratee
  2 + ( parserToIteratee ) where
  3 +
  4 +
  5 +------------------------------------------------------------------------------
  6 +import qualified Data.Attoparsec as Atto
  7 +import Data.Attoparsec hiding (many, Result(..))
  8 +import Data.ByteString (ByteString)
  9 +import qualified Data.ByteString as S
  10 +import qualified Data.ByteString.Lazy as L
  11 +import Data.Iteratee
  12 +import Data.Iteratee.WrappedByteString
  13 +import Data.Word
  14 +------------------------------------------------------------------------------
  15 +
  16 +type Stream = StreamG WrappedByteString Word8
  17 +type IterV m = IterGV WrappedByteString Word8 m
  18 +
  19 +
  20 +parserToIteratee :: (Monad m) =>
  21 + Parser a
  22 + -> IterateeG WrappedByteString Word8 m a
  23 +parserToIteratee p = IterateeG $ f (\s -> parse p s)
  24 + where
  25 + f :: (Monad m) =>
  26 + (ByteString -> Atto.Result a)
  27 + -> Stream
  28 + -> m (IterV m a)
  29 + f k (EOF Nothing) = finalChunk $ k S.empty
  30 + f _ (EOF (Just e)) = reportError e
  31 + f k (Chunk s) = let s' = S.concat $ L.toChunks $ fromWrap s
  32 + in if S.null s'
  33 + then return $ Cont (IterateeG $ f k) Nothing
  34 + else chunk s' k
  35 +
  36 +
  37 + finalChunk :: (Monad m) => Atto.Result a -> m (IterV m a)
  38 + finalChunk (Atto.Fail _ _ m) =
  39 + return $ Cont (error $ show m)
  40 + (Just $ Err m)
  41 +
  42 + finalChunk (Atto.Done rest r) =
  43 + return $ Done r (Chunk $ toWrap $ L.fromChunks [rest])
  44 +
  45 + finalChunk (Atto.Partial _) =
  46 + return $ Cont (error "parser did not produce a value")
  47 + (Just $ Err "parser did not produce a value")
  48 +
  49 + reportError e = return $ Cont (error $ show e) (Just e)
  50 +
  51 + chunk :: (Monad m) =>
  52 + ByteString
  53 + -> (ByteString -> Atto.Result a)
  54 + -> m (IterV m a)
  55 + chunk s k = do
  56 + let r = k s
  57 + case r of
  58 + (Atto.Fail _ _ m) -> return $
  59 + Cont (throwErr (Err m)) (Just $ Err m)
  60 + (Atto.Done rest x) -> return $ Done x (Chunk $ toWrap $ L.fromChunks [rest])
  61 + (Atto.Partial z) -> return $
  62 + Cont (IterateeG $ f z) Nothing
  63 +
  64 +
  65 +-- | lazy bytestring -> wrapped bytestring
  66 +toWrap :: L.ByteString -> WrappedByteString Word8
  67 +toWrap = WrapBS . S.concat . L.toChunks
  68 +{-# INLINE toWrap #-}
  69 +
  70 +-- | wrapped bytestring -> lazy bytestring
  71 +fromWrap :: WrappedByteString Word8 -> L.ByteString
  72 +fromWrap = L.fromChunks . (:[]) . unWrap
  73 +{-# INLINE fromWrap #-}

0 comments on commit 3adce71

Please sign in to comment.
Something went wrong with that request. Please try again.