I have specified the following ANTLR grammar:
expression: ...
| extractor=EXTRACTOR '(' setElementDefinition ',' expression ')' #setExtractorExp
| ... ;
EXTRACTOR: 'select'
| 'choose' ;
I would like to know which type of extraction I am dealing with when parsing this expression. One way of doing it is by comparing the extractor field with a string containing the extractor type, like this:
@Override
public Expression visitSetExtractorExp(MyParser.SetExtractorExpContext ctx) {
if(ctx.extractor.getText().equals("select")) { ... }
}
But I don't like to duplicate the names of the extractors here, in case I choose to change the names of the extractors later. So is there a way to access the lexer tokens?
I am imagining something like if(ctx.extractor == MyLexer.EXTRACTOR.choose)
.