4

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.

Hiro
  • 475
  • 4
  • 11

1 Answers1

4
import Language.Haskell.Exts.QQ
import Language.Haskell.TH.Quote

dd :: QuasiQuoter
dd = quoteFile dec
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Thank you for the response. I found that module in haskell-src-exts-qq package, but I got the error of "Exception when trying to run compile-time code: Unimplemented." I read the [source](http://hackage.haskell.org/packages/archive/haskell-src-exts-qq/0.6.0/doc/html/src/Language-Haskell-Exts-QQ.html#dec), and actually declaration quasiquoter is not implemented. Maybe I could just write a similar code as antiquoteExp in the source? (I am not sure how, though.) – Hiro Aug 25 '12 at 08:38