| 1 |
(library (srfi :35 conditions) |
| 2 |
(export make-condition-type condition-type? condition-has-type? |
| 3 |
condition-ref make-compound-condition |
| 4 |
extract-condition define-condition-type |
| 5 |
&condition make-condition condition? condition |
| 6 |
&serious serious-condition? |
| 7 |
&error error? |
| 8 |
&message message-condition? condition-message) |
| 9 |
(import |
| 10 |
(except (rnrs) define-condition-type condition |
| 11 |
&condition condition? |
| 12 |
&serious serious-condition? |
| 13 |
&error error? |
| 14 |
&message message-condition? condition-message |
| 15 |
define-record-type |
| 16 |
error) |
| 17 |
(only (srfi :1) lset= lset-intersection lset-difference any) |
| 18 |
(srfi :9) |
| 19 |
(srfi :23)) |
| 20 |
|
| 21 |
;; reference implementation from srfi 35: overlaps some of r6rs conditions |
| 22 |
|
| 23 |
;; Copyright (C) Richard Kelsey, Michael Sperber (2002). All Rights Reserved. |
| 24 |
;; |
| 25 |
;; Permission is hereby granted, free of charge, to any person obtaining a |
| 26 |
;; copy of this software and associated documentation files (the "Software"), |
| 27 |
;; to deal in the Software without restriction, including without limitation |
| 28 |
;; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 29 |
;; and/or sell copies of the Software, and to permit persons to whom the |
| 30 |
;; Software is furnished to do so, subject to the following conditions: |
| 31 |
;; |
| 32 |
;; The above copyright notice and this permission notice shall be included in |
| 33 |
;; all copies or substantial portions of the Software. |
| 34 |
;; |
| 35 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 36 |
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 37 |
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 38 |
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 39 |
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 40 |
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 41 |
;; DEALINGS IN THE SOFTWARE. |
| 42 |
|
| 43 |
(define-record-type :condition-type |
| 44 |
(really-make-condition-type name supertype fields all-fields) |
| 45 |
condition-type? |
| 46 |
(name condition-type-name) |
| 47 |
(supertype condition-type-supertype) |
| 48 |
(fields condition-type-fields) |
| 49 |
(all-fields condition-type-all-fields)) |
| 50 |
|
| 51 |
(define (make-condition-type name supertype fields) |
| 52 |
(if (not (symbol? name)) |
| 53 |
(error "make-condition-type: name is not a symbol" |
| 54 |
name)) |
| 55 |
(if (not (condition-type? supertype)) |
| 56 |
(error "make-condition-type: supertype is not a condition type" |
| 57 |
supertype)) |
| 58 |
(if (not |
| 59 |
(null? (lset-intersection eq? |
| 60 |
(condition-type-all-fields supertype) |
| 61 |
fields))) |
| 62 |
(error "duplicate field name" )) |
| 63 |
(really-make-condition-type name |
| 64 |
supertype |
| 65 |
fields |
| 66 |
(append (condition-type-all-fields supertype) |
| 67 |
fields))) |
| 68 |
|
| 69 |
(define-syntax define-condition-type |
| 70 |
(syntax-rules () |
| 71 |
((define-condition-type ?name ?supertype ?predicate |
| 72 |
(?field1 ?accessor1) ...) |
| 73 |
(begin |
| 74 |
(define ?name |
| 75 |
(make-condition-type '?name |
| 76 |
?supertype |
| 77 |
'(?field1 ...))) |
| 78 |
(define (?predicate thing) |
| 79 |
(and (condition? thing) |
| 80 |
(condition-has-type? thing ?name))) |
| 81 |
(define (?accessor1 condition) |
| 82 |
(condition-ref (extract-condition condition ?name) |
| 83 |
'?field1)) |
| 84 |
...)))) |
| 85 |
|
| 86 |
(define (condition-subtype? subtype supertype) |
| 87 |
(let recur ((subtype subtype)) |
| 88 |
(cond ((not subtype) #f) |
| 89 |
((eq? subtype supertype) #t) |
| 90 |
(else |
| 91 |
(recur (condition-type-supertype subtype)))))) |
| 92 |
|
| 93 |
(define (condition-type-field-supertype condition-type field) |
| 94 |
(let loop ((condition-type condition-type)) |
| 95 |
(cond ((not condition-type) #f) |
| 96 |
((memq field (condition-type-fields condition-type)) |
| 97 |
condition-type) |
| 98 |
(else |
| 99 |
(loop (condition-type-supertype condition-type)))))) |
| 100 |
|
| 101 |
; The type-field-alist is of the form |
| 102 |
; ((<type> (<field-name> . <value>) ...) ...) |
| 103 |
(define-record-type :condition |
| 104 |
(really-make-condition type-field-alist) |
| 105 |
condition? |
| 106 |
(type-field-alist condition-type-field-alist)) |
| 107 |
|
| 108 |
(define (make-condition type . field-plist) |
| 109 |
(let ((alist (let label ((plist field-plist)) |
| 110 |
(if (null? plist) |
| 111 |
'() |
| 112 |
(cons (cons (car plist) |
| 113 |
(cadr plist)) |
| 114 |
(label (cddr plist))))))) |
| 115 |
(if (not (lset= eq? |
| 116 |
(condition-type-all-fields type) |
| 117 |
(map car alist))) |
| 118 |
(error "condition fields don't match condition type")) |
| 119 |
(really-make-condition (list (cons type alist))))) |
| 120 |
|
| 121 |
(define (condition-has-type? condition type) |
| 122 |
(any (lambda (has-type) |
| 123 |
(condition-subtype? has-type type)) |
| 124 |
(condition-types condition))) |
| 125 |
|
| 126 |
(define (condition-ref condition field) |
| 127 |
(type-field-alist-ref (condition-type-field-alist condition) |
| 128 |
field)) |
| 129 |
|
| 130 |
(define (type-field-alist-ref type-field-alist field) |
| 131 |
(let loop ((type-field-alist type-field-alist)) |
| 132 |
(cond ((null? type-field-alist) |
| 133 |
(error "type-field-alist-ref: field not found" |
| 134 |
type-field-alist field)) |
| 135 |
((assq field (cdr (car type-field-alist))) |
| 136 |
=> cdr) |
| 137 |
(else |
| 138 |
(loop (cdr type-field-alist)))))) |
| 139 |
|
| 140 |
(define (make-compound-condition condition-1 . conditions) |
| 141 |
(really-make-condition |
| 142 |
(apply append (map condition-type-field-alist |
| 143 |
(cons condition-1 conditions))))) |
| 144 |
|
| 145 |
(define (extract-condition condition type) |
| 146 |
(let ((entry (find (lambda (entry) |
| 147 |
(condition-subtype? (car entry) type)) |
| 148 |
(condition-type-field-alist condition)))) |
| 149 |
(if (not entry) |
| 150 |
(error "extract-condition: invalid condition type" |
| 151 |
condition type)) |
| 152 |
(really-make-condition |
| 153 |
(list (cons type |
| 154 |
(map (lambda (field) |
| 155 |
(assq field (cdr entry))) |
| 156 |
(condition-type-all-fields type))))))) |
| 157 |
|
| 158 |
(define-syntax condition |
| 159 |
(syntax-rules () |
| 160 |
((condition (?type1 (?field1 ?value1) ...) ...) |
| 161 |
(type-field-alist->condition |
| 162 |
(list |
| 163 |
(cons ?type1 |
| 164 |
(list (cons '?field1 ?value1) ...)) |
| 165 |
...))))) |
| 166 |
|
| 167 |
(define (type-field-alist->condition type-field-alist) |
| 168 |
(really-make-condition |
| 169 |
(map (lambda (entry) |
| 170 |
(cons (car entry) |
| 171 |
(map (lambda (field) |
| 172 |
(or (assq field (cdr entry)) |
| 173 |
(cons field |
| 174 |
(type-field-alist-ref type-field-alist field)))) |
| 175 |
(condition-type-all-fields (car entry))))) |
| 176 |
type-field-alist))) |
| 177 |
|
| 178 |
(define (condition-types condition) |
| 179 |
(map car (condition-type-field-alist condition))) |
| 180 |
|
| 181 |
(define (check-condition-type-field-alist the-type-field-alist) |
| 182 |
(let loop ((type-field-alist the-type-field-alist)) |
| 183 |
(if (not (null? type-field-alist)) |
| 184 |
(let* ((entry (car type-field-alist)) |
| 185 |
(type (car entry)) |
| 186 |
(field-alist (cdr entry)) |
| 187 |
(fields (map car field-alist)) |
| 188 |
(all-fields (condition-type-all-fields type))) |
| 189 |
(for-each (lambda (missing-field) |
| 190 |
(let ((supertype |
| 191 |
(condition-type-field-supertype type missing-field))) |
| 192 |
(if (not |
| 193 |
(any (lambda (entry) |
| 194 |
(let ((type (car entry))) |
| 195 |
(condition-subtype? type supertype))) |
| 196 |
the-type-field-alist)) |
| 197 |
(error "missing field in condition construction" |
| 198 |
type |
| 199 |
missing-field)))) |
| 200 |
(lset-difference eq? all-fields fields)) |
| 201 |
(loop (cdr type-field-alist)))))) |
| 202 |
|
| 203 |
(define &condition (really-make-condition-type '&condition |
| 204 |
#f |
| 205 |
'() |
| 206 |
'())) |
| 207 |
|
| 208 |
(define-condition-type &message &condition |
| 209 |
message-condition? |
| 210 |
(message condition-message)) |
| 211 |
|
| 212 |
(define-condition-type &serious &condition |
| 213 |
serious-condition?) |
| 214 |
|
| 215 |
(define-condition-type &error &serious |
| 216 |
error?)) |