packages feed

QuasiText (empty) → 0.1.2.0

raw patch · 4 files changed

+142/−0 lines, 4 filesdep +attoparsecdep +basedep +haskell-src-metasetup-changed

Dependencies added: attoparsec, base, haskell-src-meta, template-haskell, text

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Mike Ledger++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Mike Ledger nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ QuasiText.cabal view
@@ -0,0 +1,21 @@+-- Initial QuasiText.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++name:                QuasiText+version:             0.1.2.0+synopsis:            A QuasiQuoter for Text.+description:         A QuasiQuoter for interpolating values into Text strings.+homepage:            https://github.com/mikeplus64/QuasiText+license:             BSD3+license-file:        LICENSE+author:              Mike Ledger+maintainer:          eleventynine@gmail.com+category:            Text+build-type:          Simple+cabal-version:       >=1.8++library+  exposed-modules:     Text.QuasiText+  -- other-modules:       +  build-depends:       base ==4.5.*, template-haskell ==2.7.*, haskell-src-meta == 0.5.*, attoparsec ==0.10.*, text ==0.11.*+  hs-source-dirs:      src
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Text/QuasiText.hs view
@@ -0,0 +1,89 @@+{-# LANGUAGE ExistentialQuantification, TemplateHaskell, QuasiQuotes, OverloadedStrings, FlexibleInstances, UndecidableInstances, IncoherentInstances #-}+-- |+-- A simple 'QuasiQuoter' for 'Text' strings. Note that to use 'embed' you need to use the OverloadedStrings extension.++module Text.QuasiText (embed, Chunk (..), getChunks) where+import Language.Haskell.TH.Quote+import Language.Haskell.TH.Syntax+import Language.Haskell.TH+import Language.Haskell.Meta (parseExp)++import Data.Attoparsec.Text+import Data.Text as T (Text, pack, unpack, append, empty, head, strip)++instance Lift Text where+    lift t = litE (stringL (unpack t))++data Chunk = +      T Text+    | E Text+    | V Text+  deriving (Show, Eq)++class Textish a where+    toText :: a -> Text++instance Textish Text where+    toText = id++instance Textish [Char] where+    toText = pack++instance Show a => Textish a where +    toText = pack . show++-- | A simple 'QuasiQuoter' to interpolate 'Text' into other pieces of 'Text'. +-- Expressions can be embedded using $(...) or $..., $... will only work for one-word expressions (best suited for just+-- variable substitution), but $(...) will work for anything..+embed :: QuasiQuoter+embed = QuasiQuoter+    { quoteExp = \s -> +        let chunks = flip map (getChunks (pack s)) $ \c ->+                    case c of+                        -- literal text+                        T t -> [| t |]++                        -- haskell expression+                        E t -> let Right e = parseExp (unpack t) in appE [| toText |] (return e) ++                        -- one-word expression+                        V t | T.head t `elem` ['a'..'z'] -> appE [| toText |] (global (mkName (unpack t)))+                            | otherwise -> let Right e = parseExp (unpack t) in appE [| toText |] (return e)++        in foldr (\l r -> appE (appE [| append |] l) r) [| empty |] chunks++    , quotePat  = error "cannot use this as a pattern"+    , quoteDec  = error "cannot use this as a declaration"+    , quoteType = error "cannot use this as a type"+    }++-- | Create 'Chunk's without any TH.+getChunks :: Text -> [Chunk]+getChunks i = let Right m = parseOnly parser (strip i) in m+  where+    parser = go []++    go s = do+        txt <- takeTill (== '$')+        evt <- choice [expression, var, fmap T takeText]+        end <- atEnd+        if end+            then return $ filter (not . blank) $ reverse (evt:T txt:s)+            else go (evt:T txt:s)++    blank (T "") = True+    blank (E "") = True+    blank (V "") = True+    blank _      = False++    var = do+        char '$'+        val <- takeTill (notInClass "a-zA-Z0-9_")+        return (V val)++    expression = do+        string "$("+        expr <- takeTill (== ')')+        char ')'+        return (E expr)+