2017-04-03 101 views
0

爲什麼我不能在解析器規則中使用setText函數?ANTLR4 setText函數不能使用解析器規則

例如:

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

如果我生成解析器和詞法與此語法解析器不知道功能的setText。 如果我這樣做的詞法規則,沒有任何問題,他正在改變每個標識符「TEST」

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

但他應該只更改標識符時,其類/函數/變量標識符。

回答

0

$Identifierorg.antlr.v4.runtime.Token類型。它是僅提供吸氣劑的接口(f.e. getText())。

下面的工作:

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

謝謝。非常有幫助。 – vizero

+0

不客氣 – Yevgeniy