{-# LANGUAGE CPP #-}
module Data.Generics.Text (
gshow, gshows,
gread
) where
#ifdef __HADDOCK__
import Prelude
#endif
import Control.Monad
import Data.Data
import Data.Generics.Aliases
import Text.ParserCombinators.ReadP
import Text.Read.Lex
gshow :: Data a => a -> String
gshow :: a -> String
gshow a
x = a -> ShowS
forall a. Data a => a -> ShowS
gshows a
x String
""
gshows :: Data a => a -> ShowS
gshows :: a -> ShowS
gshows = ( \a
t ->
Char -> ShowS
showChar Char
'('
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> ShowS
showString (String -> ShowS) -> (a -> String) -> a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constr -> String
showConstr (Constr -> String) -> (a -> Constr) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Constr
forall a. Data a => a -> Constr
toConstr (a -> ShowS) -> a -> ShowS
forall a b. (a -> b) -> a -> b
$ a
t)
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ShowS -> ShowS -> ShowS) -> ShowS -> [ShowS] -> ShowS
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) ShowS
forall a. a -> a
id ([ShowS] -> ShowS) -> (a -> [ShowS]) -> a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Data a => a -> ShowS) -> a -> [ShowS]
forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ ((Char -> ShowS
showChar Char
' ' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (ShowS -> ShowS) -> (d -> ShowS) -> d -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. d -> ShowS
forall a. Data a => a -> ShowS
gshows) (a -> ShowS) -> a -> ShowS
forall a b. (a -> b) -> a -> b
$ a
t)
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
')'
) (a -> ShowS) -> (String -> ShowS) -> a -> ShowS
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` (String -> ShowS
forall a. Show a => a -> ShowS
shows :: String -> ShowS)
gread :: Data a => ReadS a
gread :: ReadS a
gread = ReadP a -> ReadS a
forall a. ReadP a -> ReadS a
readP_to_S ReadP a
forall a'. Data a' => ReadP a'
gread'
where
gread' :: Data a' => ReadP a'
gread' :: ReadP a'
gread' = ReadP a'
allButString ReadP a' -> ReadP String -> ReadP a'
forall (m :: * -> *) a b.
(Monad m, Typeable a, Typeable b) =>
m a -> m b -> m a
`extR` ReadP String
stringCase
where
stringCase :: ReadP String
stringCase :: ReadP String
stringCase = ReadS String -> ReadP String
forall a. ReadS a -> ReadP a
readS_to_P ReadS String
forall a. Read a => ReadS a
reads
myDataType :: DataType
myDataType = a' -> DataType
forall a. Data a => a -> DataType
dataTypeOf (ReadP a' -> a'
forall a''. ReadP a'' -> a''
getArg ReadP a'
allButString)
where
getArg :: ReadP a'' -> a''
getArg :: ReadP a'' -> a''
getArg = ReadP a'' -> a''
forall a. HasCallStack => a
undefined
allButString :: ReadP a'
allButString =
do
ReadP ()
skipSpaces
Char
_ <- Char -> ReadP Char
char Char
'('
ReadP ()
skipSpaces
String
str <- ReadP String
parseConstr
Constr
con <- String -> ReadP Constr
str2con String
str
a'
x <- (forall a'. Data a' => ReadP a') -> Constr -> ReadP a'
forall (m :: * -> *) a.
(Monad m, Data a) =>
(forall d. Data d => m d) -> Constr -> m a
fromConstrM forall a'. Data a' => ReadP a'
gread' Constr
con
ReadP ()
skipSpaces
Char
_ <- Char -> ReadP Char
char Char
')'
ReadP ()
skipSpaces
a' -> ReadP a'
forall (m :: * -> *) a. Monad m => a -> m a
return a'
x
str2con :: String -> ReadP Constr
str2con :: String -> ReadP Constr
str2con = ReadP Constr
-> (Constr -> ReadP Constr) -> Maybe Constr -> ReadP Constr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ReadP Constr
forall (m :: * -> *) a. MonadPlus m => m a
mzero Constr -> ReadP Constr
forall (m :: * -> *) a. Monad m => a -> m a
return
(Maybe Constr -> ReadP Constr)
-> (String -> Maybe Constr) -> String -> ReadP Constr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataType -> String -> Maybe Constr
readConstr DataType
myDataType
parseConstr :: ReadP String
parseConstr :: ReadP String
parseConstr =
String -> ReadP String
string String
"[]"
ReadP String -> ReadP String -> ReadP String
forall a. ReadP a -> ReadP a -> ReadP a
<++ String -> ReadP String
string String
"()"
ReadP String -> ReadP String -> ReadP String
forall a. ReadP a -> ReadP a -> ReadP a
<++ ReadP String
infixOp
ReadP String -> ReadP String -> ReadP String
forall a. ReadP a -> ReadP a -> ReadP a
<++ ReadP String
hsLex
infixOp :: ReadP String
infixOp :: ReadP String
infixOp = do Char
c1 <- Char -> ReadP Char
char Char
'('
String
str <- (Char -> Bool) -> ReadP String
munch1 (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
(==) Char
')')
Char
c2 <- Char -> ReadP Char
char Char
')'
String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> ReadP String) -> String -> ReadP String
forall a b. (a -> b) -> a -> b
$ [Char
c1] String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char
c2]