records: `recutils->alist' recognizes comments.
* guix/records.scm (%recutils-comment-rx): New variable. (recutils->alist): Match comments. * tests/records.scm ("recutils->alist"): Add comments.
This commit is contained in:
parent
fdc1bf659d
commit
b7b8828801
|
@ -217,13 +217,23 @@ PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
|
||||||
(define %recutils-field-rx
|
(define %recutils-field-rx
|
||||||
(make-regexp "^([[:graph:]]+): (.*)$"))
|
(make-regexp "^([[:graph:]]+): (.*)$"))
|
||||||
|
|
||||||
|
(define %recutils-comment-rx
|
||||||
|
;; info "(recutils) Comments"
|
||||||
|
(make-regexp "^#"))
|
||||||
|
|
||||||
(define (recutils->alist port)
|
(define (recutils->alist port)
|
||||||
"Read a recutils-style record from PORT and return it as a list of key/value
|
"Read a recutils-style record from PORT and return it as a list of key/value
|
||||||
pairs. Stop upon an empty line (after consuming it) or EOF."
|
pairs. Stop upon an empty line (after consuming it) or EOF."
|
||||||
(let loop ((line (read-line port))
|
(let loop ((line (read-line port))
|
||||||
(result '()))
|
(result '()))
|
||||||
(cond ((or (eof-object? line) (string-null? line))
|
(cond ((eof-object? line)
|
||||||
(reverse result))
|
(reverse result))
|
||||||
|
((string-null? line)
|
||||||
|
(if (null? result)
|
||||||
|
(loop (read-line port) result) ; leading space: ignore it
|
||||||
|
(reverse result))) ; end-of-record marker
|
||||||
|
((regexp-exec %recutils-comment-rx line)
|
||||||
|
(loop (read-line port) result))
|
||||||
((regexp-exec %recutils-field-rx line)
|
((regexp-exec %recutils-field-rx line)
|
||||||
=>
|
=>
|
||||||
(lambda (match)
|
(lambda (match)
|
||||||
|
|
|
@ -138,13 +138,23 @@
|
||||||
("Something_else" . "chbouib"))
|
("Something_else" . "chbouib"))
|
||||||
(("Name" . "bar")
|
(("Name" . "bar")
|
||||||
("Version" . "1.5")))
|
("Version" . "1.5")))
|
||||||
(let ((p (open-input-string "Name: foo
|
(let ((p (open-input-string "
|
||||||
|
# Comment following an empty line, and
|
||||||
|
# preceding a couple of empty lines, all of
|
||||||
|
# which should be silently consumed.
|
||||||
|
|
||||||
|
|
||||||
|
Name: foo
|
||||||
Version: 0.1
|
Version: 0.1
|
||||||
|
# Comment right in the middle,
|
||||||
|
# spanning two lines.
|
||||||
Synopsis: foo bar
|
Synopsis: foo bar
|
||||||
Something_else: chbouib
|
Something_else: chbouib
|
||||||
|
|
||||||
|
# Comment right before.
|
||||||
Name: bar
|
Name: bar
|
||||||
Version: 1.5")))
|
Version: 1.5
|
||||||
|
# Comment at the end.")))
|
||||||
(list (recutils->alist p)
|
(list (recutils->alist p)
|
||||||
(recutils->alist p))))
|
(recutils->alist p))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue