如何解析groovy代码? [英] How to parse groovy code?

查看:97
本文介绍了如何解析groovy代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于groovy擅长解析几乎任何东西,因此搜索如何解析常规代码并不会揭示任何好结果 - 所以我希望SO社区能够提供帮助: - )

我想为grails域类编写某种(图形)编辑器,但不想重新发明轮子。



这很容易通过反射来检查域类,但我想更进一步 - 我想修改代码并将其写回为域类文件。



一个问题在于反射(也是AST的afaik)会抛弃所有的注释和格式(格式不是大问题,我可以打印文件)...

任何想法,我可以找到一个groovy解析器,我可以建立我的想法?

解决方案

你应该有一个密集的看看 GroovyDocTool 课程RCE。 GroovyDoc使用GroovyLexer和GroovyRecognizer来解析类文本(生成GroovyDoc HTML文档文件,类似于JavaDoc HTML文件),并利用这些类从给定的AST创建AST文件源代码文本。

生成的AST和源代码文本用于遍历类结构(参见SimpleGroovyClassDocAssembler ),提取注释和各种其他元数据以填充GroovyDoc特定数据结构。


Since groovy is good at parsing nearly anything, a search on how to parse groovy code will not reveal any good results - so I hope that the SO community is able to help :-)

I would like to write some kind of (graphical) editor for grails domain classes, but don't want to reinvent the wheel.

It's easy to inspect the domain class through reflection, but I would like to go one step further - I want to modify the code and write it back as a domain class file.

One problem is that reflection (and afaik the AST too) will throw away all comments and formatting (formatting is not the big problem, I could pretty print the file)...

Any ideas where I can find a groovy parser upon which I can build my ideas?

解决方案

You should have an intensive look at the GroovyDocTool class source.

GroovyDoc uses the GroovyLexer and GroovyRecognizer to parse class texts (to generate GroovyDoc HTML documentation files, similar to JavaDoc HTML files) and utilizes these classes to create an AST from the given source text.

The generated AST and the source code text are used to walk through the class structure (see SimpleGroovyClassDocAssembler), extract comments and various other meta-data to fill the GroovyDoc specific data structures.

这篇关于如何解析groovy代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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