简体   繁体   English

ANTLR4 setText函数不适用于解析器规则

[英]ANTLR4 setText function not working with parser rules

Why can not I use setText function in parser rules ? 为什么不能在解析器规则中使用setText函数?

For example: 例如:

normalClassDeclaration
:   classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? classBody
    {
    $Identifier.setText("TEST");
    }
;

If I generate the parser and lexer with this grammar, the parser doesn't know the function setText. 如果我使用此语法生成解析器和词法分析器,则解析器将不知道setText函数。 If I do this in the lexer rules, there is no problem and he is changing every identifier to "TEST" 如果我在词法分析器规则中执行此操作,则没有问题,他将每个标识符更改为“ TEST”

Identifier
:   JavaLetter JavaLetterOrDigit*
{
setText("V");
}
;

But he should only change the identifier when its a class/function/variable identifier. 但是他只应在标识符为类/函数/变量标识符时更改标识符。

$Identifier is of type org.antlr.v4.runtime.Token . $Identifier的类型为org.antlr.v4.runtime.Token It is a interface providing only getter (fe getText() ). 它是仅提供getter(fe getText() )的接口。

following will work: 以下将工作:

normalClassDeclaration
:   classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? classBody
    {
    ((CommonToken)$Identifier).setText("TEST");
    }
;

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

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