5

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.

Chris Smith
  • 2,615
  • 19
  • 18
  • I imagine you want to retrieve the `DynFlags` in effect. Unfortunately, I can't help you actually do that. – dfeuer Nov 20 '19 at 20:00
  • You could try getting the `ms_hspp_opts` from a `ModSummary`. I'm not sure if those would be the right options, but they might be! – dfeuer Nov 20 '19 at 20:04
  • 1
    I believe those two pragmas, at least, are required to be at the very top of the file. I'd just parse them myself. If you want `{-# OVERLAPPABLE #-}` and stuff that would be a different story... – luqui Nov 21 '19 at 05:01
  • Thanks! I spent quite a few hours tonight trying to make ghc-exactprint work, but wasn't able. I threw something together in the spirit of luqui's comment, and I think it'll be good enough. – Chris Smith Nov 22 '19 at 04:11
  • @luqui, what if there's CPP around language options? Or if they're `#include`d? Or if they come in from Cabal? This sounds a bit fishy. – dfeuer Nov 22 '19 at 04:18

0 Answers0