; perl-mode.el: Detect regexes immediately after "|&"

* lisp/progmodes/perl-mode.el (perl-syntax-propertize-function):
Add "|&" to the list of characters after which a slash starts a
regular expression (Bug#23992).

* test/lisp/progmodes/cperl-mode-tests.el (cperl-test-ppss):
Correct the docstring.
(cperl-test-bug-23992): New test for Bug#23992.
(cperl-test-bug-42168): Adapt inline comments to the current code.

* test/lisp/progmodes/cperl-mode-resources/cperl-bug-23992.pl:
Resource file with example code from the bug report.
This commit is contained in:
Harald Jörg 2021-06-08 23:23:25 +02:00
parent 9625e3026b
commit 90f54aad5e
3 changed files with 36 additions and 6 deletions

View file

@ -285,7 +285,7 @@
(put-text-property (match-beginning 2) (match-end 2)
'syntax-table (string-to-syntax "\""))
(perl-syntax-propertize-special-constructs end)))))
("\\(^\\|[?:.,;=!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\(?:\\([^])}>= \n\t]\\)\\|\\(?3:=\\)[^>]\\)"
("\\(^\\|[?:.,;=|&!~({[ \t]\\)\\([msy]\\|q[qxrw]?\\|tr\\)\\>\\s-*\\(?:\\([^])}>= \n\t]\\)\\|\\(?3:=\\)[^>]\\)"
;; Nasty cases:
;; /foo/m $a->m $#m $m @m %m
;; \s (appears often in regexps).

View file

@ -0,0 +1,10 @@
# Test file for Bug#23992
#
# The "||" case is directly from the report,
# the "&&" case has been added for symmetry.
s/LEFT/L/g || s/RIGHT/R/g || s/aVALUE\D+//g;
s/LEFT/L/g||s/RIGHT/R/g||s/aVALUE\D+//g;
s/LEFT/L/g && s/RIGHT/R/g && s/aVALUE\D+//g;
s/LEFT/L/g&&s/RIGHT/R/g&&s/aVALUE\D+//g;

View file

@ -37,7 +37,7 @@
;;; Utilities
(defun cperl-test-ppss (text regexp)
"Return the `syntax-ppss' of the first character matched by REGEXP in TEXT."
"Return the `syntax-ppss' after the last character matched by REGEXP in TEXT."
(interactive)
(with-temp-buffer
(insert text)
@ -377,6 +377,26 @@ documentation it does the right thing anyway."
(cperl-indent-command)
(forward-line 1))))
(ert-deftest cperl-test-bug-23992 ()
"Verify that substitutions are fontified directly after \"|&\".
Regular expressions are strings in both perl-mode and cperl-mode."
(with-temp-buffer
(insert-file-contents (ert-resource-file "cperl-bug-23992.pl"))
(funcall cperl-test-mode)
(goto-char (point-min))
;; "or" operator, with spaces
(search-forward "RIGHT")
(should (nth 3 (syntax-ppss)))
;; "or" operator, without spaces
(search-forward "RIGHT")
(should (nth 3 (syntax-ppss)))
;; "and" operator, with spaces
(search-forward "RIGHT")
(should (nth 3 (syntax-ppss)))
;; "and" operator, without spaces
(search-forward "RIGHT")
(should (nth 3 (syntax-ppss)))))
(ert-deftest cperl-test-bug-28650 ()
"Verify that regular expressions are recognized after 'return'.
The test uses the syntax property \"inside a string\" for the
@ -448,14 +468,14 @@ If seen as regular expression, then the slash is displayed using
font-lock-constant-face. If seen as a division, then it doesn't
have a face property."
:tags '(:fontification)
;; The next two Perl expressions have divisions. Perl "punctuation"
;; operators don't get a face.
;; The next two Perl expressions have divisions. The slash does not
;; start a string.
(let ((code "{ $a++ / $b }"))
(should (equal (nth 8 (cperl-test-ppss code "/")) nil)))
(let ((code "{ $a-- / $b }"))
(should (equal (nth 8 (cperl-test-ppss code "/")) nil)))
;; The next two Perl expressions have regular expressions. The
;; delimiter of a RE is fontified with font-lock-constant-face.
;; The next two Perl expressions have regular expressions. The slash
;; starts a string.
(let ((code "{ $a+ / $b } # /"))
(should (equal (nth 8 (cperl-test-ppss code "/")) 7)))
(let ((code "{ $a- / $b } # /"))