From patchwork Fri Feb 9 19:25:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herman Rimm X-Patchwork-Id: 59968 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id D985127BBE2; Fri, 9 Feb 2024 19:27:44 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id AD1FA27BBEB for ; Fri, 9 Feb 2024 19:27:43 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rYWWS-0002VX-Nx; Fri, 09 Feb 2024 14:26:52 -0500 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 1rYWWR-0002Ul-JP for guix-patches@gnu.org; Fri, 09 Feb 2024 14:26:51 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rYWWR-0000hz-As; Fri, 09 Feb 2024 14:26:51 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rYWWd-0001bS-GV; Fri, 09 Feb 2024 14:27:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68935] [PATCH v2 4/6] utils: Add find-expression procedure. Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Fri, 09 Feb 2024 19:27:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68935 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 68935@debbugs.gnu.org Cc: ludo@gnu.org, Herman Rimm , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 68935-submit@debbugs.gnu.org id=B68935.17075068076074 (code B ref 68935); Fri, 09 Feb 2024 19:27:03 +0000 Received: (at 68935) by debbugs.gnu.org; 9 Feb 2024 19:26:47 +0000 Received: from localhost ([127.0.0.1]:60234 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rYWWN-0001Zp-Ee for submit@debbugs.gnu.org; Fri, 09 Feb 2024 14:26:47 -0500 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:52227 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rYWWK-0001Ya-8O for 68935@debbugs.gnu.org; Fri, 09 Feb 2024 14:26:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1707506774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iglSg6lJSvpt1mvW6yDKA1v6LBrFqLDh2JD8IjI932s=; b=MRNR7yIGmS53WTOZ9vhbX4SwkOYaON7jlqqfRJz8gbeGb3QdQkvIhb4/viH3aoyL9uwUXb qd/WgKIRGo5h4L5687gQZSbAb9GlvsZ3FXbvrLxSubuYGbVb20vR2Q3WFO+gHwVydvsvHp wxfF6TsWycGiu/otX9xJi+V2icq+C8aQflH6eFkFXmw2R/xuhRBVu9bsoux+xcTdj8Mpko vpBrcV2EyOVoA2Th+lav1Bpu6XEzd6qlLwEgJOTpDs217sALrFokXHBPxHsNH5G64IEtAH xBCvxdJ8hs66KvBqZ92N2DOsUX4ugKPmiKpvO+weP4oX/ZF6gvIPLq901YFpTA== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id a31a6ace (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO); Fri, 9 Feb 2024 19:26:14 +0000 (UTC) Date: Fri, 9 Feb 2024 20:25:16 +0100 Message-ID: <028a3d700965947fa0547eb17d4894b8302eb67c.1707505804.git.herman@rimm.ee> X-Mailer: git-send-email 2.41.0 In-Reply-To: <3c0da641938e96a9f0c491abb74b30c75634df9a.1707505804.git.herman@rimm.ee> References: <3c0da641938e96a9f0c491abb74b30c75634df9a.1707505804.git.herman@rimm.ee> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Herman Rimm X-ACL-Warn: , Herman Rimm via Guix-patches X-Patchwork-Original-From: Herman Rimm via Guix-patches via From: Herman Rimm Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm (find-expression): Add and export procedure. * tests/utils.scm ("find-expression"): Add test. Change-Id: Ie209df39c1f006b20aa6436fb1aef4c84b1694ee --- guix/utils.scm | 24 ++++++++++++++++++++++++ tests/utils.scm | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/guix/utils.scm b/guix/utils.scm index 8f0bc2399e..315feeb232 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -148,6 +148,7 @@ (define-module (guix utils) edit-expression delete-expression insert-expression + find-expression filtered-port decompressed-port @@ -513,6 +514,29 @@ (define (insert-expression source-properties expr) (string-append expr "\n\n" str)))) (edit-expression source-properties insert))) +(define (find-expression file expr proc) + "Search in FILE for a top-level expression which alphabetically +succeeds EXPR. Call PROC with the location if found, or with #f +otherwise." + (let* ((name (match expr + (('define-public symbol _ ...) + (symbol->string symbol)))) + (source-properties + (call-with-input-file + file + (lambda (port) + (do ((syntax (read-syntax port) + (read-syntax port))) + ((match (syntax->datum syntax) + (('define-public symbol _ ...) + (string> (symbol->string symbol) + name)) + ((? eof-object?) #t) + (_ #f)) + (if (eof-object? syntax) + #f (syntax-source syntax)))))))) + (proc source-properties))) + ;;; ;;; Keyword arguments. diff --git a/tests/utils.scm b/tests/utils.scm index cd54112846..feaed4b561 100644 --- a/tests/utils.scm +++ b/tests/utils.scm @@ -288,6 +288,22 @@ (define-public package-2\n 'package)\n" `(define-public package-1 'package)) (call-with-input-file temp-file get-string-all))) +(test-equal "find-expression" + (list `((filename . ,temp-file) (line . 0) (column . 0)) + `((filename . ,temp-file) (line . 5) (column . 0)) + #f) + (begin + (call-with-output-file temp-file + (lambda (port) + (display "(define-public package-1\n 'foo)\n\n" port) + (display "(define foo 'bar)\n\n" port) + (display "(define-public package-2\n 'baz)\n" port))) + (map (lambda (expr) + (find-expression temp-file expr identity)) + (list `(define-public package 'foo) + `(define-public package-1 'bar) + `(define-public package-2 'baz))))) + (test-equal "string-distance" '(0 1 1 5 5) (list