1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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*
33 ;;; Utilities for dealing with Scheme records.
37 (define-syntax record-error
39 "Report a syntactic error in use of CONSTRUCTOR."
40 ((_ constructor form fmt args ...)
41 (syntax-violation constructor
42 (format #f fmt args ...)
45 (define-syntax define-record-type*
47 "Define the given record type such that an additional \"syntactic
48 constructor\" is defined, which allows instances to be constructed with named
49 field initializers, à la SRFI-35, as well as default values."
50 (define (make-syntactic-constructor type name ctor fields thunked defaults)
51 "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
52 expects all of FIELDS to be initialized. DEFAULTS is the list of
53 FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
55 (with-syntax ((type type)
62 (define (record-inheritance orig-record field+value)
63 ;; Produce code that returns a record identical to
64 ;; ORIG-RECORD, except that values for the FIELD+VALUE alist
66 (define (field-inherited-value f)
67 (and=> (find (lambda (x)
68 (eq? f (car (syntax->datum x))))
72 ;; Make sure there are no unknown field names.
73 (let* ((fields (map (compose car syntax->datum)
75 (unexpected (lset-difference eq? fields 'expected)))
76 (when (pair? unexpected)
77 (record-error 'name s "extraneous field initializers ~a"
81 #,@(map (lambda (field index)
82 (or (field-inherited-value field)
83 #`(struct-ref #,orig-record
86 (iota (length 'expected)))))
88 (define (thunked-field? f)
89 (memq (syntax->datum f) '#,thunked))
91 (define (field-bindings field+value)
92 ;; Return field to value bindings, for use in 'let*' below.
93 (map (lambda (field+value)
94 (syntax-case field+value ()
97 #,(if (thunked-field? #'field)
102 (syntax-case s (inherit #,@fields)
103 ((_ (inherit orig-record) (field value) (... ...))
104 #`(let* #,(field-bindings #'((field value) (... ...)))
105 #,(record-inheritance #'orig-record
106 #'((field value) (... ...)))))
107 ((_ (field value) (... ...))
108 (let ((fields (map syntax->datum #'(field (... ...))))
109 (dflt (map (match-lambda
111 (list (syntax->datum f) v)))
114 (define (field-value f)
115 (or (and=> (find (lambda (x)
116 (eq? f (car (syntax->datum x))))
117 #'((field value) (... ...)))
121 (syntax->datum f)))))
122 (if (thunked-field? f)
123 #`(lambda () #,value)
126 (let ((fields (append fields (map car dflt))))
127 (cond ((lset= eq? fields 'expected)
128 #`(let* #,(field-bindings
129 #'((field value) (... ...)))
130 (ctor #,@(map field-value 'expected))))
131 ((pair? (lset-difference eq? fields 'expected))
132 (record-error 'name s
133 "extraneous field initializers ~a"
134 (lset-difference eq? fields
137 (record-error 'name s
138 "missing field initializers ~a"
139 (lset-difference eq? 'expected
142 (define (field-default-value s)
143 (syntax-case s (default)
144 ((field (default val) _ ...)
145 (list #'field #'val))
146 ((field _ options ...)
147 (field-default-value #'(field options ...)))
150 (define (thunked-field? s)
151 ;; Return the field name if the field defined by S is thunked.
152 (syntax-case s (thunked)
153 ((field (thunked) _ ...)
155 ((field _ options ...)
156 (thunked-field? #'(field options ...)))
159 (define (thunked-field-accessor-name field)
160 ;; Return the name (an unhygienic syntax object) of the "real"
161 ;; getter for field, which is assumed to be a thunked field.
162 (syntax-case field ()
163 ((field get options ...)
164 (let* ((getter (syntax->datum #'get))
165 (real-getter (symbol-append '% getter '-real)))
166 (datum->syntax #'get real-getter)))))
168 (define (field-spec->srfi-9 field)
169 ;; Convert a field spec of our style to a SRFI-9 field spec of the
171 (syntax-case field ()
172 ((name get options ...)
174 #,(if (thunked-field? field)
175 (thunked-field-accessor-name field)
178 (define (thunked-field-accessor-definition field)
179 ;; Return the real accessor for FIELD, which is assumed to be a
181 (syntax-case field ()
183 (with-syntax ((real-get (thunked-field-accessor-name field)))
184 #'(define-inlinable (get x)
185 ;; The real value of that field is a thunk, so call it.
189 ((_ type syntactic-ctor ctor pred
190 (field get options ...) ...)
191 (let* ((field-spec #'((field get options ...) ...)))
192 (with-syntax (((field-spec* ...)
193 (map field-spec->srfi-9 field-spec))
194 ((thunked-field-accessor ...)
195 (filter-map (lambda (field)
196 (and (thunked-field? field)
197 (thunked-field-accessor-definition
201 (define-record-type type
205 (begin thunked-field-accessor ...)
206 #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
208 (filter-map thunked-field? field-spec)
209 (filter-map field-default-value
210 #'((field options ...)
213 (define* (alist->record alist make keys
214 #:optional (multiple-value-keys '()))
215 "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
216 are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
217 times in ALIST, and thus their value is a list."
218 (let ((args (map (lambda (key)
219 (if (member key multiple-value-keys)
220 (filter-map (match-lambda
222 (and (equal? k key) v)))
224 (assoc-ref alist key)))
228 (define (object->fields object fields port)
229 "Write OBJECT (typically a record) as a series of recutils-style fields to
230 PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
231 (let loop ((fields fields))
235 (((field . get) rest ...)
236 (format port "~a: ~a~%" field (get object))
239 (define %recutils-field-rx
240 (make-regexp "^([[:graph:]]+): (.*)$"))
242 (define %recutils-comment-rx
243 ;; info "(recutils) Comments"
246 (define %recutils-plus-rx
247 (make-regexp "^\\+ ?(.*)$"))
249 (define (recutils->alist port)
250 "Read a recutils-style record from PORT and return it as a list of key/value
251 pairs. Stop upon an empty line (after consuming it) or EOF."
252 (let loop ((line (read-line port))
254 (cond ((eof-object? line)
258 (loop (read-line port) result) ; leading space: ignore it
259 (reverse result))) ; end-of-record marker
260 ((regexp-exec %recutils-comment-rx line)
261 (loop (read-line port) result))
262 ((regexp-exec %recutils-plus-rx line)
266 (((field . value) rest ...)
267 (loop (read-line port)
268 `((,field . ,(string-append value "\n"
269 (match:substring m 1)))
271 ((regexp-exec %recutils-field-rx line)
274 (loop (read-line port)
275 (alist-cons (match:substring match 1)
276 (match:substring match 2)
279 (error "unmatched line" line)))))
281 ;;; records.scm ends here