records: `recutils->alist' recognizes comments.
[jackhill/guix/guix.git] / guix / records.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix records)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-26)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 regex)
25 #:use-module (ice-9 rdelim)
26 #:export (define-record-type*
27 alist->record
28 object->fields
29 recutils->alist))
30
31 ;;; Commentary:
32 ;;;
33 ;;; Utilities for dealing with Scheme records.
34 ;;;
35 ;;; Code:
36
37 (define-syntax define-record-type*
38 (lambda (s)
39 "Define the given record type such that an additional \"syntactic
40 constructor\" is defined, which allows instances to be constructed with named
41 field initializers, à la SRFI-35, as well as default values."
42 (define (make-syntactic-constructor type name ctor fields thunked defaults)
43 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
44 expects all of FIELDS to be initialized. DEFAULTS is the list of
45 FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
46 thunked fields."
47 (with-syntax ((type type)
48 (name name)
49 (ctor ctor)
50 (expected fields)
51 (defaults defaults))
52 #`(define-syntax name
53 (lambda (s)
54 (define (record-inheritance orig-record field+value)
55 ;; Produce code that returns a record identical to
56 ;; ORIG-RECORD, except that values for the FIELD+VALUE alist
57 ;; prevail.
58 (define (field-inherited-value f)
59 (and=> (find (lambda (x)
60 (eq? f (car (syntax->datum x))))
61 field+value)
62 car))
63
64 #`(make-struct type 0
65 #,@(map (lambda (field index)
66 (or (field-inherited-value field)
67 #`(struct-ref #,orig-record
68 #,index)))
69 'expected
70 (iota (length 'expected)))))
71
72 (define (thunked-field? f)
73 (memq (syntax->datum f) '#,thunked))
74
75 (define (field-bindings field+value)
76 ;; Return field to value bindings, for use in `letrec*' below.
77 (map (lambda (field+value)
78 (syntax-case field+value ()
79 ((field value)
80 #`(field
81 #,(if (thunked-field? #'field)
82 #'(lambda () value)
83 #'value)))))
84 field+value))
85
86 (syntax-case s (inherit #,@fields)
87 ((_ (inherit orig-record) (field value) (... ...))
88 #`(letrec* #,(field-bindings #'((field value) (... ...)))
89 #,(record-inheritance #'orig-record
90 #'((field value) (... ...)))))
91 ((_ (field value) (... ...))
92 (let ((fields (map syntax->datum #'(field (... ...))))
93 (dflt (map (match-lambda
94 ((f v)
95 (list (syntax->datum f) v)))
96 #'defaults)))
97
98 (define (field-value f)
99 (or (and=> (find (lambda (x)
100 (eq? f (car (syntax->datum x))))
101 #'((field value) (... ...)))
102 car)
103 (let ((value
104 (car (assoc-ref dflt
105 (syntax->datum f)))))
106 (if (thunked-field? f)
107 #`(lambda () #,value)
108 value))))
109
110 (let-syntax ((error*
111 (syntax-rules ()
112 ((_ fmt args (... ...))
113 (syntax-violation 'name
114 (format #f fmt args
115 (... ...))
116 s)))))
117 (let ((fields (append fields (map car dflt))))
118 (cond ((lset= eq? fields 'expected)
119 #`(letrec* #,(field-bindings
120 #'((field value) (... ...)))
121 (ctor #,@(map field-value 'expected))))
122 ((pair? (lset-difference eq? fields 'expected))
123 (error* "extraneous field initializers ~a"
124 (lset-difference eq? fields 'expected)))
125 (else
126 (error* "missing field initializers ~a"
127 (lset-difference eq? 'expected
128 fields)))))))))))))
129
130 (define (field-default-value s)
131 (syntax-case s (default)
132 ((field (default val) _ ...)
133 (list #'field #'val))
134 ((field _ options ...)
135 (field-default-value #'(field options ...)))
136 (_ #f)))
137
138 (define (thunked-field? s)
139 ;; Return the field name if the field defined by S is thunked.
140 (syntax-case s (thunked)
141 ((field (thunked) _ ...)
142 #'field)
143 ((field _ options ...)
144 (thunked-field? #'(field options ...)))
145 (_ #f)))
146
147 (define (thunked-field-accessor-name field)
148 ;; Return the name (an unhygienic syntax object) of the "real"
149 ;; getter for field, which is assumed to be a thunked field.
150 (syntax-case field ()
151 ((field get options ...)
152 (let* ((getter (syntax->datum #'get))
153 (real-getter (symbol-append '% getter '-real)))
154 (datum->syntax #'get real-getter)))))
155
156 (define (field-spec->srfi-9 field)
157 ;; Convert a field spec of our style to a SRFI-9 field spec of the
158 ;; form (field get).
159 (syntax-case field ()
160 ((name get options ...)
161 #`(name
162 #,(if (thunked-field? field)
163 (thunked-field-accessor-name field)
164 #'get)))))
165
166 (define (thunked-field-accessor-definition field)
167 ;; Return the real accessor for FIELD, which is assumed to be a
168 ;; thunked field.
169 (syntax-case field ()
170 ((name get _ ...)
171 (with-syntax ((real-get (thunked-field-accessor-name field)))
172 #'(define-inlinable (get x)
173 ;; The real value of that field is a thunk, so call it.
174 ((real-get x)))))))
175
176 (syntax-case s ()
177 ((_ type syntactic-ctor ctor pred
178 (field get options ...) ...)
179 (let* ((field-spec #'((field get options ...) ...)))
180 (with-syntax (((field-spec* ...)
181 (map field-spec->srfi-9 field-spec))
182 ((thunked-field-accessor ...)
183 (filter-map (lambda (field)
184 (and (thunked-field? field)
185 (thunked-field-accessor-definition
186 field)))
187 field-spec)))
188 #`(begin
189 (define-record-type type
190 (ctor field ...)
191 pred
192 field-spec* ...)
193 (begin thunked-field-accessor ...)
194 #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
195 #'(field ...)
196 (filter-map thunked-field? field-spec)
197 (filter-map field-default-value
198 #'((field options ...)
199 ...))))))))))
200
201 (define (alist->record alist make keys)
202 "Apply MAKE to the values associated with KEYS in ALIST."
203 (let ((args (map (cut assoc-ref alist <>) keys)))
204 (apply make args)))
205
206 (define (object->fields object fields port)
207 "Write OBJECT (typically a record) as a series of recutils-style fields to
208 PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
209 (let loop ((fields fields))
210 (match fields
211 (()
212 object)
213 (((field . get) rest ...)
214 (format port "~a: ~a~%" field (get object))
215 (loop rest)))))
216
217 (define %recutils-field-rx
218 (make-regexp "^([[:graph:]]+): (.*)$"))
219
220 (define %recutils-comment-rx
221 ;; info "(recutils) Comments"
222 (make-regexp "^#"))
223
224 (define (recutils->alist port)
225 "Read a recutils-style record from PORT and return it as a list of key/value
226 pairs. Stop upon an empty line (after consuming it) or EOF."
227 (let loop ((line (read-line port))
228 (result '()))
229 (cond ((eof-object? line)
230 (reverse result))
231 ((string-null? line)
232 (if (null? result)
233 (loop (read-line port) result) ; leading space: ignore it
234 (reverse result))) ; end-of-record marker
235 ((regexp-exec %recutils-comment-rx line)
236 (loop (read-line port) result))
237 ((regexp-exec %recutils-field-rx line)
238 =>
239 (lambda (match)
240 (loop (read-line port)
241 (alist-cons (match:substring match 1)
242 (match:substring match 2)
243 result))))
244 (else
245 (error "unmatched line" line)))))
246
247 ;;; records.scm ends here