]> git.eshelyaron.com Git - emacs.git/commitdiff
Add 'rx' pattern for pcase.
authorPhilipp Stephani <phst@google.com>
Thu, 20 Jul 2017 19:36:18 +0000 (21:36 +0200)
committerPhilipp Stephani <phst@google.com>
Sun, 23 Jul 2017 20:32:23 +0000 (22:32 +0200)
* lisp/emacs-lisp/rx.el (rx): New pcase macro.
* test/lisp/emacs-lisp/rx-tests.el (rx-pcase): Add unit test.

etc/NEWS
lisp/emacs-lisp/pcase.el
lisp/emacs-lisp/rx.el
test/lisp/emacs-lisp/rx-tests.el

index 4cb02bf518adf50d3a1d390c32431bfc3b6135cc..f43491b63060beae3a0530716f2f67423141a2fe 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1555,6 +1555,9 @@ manual.
 ** 'tcl-auto-fill-mode' is now declared obsolete.  Its functionality
 can be replicated simply by setting 'comment-auto-fill-only-comments'.
 
+** New pcase pattern 'rx' to match against a rx-style regular
+expression.
+
 \f
 * Changes in Emacs 26.1 on Non-Free Operating Systems
 
index 4a06ab25d3e4d2236895d491ee0e05c56c6f68eb..b40161104d21eac97dd917c5a377e8c0945e8fa7 100644 (file)
@@ -930,6 +930,5 @@ QPAT can take the following forms:
    ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat)
    (t (error "Unknown QPAT: %S" qpat))))
 
-
 (provide 'pcase)
 ;;; pcase.el ends here
index 386232c6eef9092094f50fe816082f80115565d5..b66f2c6d5121a5c7f19a7aea13efb53d49fb4625 100644 (file)
@@ -1169,6 +1169,62 @@ enclosed in `(and ...)'.
         (rx-to-string `(and ,@regexps) t))
        (t
         (rx-to-string (car regexps) t))))
+
+
+(pcase-defmacro rx (&rest regexps)
+  "Build a `pcase' pattern matching `rx' regexps.
+The REGEXPS are interpreted as by `rx'.  The pattern matches if
+the regular expression so constructed matches the object, as if
+by `string-match'.
+
+In addition to the usual `rx' constructs, REGEXPS can contain the
+following constructs:
+
+  (let VAR FORM...)  creates a new explicitly numbered submatch
+                     that matches FORM and binds the match to
+                     VAR.
+  (backref VAR)      creates a backreference to the submatch
+                     introduced by a previous (let VAR ...)
+                     construct.
+
+The VARs are associated with explicitly numbered submatches
+starting from 1.  Multiple occurrences of the same VAR refer to
+the same submatch.
+
+If a case matches, the match data is modified as usual so you can
+use it in the case body, but you still have to pass the correct
+string as argument to `match-string'."
+  (let* ((vars ())
+         (rx-constituents
+          `((let
+             ,(lambda (form)
+                (rx-check form)
+                (let ((var (cadr form)))
+                  (cl-check-type var symbol)
+                  (let ((i (or (cl-position var vars :test #'eq)
+                               (prog1 (length vars)
+                                 (setq vars `(,@vars ,var))))))
+                    (rx-form `(submatch-n ,(1+ i) ,@(cddr form))))))
+             1 nil)
+            (backref
+             ,(lambda (form)
+                (rx-check form)
+                (rx-backref
+                 `(backref ,(let ((var (cadr form)))
+                              (if (integerp var) var
+                                (1+ (cl-position var vars :test #'eq)))))))
+             1 1
+             ,(lambda (var)
+                (cond ((integerp var) (rx-check-backref var))
+                      ((memq var vars) t)
+                      (t (error "rx `backref' variable must be one of %s: %s"
+                                vars var)))))
+            ,@rx-constituents))
+         (regexp (rx-to-string `(seq ,@regexps) :no-group)))
+    `(and (pred (string-match ,regexp))
+          ,@(cl-loop for i from 1
+                     for var in vars
+                     collect `(app (match-string ,i) ,var)))))
 \f
 ;; ;; sregex.el replacement
 
index 8b7945c9d27108c61ab61943a167e4006bec6663..8f353b7e863e7b9140f55c73411376b66bdddcae 100644 (file)
                                   (number-sequence ?< ?\])
                                   (number-sequence ?- ?:))))))
 
+(ert-deftest rx-pcase ()
+  (should (equal (pcase "a 1 2 3 1 1 b"
+                   ((rx (let u (+ digit)) space
+                        (let v (+ digit)) space
+                        (let v (+ digit)) space
+                        (backref u) space
+                        (backref 1))
+                    (list u v)))
+                 '("1" "3"))))
+
 (provide 'rx-tests)
 ;; rx-tests.el ends here.