From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 0LDHKhDe0mEKAQEAgWs5BA (envelope-from ) for ; Mon, 03 Jan 2022 12:29:20 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id oIsfKBDe0mG/tAAA9RJhRA (envelope-from ) for ; Mon, 03 Jan 2022 12:29:20 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9E07A24D0A for ; Mon, 3 Jan 2022 12:29:19 +0100 (CET) Received: from localhost ([::1]:53748 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LWg-00027W-MJ for larch@yhetil.org; Mon, 03 Jan 2022 06:29:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51506) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSt-0005JF-Go for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:23 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50957) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSa-0001ox-2i for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:19 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n4LSZ-0005wK-V4 for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#52974] [PATCH 2/5] style: Allow special forms to be scoped. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 03 Jan 2022 11:25:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 52974 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 52974@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 52974-submit@debbugs.gnu.org id=B52974.164120910022781 (code B ref 52974); Mon, 03 Jan 2022 11:25:03 +0000 Received: (at 52974) by debbugs.gnu.org; 3 Jan 2022 11:25:00 +0000 Received: from localhost ([127.0.0.1]:34264 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSW-0005vI-7K for submit@debbugs.gnu.org; Mon, 03 Jan 2022 06:25:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50526) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSS-0005uS-IC for 52974@debbugs.gnu.org; Mon, 03 Jan 2022 06:24:57 -0500 Received: from [2001:470:142:3::e] (port=49188 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSN-0001kN-A4; Mon, 03 Jan 2022 06:24:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=dbVUc/ju0fBSJUMrMz65uedPu3n6kuRNBg3iqIk4zjk=; b=Mdu2Fp48AZHC0Z26EWYt 9g+BBPRZuNiMcUC0zhy7twf6GqsQG6Zs/eQkngfMGRrkNsC05zen2dREXG2EGn5ekBLXdNWaW8uwV NaDOm+/I59sHXCnYrXHvUQijD40OjbpIe7qIkQAxo+CFMaal4kkvODpyUBRMr9HWdj0h58IVBVOdZ Crt+7Wt3fjs3DMwXK+APAmfWQt6syGZNEb6feKP0cfWFchBFCQyVHaDqB+4eZ0y2FOqws2SMfJyq3 xHrqSMpnjmyT01Cj9NGpccsTiTuD9pMwGZtOm5EGgW99y2ejtDkBy5jFffke3a07vRgTDk8gutEEj NnRe4RhzPnPJPQ==; Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=42680 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSN-0005n0-E1; Mon, 03 Jan 2022 06:24:51 -0500 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Mon, 3 Jan 2022 12:24:36 +0100 Message-Id: <20220103112439.14377-2-ludo@gnu.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220103112439.14377-1-ludo@gnu.org> References: <20220103112439.14377-1-ludo@gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1641209359; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=dbVUc/ju0fBSJUMrMz65uedPu3n6kuRNBg3iqIk4zjk=; b=HQ0zctmLxZSEglKNjfqo+yPaA0B2kRZiDX4dYcgEKKBYS/f16CPoMvz4SLBTpIK5gcgghM Q4fnIQZDbyDW5/EWaZdVMkfByHlulWxD4E4GsQ57HUiSADAc48mD4AupzhCWq64hHiWrWc Swu+A+dQYTrEstw/9DdW5BZCxZK0SzUhMRXSz8e4NwYOgtwVzLqEN6L7HfvuNl1Z7yxZAR oltfrdlJfsMT5VfaSUUeMVQJi6jqNSJX00mCPbtteE24WXn9z4frmpSYVreWe7+2hpS2xm ESDv/VuFQuDMl//qJWPPBLAmw8C3mFYau3ix+Bl7O2BGSdPZ4a0I8QuSBZX9Bw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1641209359; a=rsa-sha256; cv=none; b=UXnKSoDN3q+G9lY9Gdaj+1vjR9vDOwdt23WIiCUr/TPOJKn4bFU+cn6XkaPx6bBlHntdkL /P9Uo/ckjilImtzMww7HvGkwr7i2C4bkXxs21RVylwL1Tt+nussA94GkkGWSDXolj6wWWh uloy0R0uerZPSm3xOCzSLzmweJwND/4rxxi9E34HZu3RFJ5iKytoY4r1ohOsA7nQ2wlKvY oqYqZ+cLuV4YMAR6mnb3ClxtaIjxRvnkP/wTEJNvmZkS3LK7cjxpVIVFgraCoMIaFhSRRL Ca+xsO7Tbf47qHtc8jZN3kRvLwA9gY1SDKmLnb157vW1s3FmkGdL2jMaifRKRA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=Mdu2Fp48; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -2.29 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=Mdu2Fp48; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 9E07A24D0A X-Spam-Score: -2.29 X-Migadu-Scanner: scn1.migadu.com X-TUID: j6yYBBEOXIbJ * guix/scripts/style.scm (vhashq): Add clause for 'lst, and change default clause. (%special-forms): Add context for 'add-after and 'add-before. Add 'replace. (prefix?, special-form-lead): New procedures. (special-form?): Remove. (pretty-print-with-comments): Add 'context' to the threaded state. Adjust 'print-sequence' and adjust 'loop' calls accordingly. * tests/style.scm: Add tests for 'replace. --- guix/scripts/style.scm | 88 +++++++++++++++++++++++++++++------------- tests/style.scm | 12 ++++++ 2 files changed, 73 insertions(+), 27 deletions(-) diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index a5204d02ef..625e942613 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -114,14 +114,19 @@ (define (read-with-comments port) ;;; (define-syntax vhashq - (syntax-rules () + (syntax-rules (quote) ((_) vlist-null) + ((_ (key (quote (lst ...))) rest ...) + (vhash-consq key '(lst ...) (vhashq rest ...))) ((_ (key value) rest ...) - (vhash-consq key value (vhashq rest ...))))) + (vhash-consq key '((() . value)) (vhashq rest ...))))) (define %special-forms ;; Forms that are indented specially. The number is meant to be understood - ;; like Emacs' 'scheme-indent-function' symbol property. + ;; like Emacs' 'scheme-indent-function' symbol property. When given an + ;; alist instead of a number, the alist gives "context" in which the symbol + ;; is a special form; for instance, context (modify-phases) means that the + ;; symbol must appear within a (modify-phases ...) expression. (vhashq ('begin 1) ('lambda 2) @@ -148,9 +153,9 @@ (define %special-forms ('operating-system 1) ('modify-inputs 2) ('modify-phases 2) - ('add-after 3) - ('add-before 3) - ;; ('replace 2) + ('add-after '(((modify-phases) . 3))) + ('add-before '(((modify-phases) . 3))) + ('replace '(((modify-phases) . 2))) ;different from 'modify-inputs' ('substitute* 2) ('substitute-keyword-arguments 2) ('call-with-input-file 2) @@ -158,8 +163,30 @@ (define %special-forms ('with-output-to-file 2) ('with-input-from-file 2))) -(define (special-form? symbol) - (vhash-assq symbol %special-forms)) +(define (prefix? candidate lst) + "Return true if CANDIDATE is a prefix of LST." + (let loop ((candidate candidate) + (lst lst)) + (match candidate + (() #t) + ((head1 . rest1) + (match lst + (() #f) + ((head2 . rest2) + (and (equal? head1 head2) + (loop rest1 rest2)))))))) + +(define (special-form-lead symbol context) + "If SYMBOL is a special form in the given CONTEXT, return its number of +arguments; otherwise return #f. CONTEXT is a stack of symbols lexically +surrounding SYMBOL." + (match (vhash-assq symbol %special-forms) + (#f #f) + ((_ . alist) + (any (match-lambda + ((prefix . level) + (and (prefix? prefix context) (- level 1)))) + alist)))) (define (escaped-string str) "Return STR with backslashes and double quotes escaped. Everything else, in @@ -192,8 +219,9 @@ (define* (pretty-print-with-comments port obj (let loop ((indent indent) (column indent) (delimited? #t) ;true if comes after a delimiter + (context '()) ;list of "parent" symbols (obj obj)) - (define (print-sequence indent column lst delimited?) + (define (print-sequence context indent column lst delimited?) (define long? (> (length lst) long-list)) @@ -223,6 +251,7 @@ (define newline? (comment? item) (loop indent column (or newline? delimited?) + context item))))))) (define (sequence-would-protrude? indent lst) @@ -243,6 +272,9 @@ (define (sequence-would-protrude? indent lst) #f)) lst)) + (define (special-form? head) + (special-form-lead head context)) + (match obj ((? comment? comment) (if (comment-margin? comment) @@ -261,45 +293,46 @@ (define (sequence-would-protrude? indent lst) (('quote lst) (unless delimited? (display " " port)) (display "'" port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('quasiquote lst) (unless delimited? (display " " port)) (display "`" port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('unquote lst) (unless delimited? (display " " port)) (display "," port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('unquote-splicing lst) (unless delimited? (display " " port)) (display ",@" port) - (loop indent (+ column (if delimited? 2 3)) #t lst)) + (loop indent (+ column (if delimited? 2 3)) #t context lst)) (('gexp lst) (unless delimited? (display " " port)) (display "#~" port) - (loop indent (+ column (if delimited? 2 3)) #t lst)) + (loop indent (+ column (if delimited? 2 3)) #t context lst)) (('ungexp obj) (unless delimited? (display " " port)) (display "#$" port) - (loop indent (+ column (if delimited? 2 3)) #t obj)) + (loop indent (+ column (if delimited? 2 3)) #t context obj)) (('ungexp-native obj) (unless delimited? (display " " port)) (display "#+" port) - (loop indent (+ column (if delimited? 2 3)) #t obj)) + (loop indent (+ column (if delimited? 2 3)) #t context obj)) (('ungexp-splicing lst) (unless delimited? (display " " port)) (display "#$@" port) - (loop indent (+ column (if delimited? 3 4)) #t lst)) + (loop indent (+ column (if delimited? 3 4)) #t context lst)) (('ungexp-native-splicing lst) (unless delimited? (display " " port)) (display "#+@" port) - (loop indent (+ column (if delimited? 3 4)) #t lst)) + (loop indent (+ column (if delimited? 3 4)) #t context lst)) (((? special-form? head) arguments ...) ;; Special-case 'let', 'lambda', 'modify-inputs', etc. so the second ;; and following arguments are less indented. - (let* ((lead (- (cdr (vhash-assq head %special-forms)) 1)) - (head (symbol->string head)) - (total (length arguments))) + (let* ((lead (special-form-lead head context)) + (context (cons head context)) + (head (symbol->string head)) + (total (length arguments))) (unless delimited? (display " " port)) (display "(" port) (display head port) @@ -327,14 +360,14 @@ (define new-column (() column) ((head . tail) (inner (- n 1) tail - (loop initial-indent - column + (loop initial-indent column (= n lead) + context head))))))) ;; Print the remaining arguments. (let ((column (print-sequence - indent new-column + context indent new-column (drop arguments (min lead total)) #t))) (display ")" port) @@ -343,14 +376,15 @@ (define new-column (let* ((overflow? (>= column max-width)) (column (if overflow? (+ indent 1) - (+ column (if delimited? 1 2))))) + (+ column (if delimited? 1 2)))) + (context (cons head context))) (if overflow? (begin (newline port) (display (make-string indent #\space) port)) (unless delimited? (display " " port))) (display "(" port) - (let* ((new-column (loop column column #t head)) + (let* ((new-column (loop column column #t context head)) (indent (if (or (>= new-column max-width) (not (symbol? head)) (sequence-would-protrude? @@ -358,7 +392,7 @@ (define new-column column (+ new-column 1)))) (define column - (print-sequence indent new-column tail #f)) + (print-sequence context indent new-column tail #f)) (display ")" port) (+ column 1)))) (_ diff --git a/tests/style.scm b/tests/style.scm index d9e8d803f4..6c449cb72e 100644 --- a/tests/style.scm +++ b/tests/style.scm @@ -453,6 +453,18 @@ (define file \"abcdefghijklmnopqrstuvwxyz\")" #:max-width 33) +(test-pretty-print "\ +(modify-phases %standard-phases + (replace 'build + ;; Nicely indented in 'modify-phases' context. + (lambda _ + #t)))") + +(test-pretty-print "\ +(modify-inputs inputs + ;; Regular indentation for 'replace' here. + (replace \"gmp\" gmp))") + (test-end) ;; Local Variables: -- 2.33.0