Interpolation 0.1 → 0.2
raw patch · 2 files changed
+22/−7 lines, 2 filesdep +haskell-src-metadep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: haskell-src-meta
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- Data/String/Interpolation.hs +20/−5
- Interpolation.cabal +2/−2
Data/String/Interpolation.hs view
@@ -2,6 +2,7 @@ module Data.String.Interpolation(str) where import Language.Haskell.TH as TH import Language.Haskell.TH.Quote+import Language.Haskell.Meta import Data.Data import Data.Maybe import Data.Char@@ -49,20 +50,30 @@ ([| (++) |]) (Just $ psToStringE xs) +runEither :: (Monad m) => [Char] -> Either [Char] t -> m t+runEither _ (Right x) = return x+runEither s (Left e) = error $ s ++" : "++ e++appM :: (Monad m) => String -> m Exp+appM expr = runEither "Parse error in antiquote" (parseExp expr) sbitToExp :: StringBits -> ExpQ sbitToExp (Str x) = TH.stringE x -sbitToExp (Var x) = (TH.varE (TH.mkName x))+sbitToExp (Var x) = appM x --(TH.varE (TH.mkName x)) sbitToExp (SVar x) = TH.appE (TH.varE (TH.mkName "show"))- (TH.varE (TH.mkName x))+ (sbitToExp (Var x)) sbitToExp (RepVar varName lstName rep sep) = [|intercalate $(sepr) (map $(lam) $(lstN))|] where sepr = psToStringE (fromMaybe [Str ""] sep) lam = TH.lamE [varN] (psToStringE rep)- varN = TH.varP $ TH.mkName varName- lstN = TH.varE $ TH.mkName lstName+ varN = runEither "Parse error in repetition pattern"+ (parsePat varName)--TH.varP $ TH.mkName varName+ lstN = runEither "Parse error in repetition binding"+ (parseExp lstName) -- TH.varE $ TH.mkName lstName ++ type PieceString = [StringBits] data StringBits = Str String | Var String | SVar String | RepVar String String @@ -79,6 +90,7 @@ parParse ('$':':':s) = SVar (takeWhile (/='$') s) -- Parse antiquotes :parParse (drop 1 (dropWhile (/='$') s))+ parParse ('$':s) = Var (takeWhile (/='$') s) :parParse (drop 1 (dropWhile (/='$') s)) @@ -86,7 +98,10 @@ (hasSep,expr,sepS) = takeRep $ drop 1 exprS (sep,restS) = break (=='#') $ sepS rest = drop 1 restS- [varName,"in",listName] = words bind+ (varNameS,listNameS) = break (=="in") . words+ $ bind+ listName = unwords . drop 1 $ listNameS+ varName = unwords varNameS in if hasSep then RepVar varName listName (parParse expr) (Just $ parParse sep)
Interpolation.cabal view
@@ -1,5 +1,5 @@ Name: Interpolation-Version: 0.1+Version: 0.2 Description: This package adds quasiquoter for multiline strings, interpolation and simple templating. Handy for text generation.@@ -13,6 +13,6 @@ Cabal-Version: >=1.2 Library- Build-Depends: base, syb, template-haskell+ Build-Depends: base >= 4, syb, template-haskell, haskell-src-meta Exposed-modules: Data.String.Interpolation ghc-options: -Wall