简体   繁体   中英

ANTLR4 setText function not working with parser rules

Why can not I use setText function in parser rules ?

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. If I do this in the lexer rules, there is no problem and he is changing every identifier to "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 . It is a interface providing only getter (fe getText() ).

following will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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