0

I need some help with ANTLR. I have the following rule:

datasource
@init{boolean joinexpr = false;}
:   
   (s1=datasourceelement (joinclause1=joinclause joinelement1=datasourceelement onclause1=onclause  (multijoinexpression)* {joinexpr=true;})?)
   -> {joinexpr}?  ^(JOINEXPRESSION 
                        ^(LEFTEXPR $s1?) 
                        $joinclause1?
                        ^(RIGHTEXPR $joinelement1?) $onclause1? multijoinexpression*
                    )                    
   -> $s1                   
;
multijoinexpression
:
  joinclause datasourceelement onclause 
  -> 
  ^(MULTIJOINEXPRESSION
                        joinclause
                        ^(RIGHTEXPR datasourceelement) onclause
                    )
;

Which is for parsing out join expressions: It eats up

(table/query) (join (table/query) on (field=field))*

but I need to handle the "( )" around each join, so It can parse something like this:

((( table1 JOIN table2 ON field1=field2) JOIN table3 ON field2=field3 ) JOIN...)

without the brackets it works fine, but i can't figure out how to add the left and right bracket to the rule, because of the multijoinexpression*

Balazs Gunics
  • 2,017
  • 2
  • 17
  • 24

1 Answers1

0

This solved the problem. I also find it really ugly, but it works.

(
  //1-2
                (LPARAM simplejoin RPARAM) multijoinexpression?
 |//2-3
        (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression? 
 |//3-4
(LPARAM (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression RPARAM) 
multijoinexpression?
 |//4-5
(LPARAM 
(LPARAM (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression RPARAM) 
multijoinexpression RPARAM) multijoinexpression?
 )  ->^(JOINEXPRESSION simplejoin multijoinexpression* )       
Balazs Gunics
  • 2,017
  • 2
  • 17
  • 24