#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2014-2017, 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 pre-exception post-exception << >> true? false? named-lambda 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 error print)) (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))))) ;;; exceptions ;;; ---------- (define pre-exception (make-exception "precondition violated" 'arguments)) (define post-exception (make-exception "postcondition violated" 'results)) ;;; (named-lambda name args xpr . xprs) ;;; ----------------------------------- ;;; can replace anonymous procedures in << and >> ;;; to improve error messages (define-syntax named-lambda (syntax-rules () ((_ name args xpr . xprs) (letrec ((name (lambda args xpr . xprs))) name)))) ;;; (<< x x? ...) ;;; ------------- ;;; pass in a checked argument in an and fashion (define (<< x . preds) (let loop ((preds preds)) (cond ((null? preds) x) (((car preds) x) (loop (cdr preds))) (else (raise (pre-exception '<< x (car preds))))))) ;(define-syntax << ; (ir-macro-transformer ; (lambda (form inject compare?) ; (let ((x (cadr form)) (checks (cddr form))) ; `(let ((arg ,x) (all-names ',checks)) ; (let loop ((checks ,(cons 'list checks)) (names ',checks)) ; (cond ; ((null? checks) arg) ; passed on success ; (((car checks) arg) ; (loop (cdr checks) (cdr names))) ; (else ; (raise (pre-exception '<< ; arg ; all-names ; (car names))))))))))) ;;; (>> x x? ...) ;;; ------------- ;;; pass out a checked result in an and fashion (define (>> x . preds) (let loop ((preds preds)) (cond ((null? preds) x) (((car preds) x) (loop (cdr preds))) (else (raise (post-exception '>> x (car preds))))))) ;(define-syntax >> ; (ir-macro-transformer ; (lambda (form inject compare?) ; (let ((x (cadr form)) (checks (cddr form))) ; `(let ((return ,x) (all-names ',checks)) ; (let loop ((checks ,(cons 'list checks)) (names ',checks)) ; (cond ; ((null? checks) return) ; passed on success ; (((car checks) return) ; (loop (cdr checks) (cdr names))) ; (else ; (raise (post-exception '>> ; return ; all-names ; (car names))))))))))) ;;; (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)))))));) (define (true? xpr) #t) (define (false? xpr) #f) ;;; (simple-exceptions [sym]) ;;; ------------------------- ;;; documentation procedure (define simple-exceptions (let ((als '( (simple-exceptions procedure: (simple-exceptions sym ..) "documentation procedure") (assert* macro: (assert* loc xpr ....) "checks, if its arguments xpr .... are not #f") (exception? procedure: (exception? xpr) "type predicate") (exception-of? procedure: (exception-of? kind-key) "returns a predicate which checks, if its argument" "is an exception of kind kind-key") (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.") (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") (named-lambda macro: (named-lambda name args xpr . xprs) "can be used in place of lambda," "possibly improving error messages") (<< procedure: (<< x x? ...) "precondition test" "passes x unchanged only if all predicates x? return #t on it") (>> procedure: (>> x x? ...) "postcondition test" "passes x unchanged only if all predicates x? return #t on it") (pre-exception exception: (pre-exception loc arg ...) "raised when a precondition is violated" "catched with (exn arguments)") (post-exception exception: (post-exception loc arg ...) "raised when a postcondition is violated" "catched with (exn results)") (true? procedure? (true? xpr) "returns always #t") (false? procedure? (false? xpr) "returns always #f") ))) (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