Thanks for the signpost Misha Arefiev, he made me look for the right places. This is what I came up with, and it covers all my initial requirements. The only limitation that I now know about is that it will highlight an invalid number suffix, as if it were right (for example, "123ulu")
(add-hook 'c-mode-common-hook (lambda () (font-lock-add-keywords nil '( ; Valid hex number (will highlight invalid suffix though) ("\\b0x[[:xdigit:]]+[uUlL]*\\b" . font-lock-string-face) ; Invalid hex number ("\\b0x\\(\\w\\|\\.\\)+\\b" . font-lock-warning-face) ; Valid floating point number. ("\\(\\b[0-9]+\\|\\)\\(\\.\\)\\([0-9]+\\(e[-]?[0-9]+\\)?\\([lL]?\\|[dD]?[fF]?\\)\\)\\b" (1 font-lock-string-face) (3 font-lock-string-face)) ; Invalid floating point number. Must be before valid decimal. ("\\b[0-9].*?\\..+?\\b" . font-lock-warning-face) ; Valid decimal number. Must be before octal regexes otherwise 0 and 0l ; will be highlighted as errors. Will highlight invalid suffix though. ("\\b\\(\\(0\\|[1-9][0-9]*\\)[uUlL]*\\)\\b" 1 font-lock-string-face) ; Valid octal number ("\\b0[0-7]+[uUlL]*\\b" . font-lock-string-face) ; Floating point number with no digits after the period. This must be ; after the invalid numbers, otherwise it will "steal" some invalid ; numbers and highlight them as valid. ("\\b\\([0-9]+\\)\\." (1 font-lock-string-face)) ; Invalid number. Must be last so it only highlights anything not ; matched above. ("\\b[0-9]\\(\\w\\|\\.\\)+?\\b" . font-lock-warning-face) )) ))
Any suggestions / optimizations / corrections are welcome!
EDIT: Stop it from highlighting numbers in the comments.
source share