simple-condition.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ;;;; Condition hierarchy
  4. ; General design copied from GNU Emacs
  5. ;; This is the low-level condition representation for use by the
  6. ;; run-time system; it uses lists whose first symbol represents the
  7. ;; condition type.
  8. ;; Systems built on top of this one may choose a different
  9. ;; representation. If they do, they need to provide a condition
  10. ;; decoder for use by the RTS to do some basic processing---mainly
  11. ;; distinguishing errors from the rest.
  12. (define *condition-types* '())
  13. (define (condition-supertypes type)
  14. (assq type *condition-types*))
  15. (define (define-condition-type type supertypes)
  16. (set! *condition-types*
  17. (cons (cons type (apply append
  18. (map (lambda (sup)
  19. (or (condition-supertypes sup)
  20. (error "unrecognized condition type"
  21. sup)))
  22. supertypes)))
  23. *condition-types*)))
  24. ;; alist of (predicate . decode)
  25. ;; Each decoder procedure should return a pair (type . stuff)
  26. (define *condition-decoder-alist* '())
  27. (define (decode-condition condition)
  28. (let loop ((alist *condition-decoder-alist*))
  29. (cond
  30. ((null? alist)
  31. (if (pair? condition)
  32. (values (car condition) (cdr condition))
  33. (values #f #f)))
  34. (((caar alist) condition)
  35. ((cdar alist) condition))
  36. (else
  37. (loop (cdr alist))))))
  38. (define (define-condition-decoder predicate decoder)
  39. (set! *condition-decoder-alist*
  40. (cons (cons predicate decoder)
  41. *condition-decoder-alist*)))
  42. (define (condition-type condition)
  43. (call-with-values
  44. (lambda () (decode-condition condition))
  45. (lambda (type stuff)
  46. type)))
  47. (define (condition-stuff condition)
  48. (call-with-values
  49. (lambda () (decode-condition condition))
  50. (lambda (type stuff)
  51. stuff)))
  52. (define (condition-predicate name)
  53. (lambda (c)
  54. (let ((type (condition-type c)))
  55. (and type
  56. (let ((probe (condition-supertypes type)))
  57. (if probe
  58. (if (memq name probe) #t #f)
  59. #f))))))
  60. ; Errors
  61. (define-condition-type 'error '())
  62. (define error? (condition-predicate 'error))
  63. (define-condition-type 'call-error '(error))
  64. (define call-error? (condition-predicate 'call-error))
  65. (define-condition-type 'read-error '(error))
  66. (define read-error? (condition-predicate 'read-error))
  67. ; VM Exceptions
  68. (define-condition-type 'vm-exception '(error))
  69. (define vm-exception? (condition-predicate 'vm-exception))
  70. (define (vm-exception-opcode c)
  71. (car (condition-stuff c)))
  72. (define (vm-exception-reason c)
  73. (cadr (condition-stuff c)))
  74. (define (vm-exception-arguments c)
  75. (cddr (condition-stuff c)))
  76. (define (make-vm-exception opcode reason args)
  77. (make-condition 'vm-exception (cons opcode (cons reason args))))
  78. ; I/O errors
  79. (define-condition-type 'i/o-error '(error))
  80. (define i/o-error? (condition-predicate 'i/o-error))
  81. (define (i/o-error-status c)
  82. (car (condition-stuff c)))
  83. (define (i/o-error-message c)
  84. (cadr (condition-stuff c)))
  85. (define (i/o-error-operation c)
  86. (caddr (condition-stuff c)))
  87. (define (i/o-error-arguments c)
  88. (cdddr (condition-stuff c)))
  89. (define (make-i/o-error status operation args)
  90. (make-condition 'i/o-error
  91. (cons status
  92. (cons (os-error-message status)
  93. (cons operation args)))))
  94. ; Decoding errors
  95. (define-condition-type 'decoding-error '(error))
  96. (define decoding-error? (condition-predicate 'decoding-error))
  97. (define (make-decoding-error encoding-name message bytes start)
  98. (make-condition 'decoding-error
  99. (list encoding-name message bytes start)))
  100. (define (decoding-error-encoding-name c)
  101. (car (condition-stuff c)))
  102. (define (decoding-error-message c)
  103. (cadr (condition-stuff c)))
  104. (define (decoding-error-bytes c)
  105. (caddr (condition-stuff c)))
  106. (define (decoding-error-start c)
  107. (cadddr (condition-stuff c)))
  108. ; Warnings
  109. (define-condition-type 'warning '())
  110. (define warning? (condition-predicate 'warning))
  111. (define-condition-type 'syntax-error '(warning))
  112. (define syntax-error? (condition-predicate 'syntax-error))
  113. ; Notes
  114. (define-condition-type 'note '())
  115. (define note? (condition-predicate 'note))
  116. ; Interrupts
  117. (define-condition-type 'interrupt '())
  118. (define interrupt? (condition-predicate 'interrupt))