{-# LANGUAGE TemplateHaskell, CPP #-}

-- | QuasiQuoter for non-interpolated strings, texts and bytestrings.
--
-- The "s" quoter contains a multi-line string with no interpolation at all,
-- except that the leading newline is trimmed and carriage returns stripped.
--
-- @
-- {-\# LANGUAGE QuasiQuotes #-}
-- import Data.Text (Text)
-- import Data.String.QQ
-- foo :: Text -- "String", "ByteString" etc also works
-- foo = [s|
-- Well here is a
--     multi-line string!
-- |]
-- @
--
-- Any instance of the IsString type is permitted.
-- 
-- (For GHC versions 6, write "[$s||]" instead of "[s||]".)
--
module Data.String.QQ (s) where
import GHC.Exts (IsString(..))
import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Quote

-- | QuasiQuoter for a non-interpolating IsString literal. The pattern portion is undefined.
s :: QuasiQuoter
s :: QuasiQuoter
s = (String -> Q Exp)
-> (String -> Q Pat)
-> (String -> Q Type)
-> (String -> Q [Dec])
-> QuasiQuoter
QuasiQuoter ((\String
a -> [|fromString a|]) (String -> Q Exp) -> (String -> String) -> String -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
trimLeadingNewline (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
removeCRs)
                 (String -> String -> Q Pat
forall a. HasCallStack => String -> a
error String
"Cannot use q as a pattern")
#if (__GLASGOW_HASKELL__ >= 700)
                 (String -> String -> Q Type
forall a. HasCallStack => String -> a
error String
"Cannot use q as a type")
                 (String -> String -> Q [Dec]
forall a. HasCallStack => String -> a
error String
"Cannot use q as a dec")
#endif
    where
    removeCRs :: String -> String
removeCRs = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r')
    trimLeadingNewline :: String -> String
trimLeadingNewline (Char
'\n':String
xs) = String
xs
    trimLeadingNewline String
xs = String
xs