{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-} -- Imports internal modules
#endif

-- |
-- Module      :  Data.Attoparsec.ByteString.Lazy
-- Copyright   :  Bryan O'Sullivan 2007-2015
-- License     :  BSD3
--
-- Maintainer  :  bos@serpentine.com
-- Stability   :  experimental
-- Portability :  unknown
--
-- Simple, efficient combinator parsing that can consume 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'.
--
-- /Note:/ The various parser functions and combinators such as
-- 'string' still expect /strict/ 'B.ByteString' parameters, and
-- return strict 'B.ByteString' results.  Behind the scenes, strict
-- 'B.ByteString' values are still used internally to store parser
-- input and manipulate it efficiently.

module Data.Attoparsec.ByteString.Lazy
    (
      Result(..)
    , module Data.Attoparsec.ByteString
    -- * Running parsers
    , parse
    , parseTest
    -- ** Result conversion
    , maybeResult
    , eitherResult
    ) where

import Control.DeepSeq (NFData(rnf))
import Data.ByteString.Lazy.Internal (ByteString(..), chunk)
import Data.List (intercalate)
import qualified Data.ByteString as B
import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.Internal.Types as T
import Data.Attoparsec.ByteString
    hiding (IResult(..), 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 NFData r => NFData (Result r) where
    rnf :: Result r -> ()
rnf (Fail bs :: ByteString
bs ctxs :: [String]
ctxs msg :: String
msg) = ByteString -> ()
rnfBS ByteString
bs () -> () -> ()
forall a b. a -> b -> b
`seq` [String] -> ()
forall a. NFData a => a -> ()
rnf [String]
ctxs () -> () -> ()
forall a b. a -> b -> b
`seq` String -> ()
forall a. NFData a => a -> ()
rnf String
msg
    rnf (Done bs :: ByteString
bs r :: r
r)        = ByteString -> ()
rnfBS ByteString
bs () -> () -> ()
forall a b. a -> b -> b
`seq` r -> ()
forall a. NFData a => a -> ()
rnf r
r
    {-# INLINE rnf #-}

rnfBS :: ByteString -> ()
rnfBS :: ByteString -> ()
rnfBS (Chunk _ xs :: ByteString
xs) = ByteString -> ()
rnfBS ByteString
xs
rnfBS Empty        = ()
{-# INLINE rnfBS #-}

instance Show r => Show (Result r) where
    show :: Result r -> String
show (Fail bs :: ByteString
bs stk :: [String]
stk msg :: String
msg) =
        "Fail " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
bs String -> ShowS
forall a. [a] -> [a] -> [a]
++ " " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
stk String -> ShowS
forall a. [a] -> [a] -> [a]
++ " " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
msg
    show (Done bs :: ByteString
bs r :: r
r)       = "Done " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
bs String -> ShowS
forall a. [a] -> [a] -> [a]
++ " " String -> ShowS
forall a. [a] -> [a] -> [a]
++ r -> String
forall a. Show a => a -> String
show r
r

fmapR :: (a -> b) -> Result a -> Result b
fmapR :: (a -> b) -> Result a -> Result b
fmapR _ (Fail st :: ByteString
st stk :: [String]
stk msg :: String
msg) = ByteString -> [String] -> String -> Result b
forall r. ByteString -> [String] -> String -> Result r
Fail ByteString
st [String]
stk String
msg
fmapR f :: a -> b
f (Done bs :: ByteString
bs r :: a
r)       = ByteString -> b -> Result b
forall r. ByteString -> r -> Result r
Done ByteString
bs (a -> b
f a
r)

instance Functor Result where
    fmap :: (a -> b) -> Result a -> Result b
fmap = (a -> b) -> Result a -> Result b
forall a b. (a -> b) -> Result a -> Result b
fmapR

-- | Run a parser and return its result.
parse :: A.Parser a -> ByteString -> Result a
parse :: Parser a -> ByteString -> Result a
parse p :: Parser a
p s :: ByteString
s = case ByteString
s of
              Chunk x :: ByteString
x xs :: ByteString
xs -> IResult ByteString a -> ByteString -> Result a
forall r. IResult ByteString r -> ByteString -> Result r
go (Parser a -> ByteString -> IResult ByteString a
forall a. Parser a -> ByteString -> Result a
A.parse Parser a
p ByteString
x) ByteString
xs
              empty :: ByteString
empty      -> IResult ByteString a -> ByteString -> Result a
forall r. IResult ByteString r -> ByteString -> Result r
go (Parser a -> ByteString -> IResult ByteString a
forall a. Parser a -> ByteString -> Result a
A.parse Parser a
p ByteString
B.empty) ByteString
empty
  where
    go :: IResult ByteString r -> ByteString -> Result r
go (T.Fail x :: ByteString
x stk :: [String]
stk msg :: String
msg) ys :: ByteString
ys      = ByteString -> [String] -> String -> Result r
forall r. ByteString -> [String] -> String -> Result r
Fail (ByteString -> ByteString -> ByteString
chunk ByteString
x ByteString
ys) [String]
stk String
msg
    go (T.Done x :: ByteString
x r :: r
r) ys :: ByteString
ys            = ByteString -> r -> Result r
forall r. ByteString -> r -> Result r
Done (ByteString -> ByteString -> ByteString
chunk ByteString
x ByteString
ys) r
r
    go (T.Partial k :: ByteString -> IResult ByteString r
k) (Chunk y :: ByteString
y ys :: ByteString
ys) = IResult ByteString r -> ByteString -> Result r
go (ByteString -> IResult ByteString r
k ByteString
y) ByteString
ys
    go (T.Partial k :: ByteString -> IResult ByteString r
k) empty :: ByteString
empty        = IResult ByteString r -> ByteString -> Result r
go (ByteString -> IResult ByteString r
k ByteString
B.empty) ByteString
empty

-- | Run a parser and print its result to standard output.
parseTest :: (Show a) => A.Parser a -> ByteString -> IO ()
parseTest :: Parser a -> ByteString -> IO ()
parseTest p :: Parser a
p s :: ByteString
s = Result a -> IO ()
forall a. Show a => a -> IO ()
print (Parser a -> ByteString -> Result a
forall a. Parser a -> ByteString -> Result a
parse Parser a
p ByteString
s)

-- | Convert a 'Result' value to a 'Maybe' value.
maybeResult :: Result r -> Maybe r
maybeResult :: Result r -> Maybe r
maybeResult (Done _ r :: r
r) = r -> Maybe r
forall a. a -> Maybe a
Just r
r
maybeResult _          = Maybe r
forall a. Maybe a
Nothing

-- | Convert a 'Result' value to an 'Either' value.
eitherResult :: Result r -> Either String r
eitherResult :: Result r -> Either String r
eitherResult (Done _ r :: r
r)        = r -> Either String r
forall a b. b -> Either a b
Right r
r
eitherResult (Fail _ [] msg :: String
msg)   = String -> Either String r
forall a b. a -> Either a b
Left String
msg
eitherResult (Fail _ ctxs :: [String]
ctxs msg :: String
msg) = String -> Either String r
forall a b. a -> Either a b
Left (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate " > " [String]
ctxs String -> ShowS
forall a. [a] -> [a] -> [a]
++ ": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg)