aboutsummaryrefslogtreecommitdiff
path: root/guix/read-print.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-08-02 17:08:53 +0200
committerLudovic Courtès <ludo@gnu.org>2022-08-08 11:22:32 +0200
commit9b00c97de41165beefe3eff936470f8e081ca600 (patch)
tree618b5533951927f672dfee73ec1e980800ec3e87 /guix/read-print.scm
parent077324a16fd6c7d2307bfa52a9863ca25cfc6a52 (diff)
downloadguix-9b00c97de41165beefe3eff936470f8e081ca600.tar
guix-9b00c97de41165beefe3eff936470f8e081ca600.tar.gz
read-print: Add code to read and write sequences of expressions/blanks.
* 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.
Diffstat (limited to 'guix/read-print.scm')
-rw-r--r--guix/read-print.scm32
1 files changed, 29 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 @@
#: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 @@ single <vertical-space> record."
((? space?) (loop))
(chr (unread-char chr port)))))
-(define (read-with-comments port)
- "Like 'read', but include <blank> objects when they're encountered."
+(define* (read-with-comments port #:key (blank-line? #t))
+ "Like 'read', but include <blank> 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 @@ single <vertical-space> record."
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 @@ single <vertical-space> record."
((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 @@ passed as-is to 'pretty-print-with-comments'."
(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))