source: project/release/5/simple-exceptions/trunk/simple-exceptions.scm @ 37040

Last change on this file since 37040 was 37040, checked in by juergen, 6 months ago

simple-exceptions 1.2 improve argument and result checks

File size: 13.6 KB
Line 
1#|[
2Author: Juergen Lorenz
3ju (at) jugilo (dot) de
4
5Copyright (c) 2014-2019, Juergen Lorenz
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11
12Redistributions of source code must retain the above copyright
13notice, this list of conditions and the following disclaimer.
14
15Redistributions in binary form must reproduce the above copyright
16notice, this list of conditions and the following disclaimer in the
17documentation and/or other materials provided with the distribution.
18
19Neither the name of the author nor the names of its contributors may be
20used to endorse or promote products derived from this software without
21specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34]|#
35
36
37(module simple-exceptions
38  (simple-exceptions exception? exception-of? make-exception
39   assert-exception argument-exception result-exception
40   exn? exn-of? make-exn argument-exn result-exn assert-exn
41   << >> <<< >>> true? false? named-lambda
42   location message arguments raise with-exn-handler guard assert*)
43
44  (import scheme (only (chicken base) void print case-lambda error)
45                 (chicken condition)
46                 (chicken module)
47                 (chicken continuation))
48  (reexport (only (chicken condition)
49                  current-exception-handler
50                  handle-exceptions
51                  condition-case))
52
53;;; (exception? xpr)
54;;; ----------------
55;;; type predicate
56(define (exception? xpr)
57  (and (condition? xpr)
58       ((condition-predicate 'exn) xpr)))
59
60;;; (exception-of? kind-key)
61;;; ------------------------
62;;; returns a predicate which checks, if its argument is an exception of
63;;; kind kind-key
64(define (exception-of? kind-key)
65  (lambda (xpr)
66    (and (exception? xpr)
67         ((condition-predicate kind-key) xpr))))
68
69
70;;; (make-exception msg . kind-keys)
71;;; ---------------------------------
72;;; returns a procedure with arguments loc and args
73;;; to construct composite-conditions of kind 'exn and kind-keys.
74;;; Note, that msg and kind-keys are independent of a concrete use of
75;;; this exception, while loc and args depend exactly on that use.
76(define (make-exception msg . kind-keys)
77  (lambda (loc . args)
78    (if (null? kind-keys)
79      (make-property-condition 'exn 
80                               'location loc
81                               'message msg
82                               'arguments args)
83      (apply make-composite-condition
84             (make-property-condition 'exn 
85                                      'location loc
86                                      'message msg
87                                      'arguments args)
88             (map (lambda (kind) (make-property-condition kind))
89                  kind-keys)))))
90
91;;; exceptions
92;;; ----------
93(define argument-exception
94  (make-exception "precondition violated" 'argument))
95(define result-exception
96  (make-exception "postcondition violated" 'result))
97(define assert-exception
98  (make-exception "assertion violated" 'assert))
99
100;;;; some aliases
101(define exn? exception?)
102(define exn-of? exception-of?)
103(define make-exn make-exception)
104(define argument-exn argument-exception)
105(define result-exn result-exception)
106(define assert-exn assert-exception)
107
108;;; (named-lambda (name . args) xpr . xprs)
109;;; -----------------------------------
110;;; can replace anonymous procedures in << and >>
111;;; to improve error messages
112(define-syntax named-lambda
113  (syntax-rules ()
114    ((_ (name . args) xpr . xprs)
115     (letrec ((name (lambda args xpr . xprs)))
116       name))))
117
118;;; (<<< loc [arg-name])
119;;; --------------------
120;;; returns a localized argument checker (<< arg . preds)
121;;; at location loc with name arg-name
122(define (<<< loc . arg-name)
123  (case-lambda
124    (() '())
125    ((arg . preds)
126     (if (null? preds)
127       arg
128       (let ((name (cond
129                     ((symbol? (car preds)) (car preds)) ; deprecated
130                     ((not (null? arg-name)) (car arg-name)) 
131                     (else #f)))
132             (preds (if (symbol? (car preds)) (cdr preds) preds)))
133         (let loop ((preds preds))
134           (cond
135             ((null? preds)
136              arg)
137             (((car preds) arg)
138              (loop (cdr preds)))
139             (else
140               (if name
141                 (raise (argument-exception loc name arg (car preds)))
142                 (raise (argument-exception loc arg (car preds)))
143                 )))))))))
144
145;;; (<< arg [arg-name] arg? ...)
146;;; ----------------------------
147;;; pass in a checked argument in an and fashion
148(define <<
149  (case-lambda
150    (() '())
151    ((arg . preds)
152     (if (null? preds)
153       arg
154       (if (symbol? (car preds)) ; deprecated
155         (apply (<<< '<< (car preds)) arg (cdr preds))
156         (apply (<<< '<<) arg preds))))))
157
158;;; (>>> loc [result-name])
159;;; -----------------------
160;;; returns a localized result checker (>> result . preds)
161;;; at location loc
162(define (>>> loc . result-name)
163  (case-lambda
164    (() '())
165    ((result . preds)
166     (if (null? preds)
167       result
168       (let ((name (cond
169                     ((symbol? (car preds)) (car preds)) ; deprecated
170                     ((not (null? result-name)) (car result-name)) 
171                     (else #f)))
172             (preds (if (symbol? (car preds)) (cdr preds) preds)))
173         (let loop ((preds preds))
174           (cond
175             ((null? preds)
176              result)
177             (((car preds) result)
178              (loop (cdr preds)))
179             (else
180               (if name
181                 (raise (result-exception loc name result (car preds)))
182                 (raise (result-exception loc result (car preds)))
183                 )))))))))
184
185;;; (>> result [result-name] result? ...)
186;;; -------------------------------------
187;;; pass out a checked result in an and fashion
188(define >>
189  (case-lambda
190    (() '())
191    ((result . preds)
192     (if (null? preds)
193       result
194       (if (symbol? (car preds)) ; deprecated
195         (apply (>>> '>> (car preds)) result (cdr preds))
196         (apply (>>> '>>) result preds))))))
197
198;;; (guard (exn cond-clause . cond-clauses) xpr . xprs)
199;;; ---------------------------------------------------
200;;; R6RS and R7RS high-level exception-handler
201(define-syntax guard
202  (syntax-rules ()
203    ((_ (exn cond-clause . cond-clauses) xpr . xprs)
204     (handle-exceptions exn (cond cond-clause . cond-clauses)
205                        xpr . xprs))))
206
207;;; (assert* loc xpr . xprs)
208;;; ------------------------
209;;; checks, if its arguments xpr . xprs are not #f.
210(define-syntax assert*
211  (syntax-rules ()
212    ((_ loc)
213     (void))
214    ((_ loc xpr . xprs)
215     (if xpr
216       (assert* loc . xprs)
217       (raise (assert-exception loc 'xpr))))))
218       ;(raise ((make-exception "assertion violated" 'assert)
219       ;        loc 'xpr))))))
220
221;;; (location exn)
222;;; --------------
223;;; returns the location property of its exception argument
224(define (location exn)
225  (assert* 'location (exception? exn))
226  (get-condition-property exn 'exn 'location 'unknown))
227
228;;; (message exn)
229;;; -------------
230;;; returns the message property of its exception argument
231(define (message exn)
232  (assert* 'message (exception? exn))
233  (get-condition-property exn 'exn 'message "no message supplied"))
234
235;;; (arguments exn)
236;;; ---------------
237;;; returns the arguments property of its exception argument
238(define (arguments exn)
239  (assert* 'arguments (exception? exn))
240  (get-condition-property exn 'exn 'arguments '()))
241
242(define (##sys#raise x)
243  (##sys#current-exception-handler x)
244  (##sys#abort
245   (##sys#make-structure
246    'condition
247    '(exn)
248    (list '(exn . message)"exception handler returned"
249          '(exn . arguments) (arguments x)
250          '(exn . location) (location x)))))
251
252;;; (raise exn)
253;;; -----------
254;;; raises a non-continuable exception
255(define (raise exn)
256  (if (exception? exn)
257    (##sys#raise exn)
258    ;(##sys#abort exn)))
259    (abort exn)))
260
261;;; (with-exn-handler handler thunk)
262;;; ----------------------------
263;;; wrapping Chicken's with-exception-handler into pop-and-call
264;;; to avoid the generation of infinite loops
265(define (with-exn-handler handler thunk)
266  ((call-with-current-continuation
267     (lambda (k)
268       (with-exception-handler ; Chicken's handler
269         (lambda (exn)
270           (k (lambda () (handler exn))))
271         thunk)))))
272;(define (with-exn-handler handler thunk)
273;  (continuation-capture
274;     (lambda (k)
275;       (let ((old-handler (current-exception-handler)))
276;         (dynamic-wind
277;           (lambda ()
278;             (current-exception-handler
279;               (lambda (exn)
280;                 (continuation-graft k (lambda () (handler exn))))))
281;           thunk
282;           (lambda ()
283;             (current-exception-handler old-handler)))))))
284
285(define (true? xpr) #t)
286(define (false? xpr) #f)
287
288;;; (simple-exceptions [sym])
289;;; -------------------------
290;;; documentation procedure
291(define simple-exceptions
292  (let ((als '(
293    (simple-exceptions
294      procedure:
295      (simple-exceptions sym ..)
296      "documentation procedure")
297    (assert*
298      macro:
299      (assert* loc xpr ....)
300      "checks, if its arguments xpr .... are  not #f")
301    (exception?
302      procedure:
303      (exception? xpr)
304      "type predicate")
305    (exn?
306      procedure:
307      (exn? xpr)
308      "alias to exception?")
309    (exception-of?
310      procedure:
311      (exception-of? kind-key)
312      "returns a predicate which checks, if its argument"
313      "is an exception of kind kind-key")
314    (exn-of?
315      procedure:
316      (exn-of? kind-key)
317      "alias to exception-of?")
318    (make-exception
319      procedure:
320      (make-exception msg . kind-keys)
321      "returns a procedure with arguments loc and args"
322      "Note, that msg and kind-keys are independent of a concrete use of"
323      "this exception, while loc and args depend exactly on that use.")
324    (make-exn
325      procedure:
326      (make-exn msg . kind-keys)
327      "alias to make-exception")
328    (location
329      procedure:
330      (location exn)
331      "returns the location property of its exception argument")
332    (message
333      procedure:
334      (message exn)
335      "returns the message property of its exception argument")
336    (arguments
337      procedure:
338      (arguments exn)
339      "returns the arguments property of its exception argument")
340    (raise
341      procedure:
342      (raise exn)
343      "raises a non-continuable exception")
344    (with-exn-handler
345      procedure:
346      (with-exn-handler handler thunk)
347      "wraps Chicken's with-exception-handler into pop-and-call"
348      "to avoid the generation of infinite loops")
349    (guard
350      macro:
351      (guard (exn cond-clause . cond-clauses) xpr . xprs)
352      "R6RS and R7RS high-level exception-handler")
353    (handle-exception
354      macro:
355      (handle-exceptions exn handle-xpr xpr . xprs)
356      "reexport of Chicken's routine")
357    (condition-case
358      macro:
359      (condition-case xpr ([var] (kind ...) body) . other-clauses)
360      "reexport of Chicken's macro")
361    (named-lambda
362      macro:
363      (named-lambda name args xpr . xprs)
364      "can be used in place of lambda,"
365      "possibly improving error messages")
366    (<<<
367      procedure:
368      (<<< loc [arg-name])
369      "returns a localized precondition test"
370      "(<< arg [arg-name] arg? ...)"
371      "at location loc with name arg-name")
372    (<<
373      procedure:
374      (<< arg [arg-name] arg? ...)
375      "precondition test"
376      "passes arg unchanged only if all predicates arg? return #t on it"
377      "or raises a argument-exception at location '<<")
378    (>>>
379      procedure:
380      (>>> loc [result-name])
381      "returns a localized postcondition test"
382      "(>> result [result-name] result? ...)"
383      "at location loc with name result-name")
384    (>>
385      procedure:
386      (>> result [result-name] result? ...)
387      "postcondition test"
388      "passes result unchanged only if all predicates result? return #t on it"
389      "or raises a result-exception at location '>>")
390    (argument-exception
391      exception:
392      (argument-exception loc arg ...)
393      "raised when a precondition is violated"
394      "catched with (exn argument)")
395    (argument-exn
396      exception:
397      (argument-exn loc arg ...)
398      "alias to argument-exception")
399    (result-exception
400      exception:
401      (result-exception loc arg ...)
402      "raised when a postcondition is violated"
403      "catched with (exn result)")
404    (result-exn
405      exception:
406      (result-exn loc arg ...)
407      "alias to result-exception")
408    (assert-exception
409      exception:
410      (assert-exception loc arg ...)
411      "raised when an assertion is violated"
412      "catched with (exn assert)")
413    (assert-exn
414      exception:
415      (assert-exn loc arg ...)
416      "alias to assert-exception")
417    (true?
418      procedure?
419      (true? xpr)
420      "returns always #t")
421    (false?
422      procedure?
423      (false? xpr)
424      "returns always #f")
425    )))
426    (case-lambda
427      (()
428       (map car als))
429      ((sym)
430       (let ((pair (assq sym als)))
431         (if pair
432           (for-each print (cdr pair))
433           (error "Not in list"
434                  sym
435                  (map car als))))))))
436
437) ; module simple-exceptions
438
Note: See TracBrowser for help on using the repository browser.