1 ;;; conditions.scm --- The R6RS conditions library
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (library (rnrs conditions (6))
30 make-message-condition
39 make-serious-condition
51 make-assertion-violation
55 make-irritants-condition
65 make-non-continuable-violation
66 non-continuable-violation?
68 &implementation-restriction
69 make-implementation-restriction-violation
70 implementation-restriction-violation?
73 make-lexical-violation
80 syntax-violation-subform
83 make-undefined-violation
85 (import (only (guile) and=> @@)
88 (rnrs records procedural (6)))
90 (define &compound-condition (make-record-type-descriptor
91 '&compound-condition #f #f #f #f
92 '#((immutable components))))
93 (define compound-condition? (record-predicate &compound-condition))
95 (define make-compound-condition
96 (record-constructor (make-record-constructor-descriptor
97 &compound-condition #f #f)))
98 (define simple-conditions
99 (let ((compound-ref (record-accessor &compound-condition 0)))
101 (cond ((compound-condition? condition)
102 (compound-ref condition))
103 ((condition-internal? condition)
106 (assertion-violation 'simple-conditions
110 (define (condition? obj)
111 (or (compound-condition? obj) (condition-internal? obj)))
115 (define (flatten cond)
116 (if (compound-condition? cond) (simple-conditions cond) (list cond)))
117 (or (for-all condition? conditions)
118 (assertion-violation 'condition "non-condition argument" conditions))
119 (if (or (null? conditions) (> (length conditions) 1))
120 (make-compound-condition (apply append (map flatten conditions)))
123 (define-syntax define-condition-type
125 ((_ condition-type supertype constructor predicate
126 (field accessor) ...)
131 (cons '(immutable f a) (transform-fields . rest)))
136 ((_ counter (f a) . rest)
140 (record-accessor condition-type counter)))
141 (generate-accessors (+ counter 1) . rest)))
142 ((_ counter) (begin)))))
144 (define condition-type
145 (make-record-type-descriptor
146 'condition-type supertype #f #f #f
147 (list->vector (transform-fields (field accessor) ...))))
150 (make-record-constructor-descriptor condition-type #f #f)))
151 (define predicate (condition-predicate condition-type))
152 (generate-accessors 0 (field accessor) ...))))))
154 (define &condition (@@ (rnrs records procedural) &condition))
155 (define &condition-constructor-descriptor
156 (make-record-constructor-descriptor &condition #f #f))
157 (define condition-internal? (record-predicate &condition))
159 (define (condition-predicate rtd)
160 (let ((rtd-predicate (record-predicate rtd)))
162 (cond ((compound-condition? obj)
163 (exists rtd-predicate (simple-conditions obj)))
164 ((condition-internal? obj) (rtd-predicate obj))
167 (define (condition-accessor rtd proc)
168 (let ((rtd-predicate (record-predicate rtd)))
170 (cond ((rtd-predicate obj) (proc obj))
171 ((compound-condition? obj)
172 (and=> (find rtd-predicate (simple-conditions obj)) proc))
175 (define-condition-type &message &condition
176 make-message-condition message-condition?
177 (message condition-message))
179 (define-condition-type &warning &condition make-warning warning?)
181 (define &serious (@@ (rnrs records procedural) &serious))
182 (define make-serious-condition
183 (@@ (rnrs records procedural) make-serious-condition))
184 (define serious-condition? (condition-predicate &serious))
186 (define-condition-type &error &serious make-error error?)
188 (define &violation (@@ (rnrs records procedural) &violation))
189 (define make-violation (@@ (rnrs records procedural) make-violation))
190 (define violation? (condition-predicate &violation))
192 (define &assertion (@@ (rnrs records procedural) &assertion))
193 (define make-assertion-violation
194 (@@ (rnrs records procedural) make-assertion-violation))
195 (define assertion-violation? (condition-predicate &assertion))
197 (define-condition-type &irritants &condition
198 make-irritants-condition irritants-condition?
199 (irritants condition-irritants))
201 (define-condition-type &who &condition
202 make-who-condition who-condition?
205 (define-condition-type &non-continuable &violation
206 make-non-continuable-violation
207 non-continuable-violation?)
209 (define-condition-type &implementation-restriction
211 make-implementation-restriction-violation
212 implementation-restriction-violation?)
214 (define-condition-type &lexical &violation
215 make-lexical-violation lexical-violation?)
217 (define-condition-type &syntax &violation
218 make-syntax-violation syntax-violation?
219 (form syntax-violation-form)
220 (subform syntax-violation-subform))
222 (define-condition-type &undefined &violation
223 make-undefined-violation undefined-violation?)