{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}

module Text.Scanf.Internal where

import Data.Char (isSpace)
import Data.List (stripPrefix)

-- | A pretty pair type to build lists with values of different types. 
-- Remember to close lists with @()@.
--
-- @
-- 3 ':+' "14" ':+' () :: 'Int' ':+' 'String' ':+' ()
-- @
data a :+ b = a :+ b
  deriving ((a :+ b) -> (a :+ b) -> Bool
((a :+ b) -> (a :+ b) -> Bool)
-> ((a :+ b) -> (a :+ b) -> Bool) -> Eq (a :+ b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq a, Eq b) => (a :+ b) -> (a :+ b) -> Bool
$c== :: forall a b. (Eq a, Eq b) => (a :+ b) -> (a :+ b) -> Bool
== :: (a :+ b) -> (a :+ b) -> Bool
$c/= :: forall a b. (Eq a, Eq b) => (a :+ b) -> (a :+ b) -> Bool
/= :: (a :+ b) -> (a :+ b) -> Bool
Eq, Eq (a :+ b)
Eq (a :+ b) =>
((a :+ b) -> (a :+ b) -> Ordering)
-> ((a :+ b) -> (a :+ b) -> Bool)
-> ((a :+ b) -> (a :+ b) -> Bool)
-> ((a :+ b) -> (a :+ b) -> Bool)
-> ((a :+ b) -> (a :+ b) -> Bool)
-> ((a :+ b) -> (a :+ b) -> a :+ b)
-> ((a :+ b) -> (a :+ b) -> a :+ b)
-> Ord (a :+ b)
(a :+ b) -> (a :+ b) -> Bool
(a :+ b) -> (a :+ b) -> Ordering
(a :+ b) -> (a :+ b) -> a :+ b
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
forall a b. (Ord a, Ord b) => Eq (a :+ b)
forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Bool
forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Ordering
forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> a :+ b
$ccompare :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Ordering
compare :: (a :+ b) -> (a :+ b) -> Ordering
$c< :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Bool
< :: (a :+ b) -> (a :+ b) -> Bool
$c<= :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Bool
<= :: (a :+ b) -> (a :+ b) -> Bool
$c> :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Bool
> :: (a :+ b) -> (a :+ b) -> Bool
$c>= :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> Bool
>= :: (a :+ b) -> (a :+ b) -> Bool
$cmax :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> a :+ b
max :: (a :+ b) -> (a :+ b) -> a :+ b
$cmin :: forall a b. (Ord a, Ord b) => (a :+ b) -> (a :+ b) -> a :+ b
min :: (a :+ b) -> (a :+ b) -> a :+ b
Ord, Int -> (a :+ b) -> ShowS
[a :+ b] -> ShowS
(a :+ b) -> String
(Int -> (a :+ b) -> ShowS)
-> ((a :+ b) -> String) -> ([a :+ b] -> ShowS) -> Show (a :+ b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> (a :+ b) -> ShowS
forall a b. (Show a, Show b) => [a :+ b] -> ShowS
forall a b. (Show a, Show b) => (a :+ b) -> String
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> (a :+ b) -> ShowS
showsPrec :: Int -> (a :+ b) -> ShowS
$cshow :: forall a b. (Show a, Show b) => (a :+ b) -> String
show :: (a :+ b) -> String
$cshowList :: forall a b. (Show a, Show b) => [a :+ b] -> ShowS
showList :: [a :+ b] -> ShowS
Show)

infixr 1 :+

-- | Typed @'scanf'@/@'printf'@ format strings.
-- They can be built using the 'Text.Scanf.fmt' quasiquote or
-- with the 'fmt_' function and format combinators.
data Format t where
  Empty :: Format ()
  Constant :: String -> Format t -> Format t
  Whitespace :: String -> Format t -> Format t
  Readable :: (Read a, Show a) => Format t -> Format (a :+ t)
  String :: Format t -> Format (String :+ t)  -- whitespace delimited
  Char :: Format t -> Format (Char :+ t)

deriving instance Show (Format t)

emptyFmt :: Format ()
emptyFmt :: Format ()
emptyFmt = Format ()
Empty

-- | Construct a format string. This is an alternative to 'Text.Scanf.fmt'
-- that doesn't rely on Template Haskell.
--
-- The components of a format string are composed using @('.')@ (function
-- composition) and @('%')@ (wrapper for constant strings).
--
-- @
-- 'fmt_' ('int' '.' \" lazy \" '%' 'string' '.' \" and \" '%' 'int' '.' \" strict \" '%' 'string')
--   :: 'Format' ('Int' ':+' 'String' ':+' 'Int' ':+' 'String' ':+' ())
-- @
fmt_ :: (Format () -> Format t) -> Format t
fmt_ :: forall t. (Format () -> Format t) -> Format t
fmt_ Format () -> Format t
f = Format () -> Format t
f Format ()
emptyFmt

-- | Append a constant string to a format string component.
--
-- N.B.: in 'scanf', spaces in the format string match any number of whitespace
-- character until the next nonspace character.
(%) :: String -> (Format t -> Format q) -> Format t -> Format q
% :: forall t q.
String -> (Format t -> Format q) -> Format t -> Format q
(%) String
s Format t -> Format q
f = String -> Format q -> Format q
forall t. String -> Format t -> Format t
constant String
s (Format q -> Format q)
-> (Format t -> Format q) -> Format t -> Format q
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format t -> Format q
f

infixr 9 %

-- | Append a constant string to a format string.
constant :: String -> Format t -> Format t
constant :: forall t. String -> Format t -> Format t
constant String
"" Format t
f = Format t
f
constant s :: String
s@(Char
c : String
_) Format t
f | Char -> Bool
isSpace Char
c =
  let (String
s0, String
s') = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isSpace String
s
  in String -> Format t -> Format t
forall t. String -> Format t -> Format t
whitespace String
s0 (String -> Format t -> Format t
forall t. String -> Format t -> Format t
constant String
s' Format t
f)
constant String
s Format t
f =
  let (String
s0, String
s') = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
isSpace String
s
  in String -> Format t -> Format t
forall t. String -> Format t -> Format t
constant' String
s0 (String -> Format t -> Format t
forall t. String -> Format t -> Format t
constant String
s' Format t
f)

-- | Append a constant string with no whitespace to a format string.
constant' :: String -> Format t -> Format t
constant' :: forall t. String -> Format t -> Format t
constant' String
s (Constant String
s' Format t
f) = String -> Format t -> Format t
forall t. String -> Format t -> Format t
Constant (String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s') Format t
f
constant' String
"" Format t
f = Format t
f
constant' String
s Format t
f = String -> Format t -> Format t
forall t. String -> Format t -> Format t
Constant String
s Format t
f

-- | Append a constant whitespace string to a format string.
whitespace :: String -> Format t -> Format t
whitespace :: forall t. String -> Format t -> Format t
whitespace String
s (Whitespace String
s' Format t
f) = String -> Format t -> Format t
forall t. String -> Format t -> Format t
Whitespace (String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s') Format t
f
whitespace String
s Format t
f = String -> Format t -> Format t
forall t. String -> Format t -> Format t
Whitespace String
s Format t
f

readable :: (Read a, Show a) => Format t -> Format (a :+ t)
readable :: forall a t. (Read a, Show a) => Format t -> Format (a :+ t)
readable = Format t -> Format (a :+ t)
forall a t. (Read a, Show a) => Format t -> Format (a :+ t)
Readable

-- | Format an 'Integer'.
integer :: Format t -> Format (Integer :+ t)
integer :: forall t. Format t -> Format (Integer :+ t)
integer = Format t -> Format (Integer :+ t)
forall a t. (Read a, Show a) => Format t -> Format (a :+ t)
Readable

-- | Format an 'Int'.
int :: Format t -> Format (Int :+ t)
int :: forall t. Format t -> Format (Int :+ t)
int = Format t -> Format (Int :+ t)
forall a t. (Read a, Show a) => Format t -> Format (a :+ t)
Readable

-- | Format a 'Double'.
double :: Format t -> Format (Double :+ t)
double :: forall t. Format t -> Format (Double :+ t)
double = Format t -> Format (Double :+ t)
forall a t. (Read a, Show a) => Format t -> Format (a :+ t)
Readable

-- | Format a 'String'.
string :: Format t -> Format (String :+ t)
string :: forall t. Format t -> Format (String :+ t)
string = Format t -> Format (String :+ t)
forall t. Format t -> Format (String :+ t)
String

-- | Format a 'Char'.
char :: Format t -> Format (Char :+ t)
char :: forall t. Format t -> Format (Char :+ t)
char = Format t -> Format (Char :+ t)
forall t. Format t -> Format (Char :+ t)
Char

readmap :: (a -> b) -> ReadS a -> ReadS b
readmap :: forall a b. (a -> b) -> ReadS a -> ReadS b
readmap a -> b
f = (([(a, String)] -> [(b, String)])
-> (String -> [(a, String)]) -> String -> [(b, String)]
forall a b. (a -> b) -> (String -> a) -> String -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(a, String)] -> [(b, String)])
 -> (String -> [(a, String)]) -> String -> [(b, String)])
-> (((a, String) -> (b, String)) -> [(a, String)] -> [(b, String)])
-> ((a, String) -> (b, String))
-> (String -> [(a, String)])
-> String
-> [(b, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, String) -> (b, String)) -> [(a, String)] -> [(b, String)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (\(a
r, String
s) -> (a -> b
f a
r, String
s))

readsFormat :: Format t -> ReadS t
readsFormat :: forall t. Format t -> ReadS t
readsFormat (Constant String
z Format t
f) String
s = do
  Just String
s' <- Maybe String -> [Maybe String]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> String -> Maybe String
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
z String
s)
  Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f String
s'
readsFormat (Readable Format t
f) String
s = do
  (a
a, String
s') <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec Int
0 String
s
  (t -> t) -> ReadS t -> ReadS t
forall a b. (a -> b) -> ReadS a -> ReadS b
readmap (a
a a -> t -> a :+ t
forall a b. a -> b -> a :+ b
:+) (Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f) String
s'
readsFormat (String Format t
f) String
s = do
  let (String
s0, String
s') = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
isSpace String
s
  (t -> t) -> ReadS t -> ReadS t
forall a b. (a -> b) -> ReadS a -> ReadS b
readmap (String
s0 String -> t -> String :+ t
forall a b. a -> b -> a :+ b
:+) (Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f) String
s'
readsFormat (Char Format t
f) String
s = do
  Char
c : String
s' <- String -> [String]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
s
  (t -> t) -> ReadS t -> ReadS t
forall a b. (a -> b) -> ReadS a -> ReadS b
readmap (Char
c Char -> t -> Char :+ t
forall a b. a -> b -> a :+ b
:+) (Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f) String
s'
readsFormat (Whitespace String
_ Format t
f) String
s = do
  let s' :: String
s' = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace String
s
  Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f String
s'
readsFormat Format t
Empty String
s = (t, String) -> [(t, String)]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((), String
s)

-- | Parse a string according to a format string.
--
-- @
-- 'scanf' ['Text.Scanf.fmt'|Hello %s|] \"Hello world!\" :: 'Maybe' ('String' ':+' ())
--   = (\"world!\" ':+' ())
-- @
scanf :: Format t -> String -> Maybe t
scanf :: forall t. Format t -> String -> Maybe t
scanf Format t
f String
s = do
  (t
r, String
_) : [(t, String)]
_ <- [(t, String)] -> Maybe [(t, String)]
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Format t -> ReadS t
forall t. Format t -> ReadS t
readsFormat Format t
f String
s)
  t -> Maybe t
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
r

-- | Print a string according to a format string.
--
-- @
-- 'printf' ['Text.Scanf.fmt'|Hello %s|] (\"everyone!\" ':+' ())
--   = \"Hello everyone!\" 
-- @
printf :: Format t -> t -> String
printf :: forall t. Format t -> t -> String
printf (Constant String
z Format t
f) t
t = String
z String -> ShowS
forall a. [a] -> [a] -> [a]
++ Format t -> t -> String
forall t. Format t -> t -> String
printf Format t
f t
t
printf (Readable Format t
f) (a
a :+ t
t) = a -> String
forall a. Show a => a -> String
show a
a String -> ShowS
forall a. [a] -> [a] -> [a]
++ Format t -> t -> String
forall t. Format t -> t -> String
printf Format t
f t
t
printf (String Format t
f) (String
s :+ t
t) = String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ Format t -> t -> String
forall t. Format t -> t -> String
printf Format t
f t
t
printf (Char Format t
f) (Char
c :+ t
t) = Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: Format t -> t -> String
forall t. Format t -> t -> String
printf Format t
f t
t
printf (Whitespace String
s Format t
f) t
t = String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ Format t -> t -> String
forall t. Format t -> t -> String
printf Format t
f t
t
printf Format t
Empty t
_ = String
""