Subject: Re: diagnosis and one approach to a fix

From: Tom Tromey <[email-masked]>
Date: Sat, 04 Feb 2017 23:01:57 -0700

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 /.../.