#|[ 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 assert-exception argument-exception result-exception exn? exn-of? make-exn argument-exn result-exn assert-exn << >> <<< >>> 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 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) ;;; (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)))) ;;; (<<< loc) ;;; --------- ;;; returns a localized argument checker (<< arg . preds) ;;; at location loc (define (<<< loc) (lambda (arg . preds) (if (null? preds) arg (let ((arg-name (if (symbol? (car preds)) (car preds) #f)) (preds (if (symbol? (car preds)) (cdr preds) preds))) (let loop ((preds preds)) (cond ((null? preds) arg) (((car preds) arg) (loop (cdr preds))) (else (if arg-name (raise (argument-exception loc arg-name arg (car preds))) (raise (argument-exception loc arg (car preds))))))))))) ;;; (<< arg [arg-name] arg? ...) ;;; ---------------------------- ;;; pass in a checked argument in an and fashion (define (<< arg . preds) (apply (<<< '<<) arg preds)) ;;; (<< x x? ...) ;;; ------------- ;;; pass in a checked argument in an and fashion ;(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 (argument-exception '<< ; arg ; all-names ; (car names))))))))))) ;;; (>>> loc) ;;; --------- ;;; returns a localized result checker (>> result . preds) ;;; at location loc (define (>>> loc) (lambda (result . preds) (if (null? preds) result (let ((result-name (if (symbol? (car preds)) (car preds) #f)) (preds (if (symbol? (car preds)) (cdr preds) preds))) (let loop ((preds preds)) (cond ((null? preds) result) (((car preds) result) (loop (cdr preds))) (else (if result-name (raise (result-exception loc result-name result (car preds))) (raise (result-exception loc 'result result (car preds))))))))))) ;;; (>> result [result-name] result? ...) ;;; ------------------------------------- ;;; pass out a checked result in an and fashion (define (>> result . preds) (apply (>>> '>>) result preds)) ;;; (>> x x? ...) ;;; ------------- ;;; pass out a checked result in an and fashion ;(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 (result-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-exception 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") (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") (named-lambda macro: (named-lambda name args xpr . xprs) "can be used in place of lambda," "possibly improving error messages") (<<< procedure: (<<< loc) "returns a localized precondition test" "(<< arg [arg-name] arg? ...)" "at location loc") (<< procedure: (<< arg [arg-name] arg? ...) "precondition test" "passes arg unchanged only if all predicates arg? return #t on it" "or raises a argument-exception at location '<<") (>>> procedure: (>>> loc) "returns a localized postcondition test" "(>> result [result-name] result? ...)" "at location loc") (>> procedure: (>> result [result-name] result? ...) "postcondition test" "passes result unchanged only if all predicates result? return #t on it" "or raises a result-exception at location '>>") (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") (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