Debian Codegraph @

%3a34/exception-handling.sls

raw
1 (library (srfi :34 exception-handling)
2 (export with-exception-handler guard raise)
3 (import (except (rnrs) with-exception-handler guard raise error) (srfi :23))
4
5 ;; reference implementation from srfi :34 documentation (though it closely matches the existing rnrs facility)
6
7 ;; Copyright (C) Richard Kelsey, Michael Sperber (2002). All Rights Reserved.
8 ;;
9 ;; Permission is hereby granted, free of charge, to any person obtaining a
10 ;; copy of this software and associated documentation files (the "Software"),
11 ;; to deal in the Software without restriction, including without limitation
12 ;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 ;; and/or sell copies of the Software, and to permit persons to whom the
14 ;; Software is furnished to do so, subject to the following conditions:
15 ;;
16 ;; The above copyright notice and this permission notice shall be included in
17 ;; all copies or substantial portions of the Software.
18 ;;
19 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 ;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;; DEALINGS IN THE SOFTWARE.
26
27 (define *current-exception-handlers*
28 (list (lambda (condition)
29 (error "unhandled exception" condition))))
30
31 (define (with-exception-handler handler thunk)
32 (with-exception-handlers (cons handler *current-exception-handlers*)
33 thunk))
34
35 (define (with-exception-handlers new-handlers thunk)
36 (let ((previous-handlers *current-exception-handlers*))
37 (dynamic-wind
38 (lambda ()
39 (set! *current-exception-handlers* new-handlers))
40 thunk
41 (lambda ()
42 (set! *current-exception-handlers* previous-handlers)))))
43
44 (define (raise obj)
45 (let ((handlers *current-exception-handlers*))
46 (with-exception-handlers (cdr handlers)
47 (lambda ()
48 ((car handlers) obj)
49 (error "handler returned"
50 (car handlers)
51 obj)))))
52
53 (define-syntax guard
54 (syntax-rules ()
55 ((guard (var clause ...) e1 e2 ...)
56 ((call-with-current-continuation
57 (lambda (guard-k)
58 (with-exception-handler
59 (lambda (condition)
60 ((call-with-current-continuation
61 (lambda (handler-k)
62 (guard-k
63 (lambda ()
64 (let ((var condition)) ; clauses may SET! var
65 (guard-aux (handler-k (lambda ()
66 (raise condition)))
67 clause ...))))))))
68 (lambda ()
69 (call-with-values
70 (lambda () e1 e2 ...)
71 (lambda args
72 (guard-k (lambda ()
73 (apply values args)))))))))))))
74
75 (define-syntax guard-aux
76 (syntax-rules (else =>)
77 ((guard-aux reraise (else result1 result2 ...))
78 (begin result1 result2 ...))
79 ((guard-aux reraise (test => result))
80 (let ((temp test))
81 (if temp
82 (result temp)
83 reraise)))
84 ((guard-aux reraise (test => result) clause1 clause2 ...)
85 (let ((temp test))
86 (if temp
87 (result temp)
88 (guard-aux reraise clause1 clause2 ...))))
89 ((guard-aux reraise (test))
90 test)
91 ((guard-aux reraise (test) clause1 clause2 ...)
92 (let ((temp test))
93 (if temp
94 temp
95 (guard-aux reraise clause1 clause2 ...))))
96 ((guard-aux reraise (test result1 result2 ...))
97 (if test
98 (begin result1 result2 ...)
99 reraise))
100 ((guard-aux reraise (test result1 result2 ...) clause1 clause2 ...)
101 (if test
102 (begin result1 result2 ...)
103 (guard-aux reraise clause1 clause2 ...))))))