CDT 词法分析器:获取评论令牌

CDT Lexer: Get Tokens for comments

我想在我的 Eclipse 插件中编写一个可以处理我的工具语言的编辑器。该语言基于 C/C++,带有额外的关键字和注释。我成功地编写了自己的语言 class,它扩展了 GPPLanguage 并利用了相应的 CDT 扩展点。

我的主要问题是我的语言有 keywords inside comments.

在这种语言中经常使用这样的东西:

/** @ctPrint
 * 
 * This is a real comment, describing this block.
 *
 * @author    Sadik     // This looks like a comment, but it's part of the syntax. The compiler will treat this in a special way.
 * @tag       CT-001    // The @tag is part of syntax, the real comment starts with //
 * @result    TRUE      // Again, @result is part of the syntax.
 * 
 * This is a final description.
 */

因此我的编译器(扩展 g++)不将注释中的某些部分视为注释。这就是为什么我想在 eclipse 中突出显示这些部分。

据我从 了解到,在原始源代码的处理过程中第一个激活的是词法分析器。 这是 CDT Lexer:

文档的一部分

In short this class converts line endings (to '\n') and trigraphs
(to their corresponding character), removes line-splices, comments and whitespace other than newline. Returns preprocessor tokens.

因此 Lexer 丢弃了我的部分源代码并且没有对其进行标记。由于 Lexer 被声明为 final,因此它并不是为了扩展它和重用它的功能而设计的。

如果我有一个扩展 CDT 的 GNUCPPParser 的解析器,该解析器将无法 'see' 我的评论,因为它们没有标记。我怎样才能拥有该部分的代币?

虽然注释标记没有以通常的方式传递给解析器(例如,它们不会作为 consume() 的结果出现),但它们并没有被完全丢弃:它们被预处理器保留,并被通过 IASTTranslationUnit.getComments() 作为 IASTComment 个节点可用。您可以使用它来查询评论作为 post 解析步骤,解析它们的内容,然后突出显示与您的特殊标记对应的源范围。