branch: externals/xr commit d08d40097f4052874c6a526813bd23a7db3d6061 Author: Mattias Engdegård <matti...@acm.org> Commit: Mattias Engdegård <matti...@acm.org>
Warn about reversed ranges in character alternatives [z-a] does not match anything; remove such ranges from the output and warn about them in xr-lint. This can lead to output such as (any), which isn't legal rx, but it's better than keeping the misleading ranges in the output. --- xr-test.el | 3 +++ xr.el | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/xr-test.el b/xr-test.el index 0186fd2..b79c211 100644 --- a/xr-test.el +++ b/xr-test.el @@ -280,6 +280,9 @@ '((2 . "Repetition of repetition") (14 . "Repetition of repetition") (25 . "Repetition of repetition")))) + (should (equal (xr-lint "[]-Qa-fz-t]") + '((1 . "Reversed range `]-Q' matches nothing") + (7 . "Reversed range `z-t' matches nothing")))) ) (provide 'xr-test) diff --git a/xr.el b/xr.el index 142a7ab..838db3e 100644 --- a/xr.el +++ b/xr.el @@ -80,8 +80,12 @@ (let ((set nil)) (cond ;; Initial ]-x range - ((looking-at (rx "]-" (not (any "]")))) - (push (match-string 0) set) + ((looking-at (rx "]-" (group (not (any "]"))))) + (if (>= (string-to-char (match-string 1)) ?\]) + (push (match-string 0) set) + (xr--report warnings (point) + (format "Reversed range `%s' matches nothing" + (match-string 0)))) (goto-char (match-end 0))) ;; Initial ] ((looking-at "]") @@ -107,14 +111,20 @@ ;; become (97 . 122) when printed. ;; TODO: Possibly convert "[0-9]" to digit, and ;; "[0-9a-fA-F]" (and permutations) to hex-digit. - (goto-char (match-end 0)) - (let ((prev (car set))) - ;; Merge with preceding range if any. - (if (and (stringp prev) - (>= (length prev) 3) - (eq (aref prev 1) ?-)) - (setq set (cons (concat prev range) (cdr set))) - (push range set))))) + (cond + ((<= (aref range 0) (aref range 2)) + (let ((prev (car set))) + ;; Merge with preceding range if any. + (if (and (stringp prev) + (>= (length prev) 3) + (eq (aref prev 1) ?-)) + (setq set (cons (concat prev range) (cdr set))) + (push range set)))) + (t + (xr--report warnings (point) + (format "Reversed range `%s' matches nothing" + range)))) + (goto-char (match-end 0)))) ((looking-at (rx eos)) (error "Unterminated character alternative")) ;; plain character (including ^ or -)