How to use ANTLR lexer token value in rewrite conditional rule

I'm kind of new to ANTLR and would like to do the following:

Given the grammar snippet below, I have a * compare_op * selection rule that can match one of many tokens. What I would like to do is write conditional rewrite rules - for example, if the DOESNOTENDWITH token does something, if DOESNOTCONTAIN does something, etc.

I just can't get it right. Is it possible to do this? Of course, I can write specific rules for each condition, but this is also not the best way.

Any suggestions?


{... snipped ...}

DOESNOTBEGINWITH        : 'does not begin with';
DOESNOTENDWITH          : 'does not end with';
DOESNOTCONTAIN          : 'does not contain';

comparison_op           : DOESNOTBEGINWITH | DOESNOTENDWITH | DOESNOTCONTAIN
condition_comparison    : (column_name comparison_op v1=valueExpression) 
                        ->  {$comparison_op.text == $DOESNOTBEGINWITH.text}?  
                                    ^(LIKE column_name $v1)
                        ->          ^(comparison_op column_name $v1);

      

+3


source to share


1 answer


Try the following:

condition_comparison
 : (column_name comparison_op v1=valueExpression) 
    -> {$comparison_op.start.getType() == DOESNOTBEGINWITH}? 
       ^(LIKE column_name $v1)
    -> ^(comparison_op column_name $v1)
 ;

      



However, I don't see any problem writing this type:

condition_comparison
 : column_name ( DOESNOTBEGINWITH valueExpression -> ^(LIKE column_name valueExpression)
               | DOESNOTENDWITH valueExpression   -> ^(DOESNOTENDWITH column_name valueExpression)
               | DOESNOTCONTAIN valueExpression   -> ^(DOESNOTCONTAIN column_name valueExpression)
               )
 ;

      

+2


source







All Articles