Search code examples
haskelltemplate-haskellelm

Read module from file in TemplateHaskell


So, most quasi-quoters for TemplateHaskell have an option where you can read in a quasi-quoted string from a file, instead of typing it in the brackets like [quoter|... some code ...|] .

I'm wondering, is there an equivalent of this for the normal TemplateHaskell quoting brackets? Namely [e|...|] [t|...|] and [d|...|].

I'm writing a translator which converts Haskell code to Elm, while simultaneously keeping my Haskell declarations, so that I can use Haskell for server-side programming and Elm for Javascript generation. To do this translation, I'd like to be able to read in a Haskell module, traverse the ADT using TemplateHaskell, and generate the translated code as a string, while additionally injecting the Haskell declarations from the module into the code.

I've got this working, but only when I write my Haskell code as [d|some decs|]. I'd like to be able to do the same, but read from an external file.


Solution

  • haskell-src-meta is a library for parsing haskell source to a template haskell AST. Specifically, if you want to read a string from a file, parse it as a Haskell module, you use Language.Haskell.Meta.Parse.parseHsModule