emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cond*


From: Ihor Radchenko
Subject: Re: cond*
Date: Mon, 08 Jan 2024 15:26:09 +0000

Richard Stallman <rms@gnu.org> writes:

>   > May you please elaborate how you see named variables in `rx'?
>
> Exactly the same as in `rx' in pcase.
>
> I suppose pcase transforms the (rx ...) construct into a regular
> expression -- right?  If this can be implemented for the (rx ...)
> pattern in pcase, it can be implemented for `rx' the function with the
> same code -- right?
>
> I expect it would work by allocating a subpattern number for each
> (let ...) variable.
>
> Please don't tell me that someone reimplemented `rx' from scratch
> for pcase, rather than using the existing `rx' code!

The way (let ...) expressions are handled inside pcase is defined in
`rx--pcase-expand'. It has nothing to do with rx operation itself.
Instead, it parser the rx pattern, and transforms it into a different
pcase match:

This
(pcase "  #+begin: something"
  ((rx line-start (0+ (any " \t")) "#+BEGIN:" (0+ (any " \t"))
     ;; matches "something"
     ;; (let VAR <sub-expression>) binds VAR to sub-expression match.
     (let matched-value (1+ word)))
   (format "We just matched \"%s\"" matched-value)))

becomes something along the lines of

(pcase "  #+begin: something"
  ((app (lambda (s)
          (and (string-match regexp-generated-by-rx s)
               (list (match-string 0 s)
                     (match-string 1 s))))
        `(_ ,matched-value))

In other words, `rx' pattern matching is implemented by rewriting the
pattern using other `pcase' constructs during macro expansion. The
original `rx' is not changed and does not assign any variables.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]