I wanted to read an external Haskell source file for compile-time AST manipulation. How can I do that? I tried something like the following, but it didn't compile with the error message "TH.hs:15:12: Declaration splices are not permitted inside declaration bracket".
--------
-- TH.hs
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
module TH where
import Language.Haskell.TH
import Language.Haskell.TH.Quote
dd :: QuasiQuoter
dd = QuasiQuoter undefined undefined undefined ddDec
ddDec file_name = do
file_cts <- runIO (readFile file_name)
-- runQ [d| dummy = 0 |] -- This can compile.
runQ [d| file_cts |] -- This does not compile.
--------
-- main.hs
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import TH
[dd|input.hs|]
--------
-- input.hs
test = putStrLn "Hello."
--------
I also tried haskell-src-exts package, but this package seems to only parse and does not resolve identifiers and type checking. So I thought TH is a better choice.