Interpolation 0.2.2 → 0.2.3
raw patch · 2 files changed
+39/−21 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- Data/String/Interpolation.hs +38/−20
- Interpolation.cabal +1/−1
Data/String/Interpolation.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE QuasiQuotes,TemplateHaskell,DeriveDataTypeable #-}+{-# LANGUAGE QuasiQuotes,TemplateHaskell,DeriveDataTypeable,PatternGuards #-} module Data.String.Interpolation(str,endline,tab) where import Language.Haskell.TH as TH import Language.Haskell.TH.Quote@@ -30,10 +30,11 @@ -- -- Multiline indentation is handled by aligning on smallest un-empty -- line after the first. Neither pattern matching nor nested #-patterns--- are supported. Normal '\\n' style escaping of special characters+-- are supported, however, see example below.+--+-- Normal '\\n' style escaping of special characters -- is intentionally not supported. Please use $endline$ or $"\n"$ --- style instead. (Also, in this beta release $,## and : symbols are--- not escaped in the interpolated expressions.+-- style instead. -- -- As an example, let's plot set of vectors with gnuplot: -- @@ -41,16 +42,16 @@ -- plotVecs :: [(String,[Double])] -> String -- plotVecs vs = -- [$str| \#\# Plot multiple vectors--- plot \#(n,_) in vs:'-' with lines lw 5 title $n$ |, \#+-- plot \#(n,_) in vs:'-' with lines lw 5 title $:n$ |, \# -- \#d in map snd vs:$singleVec d$$endline$e$endline$\# |] -- where -- singleVec n = [$str|\#(e,i) in zip n [1..]: $:i$ $:e$|$endline$\#|] -- @ -- -- @--- *Gnuplotter> plotVecs [("A",[1..5]),("B",[2..6])]+-- *Gnuplotter> plotVecs [(\"A\",[1..5]),(\"B\",[2..6])] -- \# Plot multiple vectors--- plot '-' with lines lw 5 title A , '-' with lines lw 5 title B +-- plot '-' with lines lw 5 title \"A\" , '-' with lines lw 5 title \"B\" -- 1 1.0 -- 2 2.0 -- 3 3.0@@ -70,6 +71,9 @@ str :: QuasiQuoter str = QuasiQuoter quoteExprExp undefined +debugStr :: QuasiQuoter +debugStr = QuasiQuoter (stringE) undefined+ -- ** Predefined strings -- | End of the line @@ -135,9 +139,14 @@ parParse ('$':s) = Var (takeWhile (/='$') s) :parParse (drop 1 (dropWhile (/='$') s)) -parParse ('#':s) = let (bind,exprS) = break (==':') s- (hasSep,expr,sepS) = takeRep $ drop 1 exprS- (sep,restS) = break (=='#') $ sepS+parParse ('#':s) = let (bind,exprS) = escapingBreak (':') + ("Repetition <"++s++"> missing body") + $ s+ (hasSep,expr,sepS) = takeRep + ("Repetition <"++s++"> missing #")$ drop 1 exprS+ (sep,restS) = escapingBreak ('#')+ ("Repetition <"++s++"> missing #")+ $ sepS rest = drop 1 restS (varNameS,listNameS) = break (=="in") . words $ bind@@ -157,21 +166,30 @@ notIn :: (Eq a) => [a] -> a -> Bool notIn s x =not $ elem x s -takeRep :: String -> (Bool,String,String)-takeRep x = takeRep' x []--takeRep' :: String -> String -> (Bool,String,String)-takeRep' [] _ = error "Repetition template does not end"-takeRep' ('|':'|':s) acc = takeRep' s ('|':acc)-takeRep' ('|':s) acc = (True,reverse acc,s)-takeRep' ('#':s) acc = (False,reverse acc,s)-takeRep' (r:s) acc = takeRep' s (r:acc)+takeRep :: String -> String -> (Bool,String,String)+takeRep e x = takeRep' x []+ where+ takeRep' :: String -> String -> (Bool,String,String)+ takeRep' [] _ = error $ e+ takeRep' ('|':'|':s) acc = takeRep' s ('|':acc)+ takeRep' ('|':s) acc = (True,reverse acc,s)+ takeRep' ('#':s) acc = (False,reverse acc,s)+ takeRep' (r:s) acc = takeRep' s (r:acc) +escapingBreak :: (Eq t) => t -> [Char] -> [t] -> ([t], [t])+escapingBreak s e st = eBreak [] st + where + eBreak acc (a:b:c) | a==s&&b==s = eBreak (s:acc) c+ | a==s = (reverse acc,b:c)+ | otherwise = eBreak (a:acc) (b:c)+ eBreak acc (a:c) | a==s = (reverse acc,c) + | otherwise = eBreak (a:acc) c+ eBreak _ [] = error e -- Normalize the indentation to match second line norming :: String -> String-norming = unlines . norming' . lines+norming = intercalate "\n" . norming' . lines norming':: [String] -> [String] norming' [] = []
Interpolation.cabal view
@@ -1,5 +1,5 @@ Name: Interpolation-Version: 0.2.2+Version: 0.2.3 Description: This package adds quasiquoter for multiline strings, interpolation and simple templating. It can handle repetition templates which makes it