1 ;;; exceptions.scm --- The R6RS exceptions library
3 ;; Copyright (C) 2010, 2011 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 exceptions (6))
21 (export guard with-exception-handler raise raise-continuable)
22 (import (rnrs base (6))
25 (rnrs records procedural (6))
26 (rnrs records inspection (6))
32 set-exception-printer!
37 (define raise (@@ (rnrs records procedural) r6rs-raise))
38 (define raise-continuable
39 (@@ (rnrs records procedural) r6rs-raise-continuable))
40 (define raise-object-wrapper?
41 (@@ (rnrs records procedural) raise-object-wrapper?))
42 (define raise-object-wrapper-obj
43 (@@ (rnrs records procedural) raise-object-wrapper-obj))
44 (define raise-object-wrapper-continuation
45 (@@ (rnrs records procedural) raise-object-wrapper-continuation))
47 (define (with-exception-handler handler thunk)
48 (with-throw-handler 'r6rs:exception
51 (if (and (not (null? args))
52 (raise-object-wrapper? (car args)))
53 (let* ((cargs (car args))
54 (obj (raise-object-wrapper-obj cargs))
55 (continuation (raise-object-wrapper-continuation cargs))
56 (handler-return (handler obj)))
58 (continuation handler-return)
59 (raise (make-non-continuable-violation))))
64 ((_ (variable cond-clause ...) . body)
65 (call/cc (lambda (continuation)
66 (with-exception-handler
68 (continuation (cond cond-clause ...)))
69 (lambda () . body)))))))
73 ((_ (variable cond-clause ... . ((else else-clause ...))) . body)
74 (guard0 (variable cond-clause ... (else else-clause ...)) . body))
75 ((_ (variable cond-clause ...) . body)
76 (guard0 (variable cond-clause ... (else (raise variable))) . body))))
78 ;;; Exception printing
80 (define (exception-printer port key args punt)
81 (cond ((and (= 1 (length args))
82 (raise-object-wrapper? (car args)))
83 (let ((obj (raise-object-wrapper-obj (car args))))
84 (cond ((condition? obj)
85 (display "ERROR: R6RS exception:\n" port)
86 (format-condition port obj))
88 (format port "ERROR: R6RS exception: `~s'" obj)))))
92 (define (format-condition port condition)
93 (let ((components (simple-conditions condition)))
94 (if (null? components)
95 (format port "Empty condition object")
96 (let loop ((i 1) (components components))
97 (cond ((pair? components)
98 (format port " ~a. " i)
99 (format-simple-condition port (car components))
100 (when (pair? (cdr components))
102 (loop (+ i 1) (cdr components))))))))
104 (define (format-simple-condition port condition)
105 (define (print-rtd-fields rtd field-names)
106 (let ((n-fields (vector-length field-names)))
109 (format port " ~a: ~s"
110 (vector-ref field-names i)
111 ((record-accessor rtd i) condition))
112 (unless (= i (- n-fields 1))
114 (let ((condition-name (record-type-name (record-rtd condition))))
115 (let loop ((rtd (record-rtd condition))
116 (rtd.fields-list '())
119 (let ((field-names (record-type-field-names rtd)))
120 (loop (record-type-parent rtd)
121 (cons (cons rtd field-names) rtd.fields-list)
122 (+ n-fields (vector-length field-names)))))
124 (let ((rtd.fields-list
125 (filter (lambda (rtd.fields)
126 (not (zero? (vector-length (cdr rtd.fields)))))
127 (reverse rtd.fields-list))))
129 ((0) (format port "~a" condition-name))
130 ((1) (format port "~a: ~s"
132 ((record-accessor (caar rtd.fields-list) 0)
135 (format port "~a:\n" condition-name)
136 (let loop ((lst rtd.fields-list))
138 (let ((rtd.fields (car lst)))
139 (print-rtd-fields (car rtd.fields) (cdr rtd.fields))
140 (when (pair? (cdr lst))
142 (loop (cdr lst)))))))))))))
144 (set-exception-printer! 'r6rs:exception exception-printer)