branch: externals/xr commit db5f17b29c96c9a1cfa50cbf831dec1d27cc9e80 Author: Mattias Engdegård <matti...@acm.org> Commit: Mattias Engdegård <matti...@acm.org>
Check suspicious curly-bracket constructs Warn about \{\} and \{,\} (which really mean \{0\} and *), and signal an error for \{N,M\} where N>M. --- xr-test.el | 5 +++++ xr.el | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/xr-test.el b/xr-test.el index c1c9b31..670a2f2 100644 --- a/xr-test.el +++ b/xr-test.el @@ -56,6 +56,7 @@ '(repeat 0 1 "a"))) (should (equal (xr "a\\{1,\\}") '(>= 1 "a"))) + (should-error (xr "a\\{3,2\\}")) ) (ert-deftest xr-backref () @@ -351,6 +352,10 @@ '((22 . "Duplicated character class `[:digit:]'")))) (should (equal (xr-lint "a*\\|b+\\|\\(?:a\\)*") '((8 . "Duplicated alternative branch")))) + (should (equal (xr-lint "a\\{,\\}") + '((1 . "Uncounted repetition")))) + (should (equal (xr-lint "a\\{\\}") + '((1 . "Implicit zero repetition")))) ) (provide 'xr-test) diff --git a/xr.el b/xr.el index 24249aa..db0adda 100644 --- a/xr.el +++ b/xr.el @@ -342,6 +342,8 @@ ;; Apply a repetition of {LOWER,UPPER} to OPERAND. ;; UPPER may be nil, meaning infinity. (defun xr--repeat (lower upper operand) + (when (and upper (> lower upper)) + (error "Invalid repetition interval")) ;; rx does not accept (= 0 ...) or (>= 0 ...), so we use ;; (repeat 0 0 ...) and (zero-or-more ...), respectively. ;; Note that we cannot just delete the operand if LOWER=UPPER=0, @@ -420,6 +422,11 @@ (comma (match-string 2)) (upper (and (match-string 3) (string-to-number (match-string 3))))) + (unless (or (match-beginning 1) (match-string 3)) + (xr--report warnings (- (match-beginning 0) 2) + (if comma + "Uncounted repetition" + "Implicit zero repetition"))) (goto-char (match-end 0)) (setq sequence (cons (xr--repeat lower