我是ANTLR的新手,并希望做到以下几点:
鉴于下面的语法片段,我有一个可以匹配多个令牌之一的选择规则* comparison_op *。我想做的是编写条件重写规则 - 例如,如果令牌是 DOESNOTENDWITH 做某事,如果它是 DOESNOTCONTAIN 做某事等。
我似乎无法做到这一点。甚至可以这样做吗?当然,我可以为每个条件编写具体的规则,但这似乎也不是最好的方法。
有什么建议吗?
{... 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);
答案 0 :(得分:2)
试试这个:
condition_comparison
: (column_name comparison_op v1=valueExpression)
-> {$comparison_op.start.getType() == DOESNOTBEGINWITH}?
^(LIKE column_name $v1)
-> ^(comparison_op column_name $v1)
;
但是,我没有看到写这样的问题:
condition_comparison
: column_name ( DOESNOTBEGINWITH valueExpression -> ^(LIKE column_name valueExpression)
| DOESNOTENDWITH valueExpression -> ^(DOESNOTENDWITH column_name valueExpression)
| DOESNOTCONTAIN valueExpression -> ^(DOESNOTCONTAIN column_name valueExpression)
)
;