#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2014-2019, 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 (simple-exceptions exception? exception-of? make-exception assert-exception argument-exception result-exception exn? exn-of? make-exn argument-exn result-exn assert-exn location message arguments raise with-exn-handler guard) (import scheme (only (chicken base) assert void print case-lambda error) (chicken condition) (chicken module) (chicken continuation)) (reexport (only (chicken condition) 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))))) ;;; exceptions ;;; ---------- (define argument-exception (make-exception "precondition violated" 'argument)) (define result-exception (make-exception "postcondition violated" 'result)) (define assert-exception (make-exception "assertion violated" 'assert)) ;;;; some aliases (define exn? exception?) (define exn-of? exception-of?) (define make-exn make-exception) (define argument-exn argument-exception) (define result-exn result-exception) (define assert-exn assert-exception) ;;; (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)))) ;;; (location exn) ;;; -------------- ;;; returns the location property of its exception argument (define (location exn) (assert (exception? exn) 'location "assertion failed" '(exception? exn)) (get-condition-property exn 'exn 'location 'unknown)) ;;; (message exn) ;;; ------------- ;;; returns the message property of its exception argument (define (message exn) (assert (exception? exn) 'message "assertion failed" '(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 (exception? exn) 'arguments "assertion failed" '(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))) (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 (lambda (k) (with-exception-handler ; Chicken's handler (lambda (exn) (k (lambda () (handler exn)))) thunk))))) ;;; (simple-exceptions [sym]) ;;; ------------------------- ;;; documentation procedure (define simple-exceptions (let ((als '( (simple-exceptions procedure: (simple-exceptions sym ..) "documentation procedure") (exception? procedure: (exception? xpr) "type predicate") (exn? procedure: (exn? xpr) "alias to exception?") (exception-of? procedure: (exception-of? kind-key) "returns a predicate which checks, if its argument" "is an exception of kind kind-key") (exn-of? procedure: (exn-of? kind-key) "alias to exception-of?") (make-exception procedure: (make-exception msg . kind-keys) "returns a procedure with arguments loc and args" "Note, that msg and kind-keys are independent of a concrete use of" "this exception, while loc and args depend exactly on that use.") (make-exn procedure: (make-exn msg . kind-keys) "alias to make-exception") (location procedure: (location exn) "returns the location property of its exception argument") (message procedure: (message exn) "returns the message property of its exception argument") (arguments procedure: (arguments exn) "returns the arguments property of its exception argument") (raise procedure: (raise exn) "raises a non-continuable exception") (with-exn-handler procedure: (with-exn-handler handler thunk) "wraps Chicken's with-exception-handler into pop-and-call" "to avoid the generation of infinite loops") (guard macro: (guard (exn cond-clause . cond-clauses) xpr . xprs) "R6RS and R7RS high-level exception-handler") (handle-exception macro: (handle-exceptions exn handle-xpr xpr . xprs) "reexport of Chicken's routine") (condition-case macro: (condition-case xpr ([var] (kind ...) body) . other-clauses) "reexport of Chicken's macro") (argument-exception exception: (argument-exception loc arg ...) "raised when a precondition is violated" "catched with (exn argument)") (argument-exn exception: (argument-exn loc arg ...) "alias to argument-exception") (result-exception exception: (result-exception loc arg ...) "raised when a postcondition is violated" "catched with (exn result)") (result-exn exception: (result-exn loc arg ...) "alias to result-exception") (assert-exception exception: (assert-exception loc arg ...) "raised when an assertion is violated" "catched with (exn assert)") (assert-exn exception: (assert-exn loc arg ...) "alias to assert-exception") ))) (case-lambda (() (map car als)) ((sym) (let ((pair (assq sym als))) (if pair (for-each print (cdr pair)) (error "Not in list" sym (map car als)))))))) ) ; module simple-exceptions