summaryrefslogtreecommitdiff
path: root/src/Language
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2024-12-03 20:41:40 +0100
committerEugen Wissner <belka@caraus.de>2024-12-03 20:41:40 +0100
commit131576e56cb4981938320569dfcf1ca814d8a42a (patch)
tree5e46d13f22aa9a0bdd672f3d24e1f8961de5f010 /src/Language
parentc95a5fcd610e3fb816058e8341caf774686f9c82 (diff)
downloadgraphql-spice-131576e56cb4981938320569dfcf1ca814d8a42a.tar.gz
Move gql to Language.GraphQL.TH
Diffstat (limited to 'src/Language')
-rw-r--r--src/Language/GraphQL/Class.hs1
-rw-r--r--src/Language/GraphQL/TH.hs49
2 files changed, 50 insertions, 0 deletions
diff --git a/src/Language/GraphQL/Class.hs b/src/Language/GraphQL/Class.hs
index 6e8b080..67ae695 100644
--- a/src/Language/GraphQL/Class.hs
+++ b/src/Language/GraphQL/Class.hs
@@ -485,6 +485,7 @@ stripIndentation code = reverse
-- | Removes leading and trailing newlines. Indentation of the first line is
-- removed from each line of the string.
+{-# DEPRECATED gql "Use Language.GraphQL.TH.gql instead" #-}
gql :: QuasiQuoter
gql = QuasiQuoter
{ quoteExp = pure . LitE . StringL . stripIndentation
diff --git a/src/Language/GraphQL/TH.hs b/src/Language/GraphQL/TH.hs
new file mode 100644
index 0000000..6e20586
--- /dev/null
+++ b/src/Language/GraphQL/TH.hs
@@ -0,0 +1,49 @@
+{- This Source Code Form is subject to the terms of the Mozilla Public License,
+ v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ obtain one at https://mozilla.org/MPL/2.0/. -}
+
+module Language.GraphQL.TH
+ ( gql
+ ) where
+
+import Language.Haskell.TH
+ ( Exp(..)
+ , Lit(..)
+ )
+import Language.Haskell.TH.Quote (QuasiQuoter(..))
+
+stripIndentation :: String -> String
+stripIndentation code = reverse
+ $ dropWhile isLineBreak
+ $ reverse
+ $ unlines
+ $ indent spaces <$> lines' withoutLeadingNewlines
+ where
+ indent 0 xs = xs
+ indent count (' ' : xs) = indent (count - 1) xs
+ indent _ xs = xs
+ withoutLeadingNewlines = dropWhile isLineBreak code
+ spaces = length $ takeWhile (== ' ') withoutLeadingNewlines
+ lines' "" = []
+ lines' string =
+ let (line, rest) = break isLineBreak string
+ reminder =
+ case rest of
+ [] -> []
+ '\r' : '\n' : strippedString -> lines' strippedString
+ _ : strippedString -> lines' strippedString
+ in line : reminder
+ isLineBreak = flip any ['\n', '\r'] . (==)
+
+-- | Removes leading and trailing newlines. Indentation of the first line is
+-- removed from each line of the string.
+gql :: QuasiQuoter
+gql = QuasiQuoter
+ { quoteExp = pure . LitE . StringL . stripIndentation
+ , quotePat = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a pattern)"
+ , quoteType = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a type)"
+ , quoteDec = const
+ $ fail "Illegal gql QuasiQuote (allowed as expression only, used as a declaration)"
+ }