As an example, lets say I want to parse mostly unstructured text with single markup element, double star **
. This is my ANTLR grammar:
text : (plain | tag)+ ;
plain : ~(TAG) ;
tag : TAG tag_inner TAG ;
tag_inner : ~(TAG) ;
TAG : '**' ;
TEXT : ('a'..'z' | ' ' | '.')+ ;
This grammar works just fine if the text I'm parsing is syntactically correct, that is for every opening **
there is a closing **
. If there is an odd number of **
s, ANTLR complains, and errors out.
How would one fix this, so that ANTLR will look ahead for a closing double star, and if there is no one treat that lone double star as plain text? I'm pretty sure ANTLR can do this and that syntactic/semantic predicates are the answer, but after an our spent reading the docs, I still can't work it out.