You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

282 lines
12 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014 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. (define-module (guix records)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-9)
  21. #:use-module (srfi srfi-26)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 regex)
  24. #:use-module (ice-9 rdelim)
  25. #:export (define-record-type*
  26. alist->record
  27. object->fields
  28. recutils->alist))
  29. ;;; Commentary:
  30. ;;;
  31. ;;; Utilities for dealing with Scheme records.
  32. ;;;
  33. ;;; Code:
  34. (define-syntax record-error
  35. (syntax-rules ()
  36. "Report a syntactic error in use of CONSTRUCTOR."
  37. ((_ constructor form fmt args ...)
  38. (syntax-violation constructor
  39. (format #f fmt args ...)
  40. form))))
  41. (define-syntax define-record-type*
  42. (lambda (s)
  43. "Define the given record type such that an additional \"syntactic
  44. constructor\" is defined, which allows instances to be constructed with named
  45. field initializers, à la SRFI-35, as well as default values."
  46. (define (make-syntactic-constructor type name ctor fields thunked defaults)
  47. "Make the syntactic constructor NAME for TYPE, that calls CTOR, and
  48. expects all of FIELDS to be initialized. DEFAULTS is the list of
  49. FIELD/DEFAULT-VALUE tuples, and THUNKED is the list of identifiers of
  50. thunked fields."
  51. (with-syntax ((type type)
  52. (name name)
  53. (ctor ctor)
  54. (expected fields)
  55. (defaults defaults))
  56. #`(define-syntax name
  57. (lambda (s)
  58. (define (record-inheritance orig-record field+value)
  59. ;; Produce code that returns a record identical to
  60. ;; ORIG-RECORD, except that values for the FIELD+VALUE alist
  61. ;; prevail.
  62. (define (field-inherited-value f)
  63. (and=> (find (lambda (x)
  64. (eq? f (car (syntax->datum x))))
  65. field+value)
  66. car))
  67. ;; Make sure there are no unknown field names.
  68. (let* ((fields (map (compose car syntax->datum)
  69. field+value))
  70. (unexpected (lset-difference eq? fields 'expected)))
  71. (when (pair? unexpected)
  72. (record-error 'name s "extraneous field initializers ~a"
  73. unexpected)))
  74. #`(make-struct type 0
  75. #,@(map (lambda (field index)
  76. (or (field-inherited-value field)
  77. #`(struct-ref #,orig-record
  78. #,index)))
  79. 'expected
  80. (iota (length 'expected)))))
  81. (define (thunked-field? f)
  82. (memq (syntax->datum f) '#,thunked))
  83. (define (field-bindings field+value)
  84. ;; Return field to value bindings, for use in 'let*' below.
  85. (map (lambda (field+value)
  86. (syntax-case field+value ()
  87. ((field value)
  88. #`(field
  89. #,(if (thunked-field? #'field)
  90. #'(lambda () value)
  91. #'value)))))
  92. field+value))
  93. (syntax-case s (inherit #,@fields)
  94. ((_ (inherit orig-record) (field value) (... ...))
  95. #`(let* #,(field-bindings #'((field value) (... ...)))
  96. #,(record-inheritance #'orig-record
  97. #'((field value) (... ...)))))
  98. ((_ (field value) (... ...))
  99. (let ((fields (map syntax->datum #'(field (... ...))))
  100. (dflt (map (match-lambda
  101. ((f v)
  102. (list (syntax->datum f) v)))
  103. #'defaults)))
  104. (define (field-value f)
  105. (or (and=> (find (lambda (x)
  106. (eq? f (car (syntax->datum x))))
  107. #'((field value) (... ...)))
  108. car)
  109. (let ((value
  110. (car (assoc-ref dflt
  111. (syntax->datum f)))))
  112. (if (thunked-field? f)
  113. #`(lambda () #,value)
  114. value))))
  115. (let ((fields (append fields (map car dflt))))
  116. (cond ((lset= eq? fields 'expected)
  117. #`(let* #,(field-bindings
  118. #'((field value) (... ...)))
  119. (ctor #,@(map field-value 'expected))))
  120. ((pair? (lset-difference eq? fields 'expected))
  121. (record-error 'name s
  122. "extraneous field initializers ~a"
  123. (lset-difference eq? fields
  124. 'expected)))
  125. (else
  126. (record-error 'name s
  127. "missing field initializers ~a"
  128. (lset-difference eq? 'expected
  129. fields))))))))))))
  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. (define (thunked-field? s)
  138. ;; Return the field name if the field defined by S is thunked.
  139. (syntax-case s (thunked)
  140. ((field (thunked) _ ...)
  141. #'field)
  142. ((field _ options ...)
  143. (thunked-field? #'(field options ...)))
  144. (_ #f)))
  145. (define (thunked-field-accessor-name field)
  146. ;; Return the name (an unhygienic syntax object) of the "real"
  147. ;; getter for field, which is assumed to be a thunked field.
  148. (syntax-case field ()
  149. ((field get options ...)
  150. (let* ((getter (syntax->datum #'get))
  151. (real-getter (symbol-append '% getter '-real)))
  152. (datum->syntax #'get real-getter)))))
  153. (define (field-spec->srfi-9 field)
  154. ;; Convert a field spec of our style to a SRFI-9 field spec of the
  155. ;; form (field get).
  156. (syntax-case field ()
  157. ((name get options ...)
  158. #`(name
  159. #,(if (thunked-field? field)
  160. (thunked-field-accessor-name field)
  161. #'get)))))
  162. (define (thunked-field-accessor-definition field)
  163. ;; Return the real accessor for FIELD, which is assumed to be a
  164. ;; thunked field.
  165. (syntax-case field ()
  166. ((name get _ ...)
  167. (with-syntax ((real-get (thunked-field-accessor-name field)))
  168. #'(define-inlinable (get x)
  169. ;; The real value of that field is a thunk, so call it.
  170. ((real-get x)))))))
  171. (syntax-case s ()
  172. ((_ type syntactic-ctor ctor pred
  173. (field get options ...) ...)
  174. (let* ((field-spec #'((field get options ...) ...)))
  175. (with-syntax (((field-spec* ...)
  176. (map field-spec->srfi-9 field-spec))
  177. ((thunked-field-accessor ...)
  178. (filter-map (lambda (field)
  179. (and (thunked-field? field)
  180. (thunked-field-accessor-definition
  181. field)))
  182. field-spec)))
  183. #`(begin
  184. (define-record-type type
  185. (ctor field ...)
  186. pred
  187. field-spec* ...)
  188. (begin thunked-field-accessor ...)
  189. #,(make-syntactic-constructor #'type #'syntactic-ctor #'ctor
  190. #'(field ...)
  191. (filter-map thunked-field? field-spec)
  192. (filter-map field-default-value
  193. #'((field options ...)
  194. ...))))))))))
  195. (define* (alist->record alist make keys
  196. #:optional (multiple-value-keys '()))
  197. "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
  198. are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
  199. times in ALIST, and thus their value is a list."
  200. (let ((args (map (lambda (key)
  201. (if (member key multiple-value-keys)
  202. (filter-map (match-lambda
  203. ((k . v)
  204. (and (equal? k key) v)))
  205. alist)
  206. (assoc-ref alist key)))
  207. keys)))
  208. (apply make args)))
  209. (define (object->fields object fields port)
  210. "Write OBJECT (typically a record) as a series of recutils-style fields to
  211. PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
  212. (let loop ((fields fields))
  213. (match fields
  214. (()
  215. object)
  216. (((field . get) rest ...)
  217. (format port "~a: ~a~%" field (get object))
  218. (loop rest)))))
  219. (define %recutils-field-rx
  220. (make-regexp "^([[:graph:]]+): (.*)$"))
  221. (define %recutils-comment-rx
  222. ;; info "(recutils) Comments"
  223. (make-regexp "^#"))
  224. (define %recutils-plus-rx
  225. (make-regexp "^\\+ ?(.*)$"))
  226. (define (recutils->alist port)
  227. "Read a recutils-style record from PORT and return it as a list of key/value
  228. pairs. Stop upon an empty line (after consuming it) or EOF."
  229. (let loop ((line (read-line port))
  230. (result '()))
  231. (cond ((eof-object? line)
  232. (reverse result))
  233. ((string-null? line)
  234. (if (null? result)
  235. (loop (read-line port) result) ; leading space: ignore it
  236. (reverse result))) ; end-of-record marker
  237. ((regexp-exec %recutils-comment-rx line)
  238. (loop (read-line port) result))
  239. ((regexp-exec %recutils-plus-rx line)
  240. =>
  241. (lambda (m)
  242. (match result
  243. (((field . value) rest ...)
  244. (loop (read-line port)
  245. `((,field . ,(string-append value "\n"
  246. (match:substring m 1)))
  247. ,@rest))))))
  248. ((regexp-exec %recutils-field-rx line)
  249. =>
  250. (lambda (match)
  251. (loop (read-line port)
  252. (alist-cons (match:substring match 1)
  253. (match:substring match 2)
  254. result))))
  255. (else
  256. (error "unmatched line" line)))))
  257. ;;; records.scm ends here