{-# LANGUAGE CPP #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE PatternGuards #-}
module Text.Show.Pretty
(
Value(..), Name
, valToStr
, valToDoc
, valToHtmlPage
, parseValue, reify, ppDoc, ppShow, pPrint
,
ppDocList, ppShowList, pPrintList
, dumpDoc, dumpStr, dumpIO, PrettyVal(..)
, valToHtml, HtmlOpts(..), defaultHtmlOpts, htmlPage, Html(..)
, getDataDir
,
PreProc(..), ppHide, ppHideNested, hideCon
, ppValue
) where
import Data.Char(isHexDigit)
import Text.PrettyPrint
import qualified Text.Show.Parser as P
import Text.Show.Value
import Text.Show.PrettyVal
import Text.Show.Html
import Data.Foldable(Foldable,toList)
import Language.Haskell.Lexer(rmSpace,lexerPass0,Token(..))
import Paths_pretty_show (getDataDir)
#if MIN_VERSION_base(4,11,0)
import Prelude hiding ( (<>) )
#else
import Prelude
#endif
{-# DEPRECATED ppValue "Please use `valToDoc` instead." #-}
ppValue :: Value -> Doc
ppValue :: Value -> Doc
ppValue = Value -> Doc
valToDoc
reify :: Show a => a -> Maybe Value
reify :: a -> Maybe Value
reify = String -> Maybe Value
parseValue (String -> Maybe Value) -> (a -> String) -> a -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
parseValue :: String -> Maybe Value
parseValue :: String -> Maybe Value
parseValue = [PosToken] -> Maybe Value
P.parseValue ([PosToken] -> Maybe Value)
-> (String -> [PosToken]) -> String -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PosToken] -> [PosToken]
rmSpace ([PosToken] -> [PosToken])
-> (String -> [PosToken]) -> String -> [PosToken]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PosToken -> [PosToken] -> [PosToken])
-> [PosToken] -> [PosToken] -> [PosToken]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr PosToken -> [PosToken] -> [PosToken]
forall a.
(Token, (a, String))
-> [(Token, (a, String))] -> [(Token, (a, String))]
joinTokens [] ([PosToken] -> [PosToken])
-> (String -> [PosToken]) -> String -> [PosToken]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [PosToken]
lexerPass0
where
joinTokens :: (Token, (a, String))
-> [(Token, (a, String))] -> [(Token, (a, String))]
joinTokens a :: (Token, (a, String))
a@(Token
t1,(a
p1,String
s1)) [(Token, (a, String))]
bs =
case [(Token, (a, String))]
bs of
(Token
_t2,(a
_,String
s2)) : [(Token, (a, String))]
more
| Token
IntLit <- Token
t1, (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isHexDigit String
s2 -> Token -> [(Token, (a, String))]
jn Token
IntLit
where jn :: Token -> [(Token, (a, String))]
jn Token
t = (Token
t,(a
p1,String
s1String -> String -> String
forall a. [a] -> [a] -> [a]
++String
s2)) (Token, (a, String))
-> [(Token, (a, String))] -> [(Token, (a, String))]
forall a. a -> [a] -> [a]
: [(Token, (a, String))]
more
[(Token, (a, String))]
_ -> (Token, (a, String))
a (Token, (a, String))
-> [(Token, (a, String))] -> [(Token, (a, String))]
forall a. a -> [a] -> [a]
: [(Token, (a, String))]
bs
ppShow :: Show a => a -> String
ppShow :: a -> String
ppShow = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (a -> Doc) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Doc
forall a. Show a => a -> Doc
ppDoc
ppShowList :: (Foldable f, Show a) => f a -> String
ppShowList :: f a -> String
ppShowList = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (f a -> Doc) -> f a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Doc
forall (f :: * -> *) a. (Foldable f, Show a) => f a -> Doc
ppDocList
ppDoc :: Show a => a -> Doc
ppDoc :: a -> Doc
ppDoc a
a = case String -> Maybe Value
parseValue String
txt of
Just Value
v -> Value -> Doc
valToDoc Value
v
Maybe Value
Nothing -> String -> Doc
text String
txt
where txt :: String
txt = a -> String
forall a. Show a => a -> String
show a
a
ppDocList :: (Foldable f, Show a) => f a -> Doc
ppDocList :: f a -> Doc
ppDocList = ([Doc] -> Doc) -> Char -> Char -> [Doc] -> Doc
blockWith [Doc] -> Doc
vcat Char
'[' Char
']' ([Doc] -> Doc) -> (f a -> [Doc]) -> f a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
forall a. Show a => a -> Doc
ppDoc ([a] -> [Doc]) -> (f a -> [a]) -> f a -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
pPrint :: Show a => a -> IO ()
pPrint :: a -> IO ()
pPrint = String -> IO ()
putStrLn (String -> IO ()) -> (a -> String) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
ppShow
pPrintList :: (Foldable f, Show a) => f a -> IO ()
pPrintList :: f a -> IO ()
pPrintList = String -> IO ()
putStrLn (String -> IO ()) -> (f a -> String) -> f a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> String
forall (f :: * -> *) a. (Foldable f, Show a) => f a -> String
ppShowList
dumpDoc :: PrettyVal a => a -> Doc
dumpDoc :: a -> Doc
dumpDoc = Value -> Doc
valToDoc (Value -> Doc) -> (a -> Value) -> a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
forall a. PrettyVal a => a -> Value
prettyVal
dumpStr :: PrettyVal a => a -> String
dumpStr :: a -> String
dumpStr = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (a -> Doc) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Doc
forall a. PrettyVal a => a -> Doc
dumpDoc
dumpIO :: PrettyVal a => a -> IO ()
dumpIO :: a -> IO ()
dumpIO = String -> IO ()
putStrLn (String -> IO ()) -> (a -> String) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. PrettyVal a => a -> String
dumpStr
valToStr :: Value -> String
valToStr :: Value -> String
valToStr = Doc -> String
forall a. Show a => a -> String
show (Doc -> String) -> (Value -> Doc) -> Value -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Doc
valToDoc
valToDoc :: Value -> Doc
valToDoc :: Value -> Doc
valToDoc Value
val = case Value
val of
Con String
c [Value]
vs -> String -> [Value] -> Doc
ppCon String
c [Value]
vs
InfixCons Value
v1 [(String, Value)]
cvs -> [Doc] -> Doc
hang_sep (Value -> [(String, Value)] -> [Doc]
go Value
v1 [(String, Value)]
cvs)
where
go :: Value -> [(String, Value)] -> [Doc]
go Value
v [] = [Value -> Doc
ppInfixAtom Value
v]
go Value
v ((String
n,Value
v2):[(String, Value)]
cvs') = (Value -> Doc
ppInfixAtom Value
v Doc -> Doc -> Doc
<+> String -> Doc
text String
n)Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:Value -> [(String, Value)] -> [Doc]
go Value
v2 [(String, Value)]
cvs'
hang_sep :: [Doc] -> Doc
hang_sep [] = Doc
empty
hang_sep (Doc
x:[Doc]
xs) = Doc -> Int -> Doc -> Doc
hang Doc
x Int
2 ([Doc] -> Doc
sep [Doc]
xs)
Rec String
c [(String, Value)]
fs -> Doc -> Int -> Doc -> Doc
hang (String -> Doc
text String
c) Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Char -> Char -> [Doc] -> Doc
block Char
'{' Char
'}' (((String, Value) -> Doc) -> [(String, Value)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (String, Value) -> Doc
ppField [(String, Value)]
fs)
where ppField :: (String, Value) -> Doc
ppField (String
x,Value
v) = Doc -> Int -> Doc -> Doc
hang (String -> Doc
text String
x Doc -> Doc -> Doc
<+> Char -> Doc
char Char
'=') Int
2 (Value -> Doc
valToDoc Value
v)
List [Value]
vs -> Char -> Char -> [Doc] -> Doc
block Char
'[' Char
']' ((Value -> Doc) -> [Value] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Doc
valToDoc [Value]
vs)
Tuple [Value]
vs -> Char -> Char -> [Doc] -> Doc
block Char
'(' Char
')' ((Value -> Doc) -> [Value] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Doc
valToDoc [Value]
vs)
Neg Value
v -> Char -> Doc
char Char
'-' Doc -> Doc -> Doc
<> Value -> Doc
ppAtom Value
v
Ratio Value
x Value
y -> Doc -> Int -> Doc -> Doc
hang (Value -> Doc
ppAtom Value
x Doc -> Doc -> Doc
<+> String -> Doc
text String
"%") Int
2 (Value -> Doc
ppAtom Value
y)
Integer String
x -> String -> Doc
text String
x
Float String
x -> String -> Doc
text String
x
Char String
x -> String -> Doc
text String
x
String String
x -> String -> Doc
text String
x
Date String
x -> String -> Doc
text String
x
Time String
x -> String -> Doc
text String
x
Quote String
x -> String -> Doc
text String
x
data PreProc a = PreProc (Value -> Value) a
instance Show a => Show (PreProc a) where
showsPrec :: Int -> PreProc a -> String -> String
showsPrec Int
p (PreProc Value -> Value
f a
a) String
cs =
case String -> Maybe Value
parseValue String
txt of
Maybe Value
Nothing -> String
txt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
cs
Just Value
v -> String -> String
wrap (Value -> String
valToStr (Value -> Value
f Value
v))
where
txt :: String
txt = Int -> a -> String -> String
forall a. Show a => Int -> a -> String -> String
showsPrec Int
p a
a String
""
wrap :: String -> String
wrap String
t = case (String
t,String
txt) of
(Char
h:String
_,Char
'(':String
_) | Char
h Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'(' -> Char
'(' Char -> String -> String
forall a. a -> [a] -> [a]
: (String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char
')' Char -> String -> String
forall a. a -> [a] -> [a]
: String
cs)
(String, String)
_ -> String
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
cs
ppHide :: (Name -> Bool) -> a -> PreProc a
ppHide :: (String -> Bool) -> a -> PreProc a
ppHide String -> Bool
p = (Value -> Value) -> a -> PreProc a
forall a. (Value -> Value) -> a -> PreProc a
PreProc (Bool -> (String -> Bool) -> Value -> Value
hideCon Bool
False String -> Bool
p)
ppHideNested :: (Name -> Bool) -> a -> PreProc a
ppHideNested :: (String -> Bool) -> a -> PreProc a
ppHideNested String -> Bool
p = (Value -> Value) -> a -> PreProc a
forall a. (Value -> Value) -> a -> PreProc a
PreProc (Bool -> (String -> Bool) -> Value -> Value
hideCon Bool
True String -> Bool
p)
ppAtom :: Value -> Doc
ppAtom :: Value -> Doc
ppAtom Value
v
| Value -> Bool
isAtom Value
v = Value -> Doc
valToDoc Value
v
| Bool
otherwise = Doc -> Doc
parens (Value -> Doc
valToDoc Value
v)
ppInfixAtom :: Value -> Doc
ppInfixAtom :: Value -> Doc
ppInfixAtom Value
v
| Value -> Bool
isInfixAtom Value
v = Value -> Doc
valToDoc Value
v
| Bool
otherwise = Doc -> Doc
parens (Value -> Doc
valToDoc Value
v)
ppCon :: Name -> [Value] -> Doc
ppCon :: String -> [Value] -> Doc
ppCon String
"" [Value]
vs = [Doc] -> Doc
sep ((Value -> Doc) -> [Value] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Doc
ppAtom [Value]
vs)
ppCon String
c [Value]
vs = Doc -> Int -> Doc -> Doc
hang (String -> Doc
text String
c) Int
2 ([Doc] -> Doc
sep ((Value -> Doc) -> [Value] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Doc
ppAtom [Value]
vs))
isAtom :: Value -> Bool
isAtom :: Value -> Bool
isAtom (Con String
_ (Value
_:[Value]
_)) = Bool
False
isAtom (InfixCons {}) = Bool
False
isAtom (Ratio {}) = Bool
False
isAtom (Neg {}) = Bool
False
isAtom Value
_ = Bool
True
isInfixAtom :: Value -> Bool
isInfixAtom :: Value -> Bool
isInfixAtom (InfixCons {}) = Bool
False
isInfixAtom (Ratio {}) = Bool
False
isInfixAtom (Neg {}) = Bool
False
isInfixAtom Value
_ = Bool
True
block :: Char -> Char -> [Doc] -> Doc
block :: Char -> Char -> [Doc] -> Doc
block = ([Doc] -> Doc) -> Char -> Char -> [Doc] -> Doc
blockWith [Doc] -> Doc
sep
blockWith :: ([Doc] -> Doc) -> Char -> Char -> [Doc] -> Doc
blockWith :: ([Doc] -> Doc) -> Char -> Char -> [Doc] -> Doc
blockWith [Doc] -> Doc
_ Char
a Char
b [] = Char -> Doc
char Char
a Doc -> Doc -> Doc
<> Char -> Doc
char Char
b
blockWith [Doc] -> Doc
f Char
a Char
b (Doc
d:[Doc]
ds) = [Doc] -> Doc
f ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$
(Char -> Doc
char Char
a Doc -> Doc -> Doc
<+> Doc
d) Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: [ Char -> Doc
char Char
',' Doc -> Doc -> Doc
<+> Doc
x | Doc
x <- [Doc]
ds ] [Doc] -> [Doc] -> [Doc]
forall a. [a] -> [a] -> [a]
++ [ Char -> Doc
char Char
b ]