From 29c5e2cea22f909af7d33b290ca0eb23c5ad6c00 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Tue, 23 Dec 2014 11:17:55 -0500 Subject: [PATCH] (js-syntax-propertize-regexp): Recognize "slash in a character class" Fixes: debbugs:19397 * lisp/progmodes/js.el (js--syntax-propertize-regexp-syntax-table): New var. (js-syntax-propertize-regexp): Use it to recognize "slash in a character class". --- lisp/ChangeLog | 6 ++++++ lisp/progmodes/js.el | 27 ++++++++++++++++++++++----- test/indent/js.js | 5 +++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index d8bb1c89f1f..12430b69fb7 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2014-12-23 Stefan Monnier + + * progmodes/js.el (js--syntax-propertize-regexp-syntax-table): New var. + (js-syntax-propertize-regexp): Use it to recognize "slash in + a character class" (bug#19397). + 2014-12-22 Stefan Monnier * completion.el: Use post-self-insert-hook (bug#19400). diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index c2c45aa4ef8..45074d338d3 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -1637,12 +1637,29 @@ 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)) + (defun js-syntax-propertize-regexp (end) - (when (eq (nth 3 (syntax-ppss)) ?/) - ;; A /.../ regexp. - (when (re-search-forward "\\(?:\\=\\|[^\\]\\)\\(?:\\\\\\\\\\)*/" end 'move) - (put-text-property (1- (point)) (point) - 'syntax-table (string-to-syntax "\"/"))))) + (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)))))) (defun js-syntax-propertize (start end) ;; Javascript allows immediate regular expression objects, written /.../. diff --git a/test/indent/js.js b/test/indent/js.js index 1924094e9d8..2d458e1b769 100644 --- a/test/indent/js.js +++ b/test/indent/js.js @@ -7,6 +7,11 @@ let c = 1, var e = 100500, + 1; +function test () +{ + return /[/]/.test ('/') // (bug#19397) +} + var f = bar('/protocols/') baz(); -- 2.39.2