漂亮的打印haskell带注释的源代码 [英] Pretty-print haskell source code with comments

查看:87
本文介绍了漂亮的打印haskell带注释的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新格式化/重印haskell源代码(删除/添加空格,换行符,改变缩进样式...)。我找到了 haskell-src-exts 软件包,它可以解析和漂亮的打印haskell源代码。



使用函数 parseFileWithComments :: ParseMode - > FilePath - > IO(ParseResult(Module,[Comment]))我也收到包含在源代码中的注释。现在我想在原始位置打印带有注释的Module / AST,但是我找不到可以实现这一点的功能。我只能漂亮地打印AST。我必须自己实现AST的打印和评论吗?或者这样的库已经存在?



澄清以下示例:



文件A.hs:

 模块A(fn1)其中

- | Haddock评论
fn1 ::
字符串 - >
字符串
fn1 _ = - 另一条评论

在ghci中,打字

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



<打印模块源代码(当然,没有评论)。我可以使用任何 prettyPrint - 函数来修改源代码格式。



现在我希望能够做到像这样:

  do 
(ast,comments)< - fromParseResult $ parseFileWithComments defaultParseModeA.hs
prettyPrintWithComments ast评论

可以获得原始文件的漂亮版本,包括评论。

解决方案

使用模块的 Annotated 版本,例如 Language.Haskell.Exts.Annotated vs Language.Haskell.Exts


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.

解决方案

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

这篇关于漂亮的打印haskell带注释的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆