summaryrefslogtreecommitdiff
path: root/lisp/progmodes/js.el
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-02-05 11:40:18 -0700
committerTom Tromey <tom@tromey.com>2017-02-11 12:30:32 -0700
commit862d6438cfa6c6c035033697751f3d002357b024 (patch)
treecc7eab8482c2fe5fabb37660d1d5a6353aefd159 /lisp/progmodes/js.el
parentc2e19a73401eb37d5c13f6c8589dc1e5d706d239 (diff)
downloademacs-862d6438cfa6c6c035033697751f3d002357b024.tar.gz
Recognize JS regexp literals more correctly
Bug#25529 * lisp/progmodes/js.el (js--syntax-propertize-regexp-regexp): New constant. (js-syntax-propertize-regexp): Use it. Remove "end" argument. (js--syntax-propertize-regexp-syntax-table): Remove. (js-syntax-propertize): Update. * test/lisp/progmodes/js-tests.el (js-mode-regexp-syntax-bug-25529): New test.
Diffstat (limited to 'lisp/progmodes/js.el')
-rw-r--r--lisp/progmodes/js.el43
1 files changed, 25 insertions, 18 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index e42e01481b6..b42b2bca822 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -1687,29 +1687,36 @@ This performs fontification according to `js--class-styles'."
js--font-lock-keywords-3)
"Font lock keywords for `js-mode'. See `font-lock-keywords'.")
-(defconst js--syntax-propertize-regexp-syntax-table
- (let ((st (make-char-table 'syntax-table (string-to-syntax "."))))
- (modify-syntax-entry ?\[ "(]" st)
- (modify-syntax-entry ?\] ")[" st)
- (modify-syntax-entry ?\\ "\\" st)
- st))
+(defconst js--syntax-propertize-regexp-regexp
+ (rx
+ ;; Start of regexp.
+ "/"
+ (0+ (or
+ ;; Match characters outside of a character class.
+ (not (any ?\[ ?/ ?\\))
+ ;; Match backslash quoted characters.
+ (and "\\" not-newline)
+ ;; Match character class.
+ (and
+ "["
+ (0+ (or
+ (not (any ?\] ?\\))
+ (and "\\" not-newline)))
+ "]")))
+ (group "/"))
+ "Regular expression matching a JavaScript regexp literal.")
(defun js-syntax-propertize-regexp (end)
(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))))))
+ (goto-char (nth 8 ppss))
+ (when (and (looking-at js--syntax-propertize-regexp-regexp)
+ ;; Don't touch text after END.
+ (<= (match-end 1) end))
+ (put-text-property (match-beginning 1) (match-end 1)
+ 'syntax-table (string-to-syntax "\"/"))
+ (goto-char (match-end 0))))))
(defun js-syntax-propertize (start end)
;; JavaScript allows immediate regular expression objects, written /.../.