forked from gregorycollins/attoparsec-iteratee
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac497f
commit cabedfe
Showing
2 changed files
with
26 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,38 @@ | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
module Data.Attoparsec.Iteratee | ||
( parserToIteratee ) where | ||
( ParseError(..), parserToIteratee ) where | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
import Control.Exception | ||
import qualified Data.Attoparsec as Atto | ||
import Data.Attoparsec hiding (many, Result(..)) | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString as S | ||
import qualified Data.ByteString.Lazy as L | ||
import qualified Data.ByteString as B | ||
import Data.Iteratee | ||
import Data.Iteratee.WrappedByteString | ||
import Data.Word | ||
import Data.Typeable | ||
------------------------------------------------------------------------------ | ||
|
||
type Stream = StreamG WrappedByteString Word8 | ||
type IterV m = IterGV WrappedByteString Word8 m | ||
data ParseError | ||
= ParseError {errorContexts :: [String], errorMessage :: String} | ||
deriving (Show, Typeable) | ||
|
||
instance Exception ParseError | ||
|
||
parserToIteratee :: (Monad m) => | ||
Parser a | ||
-> IterateeG WrappedByteString Word8 m a | ||
parserToIteratee p = IterateeG $ f (\s -> parse p s) | ||
where | ||
f :: (Monad m) => | ||
(ByteString -> Atto.Result a) | ||
-> Stream | ||
-> m (IterV m a) | ||
f k (EOF Nothing) = finalChunk $ feed (k S.empty) S.empty | ||
f _ (EOF (Just e)) = reportError e | ||
f k (Chunk s) = let s' = S.concat $ L.toChunks $ fromWrap s | ||
in if S.null s' | ||
then return $ Cont (IterateeG $ f k) Nothing | ||
else chunk s' k | ||
|
||
|
||
finalChunk :: (Monad m) => Atto.Result a -> m (IterV m a) | ||
finalChunk (Atto.Fail _ _ m) = | ||
return $ Cont (error $ show m) | ||
(Just $ Err m) | ||
|
||
finalChunk (Atto.Done rest r) | ||
| S.null rest = | ||
return $ Done r (EOF Nothing) | ||
| otherwise = | ||
return $ Done r (Chunk $ toWrap $ L.fromChunks [rest]) | ||
|
||
finalChunk (Atto.Partial _) = | ||
return $ Cont (error "parser did not produce a value") | ||
(Just $ Err "parser did not produce a value") | ||
|
||
reportError e = return $ Cont (error $ show e) (Just e) | ||
|
||
chunk :: (Monad m) => | ||
ByteString | ||
-> (ByteString -> Atto.Result a) | ||
-> m (IterV m a) | ||
chunk s k = do | ||
let r = k s | ||
case r of | ||
(Atto.Fail _ _ m) -> return $ | ||
Cont (throwErr (Err m)) (Just $ Err m) | ||
(Atto.Done rest x) -> return $ Done x (Chunk $ toWrap $ L.fromChunks [rest]) | ||
(Atto.Partial z) -> return $ | ||
Cont (IterateeG $ f z) Nothing | ||
|
||
|
||
-- | lazy bytestring -> wrapped bytestring | ||
toWrap :: L.ByteString -> WrappedByteString Word8 | ||
toWrap = WrapBS . S.concat . L.toChunks | ||
{-# INLINE toWrap #-} | ||
|
||
-- | wrapped bytestring -> lazy bytestring | ||
fromWrap :: WrappedByteString Word8 -> L.ByteString | ||
fromWrap = L.fromChunks . (:[]) . unWrap | ||
{-# INLINE fromWrap #-} | ||
-> Iteratee ByteString m a | ||
parserToIteratee p = icont (f (parse p)) Nothing | ||
where f k (EOF Nothing) = case feed (k B.empty) B.empty of | ||
Atto.Fail _ err dsc -> throwErr (toException $ ParseError err dsc) | ||
Atto.Partial _ -> throwErr (toException EofException) | ||
Atto.Done rest v | ||
| B.null rest -> idone v (EOF Nothing) | ||
| otherwise -> idone v (Chunk rest) | ||
f _ (EOF (Just e)) = throwErr e | ||
f k (Chunk s) | ||
| B.null s = icont (f k) Nothing | ||
| otherwise = case k s of | ||
Atto.Fail _ err dsc -> throwErr (toException $ ParseError err dsc) | ||
Atto.Partial k' -> icont (f k') Nothing | ||
Atto.Done rest v -> idone v (Chunk rest) |