module Text.StringTemplate.QQ (stmp) where
import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Quote
import Text.StringTemplate.Base
quoteTmplExp :: String -> TH.ExpQ
quoteTmplPat :: String -> TH.PatQ
stmp :: QuasiQuoter
stmp = QuasiQuoter {quoteExp = quoteTmplExp, quotePat = quoteTmplPat}
quoteTmplPat = error "Cannot apply stmp quasiquoter in patterns"
quoteTmplExp s = return tmpl
where
vars = case parseSTMPNames ('$','$') s of
Right (xs,_,_) -> xs
Left err -> fail $ show err
base = TH.AppE (TH.VarE (TH.mkName "Text.StringTemplate.newSTMP")) (TH.LitE (TH.StringL s))
tmpl = foldr addAttrib base vars
addAttrib var = TH.AppE
(TH.AppE (TH.AppE (TH.VarE (TH.mkName "Text.StringTemplate.setAttribute"))
(TH.LitE (TH.StringL ('`' : var ++ "`"))))
(TH.VarE (TH.mkName var)))