Antlr:创建组合运算符

Antlr: Create combined operators

我正在使用 antlr 来解析 sql 条件查询,例如:

[where] Field1 = "Param1" and Field2 = "Param2"

对于这种情况,我有一个 antlr 条件:

andExpr
    :   inExpr
        (
            AND^
            inExpr
        )*
    ;

inExpr      
    :    (eqcompareExpr)
        (
            IN^
            args
        )?  
    ;

eqcompareExpr
    :   (compareExpr)
        (
            (EQUAL^|NOTEQ^|IS^|LIKE^)
            (compareExpr)
        )?
    ;

compareExpr
    :   addExpr
        (
            (LESS^|MORE^|LESSEQ^|MOREEQ^)
            addExpr 
        )?
    ;

和像这样的代币:

LIKE    :   "like"
    ;
AND :   "and"
    ;
OR  :   "or"

如何更改 antlr 代码以使用 'contains' 添加组合条件,例如:

[where] Field1 = "Param1" and Contains (Field2, "Param2")

也可以这样查询:

[where] Contains (Field2, "Param2")

[where] Field1 = "Param1" or Contains (Field2, "Param2")

也许以下规则可以满足您的需求

andExpr
    :   inOrContainsExpr
        (
            AND
            inOrContainsExpr
        )*
    ;

inOrContainsExpr:   inExpr | containsExpr;

inExpr
    :   (eqcompareExpr) ( IN^ args ) ?
    ;

containsExpr
    :   CONTAINS^ args
    ;

等等。 希望这有帮助