diff --git a/pkgs/srfi-pkgs/srfi-doc/srfi/scribblings/srfi.scrbl b/pkgs/srfi-pkgs/srfi-doc/srfi/scribblings/srfi.scrbl index 5b720faeff..d0c7b029f3 100644 --- a/pkgs/srfi-pkgs/srfi-doc/srfi/scribblings/srfi.scrbl +++ b/pkgs/srfi-pkgs/srfi-doc/srfi/scribblings/srfi.scrbl @@ -707,6 +707,10 @@ This SRFI's syntax is part of Racket's default reader. (raise #f "raise") )] +An @racket[else] is recognized as either the one from +@racketmodname[racket/base] or as an identifier with the symbolic form +@racket['else] and no binding. + @; ---------------------------------------- @srfi[35]{Conditions} diff --git a/pkgs/srfi-pkgs/srfi-lib/srfi/34/exception.rkt b/pkgs/srfi-pkgs/srfi-lib/srfi/34/exception.rkt index eef927a15e..7b79798df6 100644 --- a/pkgs/srfi-pkgs/srfi-lib/srfi/34/exception.rkt +++ b/pkgs/srfi-pkgs/srfi-lib/srfi/34/exception.rkt @@ -1,25 +1,32 @@ +#lang racket/base +(require (for-syntax racket/base + syntax/stx)) + ;; SRFI 34 for PLT Scheme ;; Zhu Chongkai, April 2005 ;; -(module exception mzscheme + +(provide with-exception-handler + guard + raise) - (provide with-exception-handler - guard - raise) - - (define-syntax with-exception-handler - (syntax-rules () - ((_ handler thunk) - (with-handlers (((lambda (exn) #t) handler)) (thunk))))) - - (define-syntax guard - (syntax-rules (else) - ((_ (var clause ... (else de ...)) e1 e2 ...) +(define-syntax with-exception-handler + (syntax-rules () + ((_ handler thunk) + (with-handlers (((lambda (exn) #t) handler)) (thunk))))) + +(define-syntax (guard stx) + (syntax-case stx () + ((_ (var clause ... (els de ...)) e1 e2 ...) + (and (identifier? #'els) + (module-or-top-identifier=? #'els #'else)) + (syntax/loc stx (with-handlers (((lambda (exn) #t) (lambda (var) (cond clause ... (else de ...))))) - e1 e2 ...)) - ((_ (var clause ...) e1 e2 ...) + e1 e2 ...))) + ((_ (var clause ...) e1 e2 ...) + (syntax/loc stx (with-handlers (((lambda (exn) #t) (lambda (var) (cond clause ... (else (raise var)))))) diff --git a/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/else.rkt b/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/else.rkt new file mode 100644 index 0000000000..ec836411c7 --- /dev/null +++ b/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/else.rkt @@ -0,0 +1,18 @@ +#lang racket/base +(require srfi/34) + +(guard (condition + (else + (display "condition: ") + (write condition) + (newline) + 'exception)) + (+ 1 (raise 'an-error))) + +(guard (condition + (#t ; <- not an identifier + (display "condition: ") + (write condition) + (newline) + 'exception)) + (+ 1 (raise 'an-error))) diff --git a/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/mz-else.rkt b/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/mz-else.rkt new file mode 100644 index 0000000000..12138ed104 --- /dev/null +++ b/pkgs/srfi-pkgs/srfi-test/tests/srfi/34/mz-else.rkt @@ -0,0 +1,10 @@ +#lang mzscheme +(require srfi/34) + +(guard (condition + (else + (display "condition: ") + (write condition) + (newline) + 'exception)) + (+ 1 (raise 'an-error)))