aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-08-01 17:42:11 +0200
committerLudovic Courtès <ludo@gnu.org>2022-08-08 11:22:32 +0200
commit38f1fb843cb3b538bf042a3baed99618953a005f (patch)
tree9f36e609145c476bc68483dc3fb468a70bfd9dca
parent632d4ccc0bcb9d73226edeb32264c74fc50867ab (diff)
downloadguix-38f1fb843cb3b538bf042a3baed99618953a005f.tar.gz
guix-38f1fb843cb3b538bf042a3baed99618953a005f.zip
read-print: Expose comment constructor.
* guix/read-print.scm (<comment>): Rename constructor to 'string->comment'. (comment): New procedure. (read-with-comments, canonicalize-comment): Use 'string->comment' instead of 'comment'.
-rw-r--r--guix/read-print.scm36
1 files 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 @@
#: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 @@
;; 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 @@
(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 @@ particular newlines, is left as is."
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