user2455111
user2455111

Reputation: 254

Antlr: Create combined operators

I'm using antlr for parse sql condition queries like:

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

for this case I have an antlr condition:

andExpr
    :   inExpr
        (
            AND^
            inExpr
        )*
    ;

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

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

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

and tokens like:

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

How do I change the antlr code for add combined conditions with 'contains' like:

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

Also the queries can be like this:

[where] Contains (Field2, "Param2")

or

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

Upvotes: 0

Views: 61

Answers (1)

gtosto
gtosto

Reputation: 1341

Maybe the following rules can satisfy your needs

andExpr
    :   inOrContainsExpr
        (
            AND
            inOrContainsExpr
        )*
    ;

inOrContainsExpr:   inExpr | containsExpr;

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

containsExpr
    :   CONTAINS^ args
    ;

and so on. Hope this helps

Upvotes: 1

Related Questions