unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "Basil L. Contovounesios" <contovob@tcd.ie>
Cc: "Mattias Engdegård" <mattiase@acm.org>,
	"Ag Ibragimov" <agzam.ibragimov@gmail.com>,
	emacs-devel@gnu.org
Subject: Re: Pattern matching on match-string groups #elisp #question
Date: Thu, 25 Feb 2021 10:32:44 -0500	[thread overview]
Message-ID: <jwvv9aggooq.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87v9agxkld.fsf@tcd.ie> (Basil L. Contovounesios's message of "Thu, 25 Feb 2021 14:55:58 +0000")

> 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))




  reply	other threads:[~2021-02-25 15:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25  5:11 Pattern matching on match-string groups #elisp #question Ag Ibragimov
2021-02-25 14:55 ` Basil L. Contovounesios
2021-02-25 15:32   ` Stefan Monnier [this message]
2021-02-25 18:28     ` Mattias Engdegård
2021-02-26  4:31       ` Stefan Monnier
2021-02-26 10:24         ` Mattias Engdegård
2021-02-26 19:38           ` Stefan Monnier
2021-02-27 10:17             ` Mattias Engdegård
2021-02-27 14:39               ` Stefan Monnier
2021-02-27 18:10                 ` Mattias Engdegård
2021-02-27 20:32                   ` Stefan Monnier
2021-02-28 13:46                     ` Mattias Engdegård
2021-02-28 15:37                       ` Stefan Monnier

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=jwvv9aggooq.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=agzam.ibragimov@gmail.com \
    --cc=contovob@tcd.ie \
    --cc=emacs-devel@gnu.org \
    --cc=mattiase@acm.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).