简体   繁体   English

从ANTLR中的〜(';')+获取文本

[英]Get Text from ~(';')+ in ANTLR

Using Antlr-grammar for parsing a SQL File, I wanna get the total text value of ~(';')+ in order to log this information I did not parse deeply. 使用Antlr语法分析SQL文件,我想获得~(';')+的总文本值,以便记录我没有深入分析的信息。 Unfortunately, calling getText() on that results only in the last word. 不幸的是,对此调用getText()只导致最后一个字。

This is the active parsing rule: 这是活动的解析规则:

...
constraintName=alter_table_constraint_name 'CHECK' content=~(';')+  #alterTableAddCheck
...

which parses this part fine: 解析这部分很好:

ALTER TABLE "UFHDBTBL"."FH01T54" 
    ADD CONSTRAINT "FH01C54_DYNAMIC" CHECK 
        (DYNAMIC IN ('Y','N'))
    ENFORCED
    ENABLE QUERY OPTIMIZATION;

Calling getText() in my program later results in the output of "OPTIMIZATION" only. 稍后在我的程序中调用getText()只会导致输出“ OPTIMIZATION”。 Anyone an idea, how to get the whole block? 任何人都知道,如何获得整个区块?

After some testing I found out that content is of type CommonToken , ie content only refers to a single token, hence it doesn't contain all tokens. 经过一些测试,我发现content的类型为CommonToken ,即content仅指单个令牌,因此它不包含所有令牌。

A fix could be, to promote the label content to a contentRule : 可以将标签content提升为contentRule

//...
constraintName=alter_table_constraint_name 'CHECK' content=contentRule  #alterTableAddCheck
//...


contentRule : ~(';')+;

Or as Sam mentioned here , use this syntax (note the += instead of the = after the label name) and get the complete text by concatenating the individual texts of each list member (this even let's you include whitespace between tokens if wished): 或如Sam在这里提到的那样,使用以下语法(在标签名称后注意+=而不是= ),并通过串联每个列表成员的各个文本来获得完整的文本(如果需要,这甚至可以让您在标记之间包含空格):

...
constraintName=alter_table_constraint_name 'CHECK' (content+=~';')+  #alterTableAddCheck
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM