bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#9183: js-mode highlights quotes inside regexp character sets as stri


From: Stefan Monnier
Subject: bug#9183: js-mode highlights quotes inside regexp character sets as string
Date: Fri, 12 Aug 2011 11:48:11 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

> Thanks, but it doesn't seem to fix anything, just mess things up in a
> somewhat different way.

It did fix the problem to some extent, but indeed it suffered from
a "multiline" problem (partly inherited from the previous code).

I've installed the patch below which should fix the problem, tho it may
introduce others.  Please try it out.

> (Just in case, I downloaded the fixed js.el and loaded that with my
> oldish Emacs 24 (started as emacs -Q), but I don't think that should
> matter, right?)

That should work OK, yes.  It won't work with Emacs-23 not with an ancient
Emacs-24, but oldish should be fine ;-)

>> But I don't know how to solve it completely because I don't know
>> Javascript enough to be sure exactly how to distinguish a /-division
>> from a /-regexp from a /-comment-starter in all cases:
>> - is something like
>> x = 1 + /a/.test("foo");
>> valid?
> Yes, it's valid, although I suspect it would be considered bad style.

Hmm, that makes it more interesting.
Especially if you ignore semantics and focus on syntax where it would
seem that "x = /foo/ / /bar/;" would then be valid, tho meaningless).

> (BTW, in Debian there is a spidermonkey-bin package, which has minimal
> dependencies and provides a `js' command, giving you a REPL.)

Thanks.

>> If so, is there a list of infix operators somewhere?
> I'd have to search for it just as you might have... There's this, for
> instance:
> https://developer.mozilla.org/en/JavaScript/Reference/Operators

Thanks, I'll keep it for next time I'm bored.


        Stefan


=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog      2011-08-12 13:49:45 +0000
+++ lisp/ChangeLog      2011-08-12 15:30:59 +0000
@@ -1,5 +1,13 @@
 2011-08-12  Stefan Monnier  <address@hidden>
 
+       * progmodes/js.el (js-syntax-propertize, js-syntax-propertize-regexp):
+       New function.
+       (js--regexp-literal, js-syntax-propertize-function): Remove.
+       (js-mode): Use js-syntax-propertize to handle multilines (bug#9183).
+       (js-mode-map): Don't rebind electric keys.
+       (js-insert-and-indent): Remove.
+       (js-mode): Setup electric-layout and electric-indent instead.
+
        * epa-file.el (epa-file-select-keys): Revert to nil default (bug#9280).
 
 2011-08-12  Daiki Ueno  <address@hidden>

=== modified file 'lisp/progmodes/js.el'
--- lisp/progmodes/js.el        2011-08-05 19:53:46 +0000
+++ lisp/progmodes/js.el        2011-08-12 15:29:28 +0000
@@ -1653,25 +1636,35 @@
                                    js--font-lock-keywords-3)
   "Font lock keywords for `js-mode'.  See `font-lock-keywords'.")
 
-;; XXX: Javascript can continue a regexp literal across lines so long
-;; as the newline is escaped with \. Account for that in the regexp
-;; below.
-(eval-and-compile
-  (defconst js--regexp-literal
-    (concat
-     ;; We want to match regular expressions only at the beginning of
-     ;; expressions.
-     ;; FIXME: Should we also allow /regexp/ after infix operators such as +,
-     ;; /, -, *, >, ...?
-     "\\(?:\\`\\|[=([{,:;]\\)\\(?:\\s-\\|\n\\)*"
-     "\\(/\\)\\(?:\\\\.\\|[^/*\\]\\)\\(?:\\\\.\\|[^/\\]\\)*\\(/\\)")
-  "Regexp matching a JavaScript regular expression literal.
-Match groups 1 and 2 are the characters forming the beginning and
-end of the literal."))
-
-(defconst js-syntax-propertize-function
+(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 "\"/")))))
+
+(defun js-syntax-propertize (start end)
+  ;; Javascript allows immediate regular expression objects, written /.../.
+  (goto-char start)
+  (js-syntax-propertize-regexp end)
+  (funcall
   (syntax-propertize-rules
-   (js--regexp-literal (1 "\"") (2 "\""))))
+    ;; Distinguish /-division from /-regexp chars (and from /-comment-starter).
+    ("\\(?:^\\|[=([{,:;]\\)\\(?:[ \t]\\)*\\(/\\)[^/*]"
+     (1 (ignore
+        (forward-char -1)
+         (when (or (not (memq (char-after (match-beginning 0)) '(?\s ?\t)))
+                   ;; If the / is at the beginning of line, we have to check
+                   ;; the end of the previous text.
+                   (save-excursion
+                     (goto-char (match-beginning 0))
+                     (forward-comment (- (point)))
+                     (memq (char-before)
+                           (eval-when-compile (append "=({[,:;" '(nil))))))
+           (put-text-property (match-beginning 1) (match-end 1)
+                              'syntax-table (string-to-syntax "\"/"))
+           (js-syntax-propertize-regexp end))))))
+   (point) end))
 
 ;;; Indentation
 
@@ -3302,7 +3295,7 @@
   (set (make-local-variable 'font-lock-defaults)
        (list js--font-lock-keywords))
   (set (make-local-variable 'syntax-propertize-function)
-       js-syntax-propertize-function)
+       #'js-syntax-propertize)
 
   (set (make-local-variable 'parse-sexp-ignore-comments) t)
   (set (make-local-variable 'parse-sexp-lookup-properties) t)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]