ฉันกำลังพยายามสร้างกฎ lexer สำหรับ 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)
...
เกิดอะไรขึ้น? วิธีแก้ปัญหาที่เป็นไปได้คืออะไร?