简体   繁体   中英

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. Unfortunately, calling getText() on that results only in the last word.

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. 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.

A fix could be, to promote the label content to a 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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