[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug#56898] [PATCH 03/13] read-print: Expose comment constructor.
From: |
Ludovic Courtès |
Subject: |
[bug#56898] [PATCH 03/13] read-print: Expose comment constructor. |
Date: |
Tue, 2 Aug 2022 23:44:09 +0200 |
* guix/read-print.scm (<comment>): Rename constructor to
'string->comment'.
(comment): New procedure.
(read-with-comments, canonicalize-comment): Use 'string->comment'
instead of 'comment'.
---
guix/read-print.scm | 36 +++++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 11 deletions(-)
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 949a713ca2..5281878504 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -23,10 +23,13 @@ (define-module (guix read-print)
#:use-module (ice-9 vlist)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-34)
+ #:use-module (srfi srfi-35)
#:export (pretty-print-with-comments
read-with-comments
object->string*
+ comment
comment?
comment->string
comment-margin?
@@ -46,11 +49,22 @@ (define-module (guix read-print)
;; A comment.
(define-record-type <comment>
- (comment str margin?)
+ (string->comment str margin?)
comment?
(str comment->string)
(margin? comment-margin?))
+(define* (comment str #:optional margin?)
+ "Return a new comment made from STR. When MARGIN? is true, return a margin
+comment; otherwise return a line comment. STR must start with a semicolon and
+end with newline, otherwise an error is raised."
+ (when (or (string-null? str)
+ (not (eqv? #\; (string-ref str 0)))
+ (not (string-suffix? "\n" str)))
+ (raise (condition
+ (&message (message "invalid comment string")))))
+ (string->comment str margin?))
+
(define (read-with-comments port)
"Like 'read', but include <comment> objects when they're encountered."
;; Note: Instead of implementing this functionality in 'read' proper, which
@@ -106,8 +120,8 @@ (define (reverse/dot lst)
(loop #f return)))
((eqv? chr #\;)
(unread-char chr port)
- (comment (read-line port 'concat)
- (not blank-line?)))
+ (string->comment (read-line port 'concat)
+ (not blank-line?)))
(else
(unread-char chr port)
(match (read port)
@@ -256,14 +270,14 @@ (define (canonicalize-comment c)
semicolons."
(let ((line (string-trim-both
(string-trim (comment->string c) (char-set #\;)))))
- (comment (string-append
- (if (comment-margin? c)
- ";"
- (if (string-null? line)
- ";;" ;no trailing space
- ";; "))
- line "\n")
- (comment-margin? c))))
+ (string->comment (string-append
+ (if (comment-margin? c)
+ ";"
+ (if (string-null? line)
+ ";;" ;no trailing space
+ ";; "))
+ line "\n")
+ (comment-margin? c))))
(define* (pretty-print-with-comments port obj
#:key
--
2.37.1
- [bug#56898] [PATCH 00/13] Put the pretty printer to good use, Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 01/13] style: Move reader and printer to (guix read-print)., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 02/13] read-print: Add System and Home special forms., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 03/13] read-print: Expose comment constructor.,
Ludovic Courtès <=
- [bug#56898] [PATCH 04/13] read-print: Introduce <blank> parent class of <comment>., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 07/13] read-print: Recognize page breaks., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 05/13] style: Adjust test to not emit blank lines., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 09/13] read-print: 'canonicalize-comment' leaves top-level comments unchanged., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 13/13] installer: Add comments and vertical space to the generated config., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 11/13] read-print: Support printing multi-line comments., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 08/13] read-print: Add code to read and write sequences of expressions/blanks., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 06/13] read-print: Read and render vertical space., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 10/13] style: Add '--whole-file' option., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 12/13] installer: Render the final configuration with (guix read-print)., Ludovic Courtès, 2022/08/02