我正在尝试编写一个 antlr 脚本,其中 rule1 有一个子规则 rule2。我在规则 1 上使用 StringTemplate。
我想要做的是在规则1使用/使用它之前重组规则2匹配的文本。我怎么做 ?
options{
output=template;
}
rule1 :
begin sub-rule2 end ';' -> meth(body={rule1.text})
sub-rule2 :
sub-rule3
| sub-rule4
| sub-rule5;
这里“meth”是字符串模板调用
如果说子规则 4 匹配“select * from dual;”,我希望将其传递给 rule1“#sql (select * from dual);”。
这是我的实际代码,我希望将 select_statement 规则匹配的语句包装在“#sql()”中,并在“stats”列表中传递给“meth”模板的“body”属性:
body
@init {
List stats = new ArrayList();
} :
BEGIN s=statement{ stats.add($s.text); } SEMI ( s=statement{ stats.add($s.text); } SEMI | pragma SEMI )*
( EXCEPTION exception_handler+ )? END ID? -> method(modifiers={"public"},returnType={"void"},name={"execute"},body={stats})
;
statement :
label*
( assign_or_call_statement
| case_statement
| close_statement
| continue_statement
| basic_loop_statement
| execute_immediate_statement
| exit_statement
| fetch_statement
| for_loop_statement
| forall_statement
| goto_statement
| if_statement
| null_statement
| open_statement
| plsql_block
| raise_statement
| return_statement
| sql_statement
| while_loop_statement
)
;
sql_statement
: (commit_statement
| delete_statement
| insert_statement
| lock_table_statement
| rollback_statement
| savepoint_statement
| select_statement
| set_transaction_statement
| update_statement )
;
select_statement :
SELECT swallow_to_semi
;
SELECT : 'select';
swallow_to_semi :
~( SEMI )+
;