From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thuna Newsgroups: gmane.emacs.bugs Subject: bug#72328: [PATCH] Nested backquote in pcase Date: Sun, 28 Jul 2024 02:40:00 +0200 Message-ID: <87jzh62vtr.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24667"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: 72328@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jul 28 03:06:17 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sXsMb-0006Cz-AT for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 28 Jul 2024 03:06:17 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sXsMH-00056m-JM; Sat, 27 Jul 2024 21:05:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sXsMC-00056N-HV for bug-gnu-emacs@gnu.org; Sat, 27 Jul 2024 21:05:52 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sXsMC-00060G-8u for bug-gnu-emacs@gnu.org; Sat, 27 Jul 2024 21:05:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=I0o7+uFlPCijem/fyWW1LyCyp4hDEUHgkaWCfUlNCjo=; b=oLbkkW77eij1RFcXuH3nhGUNOhmDaWSfPfNzTZvzLuZHy9WH0k943g1izhuPP8DhH/kWVUfQl2TlzsJdbcXtWmj4nNfpPijeJqeVaH31j3D09o+m/u1fgH5g8W6XDywt0BY08ASmAbnFU9lZg6NXCFwDixCxYkNOiKcOiRkJJhqREFb/6cgwnWw7f1xfUp59g/ZB1AIhKqPZ+WwX2r2sdAMhbUV3FgTyn5v0im9z7hFYj4xES6pOnbOf1ckhWQ0iAAFkhxyfec3iUMCVUspVE9wWnh/KHlNqSGEmi58Rcmi5kxZ68g5zPeGvVfFIdY2/KIhIUjXkGhrBk2H+ATcCyw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sXsMM-0006H0-Dc for bug-gnu-emacs@gnu.org; Sat, 27 Jul 2024 21:06:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Thuna Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 28 Jul 2024 01:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 72328 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.172212872924063 (code B ref -1); Sun, 28 Jul 2024 01:06:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 28 Jul 2024 01:05:29 +0000 Original-Received: from localhost ([127.0.0.1]:42531 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sXsLo-0006G2-H9 for submit@debbugs.gnu.org; Sat, 27 Jul 2024 21:05:28 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:34768) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sXsLm-0006Fv-TZ for submit@debbugs.gnu.org; Sat, 27 Jul 2024 21:05:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sXsJW-0004l6-8b for bug-gnu-emacs@gnu.org; Sat, 27 Jul 2024 21:03:06 -0400 Original-Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sXsJU-0005fE-DM for bug-gnu-emacs@gnu.org; Sat, 27 Jul 2024 21:03:05 -0400 Original-Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-428101fa30aso7176535e9.3 for ; Sat, 27 Jul 2024 18:03:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722128582; x=1722733382; darn=gnu.org; h=user-agent:mime-version:message-id:date:subject:to:from:from:to:cc :subject:date:message-id:reply-to; bh=I0o7+uFlPCijem/fyWW1LyCyp4hDEUHgkaWCfUlNCjo=; b=YDECmJL2wj1VdCIdicl/fFAl7pu7avFVXdEl/1zU+vHGUrOiSlxSzknt8EgREdxtCE LqF3gP8gwOBezDNJZ+c8hbuXb04GoZmdco0vitv0I26ticYGtDFvGBLD6ekfLFUH7493 00UXHmbmOdbAGidh6pb/5LPTYNADawb1BhDdb8qKBANGen+5wzmHcFnsVIrVmwzMqDVM 8w5WCeAvC9KjFqvqHcxcYSkK/C0XfluaHW255T3EJawa/eR50j+6ggX8ReurBuTno6Ww NDRRzXrtj/dbZZ8eIcWLMtj+SkjKZKDE2KPlSURiU8dh0agodxPTPjmTWiRx5xl/JkVg u6og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722128582; x=1722733382; h=user-agent:mime-version:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=I0o7+uFlPCijem/fyWW1LyCyp4hDEUHgkaWCfUlNCjo=; b=Wh9/Wz0qNMNUadv0rHcBHaKfISVpkl8gHucJHQSoq18HVyy8CbOuVUTse6txOWdKCj H1knjy9g6Wjegpdwz0bxjkCL87clHCyBhaHYAgN0n+4dS+upl2ZkjAUnjW357R1osUJ1 jot8YMYp1i5Q5Qi19qmLjaJ4B7vPCrEMAE3LzBNApc8HI4raWqr1g8VFowZb12Ys6pWd hWdRKEurT3PL9dZJEau/yh94pfckGrL2NUw68AxkElpdfDW5YcdbAkAuTPEbOo5YCw9K o5ha6R1KEUXJpiPbnNjexaNi4tm9XGzcYTHGU/1Y5p/iblwjLPSPvrzOly5TU1UqG2W5 J1pQ== X-Gm-Message-State: AOJu0YwE00WT9cd1UU/4hvjaDkzMwC3MUzvQu/UepZMddiZmWNmNPh1n ySxF7jrkLW4/OI/o9IqyDvWVgo5FTWjvCrdOpX28QttrHidbHPHKx7W0BA== X-Google-Smtp-Source: AGHT+IFf607LH08lZZjau9qhj9xREwQqpJNKm4XFVsA7DVrWWn+5JOCC8Kzr7fGZMJ+Lkk4BDCdDYg== X-Received: by 2002:a05:600c:1f96:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-4281310b8dfmr25357285e9.34.1722128581697; Sat, 27 Jul 2024 18:03:01 -0700 (PDT) Original-Received: from thuna-lis3 ([85.106.105.81]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367fca75sm8686492f8f.58.2024.07.27.18.03.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jul 2024 18:03:01 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=thuna.cing@gmail.com; helo=mail-wm1-x32a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:289449 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Handle-nested-backquotes-in-pcase.patch >From 9e7ab85e84bbdaf2520dee52aeec8b650b349c6a Mon Sep 17 00:00:00 2001 From: Thuna 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 --=-=-=--