GNU bug report logs -
#25529
25.1.90; js-mode: Regexp literal with unbalanced brackets breaks font-lock
Previous Next
Reported by: Mikhail Gusarov <mikhail <at> hola.org>
Date: Wed, 25 Jan 2017 11:15:02 UTC
Severity: minor
Found in version 25.1.90
Done: Tom Tromey <tom <at> tromey.com>
Bug is archived. No further changes may be made.
Full log
Message #11 received at 25529 <at> debbugs.gnu.org (full text, mbox):
Tom> I don't think there is a way to teach parse-partial-sexp that "^" is a
Tom> quote only in this one specific instance.
This doesn't actually make sense anyway.
The "^" is a distraction, as this is valid:
let x = /[[]/;
Tom> One possible fix here would be to change this function to do a simple
Tom> parse of the regexp literal. I think it would only really have to
Tom> handle parsing bracket syntax and looking for the terminating "/". The
Tom> current code also looks for balanced parens, but I don't think this is
Tom> actually needed.
I've appended a patch implementing this idea.
Tom
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index e42e014..083cef9 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -1698,18 +1698,30 @@ js-syntax-propertize-regexp
(let ((ppss (syntax-ppss)))
(when (eq (nth 3 ppss) ?/)
;; A /.../ regexp.
- (while
- (when (re-search-forward "\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*/"
- end 'move)
- (if (nth 1 (with-syntax-table
- js--syntax-propertize-regexp-syntax-table
- (let ((parse-sexp-lookup-properties nil))
- (parse-partial-sexp (nth 8 ppss) (point)))))
- ;; A / within a character class is not the end of a regexp.
- t
- (put-text-property (1- (point)) (point)
- 'syntax-table (string-to-syntax "\"/"))
- nil))))))
+ (let ((keep-going t)
+ (backslash nil)
+ (in-bracket nil))
+ (while keep-going
+ (forward-char)
+ (let ((c (char-after)))
+ (cond
+ (backslash
+ (setq backslash nil))
+ ((eq c ?\\)
+ (setq backslash t))
+ ((eq c ?\[)
+ ;; Note that inside a bracket we can see another unescaped open
+ ;; bracket.
+ (setq in-bracket t))
+ ((eq c ?\])
+ (setq in-bracket nil))
+ ((eq c ?/)
+ (unless in-bracket
+ ;; We're done.
+ (setq keep-going nil)
+ (put-text-property
+ (point) (1+ (point))
+ 'syntax-table (string-to-syntax "\"/")))))))))))
(defun js-syntax-propertize (start end)
;; JavaScript allows immediate regular expression objects, written /.../.
This bug report was last modified 8 years and 180 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.