When writing a major mode, it's often useful to know 'is point in a string?' 'is point in a comment?'.
Most major modes seem to attempt to parse the programming language. For example:
python-syntax-content
callssyntax-ppss
haskell-fill-paragraph
callssyntax-ppss
andre-search-forward
c-in-comment-line-prefix-p
moves point around and callslooking-at
sp-point-in-comment
callssyntax-ppss
but also checks if it's on a comment delimeter
However, this doesn't work in some cases. In org-mode buffers, comments in source blocks are not correctly detected by these approaches.
It also seems pointless, since the buffer is already showing comments highlighted.
Instead, you could simply inspect the faces at point:
(defun wh--get-faces (pos)
"Get all the font faces at POS."
(remq nil
(list
(get-char-property pos 'read-face-name)
(get-char-property pos 'face)
(plist-get (text-properties-at pos) 'face))))
(defun wh-string-p (pos)
"Return non-nil if POS is inside a string."
(memq 'font-lock-string-face (wh--get-faces pos)))
Why don't major modes do this? The buffer is already fontified, so I would expect this to be faster, more robust, and require less code.