here-1.2.14/0000755000000000000000000000000014462600421010707 5ustar0000000000000000here-1.2.14/here.cabal0000644000000000000000000000213614462600421012620 0ustar0000000000000000name: here version: 1.2.14 synopsis: Here docs & interpolated strings via quasiquotation description: Here docs & interpolated strings via quasiquotation license: BSD3 license-file: LICENSE author: Taylor M. Hedberg maintainer: t@tmh.cc copyright: ©2013 Taylor M. Hedberg, ©2014 Google Inc. homepage: https://github.com/tmhedberg/here category: Data build-type: Simple cabal-version: >=1.10 source-repository head type: git location: git://github.com/tmhedberg/here.git library hs-source-dirs: src exposed-modules: Data.String.Here, Data.String.Here.Interpolated, Data.String.Here.Uninterpolated other-modules: Data.String.Here.Internal build-depends: base >= 4.5 && < 5, haskell-src-meta >= 0.6 && < 0.9, mtl >=2.1 && < 2.4, parsec ==3.1.*, template-haskell ghc-options: -Wall default-language: Haskell2010 test-suite here-test type: exitcode-stdio-1.0 main-is: Spec.hs hs-source-dirs: test build-depends: base >=4.5 && <5, here, hspec, bytestring, text other-modules: Data.String.HereSpec default-language: Haskell2010 here-1.2.14/LICENSE0000644000000000000000000000302714462600421011716 0ustar0000000000000000Copyright (c) 2013, Taylor Hedberg Copyright (c) 2014, Google, Inc. 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 Taylor Hedberg 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. here-1.2.14/Setup.hs0000644000000000000000000000005614462600421012344 0ustar0000000000000000import Distribution.Simple main = defaultMain here-1.2.14/src/0000755000000000000000000000000014462600421011476 5ustar0000000000000000here-1.2.14/src/Data/0000755000000000000000000000000014462600421012347 5ustar0000000000000000here-1.2.14/src/Data/String/0000755000000000000000000000000014462600421013615 5ustar0000000000000000here-1.2.14/src/Data/String/Here.hs0000644000000000000000000000045014462600421015033 0ustar0000000000000000-- | Here docs and string interpolation via quasiquotation module Data.String.Here ( module Data.String.Here.Interpolated , module Data.String.Here.Uninterpolated ) where import Data.String.Here.Interpolated import Data.String.Here.Uninterpolated here-1.2.14/src/Data/String/Here/0000755000000000000000000000000014462600421014500 5ustar0000000000000000here-1.2.14/src/Data/String/Here/Interpolated.hs0000644000000000000000000001203114462600421017463 0ustar0000000000000000{-# LANGUAGE NamedFieldPuns, RecordWildCards, TemplateHaskell, FlexibleContexts #-} {-# OPTIONS_GHC -fno-warn-missing-fields #-} -- | Interpolated here docs module Data.String.Here.Interpolated (i, iTrim, template) where import Control.Applicative hiding ((<|>)) import Control.Monad import Control.Monad.State import Data.Char import Data.Maybe import Data.Monoid import Data.String import Data.Typeable import Language.Haskell.Meta import Language.Haskell.TH import Language.Haskell.TH.Quote import Text.Parsec import Text.Parsec.Prim import Text.Parsec.String import Data.String.Here.Internal data StringPart = Lit String | Esc Char | Anti (Q Exp) data HsChompState = HsChompState { quoteState :: QuoteState , braceCt :: Int , consumed :: String , prevCharWasIdentChar :: Bool } data QuoteState = None | Single EscapeState | Double EscapeState data EscapeState = Escaped | Unescaped -- | Quote a here doc with embedded antiquoted expressions -- -- Any expression occurring between @${@ and @}@ (for which the type must have -- 'Show' and 'Typeable' instances) will be interpolated into the quoted -- string. -- -- Characters preceded by a backslash are treated literally. This enables the -- inclusion of the literal substring @${@ within your quoted text by writing -- it as @\\${@. The literal sequence @\\${@ may be written as @\\\\${@. i :: QuasiQuoter i = QuasiQuoter {quoteExp = quoteInterp} -- | Like 'i', but with leading and trailing whitespace trimmed iTrim :: QuasiQuoter iTrim = QuasiQuoter {quoteExp = quoteInterp . trim} -- | Quote the contents of a file as with 'i' -- -- This enables usage as a simple template engine template :: QuasiQuoter template = quoteDependentFile i quoteInterp :: String -> Q Exp quoteInterp s = either (handleError s) combineParts (parseInterp s) handleError :: String -> ParseError -> Q Exp handleError expStr parseError = error $ "Failed to parse interpolated expression in string: " ++ expStr ++ "\n" ++ show parseError combineParts :: [StringPart] -> Q Exp combineParts = combine . map toExpQ where toExpQ (Lit s) = stringE s toExpQ (Esc c) = stringE [c] toExpQ (Anti expq) = [|toString $expq|] combine [] = stringE "" combine parts = foldr1 (\subExpr acc -> [|$subExpr <> $acc|]) parts toString :: (Show a, Typeable a, Typeable b, IsString b) => a -> b toString x = fromMaybe (fromString $ show x) (cast x) parseInterp :: String -> Either ParseError [StringPart] parseInterp = parse p_interp "" p_interp :: Parser [StringPart] p_interp = manyTill p_stringPart eof p_stringPart :: Parser StringPart p_stringPart = p_anti <|> p_esc <|> p_lit p_anti :: Parser StringPart p_anti = Anti <$> between (try p_antiOpen) p_antiClose p_antiExpr p_antiOpen :: Parser String p_antiOpen = string "${" p_antiClose :: Parser String p_antiClose = string "}" p_antiExpr :: Parser (Q Exp) p_antiExpr = p_untilUnbalancedCloseBrace >>= either fail (return . return) . parseExp p_untilUnbalancedCloseBrace :: Parser String p_untilUnbalancedCloseBrace = evalStateT go $ HsChompState None 0 "" False where go = do c <- lift anyChar modify $ \st@HsChompState {consumed} -> st {consumed = c:consumed} HsChompState {..} <- get let next = setIdentifierCharState c >> go case quoteState of None -> case c of '{' -> incBraceCt 1 >> next '}' | braceCt > 0 -> incBraceCt (-1) >> next | otherwise -> stepBack >> return (reverse $ tail consumed) '\'' -> unless prevCharWasIdentChar (setQuoteState $ Single Unescaped) >> next '"' -> setQuoteState (Double Unescaped) >> next _ -> next Single Unescaped -> do case c of '\\' -> setQuoteState (Single Escaped) '\'' -> setQuoteState None _ -> return () next Single Escaped -> setQuoteState (Single Unescaped) >> next Double Unescaped -> do case c of '\\' -> setQuoteState (Double Escaped) '"' -> setQuoteState None _ -> return () next Double Escaped -> setQuoteState (Double Unescaped) >> next stepBack = lift $ updateParserState (\s -> s {statePos = incSourceColumn (statePos s) (-1)}) >> getInput >>= setInput . ('}':) incBraceCt n = modify $ \st@HsChompState {braceCt} -> st {braceCt = braceCt + n} setQuoteState qs = modify $ \st -> st {quoteState = qs} setIdentifierCharState c = modify $ \st -> st {prevCharWasIdentChar = or [isLetter c, isDigit c, c == '_', c == '\'']} p_esc :: Parser StringPart p_esc = Esc <$> (char '\\' *> anyChar) p_lit :: Parser StringPart p_lit = fmap Lit $ try (litCharTil $ try $ lookAhead p_antiOpen <|> lookAhead (string "\\")) <|> litCharTil eof where litCharTil = manyTill $ noneOf ['\\'] here-1.2.14/src/Data/String/Here/Uninterpolated.hs0000644000000000000000000000115414462600421020032 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-missing-fields #-} -- | Literal, uninterpolated here docs module Data.String.Here.Uninterpolated (here, hereFile, hereLit) where import Language.Haskell.TH import Language.Haskell.TH.Quote import Data.String.Here.Internal -- | Quote a here doc, stripping leading and trailing whitespace here :: QuasiQuoter here = QuasiQuoter {quoteExp = stringE . trim} -- | Quote a here doc literally, with no whitespace stripping hereLit :: QuasiQuoter hereLit = QuasiQuoter {quoteExp = stringE} -- | Splice a file's contents as a here doc hereFile :: QuasiQuoter hereFile = quoteDependentFile hereLit here-1.2.14/src/Data/String/Here/Internal.hs0000644000000000000000000000137614462600421016617 0ustar0000000000000000{-# LANGUAGE NamedFieldPuns #-} {-# OPTIONS_GHC -fno-warn-missing-fields #-} module Data.String.Here.Internal (trim, quoteDependentFile) where import Data.Char import Language.Haskell.TH.Quote import Language.Haskell.TH.Syntax trim :: String -> String trim = trimTail . dropWhile isSpace trimTail :: String -> String trimTail "" = "" trimTail s = take (lastNonBlank s) s where lastNonBlank = (+1) . fst . foldl acc (0, 0) acc (l, n) c | isSpace c = (l, n + 1) | otherwise = (n, n + 1) quoteDependentFile :: QuasiQuoter -> QuasiQuoter quoteDependentFile QuasiQuoter {quoteExp} = QuasiQuoter { quoteExp = \filename -> do addDependentFile filename runIO (readFile filename) >>= quoteExp } here-1.2.14/test/0000755000000000000000000000000014462600421011666 5ustar0000000000000000here-1.2.14/test/Spec.hs0000644000000000000000000000005414462600421013113 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} here-1.2.14/test/Data/0000755000000000000000000000000014462600421012537 5ustar0000000000000000here-1.2.14/test/Data/String/0000755000000000000000000000000014462600421014005 5ustar0000000000000000here-1.2.14/test/Data/String/HereSpec.hs0000644000000000000000000000345114462600421016042 0ustar0000000000000000{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-} module Data.String.HereSpec where import Test.Hspec import qualified Data.Char as Char import qualified Data.ByteString as BS import qualified Data.Text as T import Data.String.Here main :: IO () main = hspec spec spec :: Spec spec = do describe "i quote" $ do it "should interpolate a number" $ do let val1 :: Int val1 = 7878 expect :: String expect = "value is 7878" actual :: String actual = [i|value is ${val1}|] actual `shouldBe` expect -- (from: https://github.com/tmhedberg/here#readme) it "should interpolate a String expression" $ do let foo :: String foo = "foo" expect :: String expect = "\"foo\", when capitalized, is FOO!" actual :: String actual = [i|"foo", when capitalized, is ${map Char.toUpper foo}!|] actual `shouldBe` expect it "should interpolate a number to a ByteString" $ do let val1 :: Int val1 = 3535 expect :: BS.ByteString expect = "value is 3535" actual :: BS.ByteString actual = [i|value is ${val1}|] actual `shouldBe` expect it "should interpolate a number to a Text" $ do let val1 :: Int val1 = 9988 expect :: T.Text expect = "value is 9988" actual :: T.Text actual = [i|value is ${val1}|] actual `shouldBe` expect -- (from: https://github.com/tmhedberg/here#readme) describe "here quote" $ do it "should be here docs" $ do let expect :: String expect = "Hello world,\n\nI am a multiline here doc!" actual :: String actual = [here| Hello world, I am a multiline here doc! |] actual `shouldBe` expect