You can specifically determine which rule can return as follows:
sub_rule2 returns [String x] : sub_rule3 {$x = ... } | sub_rule4 {$x = "#sql (" + $sub_rule4.text + ");";} | sub_rule5 {$x = ... } ;
Now sub_rule2 returns a String x , which you can use as follows:
rule1 : ... sub_rule2 ... -> meth(body={sub_rule2.x}) ;
Pay attention to sub_rule2.x .
EDIT
You can also create a custom method that checks whether the text to be added to the List starts with "select " as follows:
grammar YourGrammarName; options{ output=template; } @parser::members { private void addStat(String stat, List<String statList>) { // 1. if `stat` starts with "select ", wrap "#sql(...)" around it. // 2. add `stat` to `statList` } } body @init { List<String> stats = new ArrayList<String>(); } : BEGIN s=statement { addStat($s.text, stats); } SEMI ( s=statement { addStat($s.text, stats); } SEMI | pragma SEMI )* (EXCEPTION exception_handler+)? END ID? -> method(modifiers={"public"},returnType={"void"},name={"execute"},body={stats}) ;
source share