#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2014-2016, Juergen Lorenz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ]|# (module simple-exceptions (export simple-exceptions exception? exception-of? make-exception location message arguments raise with-exn-handler guard assert*) (import scheme (only chicken case-lambda void condition? condition-predicate make-property-condition make-composite-condition get-condition-property continuation-capture continuation-graft)) (reexport (only chicken current-exception-handler handle-exceptions condition-case)) ;;; (exception? xpr) ;;; ---------------- ;;; type predicate (define (exception? xpr) (and (condition? xpr) ((condition-predicate 'exn) xpr))) ;;; (exception-of? kind-key) ;;; ------------------------ ;;; returns a predicate which checks, if its argument is an exception of ;;; kind kind-key (define (exception-of? kind-key) (lambda (xpr) (and (exception? xpr) ((condition-predicate kind-key) xpr)))) ;;; (make-exception msg . kind-keys) ;;; --------------------------------- ;;; returns a procedure with arguments loc and args ;;; to construct composite-conditions of kind 'exn and kind-keys. ;;; Note, that msg and kind-keys are independent of a concrete use of ;;; this exception, while loc and args depend exactly on that use. (define (make-exception msg . kind-keys) (lambda (loc . args) (if (null? kind-keys) (make-property-condition 'exn 'location loc 'message msg 'arguments args) (apply make-composite-condition (make-property-condition 'exn 'location loc 'message msg 'arguments args) (map (lambda (kind) (make-property-condition kind)) kind-keys))))) ;;; (guard (exn cond-clause . cond-clauses) xpr . xprs) ;;; --------------------------------------------------- ;;; R6RS and R7RS high-level exception-handler (define-syntax guard (syntax-rules () ((_ (exn cond-clause . cond-clauses) xpr . xprs) (handle-exceptions exn (cond cond-clause . cond-clauses) xpr . xprs)))) ;;; (assert* loc xpr . xprs) ;;; ------------------------ ;;; checks, if its arguments xpr . xprs are not #f. (define-syntax assert* (syntax-rules () ((_ loc) (void)) ((_ loc xpr . xprs) (if xpr (assert* loc . xprs) ;(raise (assert-exn loc 'xpr)))))) (raise ((make-exception "assertion violated" 'assert) loc 'xpr)))))) ;;; (location exn) ;;; -------------- ;;; returns the location property of its exception argument (define (location exn) (assert* 'location (exception? exn)) (get-condition-property exn 'exn 'location 'unknown)) ;;; (message exn) ;;; ------------- ;;; returns the message property of its exception argument (define (message exn) (assert* 'message (exception? exn)) (get-condition-property exn 'exn 'message "no message supplied")) ;;; (arguments exn) ;;; --------------- ;;; returns the arguments property of its exception argument (define (arguments exn) (assert* 'arguments (exception? exn)) (get-condition-property exn 'exn 'arguments '())) (define (##sys#raise x) (##sys#current-exception-handler x) (##sys#abort (##sys#make-structure 'condition '(exn) (list '(exn . message)"exception handler returned" '(exn . arguments) (arguments x) '(exn . location) (location x))))) ;;; (raise exn) ;;; ----------- ;;; raises a non-continuable exception (define (raise exn) (if (exception? exn) (##sys#raise exn) (##sys#abort exn))) ;;; (with-exn-handler handler thunk) ;;; ---------------------------- ;;; wrapping Chicken's with-exception-handler into pop-and-call ;;; to avoid the generation of infinite loops (define (with-exn-handler handler thunk) ;((call-with-current-continuation (continuation-capture (lambda (k) ;(with-exception-handler ; Chicken's handler ; (lambda (exn) ; (k (lambda () (handler exn)))) ; thunk))))) (let ((old-handler (current-exception-handler))) (dynamic-wind (lambda () (current-exception-handler (lambda (exn) ;(k (lambda () (handler exn)))))) (continuation-graft k (lambda () (handler exn)))))) thunk (lambda () (current-exception-handler old-handler)))))));) ;;; (simple-exceptions [sym]) ;;; ------------------------- ;;; documentation procedure (define simple-exceptions (let ( (signatures '( (assert* loc xpr . xprs) (exception? xpr) (exception-of? kind-key) (make-exception msg . kind-keys) (location exn) (message exn) (arguments exn) (raise exn) (with-exn-handler handler thunk) (handle-exceptions exn handle-xpr xpr . xprs) (guard (exn cond-clause . cond-clauses) xpr . xprs) (condition-case xpr ([var] (kind ...) body) . other-clauses))) ) (case-lambda (() (map car signatures)) ((sym) (assq sym signatures))))) ) ; module simple-exceptions