Skip to content

Instantly share code, notes, and snippets.

@JustinChristensen
Created July 30, 2023 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinChristensen/5b41ae2f0009729f6fe760d8e8885d8f to your computer and use it in GitHub Desktop.
Save JustinChristensen/5b41ae2f0009729f6fe760d8e8885d8f to your computer and use it in GitHub Desktop.
Text QuasiQuoter
module Quoter where
import Data.Char (isSpace)
import Language.Haskell.TH
import Language.Haskell.TH.Quote
data ParserState =
Start
| Break
| Line
deriving (Show, Eq)
parse :: String -> String
parse s = parse_ Start s
where
parse_ _ [] = []
parse_ Start ('\n':cs) = parse_ Break cs
parse_ Start cs = parse_ Break cs
parse_ Break (c:cs) | isSpace c = parse_ Break cs
| otherwise = c : parse_ Line cs
parse_ Line ('\n':[]) = []
parse_ Line ('\n':cs) = '\n' : parse_ Break cs
parse_ Line (c:cs) = c : parse_ Line cs
quoteExpr :: String -> Q Exp
quoteExpr s = pure $ LitE $ StringL $ parse s
text :: QuasiQuoter
text = QuasiQuoter quoteExpr notSupported notSupported notSupported
where
notSupported _ = fail "Text quoter in this context is not supported"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment