Allow zero-argument rx or' and
seq' forms
Make the rx `or' and `seq' forms accept zero arguments to produce a never-matching regexp and an empty string, respectively. * lisp/emacs-lisp/rx.el (rx-constituents, rx-or): Permit zero args. (rx): Amend doc string for `or' and `seq'. * test/lisp/emacs-lisp/rx-tests.el (rx-or, rx-seq): Test the change. * etc/NEWS (Changes in Specialized Modes and Packages): Mention the change.
This commit is contained in:
parent
e9f9827eb0
commit
b552fc05c2
3 changed files with 21 additions and 6 deletions
6
etc/NEWS
6
etc/NEWS
|
@ -1321,6 +1321,12 @@ when given in a string. Previously, '(any "\x80-\xff")' would match
|
||||||
characters U+0080...U+00FF. Now the expression matches raw bytes in
|
characters U+0080...U+00FF. Now the expression matches raw bytes in
|
||||||
the 128...255 range, as expected.
|
the 128...255 range, as expected.
|
||||||
|
|
||||||
|
*** The rx 'or' and 'seq' forms no longer require any arguments.
|
||||||
|
(or) produces a regexp that never matches anything, while (seq)
|
||||||
|
matches the empty string, each being an identity for the operation.
|
||||||
|
This also works for their aliases: '|' for 'or'; ':', 'and' and
|
||||||
|
'sequence' for 'seq'.
|
||||||
|
|
||||||
** Frames
|
** Frames
|
||||||
|
|
||||||
+++
|
+++
|
||||||
|
|
|
@ -110,11 +110,11 @@
|
||||||
;; FIXME: support macros.
|
;; FIXME: support macros.
|
||||||
|
|
||||||
(defvar rx-constituents ;Not `const' because some modes extend it.
|
(defvar rx-constituents ;Not `const' because some modes extend it.
|
||||||
'((and . (rx-and 1 nil))
|
'((and . (rx-and 0 nil))
|
||||||
(seq . and) ; SRE
|
(seq . and) ; SRE
|
||||||
(: . and) ; SRE
|
(: . and) ; SRE
|
||||||
(sequence . and) ; sregex
|
(sequence . and) ; sregex
|
||||||
(or . (rx-or 1 nil))
|
(or . (rx-or 0 nil))
|
||||||
(| . or) ; SRE
|
(| . or) ; SRE
|
||||||
(not-newline . ".")
|
(not-newline . ".")
|
||||||
(nonl . not-newline) ; SRE
|
(nonl . not-newline) ; SRE
|
||||||
|
@ -390,9 +390,11 @@ FORM is of the form `(and FORM1 ...)'."
|
||||||
"Parse and produce code from FORM, which is `(or FORM1 ...)'."
|
"Parse and produce code from FORM, which is `(or FORM1 ...)'."
|
||||||
(rx-check form)
|
(rx-check form)
|
||||||
(rx-group-if
|
(rx-group-if
|
||||||
(if (memq nil (mapcar 'stringp (cdr form)))
|
(cond
|
||||||
(mapconcat (lambda (x) (rx-form x '|)) (cdr form) "\\|")
|
((null (cdr form)) regexp-unmatchable)
|
||||||
|
((cl-every #'stringp (cdr form))
|
||||||
(regexp-opt (cdr form) nil t))
|
(regexp-opt (cdr form) nil t))
|
||||||
|
(t (mapconcat (lambda (x) (rx-form x '|)) (cdr form) "\\|")))
|
||||||
(and (memq rx-parent '(: * t)) rx-parent)))
|
(and (memq rx-parent '(: * t)) rx-parent)))
|
||||||
|
|
||||||
|
|
||||||
|
@ -1121,6 +1123,7 @@ CHAR
|
||||||
`(seq SEXP1 SEXP2 ...)'
|
`(seq SEXP1 SEXP2 ...)'
|
||||||
`(sequence SEXP1 SEXP2 ...)'
|
`(sequence SEXP1 SEXP2 ...)'
|
||||||
matches what SEXP1 matches, followed by what SEXP2 matches, etc.
|
matches what SEXP1 matches, followed by what SEXP2 matches, etc.
|
||||||
|
Without arguments, matches the empty string.
|
||||||
|
|
||||||
`(submatch SEXP1 SEXP2 ...)'
|
`(submatch SEXP1 SEXP2 ...)'
|
||||||
`(group SEXP1 SEXP2 ...)'
|
`(group SEXP1 SEXP2 ...)'
|
||||||
|
@ -1136,7 +1139,7 @@ CHAR
|
||||||
`(| SEXP1 SEXP2 ...)'
|
`(| SEXP1 SEXP2 ...)'
|
||||||
matches anything that matches SEXP1 or SEXP2, etc. If all
|
matches anything that matches SEXP1 or SEXP2, etc. If all
|
||||||
args are strings, use `regexp-opt' to optimize the resulting
|
args are strings, use `regexp-opt' to optimize the resulting
|
||||||
regular expression.
|
regular expression. Without arguments, never matches anything.
|
||||||
|
|
||||||
`(minimal-match SEXP)'
|
`(minimal-match SEXP)'
|
||||||
produce a non-greedy regexp for SEXP. Normally, regexps matching
|
produce a non-greedy regexp for SEXP. Normally, regexps matching
|
||||||
|
|
|
@ -107,7 +107,13 @@
|
||||||
"ab"))
|
"ab"))
|
||||||
(should (equal (and (string-match (rx (or "a" "ab" "abc")) s)
|
(should (equal (and (string-match (rx (or "a" "ab" "abc")) s)
|
||||||
(match-string 0 s))
|
(match-string 0 s))
|
||||||
"a"))))
|
"a")))
|
||||||
|
;; Test zero-argument `or'.
|
||||||
|
(should (equal (rx (or)) regexp-unmatchable)))
|
||||||
|
|
||||||
|
(ert-deftest rx-seq ()
|
||||||
|
;; Test zero-argument `seq'.
|
||||||
|
(should (equal (rx (seq)) "")))
|
||||||
|
|
||||||
(provide 'rx-tests)
|
(provide 'rx-tests)
|
||||||
;; rx-tests.el ends here.
|
;; rx-tests.el ends here.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue