Catching (and keeping) all comments with ANTLR

前端 未结 6 1192
抹茶落季
抹茶落季 2020-12-10 19:49

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

6条回答
  •  没有蜡笔的小新
    2020-12-10 20:01

    Section 12.1 in "The Definitive Antlr 4 Reference" shows how to get access to comments without having to sprinkle the comments rules throughout the grammar. In short you add this to the grammar file:

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

    Then for your comments rules do this:

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

    Then in your code ask for the tokens through the getHiddenTokensToLeft/getHiddenTokensToRight and look at the 12.1 section in the book and you will see how to do this.

提交回复
热议问题