解析haskell保留注释/格式 [英] Parsing haskell preserving comments / formatting

查看:338
本文介绍了解析haskell保留注释/格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些源代码转换(自动导入列表清理),我想保留注释和格式。我听说了一些关于这样做的解析器的东西,我认为是ghc解析器。



看起来我可以用hs-src-通过从文件中提取内容来扩展Language.Haskell.Exts.Annotate及其SrcSpans。我认为SrcsSpanInfo只涵盖解析的部分,但我理论上可以通过查看它们之间的内容来理解评论。但没有详细记录,也没有我能找到的辅助函数,而且看起来很麻烦。没有简单的方法可以打印出解析后的表达式,包括格式和注释。所以我认为这并不意味着以这种方式使用,只是为了突出显示文件中的代码或其他内容。我的印象是,作者的意图是使用注释来支持这一点,但从来没有解决它。



看起来yi和leksah都不这样做。我觉得像HaRe可能,但它不是超级文件。在这里有一个haskell解析器吗?

解决方案 haskell-src-exts 最近得到了对保留评论,它已经记录 src span 。我不确定是否支持漂亮的打印,但你可能会得到这个工作。



GHC解析器也做类似的事情。


I want to do some source code transformation (automatic import list cleanup) and I'd like to preserve comments and formatting. I heard some stuff on and off about parsers that do this, I think for the ghc parser.

It looks like I might be able to do this with hs-src-exts Language.Haskell.Exts.Annotate and its SrcSpans by pulling things out of the file. I think the SrcsSpanInfo only covers the parsed parts, but I could theoretically figure out the comments by looking at what's in between. But it's not documented in much detail, and there are no helper functions I can find, and it looks like a hassle, e.g. there's no easy way to print out a parsed expression including formatting and comments. So I think it's not meant to be used in this way, it's just so you can highlight code in the file or something. My impression is that the author meant to use annotations to support this, but never got around to it.

It looks like neither yi nor leksah do this. I feel like HaRe might, but it's not super documented. Is there a haskell parser out there that does this?

解决方案

The haskell-src-exts recently got support for preserving comments, and it already records src spans. I'm not sure if pretty printing is supported, but you could probably get that working.

The GHC parser also does similar things.

这篇关于解析haskell保留注释/格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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