srfi-34.scm 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #:export (with-exception-handler)
  25. #:replace (raise)
  26. #:export-syntax (guard))
  27. (cond-expand-provide (current-module) '(srfi-34))
  28. (define throw-key 'srfi-34)
  29. (define (with-exception-handler handler thunk)
  30. "Returns the result(s) of invoking THUNK. HANDLER must be a
  31. procedure that accepts one argument. It is installed as the current
  32. exception handler for the dynamic extent (as determined by
  33. dynamic-wind) of the invocation of THUNK."
  34. (with-throw-handler throw-key
  35. thunk
  36. (lambda (key obj)
  37. (handler obj))))
  38. (define (raise obj)
  39. "Invokes the current exception handler on OBJ. The handler is
  40. called in the dynamic environment of the call to raise, except that
  41. the current exception handler is that in place for the call to
  42. with-exception-handler that installed the handler being called. The
  43. handler's continuation is otherwise unspecified."
  44. (throw throw-key obj))
  45. (define-syntax guard
  46. (syntax-rules (else)
  47. "Syntax: (guard (<var> <clause1> <clause2> ...) <body>)
  48. Each <clause> should have the same form as a `cond' clause.
  49. Semantics: Evaluating a guard form evaluates <body> with an exception
  50. handler that binds the raised object to <var> and within the scope of
  51. that binding evaluates the clauses as if they were the clauses of a
  52. cond expression. That implicit cond expression is evaluated with the
  53. continuation and dynamic environment of the guard expression. If
  54. every <clause>'s <test> evaluates to false and there is no else
  55. clause, then raise is re-invoked on the raised object within the
  56. dynamic environment of the original call to raise except that the
  57. current exception handler is that of the guard expression."
  58. ((guard (var clause ... (else e e* ...)) body body* ...)
  59. (catch throw-key
  60. (lambda () body body* ...)
  61. (lambda (key var)
  62. (cond clause ...
  63. (else e e* ...)))))
  64. ((guard (var clause clause* ...) body body* ...)
  65. (catch throw-key
  66. (lambda () body body* ...)
  67. (lambda (key var)
  68. (cond clause clause* ...
  69. (else (throw key var))))))))
  70. ;;; (srfi srfi-34) ends here.