srfi-35.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ;;; srfi-35.scm --- Conditions -*- coding: utf-8 -*-
  2. ;; Copyright (C) 2007-2011, 2017 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Ludovic Courtès <ludo@gnu.org>
  18. ;;; Commentary:
  19. ;; This is an implementation of SRFI-35, "Conditions". Conditions are a
  20. ;; means to convey information about exceptional conditions between parts of
  21. ;; a program.
  22. ;;; Code:
  23. (define-module (srfi srfi-35)
  24. #:use-module (srfi srfi-1)
  25. #:export (make-condition-type condition-type?
  26. make-condition condition? condition-has-type? condition-ref
  27. make-compound-condition extract-condition
  28. define-condition-type condition
  29. &condition
  30. &message message-condition? condition-message
  31. &serious serious-condition?
  32. &error error?))
  33. (cond-expand-provide (current-module) '(srfi-35))
  34. ;;;
  35. ;;; Condition types.
  36. ;;;
  37. (define %condition-type-vtable
  38. ;; The vtable of all condition types.
  39. ;; user fields: id, parent, all-field-names
  40. (let ((s (make-vtable (string-append standard-vtable-fields "pwpwpw")
  41. (lambda (ct port)
  42. (format port "#<condition-type ~a ~a>"
  43. (condition-type-id ct)
  44. (number->string (object-address ct)
  45. 16))))))
  46. (set-struct-vtable-name! s 'condition-type)
  47. s))
  48. (define (%make-condition-type layout id parent all-fields)
  49. (let ((struct (make-struct/no-tail %condition-type-vtable
  50. (make-struct-layout layout) ;; layout
  51. print-condition ;; printer
  52. id parent all-fields)))
  53. ;; Hack to associate STRUCT with a name, providing a better name for
  54. ;; GOOPS classes as returned by `class-of' et al.
  55. (set-struct-vtable-name! struct (cond ((symbol? id) id)
  56. ((string? id) (string->symbol id))
  57. (else (string->symbol ""))))
  58. struct))
  59. (define (condition-type? obj)
  60. "Return true if OBJ is a condition type."
  61. (and (struct? obj)
  62. (eq? (struct-vtable obj)
  63. %condition-type-vtable)))
  64. (define (condition-type-id ct)
  65. (and (condition-type? ct)
  66. (struct-ref ct (+ vtable-offset-user 0))))
  67. (define (condition-type-parent ct)
  68. (and (condition-type? ct)
  69. (struct-ref ct (+ vtable-offset-user 1))))
  70. (define (condition-type-all-fields ct)
  71. (and (condition-type? ct)
  72. (struct-ref ct (+ vtable-offset-user 2))))
  73. (define (struct-layout-for-condition field-names)
  74. ;; Return a string denoting the layout required to hold the fields listed
  75. ;; in FIELD-NAMES.
  76. (let loop ((field-names field-names)
  77. (layout '("pw")))
  78. (if (null? field-names)
  79. (string-concatenate/shared layout)
  80. (loop (cdr field-names)
  81. (cons "pw" layout)))))
  82. (define (print-condition c port)
  83. ;; Print condition C to PORT in a way similar to how records print:
  84. ;; #<condition TYPE [FIELD: VALUE ...] ADDRESS>.
  85. (define (field-values)
  86. (let* ((type (struct-vtable c))
  87. (strings (fold (lambda (field result)
  88. (cons (format #f "~A: ~S" field
  89. (condition-ref c field))
  90. result))
  91. '()
  92. (condition-type-all-fields type))))
  93. (string-join (reverse strings) " ")))
  94. (format port "#<condition ~a [~a] ~a>"
  95. (condition-type-id (condition-type c))
  96. (field-values)
  97. (number->string (object-address c) 16)))
  98. (define (make-condition-type id parent field-names)
  99. "Return a new condition type named ID, inheriting from PARENT, and with the
  100. fields whose names are listed in FIELD-NAMES. FIELD-NAMES must be a list of
  101. symbols and must not contain names already used by PARENT or one of its
  102. supertypes."
  103. (if (symbol? id)
  104. (if (condition-type? parent)
  105. (let ((parent-fields (condition-type-all-fields parent)))
  106. (if (and (every symbol? field-names)
  107. (null? (lset-intersection eq?
  108. field-names parent-fields)))
  109. (let* ((all-fields (append parent-fields field-names))
  110. (layout (struct-layout-for-condition all-fields)))
  111. (%make-condition-type layout
  112. id parent all-fields))
  113. (error "invalid condition type field names"
  114. field-names)))
  115. (error "parent is not a condition type" parent))
  116. (error "condition type identifier is not a symbol" id)))
  117. (define (make-compound-condition-type id parents)
  118. ;; Return a compound condition type made of the types listed in PARENTS.
  119. ;; All fields from PARENTS are kept, even same-named ones, since they are
  120. ;; needed by `extract-condition'.
  121. (cond ((null? parents)
  122. (error "`make-compound-condition-type' passed empty parent list"
  123. id))
  124. ((null? (cdr parents))
  125. (car parents))
  126. (else
  127. (let* ((all-fields (append-map condition-type-all-fields
  128. parents))
  129. (layout (struct-layout-for-condition all-fields)))
  130. (%make-condition-type layout
  131. id
  132. parents ;; list of parents!
  133. all-fields)))))
  134. ;;;
  135. ;;; Conditions.
  136. ;;;
  137. (define (condition? c)
  138. "Return true if C is a condition."
  139. (and (struct? c)
  140. (condition-type? (struct-vtable c))))
  141. (define (condition-type c)
  142. (and (struct? c)
  143. (let ((vtable (struct-vtable c)))
  144. (if (condition-type? vtable)
  145. vtable
  146. #f))))
  147. (define (condition-has-type? c type)
  148. "Return true if condition C has type TYPE."
  149. (if (and (condition? c) (condition-type? type))
  150. (let loop ((ct (condition-type c)))
  151. (or (eq? ct type)
  152. (and ct
  153. (let ((parent (condition-type-parent ct)))
  154. (if (list? parent)
  155. (any loop parent) ;; compound condition
  156. (loop (condition-type-parent ct)))))))
  157. (throw 'wrong-type-arg "condition-has-type?"
  158. "Wrong type argument")))
  159. (define (condition-ref c field-name)
  160. "Return the value of the field named FIELD-NAME from condition C."
  161. (if (condition? c)
  162. (if (symbol? field-name)
  163. (let* ((type (condition-type c))
  164. (fields (condition-type-all-fields type))
  165. (index (list-index (lambda (name)
  166. (eq? name field-name))
  167. fields)))
  168. (if index
  169. (struct-ref c index)
  170. (error "invalid field name" field-name)))
  171. (error "field name is not a symbol" field-name))
  172. (throw 'wrong-type-arg "condition-ref"
  173. "Wrong type argument: ~S" c)))
  174. (define (make-condition-from-values type values)
  175. (apply make-struct/no-tail type values))
  176. (define (make-condition type . field+value)
  177. "Return a new condition of type TYPE with fields initialized as specified
  178. by FIELD+VALUE, a sequence of field names (symbols) and values."
  179. (if (condition-type? type)
  180. (let* ((all-fields (condition-type-all-fields type))
  181. (inits (fold-right (lambda (field inits)
  182. (let ((v (memq field field+value)))
  183. (if (pair? v)
  184. (cons (cadr v) inits)
  185. (error "field not specified"
  186. field))))
  187. '()
  188. all-fields)))
  189. (make-condition-from-values type inits))
  190. (throw 'wrong-type-arg "make-condition"
  191. "Wrong type argument: ~S" type)))
  192. (define (make-compound-condition . conditions)
  193. "Return a new compound condition composed of CONDITIONS."
  194. (let* ((types (map condition-type conditions))
  195. (ct (make-compound-condition-type 'compound types))
  196. (inits (append-map (lambda (c)
  197. (let ((ct (condition-type c)))
  198. (map (lambda (f)
  199. (condition-ref c f))
  200. (condition-type-all-fields ct))))
  201. conditions)))
  202. (make-condition-from-values ct inits)))
  203. (define (extract-condition c type)
  204. "Return a condition of condition type TYPE with the field values specified
  205. by C."
  206. (define (first-field-index parents)
  207. ;; Return the index of the first field of TYPE within C.
  208. (let loop ((parents parents)
  209. (index 0))
  210. (let ((parent (car parents)))
  211. (cond ((null? parents)
  212. #f)
  213. ((eq? parent type)
  214. index)
  215. ((pair? parent)
  216. (or (loop parent index)
  217. (loop (cdr parents)
  218. (+ index
  219. (apply + (map condition-type-all-fields
  220. parent))))))
  221. (else
  222. (let ((shift (length (condition-type-all-fields parent))))
  223. (loop (cdr parents)
  224. (+ index shift))))))))
  225. (define (list-fields start-index field-names)
  226. ;; Return a list of the form `(FIELD-NAME VALUE...)'.
  227. (let loop ((index start-index)
  228. (field-names field-names)
  229. (result '()))
  230. (if (null? field-names)
  231. (reverse! result)
  232. (loop (+ 1 index)
  233. (cdr field-names)
  234. (cons* (struct-ref c index)
  235. (car field-names)
  236. result)))))
  237. (if (and (condition? c) (condition-type? type))
  238. (let* ((ct (condition-type c))
  239. (parent (condition-type-parent ct)))
  240. (cond ((eq? type ct)
  241. c)
  242. ((pair? parent)
  243. ;; C is a compound condition.
  244. (let ((field-index (first-field-index parent)))
  245. ;;(format #t "field-index: ~a ~a~%" field-index
  246. ;; (list-fields field-index
  247. ;; (condition-type-all-fields type)))
  248. (apply make-condition type
  249. (list-fields field-index
  250. (condition-type-all-fields type)))))
  251. (else
  252. ;; C does not have type TYPE.
  253. #f)))
  254. (throw 'wrong-type-arg "extract-condition"
  255. "Wrong type argument")))
  256. ;;;
  257. ;;; Syntax.
  258. ;;;
  259. (define-syntax-rule (define-condition-type name parent pred (field-name field-accessor) ...)
  260. (begin
  261. (define name
  262. (make-condition-type 'name parent '(field-name ...)))
  263. (define (pred c)
  264. (condition-has-type? c name))
  265. (define (field-accessor c)
  266. (condition-ref c 'field-name))
  267. ...))
  268. (define-syntax-rule (compound-condition (type ...) (field ...))
  269. ;; Create a compound condition using `make-compound-condition-type'.
  270. (condition ((make-compound-condition-type '%compound `(,type ...))
  271. field ...)))
  272. (define-syntax condition-instantiation
  273. ;; Build the `(make-condition type ...)' call.
  274. (syntax-rules ()
  275. ((_ type (out ...))
  276. (make-condition type out ...))
  277. ((_ type (out ...) (field-name field-value) rest ...)
  278. (condition-instantiation type (out ... 'field-name field-value) rest ...))))
  279. (define-syntax condition
  280. (syntax-rules ()
  281. ((_ (type field ...))
  282. (condition-instantiation type () field ...))
  283. ((_ (type field ...) ...)
  284. (compound-condition (type ...) (field ... ...)))))
  285. ;;;
  286. ;;; Standard condition types.
  287. ;;;
  288. (define &condition
  289. ;; The root condition type.
  290. (make-struct/no-tail %condition-type-vtable
  291. (make-struct-layout "")
  292. (lambda (c port)
  293. (display "<&condition>"))
  294. '&condition #f '() '()))
  295. (define-condition-type &message &condition
  296. message-condition?
  297. (message condition-message))
  298. (define-condition-type &serious &condition
  299. serious-condition?)
  300. (define-condition-type &error &serious
  301. error?)
  302. ;;; srfi-35.scm ends here