[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/xr d6740ce 2/3: Detect misplaced `]' inside character a
From: |
Mattias Engdegård |
Subject: |
[elpa] externals/xr d6740ce 2/3: Detect misplaced `]' inside character alternatives |
Date: |
Sun, 17 Mar 2019 09:10:24 -0400 (EDT) |
branch: externals/xr
commit d6740ceb190e71bab2d992f2ad35b9f6418283de
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>
Detect misplaced `]' inside character alternatives
Add an ad-hoc check for a rare but serious mistake: attempts to include
literal [ and ] inside a character alternative without placing the ] first,
resulting in a [...[...]...] pattern.
There could be false positives but none have been seen in emacs or elpa.
---
xr-test.el | 4 ++++
xr.el | 15 +++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/xr-test.el b/xr-test.el
index 670a2f2..4d197bf 100644
--- a/xr-test.el
+++ b/xr-test.el
@@ -356,6 +356,10 @@
'((1 . "Uncounted repetition"))))
(should (equal (xr-lint "a\\{\\}")
'((1 . "Implicit zero repetition"))))
+ (should (equal (xr-lint "[0-9[|]*/]")
+ '((4 . "Suspect `[' in char alternative"))))
+ (should (equal (xr-lint "[^][-].]")
+ nil))
)
(provide 'xr-test)
diff --git a/xr.el b/xr.el
index db0adda..eba3a10 100644
--- a/xr.el
+++ b/xr.el
@@ -126,6 +126,21 @@
;; plain character (including ^ or -)
(t
(let ((ch (following-char)))
+ (when (and (eq ch ?\[)
+ ;; Ad-hoc pattern attempting to catch mistakes
+ ;; on the form [...[...]...]
+ ;; where we are ^here
+ (looking-at (rx "["
+ (zero-or-more (not (any "[]")))
+ "]"
+ (zero-or-more (not (any "[]")))
+ (not (any "[\\"))
+ "]"))
+ ;; Only if the alternative didn't start with ]
+ (not (and intervals
+ (eq (aref (car (last intervals)) 0) ?\]))))
+ (xr--report warnings (point)
+ "Suspect `[' in char alternative"))
(push (vector ch ch (point)) intervals))
(forward-char 1))))