使用 ANTLR 捕获(并保留)所有评论 [英] Catching (and keeping) all comments with ANTLR

查看:24
本文介绍了使用 ANTLR 捕获(并保留)所有评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ANTLR 中编写语法,将 Java 源文件解析为 AST 以供以后分析.与其他解析器(如 JavaDoc)不同,我试图保留所有注释.这是很难的注释,可以在代码中的任何地方使用.如果源代码中某处的注释与语法不匹配,则 ANTLR 无法完成文件的解析.

I'm writing a grammar in ANTLR that parses Java source files into ASTs for later analysis. Unlike other parsers (like JavaDoc) I'm trying to keep all of the comments. This is difficult comments can be used literally anywhere in the code. If a comment is somewhere in the source code that doesn't match the grammar, ANTLR can't finish parsing the file.

有没有办法让 ANTLR 自动将它找到的任何注释添加到 AST 中?我知道词法分析器可以使用 {skip();} 或通过将文本发送到隐藏频道来简单地忽略所有评论.设置了这两个选项中的任何一个后,ANTLR 都可以毫无问题地解析文件.

Is there a way to make ANTLR automatically add any comments it finds to the AST? I know the lexer can simply ignore all of the comments using either {skip();} or by sending the text to the hidden channel. With either of those options set, ANTLR parses the file without any problems at all.

欢迎提出任何想法.

推荐答案

有没有办法让 ANTLR 自动将它找到的任何注释添加到 AST 中?

Is there a way to make ANTLR automatically add any comments it finds to the AST?

不,您必须在整个语法中添加额外的comments 规则,以说明所有有效的评论可能出现的位置:

No, you'll have to sprinkle your entire grammar with extra comments rules to account for all the valid places comments can occur:

...

if_stat
 : 'if' comments '(' comments expr comments ')' comments ...
 ;

...

comments
 : (SingleLineComment | MultiLineComment)*
 ;

SingleLineComment
 : '//' ~('\r' | '\n')*
 ;

MultiLineComment
 : '/*' .* '*/'
 ;

这篇关于使用 ANTLR 捕获(并保留)所有评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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