{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
module Language.Haskell.Printf (
s,
t,
p,
hp,
) where
import Control.Monad.IO.Class
import Language.Haskell.Printf.Lib
import Language.Haskell.TH.Lib
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
import System.IO (hPutStr)
s :: QuasiQuoter
s :: QuasiQuoter
s = (String -> ExpQ) -> QuasiQuoter
quoter forall a b. (a -> b) -> a -> b
$ \String
s' -> do
([Pat]
lhss, Exp
rhs) <- String -> OutputType -> Q ([Pat], Exp)
toSplices String
s' OutputType
OutputString
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Pat] -> Exp -> Exp
LamE [Pat]
lhss Exp
rhs
t :: QuasiQuoter
t :: QuasiQuoter
t = (String -> ExpQ) -> QuasiQuoter
quoter forall a b. (a -> b) -> a -> b
$ \String
s' -> do
([Pat]
lhss, Exp
rhs) <- String -> OutputType -> Q ([Pat], Exp)
toSplices String
s' OutputType
OutputText
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Pat] -> Exp -> Exp
LamE [Pat]
lhss Exp
rhs
p :: QuasiQuoter
p :: QuasiQuoter
p = (String -> ExpQ) -> QuasiQuoter
quoter forall a b. (a -> b) -> a -> b
$ \String
s' -> do
([Pat]
lhss, Exp
rhs) <- String -> OutputType -> Q ([Pat], Exp)
toSplices String
s' OutputType
OutputString
forall (m :: * -> *). Quote m => [m Pat] -> m Exp -> m Exp
lamE (forall a b. (a -> b) -> [a] -> [b]
map forall (f :: * -> *) a. Applicative f => a -> f a
pure [Pat]
lhss) [|liftIO (putStr $(pure rhs))|]
hp :: QuasiQuoter
hp :: QuasiQuoter
hp = (String -> ExpQ) -> QuasiQuoter
quoter forall a b. (a -> b) -> a -> b
$ \String
s' -> do
([Pat]
lhss, Exp
rhs) <- String -> OutputType -> Q ([Pat], Exp)
toSplices String
s' OutputType
OutputString
Name
h <- forall (m :: * -> *). Quote m => String -> m Name
newName String
"h"
forall (m :: * -> *). Quote m => [m Pat] -> m Exp -> m Exp
lamE (forall (m :: * -> *). Quote m => Name -> m Pat
varP Name
h forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map forall (f :: * -> *) a. Applicative f => a -> f a
pure [Pat]
lhss) [|liftIO (hPutStr $(varE h) $(pure rhs))|]
quoter :: (String -> ExpQ) -> QuasiQuoter
quoter :: (String -> ExpQ) -> QuasiQuoter
quoter String -> ExpQ
e =
QuasiQuoter
{ quoteExp :: String -> ExpQ
quoteExp = String -> ExpQ
e
, quotePat :: String -> Q Pat
quotePat = forall a. HasCallStack => String -> a
error String
"this quoter cannot be used in a pattern context"
, quoteType :: String -> Q Type
quoteType = forall a. HasCallStack => String -> a
error String
"this quoter cannot be used in a type context"
, quoteDec :: String -> Q [Dec]
quoteDec = forall a. HasCallStack => String -> a
error String
"this quoter cannot be used in a declaration context"
}