parsing - Can the antlr4 parser see matching opening and closing text pattern? -
i'm matching user-defined html-template tags (simplified):
{% label %} ... {% endlabel %}   the "label" alphanumeric value user can define himself, e.g.:
{% mytag %}<div>...</div>{% endmytag %}   is there way tell parser label start tag text has match endlabel end tag text?  in other words, want invalid:
{% mytag %}<div>...</div>{% endnotmatchingtag %}   my lexer looks 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 parser rule looks similar this:
customtag: tagstart label tagend block tagstart endlabel tagend;       (and block matches text or other tags recursively)
right i'm checking match in listener, hoping in parser.   there way ensure endlabel equal 'end' + label @ parser level in antlr4?
... , possible if weren't prepending 'end' in lexer?
create 2 additional lexer rules
endtag : tagstart endlabel tagend; starttag : tagstart label tagend;   make sure token endlabel not subsumed label (yet label matches same text, preferred because first in grammar!)
use new tokens in grammar, similar did:
taggedelement : starttag othernodes endtag;   and insert semantic predicate
taggedelement : starttag othernodes endtag {matches($starttag.text,$endtag.text)};   where matches true if tags matching.
Comments
Post a Comment