9

I'm trying to reformat/reprint haskell source code (remove/add whitespace, linebreaks, change indention style...). I've found the package haskell-src-exts which can parse and pretty-print haskell source code.

Using the function parseFileWithComments :: ParseMode -> FilePath -> IO (ParseResult (Module, [Comment])) I also get the comments included in the source code. Now I want to print the Module/AST with the comments at the original positions, but I cannot find a function which will do that. I can only pretty-print the AST. Do I have to implement printing of the AST plus the comments myself or does such a library already exist?

To clarify consider following example:

file A.hs:

module A (fn1) where

-- | Haddock-comment
fn1 ::
    String ->
    String
fn1 _ = "" -- another comment

In ghci, typing

Prelude Control.Monad.Reader Language.Haskell.Exts> (liftM prettyPrint) $ (liftM fst) $ (liftM fromParseResult) $ parseFileWithComments defaultParseMode "A.hs"`

prints the module source code (without the comments, of course). I can use any prettyPrint-function to modify the source code formatting.

Now I want to be able to do something like this:

do
    (ast, comments) <- fromParseResult $ parseFileWithComments defaultParseMode "A.hs"
    prettyPrintWithComments ast comments

to get a pretty-printed version of the original file including the comments.

haja
  • 302
  • 2
  • 8

2 Answers2

6

Use the Annotated versions of the modules, e.g. Language.Haskell.Exts.Annotated vs Language.Haskell.Exts.

ivanm
  • 3,927
  • 22
  • 29
  • That's quite helpful, but not exactly what I wanted. I still want to be able to reformat the actual source code with a configured pretty-print `Style` and `PPHsMode`. – haja Feb 22 '12 at 12:14
  • @haja: why can't you? It's still an instance of haskell-src-ext's `Pretty` class... – ivanm Feb 23 '12 at 00:40
  • but then again, how can the original comments be included in the pretty-print output? I can't find a function which pretty-prints the ast _including_ comments. – haja Feb 23 '12 at 07:53
  • @haja: OK, my mistake: I thought that comments were included in the AST. However, does the `Language.Haskell.Exts.Annotated.ExactPrint` module solve your issue? – ivanm Feb 23 '12 at 11:19
  • sadly not, since I still want to reformat the source code, which is not possible with `Language.Haskell.Exts.Annotated.ExactPrint`. It only prints the source code exactly as it was before parsing. – haja Feb 24 '12 at 11:54
  • 1
    @haja From that module: *"The input is a (semi-concrete) abstract syntax tree, annotated with exact source information"* - doesn't that mean you can modify that *format* syntax tree and `ExactPrint` will then print it according to your formatting changes? – nh2 Aug 22 '12 at 01:20
  • @nh2 I'm afraid I don't really understand what you mean. There is no _format_ syntax tree, just an _abstract syntax tree_. The format is applied to the printed output, not to the ast. The ast stays the same. – haja Sep 11 '12 at 12:27
  • @haja I was suggesting the following: You call e.g. `exactPrint m comments` where `m` might be `Module SrcLoc`. You could transform `m` so that it looks like what you want, modifying the `SrcLoc`s; same with the `comments`. Yes, that is probably tedious and complicated. I emailed the maintainer of haskell-src-exts (see hackage page), and apparently he is interested in making tasks like this more convenient. I suggested an intermediate parse tree with all information (comments and whitespace) present. Perhaps you should join the discussion. – nh2 Sep 11 '12 at 16:46
3

Depending on what kind of pretty printing do you want to do, you might want to take a look at the hscolour package, which is used to colorize Haskell source code into various output formats.

In particular, the module Language.Haskell.HsColour.Classify contains a Haskell tokenizer which preserves whitespace and comments, which might serve as a good starting point.

shang
  • 24,642
  • 3
  • 58
  • 86
  • thanks, but I'm not looking for this kind of pretty-printing. I updated my question to clarify what kind of pretty-printing I want. – haja Feb 23 '12 at 07:55