srfi-34.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;; srfi-34.scm --- Exception handling for programs
  2. ;; Copyright (C) 2003, 2006, 2008, 2010 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Author: Neil Jerram <neil@ossau.uklinux.net>
  18. ;;; Commentary:
  19. ;; This is an implementation of SRFI-34: Exception Handling for
  20. ;; Programs. For documentation please see the SRFI-34 document; this
  21. ;; module is not yet documented at all in the Guile manual.
  22. ;;; Code:
  23. (define-module (srfi srfi-34)
  24. #:re-export (with-exception-handler
  25. (raise-exception . raise))
  26. #:export-syntax (guard))
  27. (cond-expand-provide (current-module) '(srfi-34))
  28. (define-syntax guard
  29. (syntax-rules (else)
  30. "Syntax: (guard (<var> <clause1> <clause2> ...) <body>)
  31. Each <clause> should have the same form as a `cond' clause.
  32. Semantics: Evaluating a guard form evaluates <body> with an exception
  33. handler that binds the raised object to <var> and within the scope of
  34. that binding evaluates the clauses as if they were the clauses of a
  35. cond expression. That implicit cond expression is evaluated with the
  36. continuation and dynamic environment of the guard expression. If
  37. every <clause>'s <test> evaluates to false and there is no else
  38. clause, then raise is re-invoked on the raised object within the
  39. dynamic environment of the original call to raise except that the
  40. current exception handler is that of the guard expression."
  41. ((guard (var clause ... (else e e* ...)) body body* ...)
  42. (with-exception-handler
  43. (lambda (var)
  44. (cond clause ...
  45. (else e e* ...)))
  46. (lambda () body body* ...)
  47. #:unwind? #t))
  48. ((guard (var clause clause* ...) body body* ...)
  49. (let ((tag (make-prompt-tag)))
  50. (call-with-prompt
  51. tag
  52. (lambda ()
  53. (with-exception-handler
  54. (lambda (exn)
  55. (abort-to-prompt tag exn)
  56. (raise-exception exn))
  57. (lambda () body body* ...)))
  58. (lambda (rewind var)
  59. (cond clause clause* ...
  60. (else (rewind)))))))))
  61. ;;; (srfi srfi-34) ends here.