3

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.

muhmuhten
  • 3,313
  • 1
  • 20
  • 26
jmite
  • 8,171
  • 6
  • 40
  • 81
  • 1
    I don't think this is possible, except possibly via CPP hackery or some other preprocessor. You could write a tool that uses something like haskell-src-exts to generate a value of type `DecsQ` though. – John L Jan 07 '14 at 05:23
  • I guess... it just seems to me that all the `[d|...|]` brackets are doing is taking a string and doing some parsing on it, I don't see why I couldn't feed it an arbitrary string (such as one read from a file). – jmite Jan 07 '14 at 06:15
  • 1
    true, this is a common pain point of Template Haskell. You can't feed it an arbitrary string because the API to do so isn't exposed. What makes it frustrating is that said API is obviously *implemented*, and even available via other quasi-quoters. Just not the built-in Template Haskell ones. Note that `haskell-src-meta` has to go through haskell-src-exts and handle parsing and AST generation itself, instead of just handing off a string. – John L Jan 07 '14 at 19:41

2 Answers2

2

Use the parseDecs in http://hackage.haskell.org/package/haskell-src-meta. [d| lets you capture the right variables a reliably from the place the quote is written |] unlike haskell-src-meta. Maybe that doesn't matter so much in your case?

aavogt
  • 1,308
  • 6
  • 14
2

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

user2407038
  • 14,400
  • 3
  • 29
  • 42