Created
July 30, 2023 06:26
-
-
Save JustinChristensen/5b41ae2f0009729f6fe760d8e8885d8f to your computer and use it in GitHub Desktop.
Text QuasiQuoter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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