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

Last change on this file since 36313 was 36313, checked in by juergen, 2 years ago

port of simple-exceptions to chicken-5

File size: 12.8 KB
Line 
1#|[
2Author: Juergen Lorenz
3ju (at) jugilo (dot) de
4
5Copyright (c) 2014-2018, 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)
119;;; ---------
120;;; returns a localized argument checker (<< arg . preds)
121;;; at location loc
122(define (<<< loc)
123  (lambda (arg . preds)
124    (if (null? preds)
125      arg
126      (let ((arg-name (if (symbol? (car preds)) (car preds) #f))
127            (preds (if (symbol? (car preds)) (cdr preds) preds)))
128        (let loop ((preds preds))
129          (cond
130            ((null? preds)
131             arg)
132            (((car preds) arg)
133             (loop (cdr preds)))
134            (else
135              (if arg-name
136                (raise (argument-exception loc arg-name arg (car preds)))
137                (raise (argument-exception loc arg (car preds)))))))))))
138
139;;; (<< arg [arg-name] arg? ...)
140;;; ----------------------------
141;;; pass in a checked argument in an and fashion
142(define (<< arg . preds)
143  (apply (<<< '<<) arg preds))
144
145;;; (>>> loc)
146;;; ---------
147;;; returns a localized result checker (>> result . preds)
148;;; at location loc
149(define (>>> loc)
150  (lambda (result . preds)
151    (if (null? preds)
152      result
153      (let ((result-name (if (symbol? (car preds)) (car preds) #f))
154            (preds (if (symbol? (car preds)) (cdr preds) preds)))
155        (let loop ((preds preds))
156          (cond
157            ((null? preds)
158             result)
159            (((car preds) result)
160             (loop (cdr preds)))
161            (else
162              (if result-name
163                (raise (result-exception loc result-name result (car preds)))
164                (raise (result-exception loc 'result result (car preds)))))))))))
165
166;;; (>> result [result-name] result? ...)
167;;; -------------------------------------
168;;; pass out a checked result in an and fashion
169(define (>> result . preds)
170  (apply (>>> '>>) result preds))
171
172;;; (guard (exn cond-clause . cond-clauses) xpr . xprs)
173;;; ---------------------------------------------------
174;;; R6RS and R7RS high-level exception-handler
175(define-syntax guard
176  (syntax-rules ()
177    ((_ (exn cond-clause . cond-clauses) xpr . xprs)
178     (handle-exceptions exn (cond cond-clause . cond-clauses)
179                        xpr . xprs))))
180
181;;; (assert* loc xpr . xprs)
182;;; ------------------------
183;;; checks, if its arguments xpr . xprs are not #f.
184(define-syntax assert*
185  (syntax-rules ()
186    ((_ loc)
187     (void))
188    ((_ loc xpr . xprs)
189     (if xpr
190       (assert* loc . xprs)
191       (raise (assert-exception loc 'xpr))))))
192       ;(raise ((make-exception "assertion violated" 'assert)
193       ;        loc 'xpr))))))
194
195;;; (location exn)
196;;; --------------
197;;; returns the location property of its exception argument
198(define (location exn)
199  (assert* 'location (exception? exn))
200  (get-condition-property exn 'exn 'location 'unknown))
201
202;;; (message exn)
203;;; -------------
204;;; returns the message property of its exception argument
205(define (message exn)
206  (assert* 'message (exception? exn))
207  (get-condition-property exn 'exn 'message "no message supplied"))
208
209;;; (arguments exn)
210;;; ---------------
211;;; returns the arguments property of its exception argument
212(define (arguments exn)
213  (assert* 'arguments (exception? exn))
214  (get-condition-property exn 'exn 'arguments '()))
215
216(define (##sys#raise x)
217  (##sys#current-exception-handler x)
218  (##sys#abort
219   (##sys#make-structure
220    'condition
221    '(exn)
222    (list '(exn . message)"exception handler returned"
223          '(exn . arguments) (arguments x)
224          '(exn . location) (location x)))))
225
226;;; (raise exn)
227;;; -----------
228;;; raises a non-continuable exception
229(define (raise exn)
230  (if (exception? exn)
231    (##sys#raise exn)
232    ;(##sys#abort exn)))
233    (abort exn)))
234
235;;; (with-exn-handler handler thunk)
236;;; ----------------------------
237;;; wrapping Chicken's with-exception-handler into pop-and-call
238;;; to avoid the generation of infinite loops
239(define (with-exn-handler handler thunk)
240  ((call-with-current-continuation
241     (lambda (k)
242       (with-exception-handler ; Chicken's handler
243         (lambda (exn)
244           (k (lambda () (handler exn))))
245         thunk)))))
246;(define (with-exn-handler handler thunk)
247;  (continuation-capture
248;     (lambda (k)
249;       (let ((old-handler (current-exception-handler)))
250;         (dynamic-wind
251;           (lambda ()
252;             (current-exception-handler
253;               (lambda (exn)
254;                 (continuation-graft k (lambda () (handler exn))))))
255;           thunk
256;           (lambda ()
257;             (current-exception-handler old-handler)))))))
258
259(define (true? xpr) #t)
260(define (false? xpr) #f)
261
262;;; (simple-exceptions [sym])
263;;; -------------------------
264;;; documentation procedure
265(define simple-exceptions
266  (let ((als '(
267    (simple-exceptions
268      procedure:
269      (simple-exceptions sym ..)
270      "documentation procedure")
271    (assert*
272      macro:
273      (assert* loc xpr ....)
274      "checks, if its arguments xpr .... are  not #f")
275    (exception?
276      procedure:
277      (exception? xpr)
278      "type predicate")
279    (exn?
280      procedure:
281      (exn? xpr)
282      "alias to exception?")
283    (exception-of?
284      procedure:
285      (exception-of? kind-key)
286      "returns a predicate which checks, if its argument"
287      "is an exception of kind kind-key")
288    (exn-of?
289      procedure:
290      (exn-of? kind-key)
291      "alias to exception-of?")
292    (make-exception
293      procedure:
294      (make-exception msg . kind-keys)
295      "returns a procedure with arguments loc and args"
296      "Note, that msg and kind-keys are independent of a concrete use of"
297      "this exception, while loc and args depend exactly on that use.")
298    (make-exn
299      procedure:
300      (make-exn msg . kind-keys)
301      "alias to make-exception")
302    (location
303      procedure:
304      (location exn)
305      "returns the location property of its exception argument")
306    (message
307      procedure:
308      (message exn)
309      "returns the message property of its exception argument")
310    (arguments
311      procedure:
312      (arguments exn)
313      "returns the arguments property of its exception argument")
314    (raise
315      procedure:
316      (raise exn)
317      "raises a non-continuable exception")
318    (with-exn-handler
319      procedure:
320      (with-exn-handler handler thunk)
321      "wraps Chicken's with-exception-handler into pop-and-call"
322      "to avoid the generation of infinite loops")
323    (guard
324      macro:
325      (guard (exn cond-clause . cond-clauses) xpr . xprs)
326      "R6RS and R7RS high-level exception-handler")
327    (handle-exception
328      macro:
329      (handle-exceptions exn handle-xpr xpr . xprs)
330      "reexport of Chicken's routine")
331    (condition-case
332      macro:
333      (condition-case xpr ([var] (kind ...) body) . other-clauses)
334      "reexport of Chicken's macro")
335    (named-lambda
336      macro:
337      (named-lambda name args xpr . xprs)
338      "can be used in place of lambda,"
339      "possibly improving error messages")
340    (<<<
341      procedure:
342      (<<< loc)
343      "returns a localized precondition test"
344      "(<< arg [arg-name] arg? ...)"
345      "at location loc")
346    (<<
347      procedure:
348      (<< arg [arg-name] arg? ...)
349      "precondition test"
350      "passes arg unchanged only if all predicates arg? return #t on it"
351      "or raises a argument-exception at location '<<")
352    (>>>
353      procedure:
354      (>>> loc)
355      "returns a localized postcondition test"
356      "(>> result [result-name] result? ...)"
357      "at location loc")
358    (>>
359      procedure:
360      (>> result [result-name] result? ...)
361      "postcondition test"
362      "passes result unchanged only if all predicates result? return #t on it"
363      "or raises a result-exception at location '>>")
364    (argument-exception
365      exception:
366      (argument-exception loc arg ...)
367      "raised when a precondition is violated"
368      "catched with (exn argument)")
369    (argument-exn
370      exception:
371      (argument-exn loc arg ...)
372      "alias to argument-exception")
373    (result-exception
374      exception:
375      (result-exception loc arg ...)
376      "raised when a postcondition is violated"
377      "catched with (exn result)")
378    (result-exn
379      exception:
380      (result-exn loc arg ...)
381      "alias to result-exception")
382    (assert-exception
383      exception:
384      (assert-exception loc arg ...)
385      "raised when an assertion is violated"
386      "catched with (exn assert)")
387    (assert-exn
388      exception:
389      (assert-exn loc arg ...)
390      "alias to assert-exception")
391    (true?
392      procedure?
393      (true? xpr)
394      "returns always #t")
395    (false?
396      procedure?
397      (false? xpr)
398      "returns always #f")
399    )))
400    (case-lambda
401      (()
402       (map car als))
403      ((sym)
404       (let ((pair (assq sym als)))
405         (if pair
406           (for-each print (cdr pair))
407           (error "Not in list"
408                  sym
409                  (map car als))))))))
410
411) ; module simple-exceptions
412
Note: See TracBrowser for help on using the repository browser.