ANTLRv4:如何读取字符串中的双引号转义双引号?

Jay*_*Dee 8 antlr4

在ANTLR v4中,我们如何使用双引号转义双引号来解析这种字符串,如在VBA中?

对于文字:

"some string with ""john doe"" in it"
Run Code Online (Sandbox Code Playgroud)

目标是识别字符串: some string with "john doe" in it

是否可以将其重写为单双引号中的双倍双引号?"" -> "

Bar*_*ers 12

像这样:

STRING
 : '"' (~[\r\n"] | '""')* '"'
 ;
Run Code Online (Sandbox Code Playgroud)

在哪里~[\r\n"] | '""'表示:

~[\r\n"]    # any char other than '\r', '\n' and double quotes
|           # OR
'""'        # two successive double quotes
Run Code Online (Sandbox Code Playgroud)

是否可以将其重写为单双引号中的双倍双引号?

不是没有嵌入自定义代码.在Java中看起来像:

STRING
 : '"' (~[\r\n"] | '""')* '"' 
   {
     String s = getText();
     s = s.substring(1, s.length() - 1); // strip the leading and trailing quotes
     s = s.replace("\"\"", "\""); // replace all double quotes with single quotes
     setText(s);
   }
 ;
Run Code Online (Sandbox Code Playgroud)

  • 对于我使用ANTLR 4,以下规则适用于双引号和单引号字符串:STRING:'"'(〜[\ r \n"] |'""')*'"'|'\''(〜[\r \n \'] |'\'\'')*'\''; (2认同)