I'm matching user-defined HTML-template tags that look like this (simplified):
{% label %} ... {% endlabel %}
The "label" is an alphanumeric value that a user can define himself, e.g.:
{% mytag %}<div>...</div>{% endmytag %}
Is there a way to tell the parser that the LABEL
start tag text has to match with the ENDLABEL
end tag text? In other words, I want this to be invalid:
{% mytag %}<div>...</div>{% endnotmatchingtag %}
My lexer looks like this:
LABEL : ALPHA (ALPHA|DIGIT|UNDERSCORE)* ;
fragment UNDERSCORE: '_' ;
fragment ALPHA: [a-zA-Z] ;
fragment DIGIT: [0-9] ;
END : 'end'
ENDLABEL : END LABEL
TAGSTART : '{%'
TAGEND : '%}'
WS : [ \t\r\n]+ -> skip ;
And the parser rule looks similar to this:
customtag: TAGSTART LABEL TAGEND block TAGSTART ENDLABEL TAGEND;
(and a block matches text or other tags recursively)
Right now I'm checking for a match in the listener, but I was hoping I could do it in the parser. Is there any way to ensure that ENDLABEL
is equal to 'end'
+ LABEL
at the parser level in Antlr4?
... and is it possible to do it if I weren't prepending 'end' in the lexer?