Use subexp highlighters with laxmatch enabled to have font-lock-mode ignore missing groups in matches:
(setq mylang-font-lock-keywords
`((,myregexp (1 font-lock-keyword-face)
(2 font-lock-warning-face nil t)
(3 font-lock-constant-face))))
The forth element of each subexp highlighter is the laxmatch
argument. If t
font-lock-mode ignores this highlighter if the corresponding group in the first element is not found in the match result of myregexp
.
See Search-based Fontification in the Emacs Lisp manual for more information.