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

Last change on this file since 36637 was 36637, checked in by juergen, 12 months ago

simple-exceptions added zero args to << and (<<< loc)

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