all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Kévin Le Gouguec" <kevin.legouguec@gmail.com>
To: Platon Pronko <platon7pronko@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: rx and rx-to-string handle (regexp VAR) differently
Date: Fri, 09 Jun 2023 07:35:29 +0200	[thread overview]
Message-ID: <87r0qlgpum.fsf@gmail.com> (raw)
In-Reply-To: <257320f0-697c-cbc3-fcfc-584d6ac7051c@gmail.com> (Platon Pronko's message of "Fri, 9 Jun 2023 08:57:52 +0800")

Platon Pronko <platon7pronko@gmail.com> writes:

>> Is there a bug to file here, or have I missed something in the
>> documentation that explains why rx can work with (regexp VAR) but
>> rx-to-string cannot?
>
> rx-to-string docs explicitly say: "The arguments to ‘literal’ and ‘regexp’ forms
> inside FORM must be constant strings."

Drats.  So they do.  Second sentence of the docstring no less; and a
completely separate paragraph in "(elisp) Rx Functions".

I have no words 🤦

> As to "why it was done like this?", I guess that's because (rx) is a macro and
> (rx-to-string) is a function. So (rx-to-string) tries to evaluate everything in
> a self-contained way, without accessing outside environment (this might prevent
> certain error cases when rx form is created in one place, then stored and
> evaluated in a different context).
> But (rx) is a macro, and thus it can be free to expand to whatever it likes - so
> it sets (rx--delayed-evaluation t) and outputs an expression that references
> outside variables.
>
>> Re(-re)ⁿ-reading that last sentence makes me tentatively conclude that
>> foo-regexp is not in rx-to-string's "current lexical environment", but
>> somehow is in rx's?
>
> In this case it actually is in rx-to-string's current lexical environment, but
> it just doesn't care - it checks if expression is a literal string and throws an
> error otherwise.
> And in rx's case it also is in the current lexical environment, however the
> symbol evaluation happens only after the macro was expanded - (rx) itself simply
> returns foo-regexp as-is:
>
> (macroexpand-1 '(rx foo)) ;; output: foo-regexp

Thanks for the perspective, much appreciated!



  parent reply	other threads:[~2023-06-09  5:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 21:44 rx and rx-to-string handle (regexp VAR) differently Kévin Le Gouguec
2023-06-09  0:57 ` Platon Pronko
2023-06-09  1:36   ` Michael Heerdegen
2023-06-09  6:20     ` Kévin Le Gouguec
2023-06-09  5:35   ` Kévin Le Gouguec [this message]
2023-06-09  6:05 ` Eli Zaretskii
2023-06-09  6:35   ` Kévin Le Gouguec

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0qlgpum.fsf@gmail.com \
    --to=kevin.legouguec@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=platon7pronko@gmail.com \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.