font-lock-string-face)
font-lock-comment-face))
+(defconst python--f-string-start-regexp
+ (rx bow
+ (or "f" "F" "fr" "Fr" "fR" "FR" "rf" "rF" "Rf" "RF")
+ (or "\"" "\"\"\"" "'" "'''"))
+ "A regular expression matching the beginning of an f-string.
+
+See URL `https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals'.")
+
(defun python--f-string-p (ppss)
"Return non-nil if the pos where PPSS was found is inside an f-string."
(and (nth 3 ppss)
- (let ((spos (1- (nth 8 ppss))))
- (and (memq (char-after spos) '(?f ?F))
+ (let* ((spos (1- (nth 8 ppss)))
+ (before-quote
+ (buffer-substring-no-properties (max (- spos 4) (point-min))
+ (min (+ spos 2) (point-max)))))
+ (and (string-match-p python--f-string-start-regexp before-quote)
(or (< (point-min) spos)
(not (memq (char-syntax (char-before spos)) '(?w ?_))))))))
(while
(progn
(while (and (not (python--f-string-p ppss))
- (re-search-forward "\\<f['\"]" limit 'move))
+ (re-search-forward python--f-string-start-regexp limit 'move))
(setq ppss (syntax-ppss)))
(< (point) limit))
(cl-assert (python--f-string-p ppss))
(195 . font-lock-string-face)
(196 . font-lock-constant-face)
(215 . font-lock-string-face) (218)
- (221 . font-lock-string-face) (274)
- (277 . font-lock-string-face) (330)
- (333 . font-lock-string-face) (386)
- (389 . font-lock-string-face) (442)
+ (221 . font-lock-string-face) (254)
+ (271 . font-lock-string-face) (274)
+ (277 . font-lock-string-face) (310)
+ (327 . font-lock-string-face) (330)
+ (333 . font-lock-string-face) (366)
+ (383 . font-lock-string-face) (386)
+ (389 . font-lock-string-face) (422)
+ (439 . font-lock-string-face) (442)
(444 . font-lock-string-face) (497)
(499 . font-lock-string-face) (552)
(555 . font-lock-string-face) (608)