From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Pattern matching on match-string groups #elisp #question Date: Thu, 25 Feb 2021 10:32:44 -0500 Message-ID: References: <87v9agxkld.fsf@tcd.ie> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35304"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: Mattias =?windows-1252?Q?Engdeg=E5rd?= , Ag Ibragimov , emacs-devel@gnu.org To: "Basil L. Contovounesios" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Feb 25 16:34:21 2021 Return-path: Envelope-to: ged-emacs-devel@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 1lFIei-00094E-Fy for ged-emacs-devel@m.gmane-mx.org; Thu, 25 Feb 2021 16:34:20 +0100 Original-Received: from localhost ([::1]:34174 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFIeh-0002Mu-Iu for ged-emacs-devel@m.gmane-mx.org; Thu, 25 Feb 2021 10:34:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49090) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFIdL-0001cb-2N for emacs-devel@gnu.org; Thu, 25 Feb 2021 10:32:55 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:30204) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFIdH-0007Mb-1Y for emacs-devel@gnu.org; Thu, 25 Feb 2021 10:32:54 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id C431F44005B; Thu, 25 Feb 2021 10:32:46 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 0783544183F; Thu, 25 Feb 2021 10:32:45 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1614267165; bh=LJu/xYerlnc0wwpp+CtlbFf94BVvGz0A1T4PJeDfM6o=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=VzpgkIPopdwLyq6Tu9qo11/gOvGmRTMjAga/VQ4bAl47iZZgHK/G79XAWT7H2sMWF RP2mKIpnZHhOJNB/ISJhHk0gqs7msiSi2i+nXlsz0qqT8TxVfdy9flkLrBjKZnZrfL OmKxT56pHLhAt2bsSxNJRRflsI/1baqXwMOlx+sh+uaiSIrgg01kS1xcOBn3bJ2poY 8BgaliMZQPmWofdr+SLzs3QrgcRLKc2bQ34WjjjocseeHAhTN0joksWi6IT5z+IJCu 3V8GvQgU6e1XLT+/nY9+/LfcOectvOrPa2lYl5bsVzuQiMq3PYtU2qvMVYu1hdA8Ij ee2/hlc8AEZeQ== Original-Received: from alfajor (unknown [216.154.41.47]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 162711203CE; Thu, 25 Feb 2021 10:32:45 -0500 (EST) In-Reply-To: <87v9agxkld.fsf@tcd.ie> (Basil L. Contovounesios's message of "Thu, 25 Feb 2021 14:55:58 +0000") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:265625 Archived-At: > The closest equivalent pcase magic I can think of is: > > (pcase "lorem-foo-1-ipsum-bar-21_baz-42" > ((rx (let group-1 "foo-" (+ num)) (* nonl) > (let group-2 "bar-" (+ num)) (* nonl) > (let group-3 "baz-" (+ num))) > (format "%s-%s-%s" group-1 group-2 group-3))) > ;; => "foo-1-bar-21-baz-42" > > The same won't work with pcase-let, so it's either unsupported or a bug. I'd say it's a bug. The patch below would fix it. Mattias, WDYT? FWIW, I have a similar pcase pattern using "old style regexps" instead of rx, but I haven't bothered to install it: (pcase-defmacro re-match (re) "Matches a string if that string matches RE. RE should be a regular expression (a string). It can use the special syntax \\(?VAR: to bind a sub-match to variable VAR. All other subgroups are treated as shy. Multiple uses of this macro in a single `pcase' are not optimized together, so don't expect lex-like performance. But in order for such optimization to be possible in some distant future, back-references are not supported." (let ((start 0) (last 0) (new-re '()) (vars '()) (gn 0)) (while (string-match "\\\\(\\(?:\\?\\([-[:alnum:]]*\\):\\)?" re start) (setq start (match-end 0)) (let ((beg (match-beginning 0)) (name (match-string 1 re))) ;; Skip false positives, either backslash-escaped or within [...]. (when (subregexp-context-p re start last) (cond ((null name) (push (concat (substring re last beg) "\\(?:") new-re)) ((string-match "\\`[0-9]" name) (error "Variable can't start with a digit: %S" name)) (t (let* ((var (intern name)) (id (cdr (assq var vars)))) (unless id (setq gn (1+ gn)) (setq id gn) (push (cons var gn) vars)) (push (concat (substring re last beg) (format "\\(?%d:" id)) new-re)))) (setq last start)))) (push (substring re last) new-re) (setq new-re (mapconcat #'identity (nreverse new-re) "")) `(and (pred stringp) (app (lambda (s) (when (string-match ,new-re s) (vector ,@(mapcar (lambda (x) `(match-string ,(cdr x) s)) vars)))) (,'\` [,@(mapcar (lambda (x) (list '\, (car x))) vars)]))))) [ Not sure why I decided to use a vector internally. ] Stefan diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el index 58584f300c..619bc32752 100644 --- a/lisp/emacs-lisp/rx.el +++ b/lisp/emacs-lisp/rx.el @@ -1437,7 +1437,8 @@ rx construct." (let* ((rx--pcase-vars nil) (regexp (rx--to-expr (rx--pcase-transform (cons 'seq regexps))))) - `(and (pred (string-match ,regexp)) + `(and (pred stringp) + (app (lambda (s) (string-match ,regexp s)) (pred identity)) ,@(let ((i 0)) (mapcar (lambda (name) (setq i (1+ i))