module Debug.Dump (d, dd, dump) where
import Internal.Utils (($>), (.>))
import qualified Internal.Utils as Utils
import qualified Internal.Parser as Parser
import Data.List
import Debug.Trace
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Language.Haskell.Meta.Parse
import Text.InterpolatedString.Perl6
dump :: QuasiQuoter
dump = QuasiQuoter {quoteExp = process}
d = dump
dd = dump
newtype HsExp a = HsExp a deriving (Functor)
unHsExp (HsExp s) = s
instance Applicative HsExp where
pure = HsExp
(HsExp f) <*> (HsExp a) = HsExp $ f a
process :: String -> Q Exp
process = id
.> splitOnCommas
.> map nameAndValue
.> joinAsColumns
.> wrapInParens
.> parseHsStrToQQExp
.> return
splitOnCommas :: String -> [HsExp String]
splitOnCommas = Parser.splitOnCommas .> map HsExp
nameAndValue :: HsExp String -> HsExp String
nameAndValue = fmap $ \str-> [qq|"({Utils.strip str}) = " ++ show ($str)|]
joinAsColumns :: [HsExp String] -> HsExp String
joinAsColumns = sequenceA .> fmap (intercalate [q| ++ "\t " ++ |])
wrapInParens :: HsExp String -> HsExp String
wrapInParens = fmap Utils.wrapInParens
parseHsStrToQQExp :: HsExp String -> Exp
parseHsStrToQQExp = unHsExp .> parseExp .> either error id