我正在尝试为 Antlr3 创建一个词法分析器规则,该规则将匹配三引号字符串。例如:
"""this is some text"""
这就是我的做法:
TEXT:
'"""' ('\\"' | ~'"')+ '"""'
{
this.setText(
this.getText()
.substring(3, this.getText().length() - 3)
.replace("\\\"", "\"")
);
}
;
效果很好,但每个单引号都必须在输入文本中转义,如下所示:
"""this is the same text, but with \"escaped quotes\" inside"""
我试图摆脱这种强制转义引号,并解析三引号之间的任何内容(!),如下所示:
"""sample text again, with "quotes" inside"""
我正在尝试将规则更改为:
TEXT:
'"""' (~'"""')+ '"""'
Antlr3 3.5 抱怨:
error(100): Spec.g:153:13: syntax error: buildnfa: NoViableAltException(58@[])
error(100): Spec.g:0:1: syntax error: buildnfa: MismatchedTreeNodeException(3!=29)
error(100): Spec.g:0:: syntax error: buildnfa: NoViableAltException(3@[])
error(100): Spec.g:0:1: syntax error: buildnfa: MismatchedTreeNodeException(29!=28)
error(10): internal error: Spec.g : java.lang.NullPointerException
org.antlr.tool.NFAFactory.build_Aplus(NFAFactory.java:516)
...
怎么了?什么是可能的解决方法?