{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Data.Attoparsec.Text.Lazy
(
Result(..)
, module Data.Attoparsec.Text
, parse
, parseTest
, maybeResult
, eitherResult
) where
import Control.DeepSeq (NFData(rnf))
import Data.List (intercalate)
import Data.Text.Lazy.Internal (Text(..), chunk)
import qualified Data.Attoparsec.Internal.Types as T
import qualified Data.Attoparsec.Text as A
import qualified Data.Text as T
import Data.Attoparsec.Text hiding (IResult(..), Result, eitherResult,
maybeResult, parse, parseWith, parseTest)
data Result r = Fail Text [String] String
| Done Text r
deriving (Int -> Result r -> ShowS
[Result r] -> ShowS
Result r -> String
(Int -> Result r -> ShowS)
-> (Result r -> String) -> ([Result r] -> ShowS) -> Show (Result r)
forall r. Show r => Int -> Result r -> ShowS
forall r. Show r => [Result r] -> ShowS
forall r. Show r => Result r -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Result r] -> ShowS
$cshowList :: forall r. Show r => [Result r] -> ShowS
show :: Result r -> String
$cshow :: forall r. Show r => Result r -> String
showsPrec :: Int -> Result r -> ShowS
$cshowsPrec :: forall r. Show r => Int -> Result r -> ShowS
Show)
instance NFData r => NFData (Result r) where
rnf :: Result r -> ()
rnf (Fail bs :: Text
bs ctxs :: [String]
ctxs msg :: String
msg) = Text -> ()
forall a. NFData a => a -> ()
rnf Text
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 :: Text
bs r :: r
r) = Text -> ()
forall a. NFData a => a -> ()
rnf Text
bs () -> () -> ()
forall a b. a -> b -> b
`seq` r -> ()
forall a. NFData a => a -> ()
rnf r
r
{-# INLINE rnf #-}
fmapR :: (a -> b) -> Result a -> Result b
fmapR :: (a -> b) -> Result a -> Result b
fmapR _ (Fail st :: Text
st stk :: [String]
stk msg :: String
msg) = Text -> [String] -> String -> Result b
forall r. Text -> [String] -> String -> Result r
Fail Text
st [String]
stk String
msg
fmapR f :: a -> b
f (Done bs :: Text
bs r :: a
r) = Text -> b -> Result b
forall r. Text -> r -> Result r
Done Text
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
parse :: A.Parser a -> Text -> Result a
parse :: Parser a -> Text -> Result a
parse p :: Parser a
p s :: Text
s = case Text
s of
Chunk x :: Text
x xs :: Text
xs -> IResult Text a -> Text -> Result a
forall r. IResult Text r -> Text -> Result r
go (Parser a -> Text -> IResult Text a
forall a. Parser a -> Text -> Result a
A.parse Parser a
p Text
x) Text
xs
empty :: Text
empty -> IResult Text a -> Text -> Result a
forall r. IResult Text r -> Text -> Result r
go (Parser a -> Text -> IResult Text a
forall a. Parser a -> Text -> Result a
A.parse Parser a
p Text
T.empty) Text
empty
where
go :: IResult Text r -> Text -> Result r
go (T.Fail x :: Text
x stk :: [String]
stk msg :: String
msg) ys :: Text
ys = Text -> [String] -> String -> Result r
forall r. Text -> [String] -> String -> Result r
Fail (Text -> Text -> Text
chunk Text
x Text
ys) [String]
stk String
msg
go (T.Done x :: Text
x r :: r
r) ys :: Text
ys = Text -> r -> Result r
forall r. Text -> r -> Result r
Done (Text -> Text -> Text
chunk Text
x Text
ys) r
r
go (T.Partial k :: Text -> IResult Text r
k) (Chunk y :: Text
y ys :: Text
ys) = IResult Text r -> Text -> Result r
go (Text -> IResult Text r
k Text
y) Text
ys
go (T.Partial k :: Text -> IResult Text r
k) empty :: Text
empty = IResult Text r -> Text -> Result r
go (Text -> IResult Text r
k Text
T.empty) Text
empty
parseTest :: (Show a) => A.Parser a -> Text -> IO ()
parseTest :: Parser a -> Text -> IO ()
parseTest p :: Parser a
p s :: Text
s = Result a -> IO ()
forall a. Show a => a -> IO ()
print (Parser a -> Text -> Result a
forall a. Parser a -> Text -> Result a
parse Parser a
p Text
s)
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
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)