From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id aCwgHkib6WICCQAAbAwnHQ (envelope-from ) for ; Tue, 02 Aug 2022 23:46:48 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id AOk6HUib6WKObwEAG6o9tA (envelope-from ) for ; Tue, 02 Aug 2022 23:46:48 +0200 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 EEBDB3D5AB for ; Tue, 2 Aug 2022 23:46:47 +0200 (CEST) Received: from localhost ([::1]:59506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzix-0002rm-4l for larch@yhetil.org; Tue, 02 Aug 2022 17:46:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44468) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhK-0001yN-4f for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55563) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhJ-0006Qp-SG for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIzhJ-0003AO-P0 for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 08/13] read-print: Add code to read and write sequences of expressions/blanks. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 02 Aug 2022 21:45:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56898 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56898@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 56898-submit@debbugs.gnu.org id=B56898.165947668612038 (code B ref 56898); Tue, 02 Aug 2022 21:45:05 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:46 +0000 Received: from localhost ([127.0.0.1]:45297 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgz-000380-MS for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:46 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59572) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgr-00035c-H3 for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:40 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55164) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgm-0006A2-9n; Tue, 02 Aug 2022 17:44:32 -0400 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=Rd6n1QK4C+HEcBGD8ObK/gAdTLW6ydpJwQ2aDI8c9qo=; b=GIxai5dcnJ345Ko/CEXj iRIaPAIRZTSVjc2DiFeNmcwz6t1Df5LetnN2VGcO8aJ73cO3Q1fqGbhgzGhLdieXjryZ27vwSM4NM kXOxqlIQEvZ0fYScxPkUx6o6l9xBZtNnBr0ts0d0N2ETuuR9ScLMK6w/1pGk8AaGQNHe6nfJU+uO2 GQHoZUnlCh0Je8v4WPN1wDa2TZIeZV0i/3H5XrlLssJ6YwLZWddSEy8W7sHymwLkdoJoK9/NNyIDI G8XZZX1OOCTM9LJ/DPSmZ4mxSePXAocfCAH7Y+h9BhurQ8W6VH/RMxdhJ25bAhBGqrwqXXg1jlLmm s6nGZ7TADEFKTA==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:52235 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 1oIzgl-0006W4-PT; Tue, 02 Aug 2022 17:44:32 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:14 +0200 Message-Id: <20220802214419.19013-8-ludo@gnu.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220802214419.19013-1-ludo@gnu.org> References: <20220802214419.19013-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-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1659476808; 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=JaLEOAWk/6cq/H7b5NdKGnFD9UhUwFL+S1zhprcFUng=; b=DDt39C1e/iybB1ctro2AzL7PEQXaSqz52NEMnO0LyLTMMhoznL28WvVmLihpNyC/qMbcNZ W/jDgmOa4+j/IHux/hK3gHHI6uqpgjQUkC/gdMAnpIh67cwXCzkVE1lpTlRJGG+f22+HhE 5Ch1gYOlX/0EkI0MJNTtDieCZF8VyZ+17EWwpQTrGmKHisbtQ/joXpcqZDdVqHf6k9Lw7O ob2V2RYwPPWdl69zyUb6xQb6AJEymY12TgUfgXmJz/me79YU3HrvRCfPGWOYyd9CFDpIfJ WYRy+oviLcIT8RC+aWwbkyPcuaxkqHi+yEUag3UOh/wFoHzBy58hYYwOnDWicg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1659476808; a=rsa-sha256; cv=none; b=dZxW8mZ6QpG6v7vcVzCQ1a3iiggPIfUpuIGH6d6/OOtnHiEo9967WFRFpMq7VE2jao0vRW +PWDCW/zSTtLkeP/bskFyC/vBIZCE5PL9tbgGANIf51P71VhVq6wq8EolyKMiDHxi/sW8L PzO/ajrjCXIC1EmSUa3RCXUcmu7E5QxcziPl5xWfe2M3Erhmj/q1bIgajGY7Bnug75emuH 0TkbMahBe+lbMQF/E42E+OGB/r7+UztSntNYvgYcttvYAvONjPoOp4geYrCD57skgA39dO te89VPigL77MLL8cherk8X35hJL6PRwHnFRm85cadjwQiaB40vcdeDRpcgVH1w== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=GIxai5dc; 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.32 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gnu.org header.s=fencepost-gnu-org header.b=GIxai5dc; 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: EEBDB3D5AB X-Spam-Score: -2.32 X-Migadu-Scanner: scn1.migadu.com X-TUID: +YAfab8Pj4ul * guix/read-print.scm (read-with-comments): Add #:blank-line? and honor it. (read-with-comments/sequence, pretty-print-with-comments/splice): New procedures. * tests/read-print.scm (test-pretty-print/sequence): New macro. Add tests using it. --- guix/read-print.scm | 32 +++++++++++++++++++++++++++++--- tests/read-print.scm | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/guix/read-print.scm b/guix/read-print.scm index 33ed6e3dbe..4a3afdd4f9 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm @@ -25,7 +25,9 @@ (define-module (guix read-print) #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) #:export (pretty-print-with-comments + pretty-print-with-comments/splice read-with-comments + read-with-comments/sequence object->string* blank? @@ -147,8 +149,9 @@ (define (read-until-end-of-line port) ((? space?) (loop)) (chr (unread-char chr port))))) -(define (read-with-comments port) - "Like 'read', but include objects when they're encountered." +(define* (read-with-comments port #:key (blank-line? #t)) + "Like 'read', but include objects when they're encountered. When +BLANK-LINE? is true, assume PORT is at the beginning of a new line." ;; Note: Instead of implementing this functionality in 'read' proper, which ;; is the best approach long-term, this code is a layer on top of 'read', ;; such that we don't have to rely on a specific Guile version. @@ -167,7 +170,7 @@ (define (reverse/dot lst) dotted)) ((x . rest) (loop (cons x result) rest))))) - (let loop ((blank-line? #t) + (let loop ((blank-line? blank-line?) (return (const 'unbalanced))) (match (read-char port) ((? eof-object? eof) @@ -217,6 +220,20 @@ (define (reverse/dot lst) ((and token '#{.}#) (if (eq? chr #\.) dot token)) (token token)))))))) + +(define (read-with-comments/sequence port) + "Read from PORT until the end-of-file is reached and return the list of +expressions and blanks that were read." + (let loop ((lst '()) + (blank-line? #t)) + (match (read-with-comments port #:blank-line? blank-line?) + ((? eof-object?) + (reverse! lst)) + ((? blank? blank) + (loop (cons blank lst) #t)) + (exp + (loop (cons exp lst) #f))))) + ;;; ;;; Comment-preserving pretty-printer. @@ -625,3 +642,12 @@ (define (object->string* obj indent . args) (apply pretty-print-with-comments port obj #:indent indent args)))) + +(define* (pretty-print-with-comments/splice port lst + #:rest rest) + "Write to PORT the expressions and blanks listed in LST." + (for-each (lambda (exp) + (apply pretty-print-with-comments port exp rest) + (unless (blank? exp) + (newline port))) + lst)) diff --git a/tests/read-print.scm b/tests/read-print.scm index 70be7754f8..94f018dd44 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm @@ -33,6 +33,16 @@ (define-syntax-rule (test-pretty-print str args ...) read-with-comments))) (pretty-print-with-comments port exp args ...)))))) +(define-syntax-rule (test-pretty-print/sequence str args ...) + "Likewise, but read and print entire sequences rather than individual +expressions." + (test-equal str + (call-with-output-string + (lambda (port) + (let ((lst (call-with-input-string str + read-with-comments/sequence))) + (pretty-print-with-comments/splice port lst args ...)))))) + (test-begin "read-print") @@ -251,6 +261,33 @@ (define-syntax-rule (test-pretty-print str args ...) ;; page break above end)") +(test-pretty-print/sequence "\ +;;; This is a top-level comment. + + +;; Above is a page break. +(this is an sexp + ;; with a comment + !!) + +;; The end.\n") + +(test-pretty-print/sequence " +;;; Hello! + +(define-module (foo bar) + #:use-module (guix) + #:use-module (gnu)) + + +;; And now, the OS. +(operating-system + (host-name \"komputilo\") + (locale \"eo_EO.UTF-8\") + + (services + (cons (service mcron-service-type) %base-services)))\n") + (test-equal "pretty-print-with-comments, canonicalize-comment" "\ (list abc -- 2.37.1