我可以在 Groovy 中使用 AST 转换来扩展其语法吗?
我已经看到了如何使用 Groovy AST 转换来扩展语言的示例,例如在方法调用之前和之后进行记录,如下所示 此处。 但是,是否也可以使用这个框架来扩展语言本身的语法? 例如,如果我希望能够解析以下内容并将其转换为 AST,然后生成一组语句,该怎么办:
newClassKeyword C { /* something here */ }
I have seen examples of how you can use the Groovy AST transformations to extend the language, e.g. to log before and after a method call as shown here. However, would it also be possible to use this framework to extend the syntax of the language itself? For instance, what if I wanted to be able to parse and transform the following into an AST and then generate a set of statements:
newClassKeyword C { /* something here */ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 Groovy 专家,但我一直在研究 1.6 版本中出现的一些新框架,我特别感兴趣的是 Spock. 这可能正是您正在寻找的,因为他们使用 AST 转换来定义用于测试的整个规范语言。
我忘了补充一点,InfoQ 有一篇文章概述了 Groovy 的新功能1.6. Guillaume LaForge 撰写了这篇文章,并在其中描述了一些有关 AST 的内容。
I'm no Groovy expert, but I have been looking at some of the new frameworks that have come out of the 1.6 release, and of particular interest to me is Spock. This is probably exactly what you're looking for, as they use AST transformations to define an entire specification language for testing.
I forgot to add that InfoQ has an article that outlines the new features of Groovy 1.6. Guillaume LaForge wrote the article, and in it he describes a little bit about the AST.
您无法使用 AST 转换来扩展 Groovy 的语法,但您可以为现有语法提供全新的语义。 因为 Groovy 的语法非常灵活,所以这可以让您走得更远。
You cannot extend Groovy's syntax with AST transformations, but you can give completely new semantics to existing syntax. Because Groovy's syntax is quite flexible, this can get you pretty far.