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: [PATCH] add compiled regexp primitive lisp object Date: Tue, 13 Aug 2024 21:25:48 -0400 Message-ID: References: <87mslxxddk.fsf@protonmail.com> <5He97LtsyeyQoTLU7d91oP2CLO8s_2afdgcNxozsFjzu8qGbB_7nXmsZL5O6Ej7K-tuEmngCcPKJpDAjxeKz4jk1DvqSUbdOLpw5U1vo1SY=@hypnicjerk.ai> <87le1avopk.fsf@protonmail.com> <2LOLmIp1X8w4CGbqq3qDrzmKVA0KzYNL1N9lBtWdB-MtEv9oCuYgJMYprG170wMPjYxeQImAmWOPatGTTl4KxZMlptNo9A9hnHt84vdN9EA=@hypnicjerk.ai> <87ttfxtszi.fsf@protonmail.com> <86r0b1o5sr.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19973"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , Pip Cet , emacs-devel@gnu.org, mattiase@acm.org, Andrea Corallo , "stefankangas@gmail.com" To: Danny McClanahan Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Aug 14 03:26:49 2024 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 1se2mn-00050l-A6 for ged-emacs-devel@m.gmane-mx.org; Wed, 14 Aug 2024 03:26:49 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1se2m0-00042L-SM; Tue, 13 Aug 2024 21:26:00 -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 1se2lx-000421-4J for emacs-devel@gnu.org; Tue, 13 Aug 2024 21:25:57 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1se2lt-0008N9-Vq; Tue, 13 Aug 2024 21:25:56 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id CD217100061; Tue, 13 Aug 2024 21:25:51 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1723598750; bh=GyYKYAqhkwPq5mkI4NNYVItX/YCgm30jKueR7qS+0UE=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=o0aPXJ9V8DzOGzSHmuYY4vOtusYMNVqnd23JWjPcbs2nFreZU7CVajWvrF7NYNL+i nHWIjrsStolzYX44ZJ1mvpj3LrDZvS5sEfFHiGt2ZjmIfjDWwYKjZtyBJZWvvRP547 IH2yWATBnwXnnV93XDw7Bxx4mXinHUBxeF4CRZ/fd1iMm4Kbwku2rWCIK5Bs2Ytu5J 5Ba20oTP4J6H5h0fLXB8ss7dRqzQZce9ByjRhOeJQIoAQutnsAnqppAUB/NCIzBMtP Ul3VDKq6L18SOcZzurda/1F+vB+hnrrJz1WMKbmcQHmFlXX1E+M8bWxQU4/zAgAqzi BMkJ9sJDvzW1A== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 47358100035; Tue, 13 Aug 2024 21:25:50 -0400 (EDT) Original-Received: from pastel (unknown [216.154.9.87]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 01E101200DB; Tue, 13 Aug 2024 21:25:49 -0400 (EDT) In-Reply-To: (Danny McClanahan's message of "Wed, 07 Aug 2024 04:23:33 +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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:322722 Archived-At: > Right now, I think the optional STRING argument to `match-string' and > `replace-match' is the strongest argument that we are currently working around > a confusing implicit dependency on implicitly-clobbered global match data by > adding new optional arguments to low-level search functions to index into this > implicit global result cache which may be invalidated at any time. I think it > would make elisp programs easier to write if we had the `struct Lisp_Match' > object introduced by this patch. Hmm... I see you're embarking on a significant rethink of the whole regexp search API. I wouldn't want to discourage you (I think there's definitely something things we could do better, the match-data being one example), but it's likely you'll end up with a whole new API. E.g. above you talk about `Lisp_Match` objects but that's largely orthogonal to whether we use strings or `Lisp_Regexp` objects. Our match-data handling is a recurring source of confusion for new (and not so new) ELisp hackers, which tend to want to sprinkle `save-match-data` all over the place. Returning `Lisp_Match` objects would incur the added cost of heap memory allocation (and hence GC pressure), tho. In my ideal world I'd want something more like (re-let (("^\\(?varname:[^=]*\\)=\\(?val:.*\\)" str)) ... use `varname` and `val` ...) [ We can implement a macro that does the above with the current C primitives, BTW. See sample `pcase` macro below. ] > Separately, I think the fact that it's possible to mix up regexp patterns > (interpreted as a regexp string) and string arguments to search against > (interpreted as a literal string), and thereby induce silent search errors, is > the strongest argument for introducing a separate `struct Lisp_Regexp' > object. I don't understand what you're saying here. Currently whether an argument is a regexp or a string is distinguished by the function rather than by the argument (i.e. whether you call `search-forward` or `re-search-forward`). And if we introduce `Lisp_Regexp` objects, I'd expect that any function which accepts `Lisp_Regexp` objects would treat a string as a (not-yet-compiled) regexp, since it'd most likely be the most common use case. [ Because separately/manually compiling regexps before using them is likely to remain the exception rather than the rule for the foreseeable future. ] > The fact that `search-spaces-regexp' can be set by a user and introduce > errors to modes which don't explicitly set it seems to bolster the argument that > introducing an explicit "compile" method for regexp patterns would make it > easier to write correct elisp extensions. Explicitly compiling the regexp indeed could help control the effect of global variables like `search-spaces-regexp`, the syntax table, or `case-fold-search`. At least, assuming they are consulted when the regexp is compiled rather than when it's used (or maybe they'd even have to be passed explicitly to the regexp-compilation function, so as not to depend on the dynamic environment). Stefan (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) (save-match-data (when (string-match ,new-re s) (vector ,@(mapcar (lambda (x) `(match-string ,(cdr x) s)) vars))))) (,'\` [,@(mapcar (lambda (x) (list '\, (car x))) vars)])))))