简体   繁体   English

添加注释到Antlr Java 8语法

[英]Adding Comments to Antlr Java 8 grammar

I wish to have 'comments' be recorded into the AST (not to have anything done with them, but to be stored for later reproduction) when using the Java8 grammar for antlr. 我希望在将AST8语法用于antlr时,将“注释”记录到AST中(不要对它们进行任何操作,而是存储以供以后再现)。 https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4

IE: I want to read a java source code file into a AST then output it again eventually but to include the IE:我想将一个java源代码文件读入AST,然后最终再次输出,但要包括

I am wondering if there is a -simple- adjustment to the grammar that would allow this... (or if my naive idea of having to integrate 'comments' into each expression is the sad truth of the matter...) and if so... what is it? 我想知道是否有一个简单的语法调整可以允许这个...(或者如果我的天真想法必须将“评论”整合到每个表达式中,这是事情的悲惨事实......)如果那么......它是什么?

COMMENT
    :   '/*' .*? '*/' -> skip
    ;

LINE_COMMENT
    :   '//' ~[\r\n]* -> skip
    ;

from what I can see, you -can- keep the comments in their own 'channel' via: 从我所看到的,你可以通过以下方式将评论保存在他们自己的“频道”中:

adding this to the grammar: 将其添加到语法中:

@lexer::members {
    public static final int WHITESPACE = 1;
    public static final int COMMENTS = 2;
}

and changing to this: 并改为:

COMMENT
    : '/*' .*? '*/' -> channel(COMMENTS)
    ;

LINE_COMMENT
    : '//' ~[\r\n]* -> channel(COMMENTS)
    ;

from: https://stackoverflow.com/a/17960734/2801237 来自: https//stackoverflow.com/a/17960734/2801237

the official 'documentation'(actually it looks like his book is really the 'real' documentation) mentions this briefly: 官方的“文档”(实际上看起来他的书真的是“真正的”文档)简要地提到了这一点:

https://github.com/antlr/antlr4/blob/master/doc/grammars.md https://github.com/antlr/antlr4/blob/master/doc/grammars.md

and the (one version of the) book says 这本书的(一个版本)说

you can send different tokens to the parser on different channels. 您可以在不同的通道上向解析器发送不同的令牌。 For example, you might want whitespace and regular comments on one channel and Javadoc comments on another when parsing Java 例如,在解析Java时,您可能希望在一个通道上使用空格和常规注释,在另一个通道上使用Javadoc注释


This is the warnings from the antlr generation that I get: (I read that you can ignore these, but... there might be a better way to do this) 这是我得到的antlr一代的警告:(我读过你可以忽略这些,但是......可能有更好的方法来做到这一点)

warning(155): java8comments.g4:1725:35: rule WS contains a lexer command with an unrecognized constant value; warning(155):java8comments.g4:1725:35:规则WS包含带有无法识别的常量值的词法分析器命令; lexer interpreters may produce incorrect output lexer解释器可能会产生不正确的输出

warning(155): java8comments.g4:1729:33: rule DOC_COMMENT contains a lexer command with an unrecognized constant value; warning(155):java8comments.g4:1729:33:规则DOC_COMMENT包含带有无法识别的常量值的词法分析器命令; lexer interpreters may produce incorrect output lexer解释器可能会产生不正确的输出

warning(155): java8comments.g4:1733:31: rule COMMENT contains a lexer command with an unrecognized constant value; warning(155):java8comments.g4:1733:31:规则COMMENT包含带有无法识别的常量值的词法分析器命令; lexer interpreters may produce incorrect output lexer解释器可能会产生不正确的输出

warning(155): java8comments.g4:1737:31: rule LINE_COMMENT contains a lexer command with an unrecognized constant value; warning(155):java8comments.g4:1737:31:规则LINE_COMMENT包含带有无法识别的常量值的词法分析器命令; lexer interpreters may produce incorrect output lexer解释器可能会产生不正确的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM