{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module Derulo
( JSON(..)
, readJSON
, showJSON
) where
import qualified Control.Monad as Monad
import qualified Data.Data as Data
import qualified Data.Functor as Functor
import qualified Data.List as List
import qualified Data.Maybe as Maybe
import qualified GHC.Generics as Generics
import qualified Text.ParserCombinators.ReadP as ReadP
data JSON
= Null
| Boolean Bool
| Number Integer
Integer
| String String
| Array [JSON]
| Object [(String, JSON)]
deriving (Typeable JSON
JSON -> DataType
JSON -> Constr
(forall b. Data b => b -> b) -> JSON -> JSON
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> JSON -> u
forall u. (forall d. Data d => d -> u) -> JSON -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JSON
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JSON -> c JSON
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JSON)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JSON)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> JSON -> m JSON
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> JSON -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> JSON -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> JSON -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> JSON -> [u]
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> JSON -> r
gmapT :: (forall b. Data b => b -> b) -> JSON -> JSON
$cgmapT :: (forall b. Data b => b -> b) -> JSON -> JSON
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JSON)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c JSON)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JSON)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c JSON)
dataTypeOf :: JSON -> DataType
$cdataTypeOf :: JSON -> DataType
toConstr :: JSON -> Constr
$ctoConstr :: JSON -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JSON
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c JSON
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JSON -> c JSON
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> JSON -> c JSON
Data.Data, JSON -> JSON -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: JSON -> JSON -> Bool
$c/= :: JSON -> JSON -> Bool
== :: JSON -> JSON -> Bool
$c== :: JSON -> JSON -> Bool
Eq, forall x. Rep JSON x -> JSON
forall x. JSON -> Rep JSON x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep JSON x -> JSON
$cfrom :: forall x. JSON -> Rep JSON x
Generics.Generic, Eq JSON
JSON -> JSON -> Bool
JSON -> JSON -> Ordering
JSON -> JSON -> JSON
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: JSON -> JSON -> JSON
$cmin :: JSON -> JSON -> JSON
max :: JSON -> JSON -> JSON
$cmax :: JSON -> JSON -> JSON
>= :: JSON -> JSON -> Bool
$c>= :: JSON -> JSON -> Bool
> :: JSON -> JSON -> Bool
$c> :: JSON -> JSON -> Bool
<= :: JSON -> JSON -> Bool
$c<= :: JSON -> JSON -> Bool
< :: JSON -> JSON -> Bool
$c< :: JSON -> JSON -> Bool
compare :: JSON -> JSON -> Ordering
$ccompare :: JSON -> JSON -> Ordering
Ord, ReadPrec [JSON]
ReadPrec JSON
Int -> ReadS JSON
ReadS [JSON]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [JSON]
$creadListPrec :: ReadPrec [JSON]
readPrec :: ReadPrec JSON
$creadPrec :: ReadPrec JSON
readList :: ReadS [JSON]
$creadList :: ReadS [JSON]
readsPrec :: Int -> ReadS JSON
$creadsPrec :: Int -> ReadS JSON
Read, Int -> JSON -> ShowS
[JSON] -> ShowS
JSON -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [JSON] -> ShowS
$cshowList :: [JSON] -> ShowS
show :: JSON -> String
$cshow :: JSON -> String
showsPrec :: Int -> JSON -> ShowS
$cshowsPrec :: Int -> JSON -> ShowS
Show)
readJSON :: String -> Maybe JSON
readJSON :: String -> Maybe JSON
readJSON = forall a. ReadP a -> String -> Maybe a
runParser ReadP JSON
pJSON
pJSON :: ReadP.ReadP JSON
pJSON :: ReadP JSON
pJSON = do
ReadP ()
pWhitespaces
JSON
value <- ReadP JSON
pValue
ReadP ()
ReadP.eof
forall (f :: * -> *) a. Applicative f => a -> f a
pure JSON
value
pValue :: ReadP.ReadP JSON
pValue :: ReadP JSON
pValue = forall a. [ReadP a] -> ReadP a
ReadP.choice [ReadP JSON
pNull, ReadP JSON
pBoolean, ReadP JSON
pNumber, ReadP JSON
pString, ReadP JSON
pArray, ReadP JSON
pObject]
pNull :: ReadP.ReadP JSON
pNull :: ReadP JSON
pNull = do
String -> ReadP ()
pSymbol String
"null"
forall (f :: * -> *) a. Applicative f => a -> f a
pure JSON
Null
pBoolean :: ReadP.ReadP JSON
pBoolean :: ReadP JSON
pBoolean = ReadP JSON
pTrue forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ ReadP JSON
pFalse
pTrue :: ReadP.ReadP JSON
pTrue :: ReadP JSON
pTrue = do
String -> ReadP ()
pSymbol String
"true"
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> JSON
Boolean Bool
True)
pFalse :: ReadP.ReadP JSON
pFalse :: ReadP JSON
pFalse = do
String -> ReadP ()
pSymbol String
"false"
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> JSON
Boolean Bool
False)
pNumber :: ReadP.ReadP JSON
pNumber :: ReadP JSON
pNumber = do
Integer
integer <- ReadP Integer
pInteger
(Integer
fraction, Integer
precision) <- forall a. a -> ReadP a -> ReadP a
ReadP.option (Integer
0, Integer
0) ReadP (Integer, Integer)
pFraction
Integer
power <- forall a. a -> ReadP a -> ReadP a
ReadP.option Integer
0 ReadP Integer
pPower
ReadP ()
pWhitespaces
let mantissa :: Integer
mantissa = Integer
integer forall a. Num a => a -> a -> a
* Integer
10 forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
precision forall a. Num a => a -> a -> a
+ Bool -> Integer -> Integer
negateIf (Integer
integer forall a. Ord a => a -> a -> Bool
<= Integer
0) Integer
fraction
let magnitude :: Integer
magnitude = Integer
power forall a. Num a => a -> a -> a
- Integer
precision
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Integer -> JSON
Number Integer
mantissa Integer
magnitude)
pInteger :: ReadP.ReadP Integer
pInteger :: ReadP Integer
pInteger = ReadP Integer
pZero forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ ReadP Integer
pNonZero
pZero :: ReadP.ReadP Integer
pZero :: ReadP Integer
pZero = do
forall a. ReadP a -> ReadP ()
ReadP.optional (Char -> ReadP Char
ReadP.char Char
'-')
forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void (Char -> ReadP Char
ReadP.char Char
'0')
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
0
pNonZero :: ReadP.ReadP Integer
pNonZero :: ReadP Integer
pNonZero = do
Char
sign <- forall a. a -> ReadP a -> ReadP a
ReadP.option Char
'+' (Char -> ReadP Char
ReadP.char Char
'-')
Char
first <- (Char -> Bool) -> ReadP Char
ReadP.satisfy Char -> Bool
isNonZeroDigit
String
rest <- (Char -> Bool) -> ReadP String
ReadP.munch Char -> Bool
isDecimalDigit
case String -> Maybe Integer
fromDecimal (Char
first forall a. a -> [a] -> [a]
: String
rest) of
Maybe Integer
Nothing -> forall a. ReadP a
ReadP.pfail
Just Integer
nonZero -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Integer -> Integer
negateIf (Char
sign forall a. Eq a => a -> a -> Bool
== Char
'-') Integer
nonZero)
pFraction :: ReadP.ReadP (Integer, Integer)
pFraction :: ReadP (Integer, Integer)
pFraction = do
forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void (Char -> ReadP Char
ReadP.char Char
'.')
String
digits <- (Char -> Bool) -> ReadP String
ReadP.munch1 Char -> Bool
isDecimalDigit
case String -> Maybe Integer
fromDecimal String
digits of
Maybe Integer
Nothing -> forall a. ReadP a
ReadP.pfail
Just Integer
fraction -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer
fraction, forall i a. Num i => [a] -> i
List.genericLength String
digits)
pPower :: ReadP.ReadP Integer
pPower :: ReadP Integer
pPower = do
forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void (Char -> ReadP Char
ReadP.char Char
'E' forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ Char -> ReadP Char
ReadP.char Char
'e')
Char
sign <- forall a. a -> ReadP a -> ReadP a
ReadP.option Char
'+' (Char -> ReadP Char
ReadP.char Char
'+' forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ Char -> ReadP Char
ReadP.char Char
'-')
String
digits <- (Char -> Bool) -> ReadP String
ReadP.munch1 Char -> Bool
isDecimalDigit
case String -> Maybe Integer
fromDecimal String
digits of
Maybe Integer
Nothing -> forall a. ReadP a
ReadP.pfail
Just Integer
magnitude -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Integer -> Integer
negateIf (Char
sign forall a. Eq a => a -> a -> Bool
== Char
'-') Integer
magnitude)
pString :: ReadP.ReadP JSON
pString :: ReadP JSON
pString = do
JSON
string <- forall open close a.
ReadP open -> ReadP close -> ReadP a -> ReadP a
ReadP.between
(Char -> ReadP Char
ReadP.char Char
'"')
(Char -> ReadP Char
ReadP.char Char
'"')
(do
String
characters <- forall a. ReadP a -> ReadP [a]
ReadP.many ReadP Char
pCharacter
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> JSON
String String
characters)
)
ReadP ()
pWhitespaces
forall (f :: * -> *) a. Applicative f => a -> f a
pure JSON
string
pCharacter :: ReadP.ReadP Char
pCharacter :: ReadP Char
pCharacter = ReadP Char
pLiteral forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ ReadP Char
pEscape
pLiteral :: ReadP.ReadP Char
pLiteral :: ReadP Char
pLiteral = (Char -> Bool) -> ReadP Char
ReadP.satisfy Char -> Bool
isLiteral
pEscape :: ReadP.ReadP Char
pEscape :: ReadP Char
pEscape = do
forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void (Char -> ReadP Char
ReadP.char Char
'\\')
Char
escape <- ReadP Char
ReadP.get
case Char
escape of
Char
'"' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'"'
Char
'/' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'/'
Char
'\\' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\\'
Char
'b' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\b'
Char
'f' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\f'
Char
'n' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\n'
Char
'r' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\r'
Char
't' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'\t'
Char
'u' -> do
String
digits <- forall a. Int -> ReadP a -> ReadP [a]
ReadP.count Int
4 ((Char -> Bool) -> ReadP Char
ReadP.satisfy Char -> Bool
isHexadecimalDigit)
case String -> Maybe Integer
fromHexadecimal String
digits of
Maybe Integer
Nothing -> forall a. ReadP a
ReadP.pfail
Just Integer
point -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Enum a => Int -> a
toEnum (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
point))
Char
_ -> forall a. ReadP a
ReadP.pfail
pArray :: ReadP.ReadP JSON
pArray :: ReadP JSON
pArray = forall open close a.
ReadP open -> ReadP close -> ReadP a -> ReadP a
ReadP.between
(String -> ReadP ()
pSymbol String
"[")
(String -> ReadP ()
pSymbol String
"]")
(do
[JSON]
values <- forall a sep. ReadP a -> ReadP sep -> ReadP [a]
ReadP.sepBy ReadP JSON
pValue (String -> ReadP ()
pSymbol String
",")
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([JSON] -> JSON
Array [JSON]
values)
)
pObject :: ReadP.ReadP JSON
pObject :: ReadP JSON
pObject = forall open close a.
ReadP open -> ReadP close -> ReadP a -> ReadP a
ReadP.between
(String -> ReadP ()
pSymbol String
"{")
(String -> ReadP ()
pSymbol String
"}")
(do
[(String, JSON)]
pairs <- forall a sep. ReadP a -> ReadP sep -> ReadP [a]
ReadP.sepBy ReadP (String, JSON)
pPair (String -> ReadP ()
pSymbol String
",")
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(String, JSON)] -> JSON
Object [(String, JSON)]
pairs)
)
pPair :: ReadP.ReadP (String, JSON)
pPair :: ReadP (String, JSON)
pPair = do
String String
key <- ReadP JSON
pString
String -> ReadP ()
pSymbol String
":"
JSON
value <- ReadP JSON
pValue
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String
key, JSON
value)
showJSON :: JSON -> String
showJSON :: JSON -> String
showJSON JSON
json = JSON -> ShowS
sJSON JSON
json String
""
sJSON :: JSON -> ShowS
sJSON :: JSON -> ShowS
sJSON JSON
json = case JSON
json of
JSON
Null -> ShowS
sNull
Boolean Bool
boolean -> Bool -> ShowS
sBoolean Bool
boolean
Number Integer
mantissa Integer
magnitude -> Integer -> Integer -> ShowS
sNumber Integer
mantissa Integer
magnitude
String String
string -> String -> ShowS
sString String
string
Array [JSON]
array -> [JSON] -> ShowS
sArray [JSON]
array
Object [(String, JSON)]
object -> [(String, JSON)] -> ShowS
sObject [(String, JSON)]
object
sNull :: ShowS
sNull :: ShowS
sNull = String -> ShowS
showString String
"null"
sBoolean :: Bool -> ShowS
sBoolean :: Bool -> ShowS
sBoolean Bool
boolean = if Bool
boolean then ShowS
sTrue else ShowS
sFalse
sTrue :: ShowS
sTrue :: ShowS
sTrue = String -> ShowS
showString String
"true"
sFalse :: ShowS
sFalse :: ShowS
sFalse = String -> ShowS
showString String
"false"
sNumber :: Integer -> Integer -> ShowS
sNumber :: Integer -> Integer -> ShowS
sNumber Integer
mantissa Integer
magnitude = forall a. Show a => a -> ShowS
shows Integer
mantissa forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'e' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Integer
magnitude
sString :: String -> ShowS
sString :: String -> ShowS
sString = forall element.
ShowS -> ShowS -> ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparatedBetween (Char -> ShowS
showChar Char
'"') (Char -> ShowS
showChar Char
'"') forall a. a -> a
id Char -> ShowS
sCharacter
sCharacter :: Char -> ShowS
sCharacter :: Char -> ShowS
sCharacter Char
character = case Char
character of
Char
'"' -> String -> ShowS
showString String
"\\\""
Char
'\\' -> String -> ShowS
showString String
"\\\\"
Char
'\b' -> String -> ShowS
showString String
"\\b"
Char
'\f' -> String -> ShowS
showString String
"\\f"
Char
'\n' -> String -> ShowS
showString String
"\\n"
Char
'\r' -> String -> ShowS
showString String
"\\r"
Char
'\t' -> String -> ShowS
showString String
"\\t"
Char
_ -> if Char -> Bool
isControl Char
character
then String -> ShowS
showString String
"\\u" forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString
(forall a. Integer -> a -> [a] -> [a]
padLeft Integer
4 Char
'0' (Integer -> String
toHexadecimal (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Enum a => a -> Int
fromEnum Char
character))))
else Char -> ShowS
showChar Char
character
sArray :: [JSON] -> ShowS
sArray :: [JSON] -> ShowS
sArray = forall element.
ShowS -> ShowS -> ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparatedBetween (Char -> ShowS
showChar Char
'[') (Char -> ShowS
showChar Char
']') (Char -> ShowS
showChar Char
',') JSON -> ShowS
sJSON
sObject :: [(String, JSON)] -> ShowS
sObject :: [(String, JSON)] -> ShowS
sObject = forall element.
ShowS -> ShowS -> ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparatedBetween (Char -> ShowS
showChar Char
'{') (Char -> ShowS
showChar Char
'}') (Char -> ShowS
showChar Char
',') (String, JSON) -> ShowS
sPair
sPair :: (String, JSON) -> ShowS
sPair :: (String, JSON) -> ShowS
sPair (String
key, JSON
value) = String -> ShowS
sString String
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
':' forall b c a. (b -> c) -> (a -> b) -> a -> c
. JSON -> ShowS
sJSON JSON
value
fromBase :: Integer -> (Char -> Maybe Integer) -> String -> Maybe Integer
fromBase :: Integer -> (Char -> Maybe Integer) -> String -> Maybe Integer
fromBase Integer
b Char -> Maybe Integer
f = forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
Monad.foldM
(\Integer
n Char
c -> do
Integer
d <- Char -> Maybe Integer
f Char
c
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer
b forall a. Num a => a -> a -> a
* Integer
n forall a. Num a => a -> a -> a
+ Integer
d)
)
Integer
0
fromDecimal :: String -> Maybe Integer
fromDecimal :: String -> Maybe Integer
fromDecimal = Integer -> (Char -> Maybe Integer) -> String -> Maybe Integer
fromBase Integer
10 Char -> Maybe Integer
fromDecimalDigit
fromDecimalDigit :: Char -> Maybe Integer
fromDecimalDigit :: Char -> Maybe Integer
fromDecimalDigit Char
c = case Char
c of
Char
'0' -> forall a. a -> Maybe a
Just Integer
0
Char
'1' -> forall a. a -> Maybe a
Just Integer
1
Char
'2' -> forall a. a -> Maybe a
Just Integer
2
Char
'3' -> forall a. a -> Maybe a
Just Integer
3
Char
'4' -> forall a. a -> Maybe a
Just Integer
4
Char
'5' -> forall a. a -> Maybe a
Just Integer
5
Char
'6' -> forall a. a -> Maybe a
Just Integer
6
Char
'7' -> forall a. a -> Maybe a
Just Integer
7
Char
'8' -> forall a. a -> Maybe a
Just Integer
8
Char
'9' -> forall a. a -> Maybe a
Just Integer
9
Char
_ -> forall a. Maybe a
Nothing
fromHexadecimal :: String -> Maybe Integer
fromHexadecimal :: String -> Maybe Integer
fromHexadecimal = Integer -> (Char -> Maybe Integer) -> String -> Maybe Integer
fromBase Integer
16 Char -> Maybe Integer
fromHexadecimalDigit
fromHexadecimalDigit :: Char -> Maybe Integer
fromHexadecimalDigit :: Char -> Maybe Integer
fromHexadecimalDigit Char
c = case Char
c of
Char
'A' -> forall a. a -> Maybe a
Just Integer
10
Char
'B' -> forall a. a -> Maybe a
Just Integer
11
Char
'C' -> forall a. a -> Maybe a
Just Integer
12
Char
'D' -> forall a. a -> Maybe a
Just Integer
13
Char
'E' -> forall a. a -> Maybe a
Just Integer
14
Char
'F' -> forall a. a -> Maybe a
Just Integer
15
Char
'a' -> forall a. a -> Maybe a
Just Integer
10
Char
'b' -> forall a. a -> Maybe a
Just Integer
11
Char
'c' -> forall a. a -> Maybe a
Just Integer
12
Char
'd' -> forall a. a -> Maybe a
Just Integer
13
Char
'e' -> forall a. a -> Maybe a
Just Integer
14
Char
'f' -> forall a. a -> Maybe a
Just Integer
15
Char
_ -> Char -> Maybe Integer
fromDecimalDigit Char
c
isControl :: Char -> Bool
isControl :: Char -> Bool
isControl Char
c = Char
'\x00' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'\x1f'
isDecimalDigit :: Char -> Bool
isDecimalDigit :: Char -> Bool
isDecimalDigit Char
c = Char
'0' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9'
isHexadecimalDigit :: Char -> Bool
isHexadecimalDigit :: Char -> Bool
isHexadecimalDigit Char
c =
Char -> Bool
isDecimalDigit Char
c Bool -> Bool -> Bool
|| Char
'A' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'F' Bool -> Bool -> Bool
|| Char
'a' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'f'
isLiteral :: Char -> Bool
isLiteral :: Char -> Bool
isLiteral Char
c = Bool -> Bool
not (Char
c forall a. Eq a => a -> a -> Bool
== Char
'"' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\\' Bool -> Bool -> Bool
|| Char -> Bool
isControl Char
c)
isNonZeroDigit :: Char -> Bool
isNonZeroDigit :: Char -> Bool
isNonZeroDigit Char
c = Char
'1' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9'
isWhitespace :: Char -> Bool
isWhitespace :: Char -> Bool
isWhitespace Char
c = Char
c forall a. Eq a => a -> a -> Bool
== Char
'\t' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
' '
negateIf :: Bool -> Integer -> Integer
negateIf :: Bool -> Integer -> Integer
negateIf Bool
p Integer
n = if Bool
p then forall a. Num a => a -> a
negate Integer
n else Integer
n
pSymbol :: String -> ReadP.ReadP ()
pSymbol :: String -> ReadP ()
pSymbol String
s = do
forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void (String -> ReadP String
ReadP.string String
s)
ReadP ()
pWhitespaces
pWhitespaces :: ReadP.ReadP ()
pWhitespaces :: ReadP ()
pWhitespaces = forall (f :: * -> *) a. Functor f => f a -> f ()
Functor.void ((Char -> Bool) -> ReadP String
ReadP.munch Char -> Bool
isWhitespace)
padLeft :: Integer -> a -> [a] -> [a]
padLeft :: forall a. Integer -> a -> [a] -> [a]
padLeft Integer
n a
x [a]
ys = forall a. [a] -> [a]
reverse (forall a. Integer -> a -> [a] -> [a]
padRight Integer
n a
x (forall a. [a] -> [a]
reverse [a]
ys))
padRight :: Integer -> a -> [a] -> [a]
padRight :: forall a. Integer -> a -> [a] -> [a]
padRight Integer
n a
x [a]
ys = if Integer
n forall a. Ord a => a -> a -> Bool
<= Integer
0
then [a]
ys
else case [a]
ys of
[] -> a
x forall a. a -> [a] -> [a]
: forall a. Integer -> a -> [a] -> [a]
padRight (Integer
n forall a. Num a => a -> a -> a
- Integer
1) a
x [a]
ys
a
y : [a]
zs -> a
y forall a. a -> [a] -> [a]
: forall a. Integer -> a -> [a] -> [a]
padRight (Integer
n forall a. Num a => a -> a -> a
- Integer
1) a
x [a]
zs
runParser :: ReadP.ReadP a -> String -> Maybe a
runParser :: forall a. ReadP a -> String -> Maybe a
runParser ReadP a
p String
s = forall a. [a] -> Maybe a
Maybe.listToMaybe
(forall a b. (a -> Maybe b) -> [a] -> [b]
Maybe.mapMaybe
(\(a
x, String
t) -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
t then forall a. a -> Maybe a
Just a
x else forall a. Maybe a
Nothing)
(forall a. ReadP a -> ReadS a
ReadP.readP_to_S ReadP a
p String
s)
)
sBetween :: ShowS -> ShowS -> (anything -> ShowS) -> anything -> ShowS
sBetween :: forall anything.
ShowS -> ShowS -> (anything -> ShowS) -> anything -> ShowS
sBetween ShowS
left ShowS
right anything -> ShowS
render anything
it = ShowS
left forall b c a. (b -> c) -> (a -> b) -> a -> c
. anything -> ShowS
render anything
it forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
right
sSeparated :: ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparated :: forall element. ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparated ShowS
separator element -> ShowS
render [element]
elements = case [element]
elements of
[] -> forall a. a -> a
id
[element
element] -> element -> ShowS
render element
element
element
element : [element]
rest ->
element -> ShowS
render element
element forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
separator forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall element. ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparated ShowS
separator element -> ShowS
render [element]
rest
sSeparatedBetween
:: ShowS -> ShowS -> ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparatedBetween :: forall element.
ShowS -> ShowS -> ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparatedBetween ShowS
left ShowS
right ShowS
separator element -> ShowS
render =
forall anything.
ShowS -> ShowS -> (anything -> ShowS) -> anything -> ShowS
sBetween ShowS
left ShowS
right (forall element. ShowS -> (element -> ShowS) -> [element] -> ShowS
sSeparated ShowS
separator element -> ShowS
render)
toBase :: Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase :: Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase Integer
b Integer -> Maybe Char
f Integer
n =
if Integer
n forall a. Eq a => a -> a -> Bool
== Integer
0 then [forall a. HasCallStack => Maybe a -> a
Maybe.fromJust (Integer -> Maybe Char
f Integer
n)] else forall a. [a] -> [a]
reverse (Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase' Integer
b Integer -> Maybe Char
f Integer
n)
toBase' :: Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase' :: Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase' Integer
b Integer -> Maybe Char
f Integer
n = case forall a. Integral a => a -> a -> (a, a)
quotRem Integer
n Integer
b of
(Integer
0, Integer
0) -> String
""
(Integer
q, Integer
r) -> forall a. HasCallStack => Maybe a -> a
Maybe.fromJust (Integer -> Maybe Char
f Integer
r) forall a. a -> [a] -> [a]
: Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase' Integer
b Integer -> Maybe Char
f Integer
q
toHexadecimal :: Integer -> String
toHexadecimal :: Integer -> String
toHexadecimal = Integer -> (Integer -> Maybe Char) -> Integer -> String
toBase Integer
16 Integer -> Maybe Char
toHexadecimalDigit
toHexadecimalDigit :: Integer -> Maybe Char
toHexadecimalDigit :: Integer -> Maybe Char
toHexadecimalDigit Integer
n = case Integer
n of
Integer
0 -> forall a. a -> Maybe a
Just Char
'0'
Integer
1 -> forall a. a -> Maybe a
Just Char
'1'
Integer
2 -> forall a. a -> Maybe a
Just Char
'2'
Integer
3 -> forall a. a -> Maybe a
Just Char
'3'
Integer
4 -> forall a. a -> Maybe a
Just Char
'4'
Integer
5 -> forall a. a -> Maybe a
Just Char
'5'
Integer
6 -> forall a. a -> Maybe a
Just Char
'6'
Integer
7 -> forall a. a -> Maybe a
Just Char
'7'
Integer
8 -> forall a. a -> Maybe a
Just Char
'8'
Integer
9 -> forall a. a -> Maybe a
Just Char
'9'
Integer
10 -> forall a. a -> Maybe a
Just Char
'a'
Integer
11 -> forall a. a -> Maybe a
Just Char
'b'
Integer
12 -> forall a. a -> Maybe a
Just Char
'c'
Integer
13 -> forall a. a -> Maybe a
Just Char
'd'
Integer
14 -> forall a. a -> Maybe a
Just Char
'e'
Integer
15 -> forall a. a -> Maybe a
Just Char
'f'
Integer
_ -> forall a. Maybe a
Nothing