我目前正在使用此处定义的XML词法分析器语法:
https://github.com/antlr/grammars-v4/blob/master/xml/XMLLexer.g4
使用给定的输入,我会生成以下词法分析器事件:
XML输入:
private void handleDragDropped(DragEvent event){
Dragboard db = event.getDragboard();
File file = db.getFiles().get(0);
}
Lexer输出
<item>
<![CDATA[
My CDATA Block
]]>
</item>
但是,我想分开'&lt; [[CDATA'开始代码和']]&gt;'来自CDATA事件的结束标记,以便我可以拥有一个仅包含该块内容的事件。我尝试了以下语法几乎可以工作,除了因为CDATA块使用非贪婪匹配,块中的每个字符都会创建一个新事件:
新语法:
https://gist.github.com/alexspurling/2e243b1c806a4482697700ea1f686d44
(差异:https://gist.github.com/alexspurling/2e243b1c806a4482697700ea1f686d44/revisions)
输出:
[@-1,0:0='<',<7>,1:0]
[@-1,1:4='item',<16>,1:1]
[@-1,5:5='>',<10>,1:5]
[@-1,6:8='\n ',<6>,1:6]
[@-1,9:42='<![CDATA[\n My CDATA Block\n ]]>',<2>,2:2]
[@-1,43:43='\n',<6>,4:5]
[@-1,44:44='<',<7>,5:0]
[@-1,45:45='/',<13>,5:1]
[@-1,46:49='item',<16>,5:2]
[@-1,50:50='>',<10>,5:6]
我想要的输出是:
[@-1,0:0='<',<6>,1:0]
[@-1,1:4='item',<15>,1:1]
[@-1,5:5='>',<9>,1:5]
[@-1,6:8='\n ',<5>,1:6]
[@-1,9:17='<![CDATA[',<2>,2:2]
[@-1,18:18='\n',<19>,2:11]
[@-1,19:19=' ',<19>,3:0]
[@-1,20:20=' ',<19>,3:1]
[@-1,21:21=' ',<19>,3:2]
[@-1,22:22=' ',<19>,3:3]
[@-1,23:23='M',<19>,3:4]
[@-1,24:24='y',<19>,3:5]
[@-1,25:25=' ',<19>,3:6]
[@-1,26:26='C',<19>,3:7]
[@-1,27:27='D',<19>,3:8]
[@-1,28:28='A',<19>,3:9]
[@-1,29:29='T',<19>,3:10]
[@-1,30:30='A',<19>,3:11]
[@-1,31:31=' ',<19>,3:12]
[@-1,32:32='B',<19>,3:13]
[@-1,33:33='l',<19>,3:14]
[@-1,34:34='o',<19>,3:15]
[@-1,35:35='c',<19>,3:16]
[@-1,36:36='k',<19>,3:17]
[@-1,37:37='\n',<19>,3:18]
[@-1,38:38=' ',<19>,4:0]
[@-1,39:39=' ',<19>,4:1]
[@-1,40:42=']]>',<18>,4:2]
[@-1,43:43='\n',<5>,4:5]
[@-1,44:44='<',<6>,5:0]
[@-1,45:45='/',<12>,5:1]
[@-1,46:49='item',<15>,5:2]
[@-1,50:50='>',<9>,5:6]
如何更改语法以实现此目的?
答案 0 :(得分:1)
您应该更改
mode IN_CDATA;
CDATA_CLOSE : ']]>' -> popMode;
CDATA_CONTENT : .+? ;
到
mode IN_CDATA;
CDATA_CLOSE : ']]>' -> popMode;
CDATA_CONTENT : (~']'+? | (']'+? (~']' ~']' ~'>')?));