我正在尝试重新格式化/重新打印haskell源代码(删除/添加空格,换行符,更改缩进样式......).我发现包haskell-src-exts可以解析和漂亮打印haskell源代码.
使用该函数parseFileWithComments :: ParseMode -> FilePath -> IO (ParseResult (Module, [Comment]))我也得到源代码中包含的注释.现在我想在原始位置打印带有注释的Module/AST,但我找不到能够做到这一点的功能.我只能打印AST.我是否必须自己实施AST的打印和评论,或者这样的库是否已经存在?
澄清以下示例:
文件A.hs:
module A (fn1) where
-- | Haddock-comment
fn1 ::
String ->
String
fn1 _ = "" -- another comment
Run Code Online (Sandbox Code Playgroud)
在ghci中,输入
Prelude Control.Monad.Reader Language.Haskell.Exts> (liftM prettyPrint) $ (liftM fst) $ (liftM fromParseResult) $ parseFileWithComments defaultParseMode "A.hs"`
Run Code Online (Sandbox Code Playgroud)
打印模块源代码(当然没有注释).我可以使用any prettyPrint-function修改源代码格式.
现在我希望能够做到这样的事情:
do
(ast, comments) <- fromParseResult $ parseFileWithComments defaultParseMode "A.hs"
prettyPrintWithComments ast comments
Run Code Online (Sandbox Code Playgroud)
获得原始文件的漂亮印刷版本,包括评论.