簡體   English   中英

處理不同的 escaping 序列?

[英]Handling different escaping sequences?

我正在使用帶有 Presto 語法的 ANTLR 來解析 SQL 查詢。 這是我用來解析查詢的原始字符串定義:

STRING
    : '\'' ( '\\' .
           | ~[\\']       // match anything other than \ and '
           | '\'\''       // match ''
           )*
      '\''
    ;

這對大多數查詢都有效,直到我看到具有不同 escaping 規則的查詢。 例如:

select 
table1(replace(replace(some_col,'\\'',''),'\"' ,'')) as features 
from table1

所以我修改了我的字符串定義,現在它看起來像:

STRING
    : '\'' ( '\\' .
           | '\\\\'  .  {HelperUtils.isNeedSpecialEscaping(this)}?       // match \ followed by any char
           | ~[\\']       // match anything other than \ and '
           | '\'\''       // match ''
           )*
      '\''
    ;

但是,這不適用於上面提到的查詢,因為我得到

'\\'',''),'

作為單個字符串。 謂詞為以下查詢返回 True。 知道如何處理這個查詢嗎?

謝謝,尼爾。

最后我能夠解決它。 這是我使用的表達式:

STRING
    : '\'' ( '\\\\'  .  {HelperUtils.isNeedSpecialEscaping(this)}?
           | '\\' (~[\\] | . {!HelperUtils.isNeedSpecialEscaping(this)}?)
           | ~[\\']       // match anything other than \ and '
           | '\'\''       // match ''
           )*
      '\''
    ;
grammar Question;

sql
@init {System.out.println("Question last update 2352");}
    : replace+ EOF
    ;

replace
    : REPLACE '(' expr ')'
    ;

expr
    : ( replace | ID ) ',' STRING ',' STRING
    ;

REPLACE : 'replace' DIGIT? ;
ID      : [a-zA-Z0-9_]+ ;
DIGIT   : [0-9] ;
STRING  : '\'' '\\\\\'' '\''                // '\\''
        | '\'' '\'\'' '\''                  // ''''
        | '\'' ~[\\']* '\'\'' ~[\\']* '\''  // 'it is 8 o''clock'
        | '\'' .*? '\'' ;
NL      : '\r'? '\n'  -> channel(HIDDEN) ;
WS      : [ \t]+      -> channel(HIDDEN) ;

文件input.txt (沒有更多示例,我只能猜測):

replace1(replace(some_col,'\\'',''),'\"' ,'')
replace2(some_col,'''','')
replace3(some_col,'abc\tdef\tghi','xyz')
replace4(some_col,'abc\ndef','xyz')
replace5(some_col,'it is 8 o''clock','8')

執行:

$ alias a4='java -jar /usr/local/lib/antlr-4.9-complete.jar'
$ alias grun='java org.antlr.v4.gui.TestRig'
$ a4 Question.g4
$ javac Question*.java
$ grun Question sql -tokens input.txt 
[@0,0:7='replace1',<REPLACE>,1:0]
[@1,8:8='(',<'('>,1:8]
[@2,9:15='replace',<REPLACE>,1:9]
[@3,16:16='(',<'('>,1:16]
[@4,17:24='some_col',<ID>,1:17]
[@5,25:25=',',<','>,1:25]
[@6,26:30=''\\''',<STRING>,1:26]
[@7,31:31=',',<','>,1:31]
[@8,32:33='''',<STRING>,1:32]
[@9,34:34=')',<')'>,1:34]
[@10,35:35=',',<','>,1:35]
[@11,36:39=''\"'',<STRING>,1:36]
[@12,40:40=' ',<WS>,channel=1,1:40]
[@13,41:41=',',<','>,1:41]
[@14,42:43='''',<STRING>,1:42]
[@15,44:44=')',<')'>,1:44]
[@16,45:45='\n',<NL>,channel=1,1:45]
[@17,46:53='replace2',<REPLACE>,2:0]
[@18,54:54='(',<'('>,2:8]
[@19,55:62='some_col',<ID>,2:9]
[@20,63:63=',',<','>,2:17]
[@21,64:67='''''',<STRING>,2:18]
[@22,68:68=',',<','>,2:22]
[@23,69:70='''',<STRING>,2:23]
[@24,71:71=')',<')'>,2:25]
[@25,72:72='\n',<NL>,channel=1,2:26]
[@26,73:80='replace3',<REPLACE>,3:0]
[@27,81:81='(',<'('>,3:8]
[@28,82:89='some_col',<ID>,3:9]
[@29,90:90=',',<','>,3:17]
[@30,91:105=''abc\tdef\tghi'',<STRING>,3:18]
[@31,106:106=',',<','>,3:33]
[@32,107:111=''xyz'',<STRING>,3:34]
[@33,112:112=')',<')'>,3:39]
[@34,113:113='\n',<NL>,channel=1,3:40]
[@35,114:121='replace4',<REPLACE>,4:0]
[@36,122:122='(',<'('>,4:8]
[@37,123:130='some_col',<ID>,4:9]
[@38,131:131=',',<','>,4:17]
[@39,132:141=''abc\ndef'',<STRING>,4:18]
[@40,142:142=',',<','>,4:28]
[@41,143:147=''xyz'',<STRING>,4:29]
[@42,148:148=')',<')'>,4:34]
[@43,149:149='\n',<NL>,channel=1,4:35]
[@44,150:157='replace5',<REPLACE>,5:0]
[@45,158:158='(',<'('>,5:8]
[@46,159:166='some_col',<ID>,5:9]
[@47,167:167=',',<','>,5:17]
[@48,168:185=''it is 8 o''clock'',<STRING>,5:18]
[@49,186:186=',',<','>,5:36]
[@50,187:189=''8'',<STRING>,5:37]
[@51,190:190=')',<')'>,5:40]
[@52,191:191='\n',<NL>,channel=1,5:41]
[@53,192:191='<EOF>',<EOF>,6:0]
Question last update 2352

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM