Chris Smith
Chris Smith

Reputation: 2645

How do I write code to modify the AST of a Haskell source file without losing the pragmas?

I need to make some changes to an existing Haskell source file, and write out the modified source code. I'd like to use the GHC API for this, so that the result is consistent with current Haskell. My current approach, which is here, is something like:

mod <- parseTheSource  -- omitted, but I'm sure this works
printForUser dynFlags outHandle neverQualify $ ppr (transform mod)

This works... almost! It strips out LANGUAGE and OPTIONS_GHC pragmas, so that sometimes files that compiled earlier will fail after the round trip.

Is there a better way to do this?

I noticed that ghc-exactprint looks very relevant, but I can't figure out how to make it work nicely. It looks like it's tied to a GHC session, which is likely to initialize DynFlags from the command line, and a lot of other junk that I don't want. If that's the right direction, I could use some help distilling what to do, and especially using my own DynFlags that I can already construct and not leaking GHC command line processing and such into the code.

Upvotes: 5

Views: 87

Answers (0)

Related Questions