{-# LANGUAGE ConstraintKinds #-}

module Language.Ltml.Pretty
    ( prettyPrint
    , prettyParseTest
    , prettyErrorParse
    )
where

import Data.Text (Text)
import Language.Ltml.Parser (Parser)
import System.Exit (exitFailure)
import System.IO (hPutStr, stderr)
import Text.Megaparsec (errorBundlePretty, runParser)
import Text.Pretty.Simple (pPrint)

type Pretty = Show

prettyPrint :: (Pretty a) => a -> IO ()
prettyPrint :: forall a. Pretty a => a -> IO ()
prettyPrint = a -> IO ()
forall (m :: * -> *) a. (MonadIO m, Show a) => a -> m ()
pPrint

prettyParseTest :: (Pretty a) => Parser a -> Text -> IO ()
prettyParseTest :: forall a. Pretty a => Parser a -> Text -> IO ()
prettyParseTest Parser a
p Text
input = Parser a -> Text -> IO a
forall a. Parser a -> Text -> IO a
prettyErrorParse Parser a
p Text
input IO a -> (a -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> IO ()
forall a. Pretty a => a -> IO ()
prettyPrint

prettyErrorParse :: Parser a -> Text -> IO a
prettyErrorParse :: forall a. Parser a -> Text -> IO a
prettyErrorParse Parser a
p =
    (ParseErrorBundle Text Void -> IO a)
-> (a -> IO a) -> Either (ParseErrorBundle Text Void) a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\ParseErrorBundle Text Void
e -> Handle -> String -> IO ()
hPutStr Handle
stderr (ParseErrorBundle Text Void -> String
forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
errorBundlePretty ParseErrorBundle Text Void
e) IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO a
forall a. IO a
exitFailure) a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
        (Either (ParseErrorBundle Text Void) a -> IO a)
-> (Text -> Either (ParseErrorBundle Text Void) a) -> Text -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser a -> String -> Text -> Either (ParseErrorBundle Text Void) a
forall e s a.
Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
runParser Parser a
p String
""