1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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 define-record-type*
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
47 (with-syntax ((type type)
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
58 (define (field-inherited-value f)
59 (and=> (find (lambda (x)
60 (eq? f (car (syntax->datum x))))
65 #,@(map (lambda (field index)
66 (or (field-inherited-value field)
67 #`(struct-ref #,orig-record
70 (iota (length 'expected)))))
72 (define (thunked-field? f)
73 (memq (syntax->datum f) '#,thunked))
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 ()
81 #,(if (thunked-field? #'field)
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
95 (list (syntax->datum f) v)))
98 (define (field-value f)
99 (or (and=> (find (lambda (x)
100 (eq? f (car (syntax->datum x))))
101 #'((field value) (... ...)))
105 (syntax->datum f)))))
106 (if (thunked-field? f)
107 #`(lambda () #,value)
112 ((_ fmt args (... ...))
113 (syntax-violation 'name
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)))
126 (error* "missing field initializers ~a"
127 (lset-difference eq? 'expected
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 ...)))
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) _ ...)
143 ((field _ options ...)
144 (thunked-field? #'(field options ...)))
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)))))
156 (define (field-spec->srfi-9 field)
157 ;; Convert a field spec of our style to a SRFI-9 field spec of the
159 (syntax-case field ()
160 ((name get options ...)
162 #,(if (thunked-field? field)
163 (thunked-field-accessor-name field)
166 (define (thunked-field-accessor-definition field)
167 ;; Return the real accessor for FIELD, which is assumed to be a
169 (syntax-case field ()
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.
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
189 (define-record-type type
193 (begin thunked-field-accessor ...)
194 #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
196 (filter-map thunked-field? field-spec)
197 (filter-map field-default-value
198 #'((field options ...)
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)))
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))
213 (((field . get) rest ...)
214 (format port "~a: ~a~%" field (get object))
217 (define %recutils-field-rx
218 (make-regexp "^([[:graph:]]+): (.*)$"))
220 (define %recutils-comment-rx
221 ;; info "(recutils) Comments"
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))
229 (cond ((eof-object? line)
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)
240 (loop (read-line port)
241 (alist-cons (match:substring match 1)
242 (match:substring match 2)
245 (error "unmatched line" line)))))
247 ;;; records.scm ends here