unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: 37659@debbugs.gnu.org
Subject: bug#37659: rx additions: anychar, unmatchable, unordered-or
Date: Sun, 27 Oct 2019 12:53:05 +0100	[thread overview]
Message-ID: <4406A98D-708A-4B04-881A-AD90F7EFF36C@acm.org> (raw)
In-Reply-To: <6B3E322E-6058-4D8B-A73C-07847411AE1D@acm.org>

[-- Attachment #1: Type: text/plain, Size: 858 bytes --]

An observation is that 'or-max' cannot currently be defined by the user, because there is no way to expand rx forms explicitly. One way to fill that hole is to add the function

  (rx-expand-definitions RX-FORM)

which would expand RX-FORM until it no longer is a user-defined form.
This would permit or-max to be defined as

(rx-define or-max (&rest forms)
  (eval `(regexp ,(regexp-opt (or-max-strings (list forms))))))

(defun or-max-strings (args)
  (mapcan (lambda (item)
            (pcase item
              ((pred stringp) (list item))
              (`(or-max . ,rest) (or-max-strings rest))
              (_ (error "Illegal `or-max' argument: %S" item))))
          (mapcar #'rx-expand-definitions args)))

Of course, if the 'or-max' operator is generally useful, it would probably still make sense to define it as a primitive.


[-- Attachment #2: 0001-Add-rx-expand-definitions.patch --]
[-- Type: application/octet-stream, Size: 2520 bytes --]

From 52df813cac0ecb38f62d6740082ee6741ca0e167 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Sun, 27 Oct 2019 12:50:15 +0100
Subject: [PATCH] Add `rx-expand-definitions'

* lisp/emacs-lisp/rx.el (rx-expand-definitions): New function.
* test/lisp/emacs-lisp/rx-tests.el (rx-expand-definitions): Test.

Add `rx-expand-definitions', allowing explicit expansion of rx forms
inside (eval ...). (Bug#37659)
---
 lisp/emacs-lisp/rx.el            |  8 ++++++++
 test/lisp/emacs-lisp/rx-tests.el | 17 +++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el
index 52a35ffa2a..660b336efd 100644
--- a/lisp/emacs-lisp/rx.el
+++ b/lisp/emacs-lisp/rx.el
@@ -143,6 +143,14 @@ rx--expand-def
                      op (cdr form) (nth 0 def) (nth 1 def))
                   (error "Not an `rx' form definition: %s" op)))))))
 
+(defun rx-expand-definitions (form)
+  "Expand FORM until it is no longer a user-defined rx construct.
+Then return the result."
+  (let ((expanded (rx--expand-def form)))
+    (if expanded
+        (rx-expand-definitions expanded)
+      form)))
+
 ;; TODO: Additions to consider:
 ;; - A construct like `or' but without the match order guarantee,
 ;;   maybe `unordered-or'.  Useful for composition or generation of
diff --git a/test/lisp/emacs-lisp/rx-tests.el b/test/lisp/emacs-lisp/rx-tests.el
index 4ecc805aea..369dab83f6 100644
--- a/test/lisp/emacs-lisp/rx-tests.el
+++ b/test/lisp/emacs-lisp/rx-tests.el
@@ -419,6 +419,23 @@ rx-def-in-not
     (should (equal (rx (not (d ?m)) (not (e symbol)))
                    "[^amz]\\S_"))))
 
+(ert-deftest rx-expand-definitions ()
+  (rx-let ((a b)
+           (b (* nonl)))
+    (should (equal (rx (eval (rx-expand-definitions 'space)))
+                   "[[:space:]]"))
+    (should (equal (rx (eval (rx-expand-definitions 'a)))
+                   ".*")))
+  (rx-let-eval '((f (x) (seq ?a x ?b))
+                 (g (y) (f (+ y))))
+    (should (equal (rx-to-string '(eval (rx-expand-definitions 'digit)) t)
+                   "[[:digit:]]"))
+    (should (equal (rx-to-string '(eval (rx-expand-definitions '(g ?c))) t)
+                   "ac+b")))
+  (rx-define rx--g "z")
+  (should (equal (rx (eval (rx-expand-definitions 'rx--g)))
+                 "z")))
+
 (ert-deftest rx-constituents ()
   (let ((rx-constituents
          (append '((beta . gamma)
-- 
2.21.0 (Apple Git-122)


  reply	other threads:[~2019-10-27 11:53 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-08  9:36 bug#37659: rx additions: anychar, unmatchable, unordered-or Mattias Engdegård
2019-10-09  8:59 ` Mattias Engdegård
2019-10-11 23:07 ` bug#37659: Mattias Engdegård <mattiase <at> acm.org> Paul Eggert
2019-10-12 10:47   ` Mattias Engdegård
2019-10-13 16:52     ` Paul Eggert
2019-10-13 19:48       ` Mattias Engdegård
2019-10-22 15:14       ` bug#37659: rx additions: anychar, unmatchable, unordered-or Mattias Engdegård
2019-10-22 15:27         ` Robert Pluim
2019-10-22 17:33         ` Paul Eggert
2019-10-23  9:15           ` Mattias Engdegård
2019-10-23 23:14             ` Paul Eggert
2019-10-24  1:56               ` Drew Adams
2019-10-24  9:09                 ` Mattias Engdegård
2019-10-24 14:24                   ` Drew Adams
2019-10-24  9:17                 ` Phil Sainty
2019-10-24 14:32                   ` Drew Adams
2019-10-24  8:58               ` Mattias Engdegård
2019-10-27 11:53                 ` Mattias Engdegård [this message]
2020-02-11 12:57           ` Mattias Engdegård
2020-02-11 15:43             ` Eli Zaretskii
2020-02-11 19:17               ` Mattias Engdegård
2020-02-12  0:52                 ` Paul Eggert
2020-02-12 11:22                   ` Mattias Engdegård
2020-02-13 18:38                     ` Mattias Engdegård
2020-02-13 18:50                       ` Paul Eggert
2020-02-13 19:16                         ` Mattias Engdegård
2020-02-13 19:30                           ` Eli Zaretskii
2020-02-13 22:23                             ` Mattias Engdegård
2020-02-14  7:45                               ` Eli Zaretskii
2020-02-14 16:15                                 ` Paul Eggert
2020-02-14 20:49                                   ` Mattias Engdegård
2020-03-01 10:09                                   ` Mattias Engdegård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4406A98D-708A-4B04-881A-AD90F7EFF36C@acm.org \
    --to=mattiase@acm.org \
    --cc=37659@debbugs.gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).