From: Thuna <thuna.cing@gmail.com>
To: 72328@debbugs.gnu.org
Subject: bug#72328: [PATCH] Nested backquote in pcase
Date: Sun, 28 Jul 2024 02:40:00 +0200 [thread overview]
Message-ID: <87jzh62vtr.fsf@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 533 bytes --]
Assuming a hypothetical `(pcase-assert OBJ PAT)' I would expect the
following to succeed:
(pcase-assert '`,nil ``,nil)
(pcase-assert '`,1 ``,,(pred integerp))
(pcase-assert '`,@(list) ``,@,`(list . ,_))
which is to say: a comma should match its argument as a pattern only if
it escapes the original backquote (currently comma at all depths
"escape" the original backquote).
In case there is any interest in this, I have prepared a patch which
both adds the above as tests to pcase-tests.el and implements them in
pcase.el.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Handle-nested-backquotes-in-pcase.patch --]
[-- Type: text/x-patch, Size: 3037 bytes --]
From 9e7ab85e84bbdaf2520dee52aeec8b650b349c6a Mon Sep 17 00:00:00 2001
From: Thuna <thuna.cing@gmail.com>
Date: Sun, 28 Jul 2024 02:34:20 +0200
Subject: [PATCH] Handle nested backquotes in pcase
* lisp/emacs-lisp/pcase.el (pcase--expand-\`): Treat nested commas and
comma-ats as literal symbol matches, only consider their arguments as
patterns (or error in the case of comma-at) if the comma escapes the
original backquote.
* test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-nested-backquote):
Add tests for the behavior of nested backquote patterns.
---
lisp/emacs-lisp/pcase.el | 17 +++++++++++------
test/lisp/emacs-lisp/pcase-tests.el | 5 +++++
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 5a7f3995311..a4c855432ce 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -1142,17 +1142,18 @@ \`
(declare (debug (pcase-QPAT)))
(pcase--expand-\` qpat))
-(defun pcase--expand-\` (qpat)
+(defun pcase--expand-\` (qpat &optional depth)
+ (unless depth (setq depth 0))
(cond
- ((eq (car-safe qpat) '\,) (cadr qpat))
- ((or (eq (car-safe qpat) '\,@) (eq qpat '...))
+ ((and (<= depth 0) (eq (car-safe qpat) '\,)) (cadr qpat))
+ ((and (<= depth 0) (or (eq (car-safe qpat) '\,@) (eq qpat '...)))
(error "Unsupported QPAT: %S" qpat))
((vectorp qpat)
(let* ((trivial t)
(contents nil)
(upats nil))
(dotimes (i (length qpat))
- (let* ((upat (pcase--expand-\` (aref qpat i))))
+ (let* ((upat (pcase--expand-\` (aref qpat i) depth)))
(if (eq (car-safe upat) 'quote)
(push (cadr upat) contents)
(setq trivial nil))
@@ -1163,8 +1164,12 @@ pcase--expand-\`
(app length ,(length qpat))
,@(nreverse upats)))))
((consp qpat)
- (let ((upata (pcase--expand-\` (car qpat)))
- (upatd (pcase--expand-\` (cdr qpat))))
+ (let ((upata (pcase--expand-\` (car qpat) depth))
+ (upatd (pcase--expand-\`
+ (cdr qpat)
+ (cond ((eq (car qpat) '\`) (1+ depth))
+ ((memq (car qpat) '(\, \,@)) (1- depth))
+ (t depth)))))
(if (and (eq (car-safe upata) 'quote) (eq (car-safe upatd) 'quote))
`'(,(cadr upata) . ,(cadr upatd))
`(and (pred consp)
diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el
index e777b71920c..2649e754beb 100644
--- a/test/lisp/emacs-lisp/pcase-tests.el
+++ b/test/lisp/emacs-lisp/pcase-tests.el
@@ -191,4 +191,9 @@ pcase-tests-mutually-exclusive
(should (pcase--mutually-exclusive-p (nth 1 x) (nth 0 x)))
(should-not (pcase--mutually-exclusive-p (nth 1 x) (nth 0 x))))))
+(ert-deftest pcase-tests-nested-backquote ()
+ (should (pcase '`,nil (``,nil t)))
+ (should (pcase '`,1 (``,,(pred integerp) t)))
+ (should (pcase '`,@(list) (``,@,`(list . ,_) t))))
+
;;; pcase-tests.el ends here.
--
2.44.2
next reply other threads:[~2024-07-28 0:40 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-28 0:40 Thuna [this message]
2024-07-28 14:52 ` bug#72328: [PATCH] Nested backquote in pcase Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-28 15:51 ` Thuna
2024-07-28 16:02 ` Eli Zaretskii
2024-07-28 16:20 ` Thuna
2024-07-29 16:03 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-29 16:45 ` Eli Zaretskii
2024-07-30 7:45 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-03 0:07 ` Thuna
2024-08-03 5:59 ` Eli Zaretskii
2024-08-03 13:22 ` Thuna
2024-08-04 17:10 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-04 21:27 ` Thuna
2024-08-05 11:52 ` Eli Zaretskii
2024-08-05 19:32 ` Thuna
2024-08-06 8:21 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-06 11:13 ` Eli Zaretskii
2024-08-06 13:09 ` Thuna
2024-08-07 3:33 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-07 11:53 ` Eli Zaretskii
2024-08-07 17:34 ` Thuna
2024-08-08 5:57 ` Eli Zaretskii
2024-08-23 3:25 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-23 14:49 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-23 16:04 ` Eli Zaretskii
2024-08-23 19:11 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-23 19:29 ` Eli Zaretskii
2024-09-07 7:18 ` Eli Zaretskii
2024-09-07 12:24 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-21 9:03 ` Eli Zaretskii
2024-09-22 13:30 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-22 14:16 ` Eli Zaretskii
2024-09-22 15:21 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-23 11:16 ` Eli Zaretskii
2024-09-24 18:02 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-25 19:04 ` Adam Porter
2024-09-26 6:13 ` Eli Zaretskii
2024-09-26 14:05 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-26 20:41 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-23 20:19 ` Thuna
2024-07-29 17:43 ` Thuna
2024-07-29 19:05 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-29 20:45 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-29 20:59 ` Thuna
2024-07-30 17:53 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
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=87jzh62vtr.fsf@gmail.com \
--to=thuna.cing@gmail.com \
--cc=72328@debbugs.gnu.org \
/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).