2
votes

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).

1

1 Answers

2
votes

Right now, your EXTRACTOR has just a single type, making "select" and "choose" pretty much the same. The only way to make a distinction is to to string comparison like you're already doing. If you don't want that, do something like this:

expression
 : ...
 | extractor '(' setElementDefinition ',' expression ')' #setExtractorExp
 | ... 
 ;

extractor
 : SELECT
 | CHOOSE
 ;

SELECT : 'select';
CHOOSE : 'choose';

and in your visitor:

@Override
public Expression visitSetExtractorExp(MyParser.SetExtractorExpContext ctx) {
  if(ctx.extractor().start.getType() == MyLexer.SELECT) { ... }
  else if(ctx.extractor().start.getType() == MyLexer.CHOOSE) { ... }
}

That way you don't have to duplicate (magic) strings.