signal.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ;; Converting from the simple conditions raised from the lower levels.
  3. ;; We offer the same interface as SIMPLE-SIGNALS, except we convert to
  4. ;; SRFI-35 conditions immediately. This is for backwards
  5. ;; compatibility only; the use of this stuff is deprecated.
  6. (define (simple-condition->condition simple-condition)
  7. (let ((type (sc:condition-type simple-condition))
  8. (stuff (sc:condition-stuff simple-condition)))
  9. (let ((shim
  10. (condition (&simple-condition
  11. (type type)
  12. (stuff stuff))))
  13. (converted
  14. ;; this is the stuff in simple-condition.scm
  15. (cond
  16. ((sc:call-error? simple-condition)
  17. (condition (&message (message (car stuff)))
  18. (&call-error
  19. (proc (cadr stuff))
  20. (args (cddr stuff)))))
  21. ((sc:read-error? simple-condition)
  22. (let* ((rev-stuff (reverse stuff)) ; brain damage
  23. (port (car rev-stuff))
  24. (irritants (reverse (cdr rev-stuff))))
  25. (condition (&message (message (car stuff)))
  26. (&irritants (values (cdr irritants)))
  27. (&i/o-port-error (port port))
  28. (&i/o-read-error))))
  29. ((sc:vm-exception? simple-condition)
  30. (let ((opcode (sc:vm-exception-opcode simple-condition))
  31. (reason (sc:vm-exception-reason simple-condition))
  32. (arguments (sc:vm-exception-arguments simple-condition)))
  33. (condition (&vm-exception
  34. (opcode opcode)
  35. (reason reason)
  36. (arguments arguments))
  37. (&message
  38. ;; kludge
  39. (message (cadr (disclose-vm-condition opcode reason arguments)))))))
  40. ((sc:i/o-error? simple-condition)
  41. (condition (&message (message (sc:i/o-error-message simple-condition)))
  42. (&primitive-i/o-error
  43. (status (sc:i/o-error-status simple-condition))
  44. (operation (sc:i/o-error-operation simple-condition))
  45. (arguments (sc:i/o-error-arguments simple-condition)))))
  46. ((sc:decoding-error? simple-condition)
  47. (condition (&decoding-error
  48. (encoding-name (sc:decoding-error-encoding-name simple-condition)))
  49. (&irritants
  50. (values (list (sc:decoding-error-bytes simple-condition)
  51. (sc:decoding-error-start simple-condition))))
  52. (&message
  53. (message (sc:decoding-error-message simple-condition)))))
  54. ((sc:error? simple-condition)
  55. (condition (&message (message (cadr simple-condition)))
  56. (&error) ; probably not always true
  57. (&irritants
  58. (values (cddr simple-condition)))))
  59. ((sc:syntax-error? simple-condition)
  60. (condition (&message (message (cadr simple-condition)))
  61. (&syntax-error)
  62. (&irritants
  63. (values (cddr simple-condition)))))
  64. ((sc:warning? simple-condition)
  65. (condition (&message (message (cadr simple-condition)))
  66. (&warning)
  67. (&irritants
  68. (values (cddr simple-condition)))))
  69. ((sc:note? simple-condition)
  70. (condition (&message (message (cadr simple-condition)))
  71. (&note)
  72. (&irritants
  73. (values (cddr simple-condition)))))
  74. ((sc:interrupt? simple-condition)
  75. (condition (&interrupt (type (cadr simple-condition)))))
  76. (else #f))))
  77. (if converted
  78. (make-compound-condition converted shim)
  79. shim))))
  80. (define (coerce-to-condition thing)
  81. (if (condition? thing)
  82. thing
  83. (simple-condition->condition thing)))
  84. (define (condition->simple-condition condition)
  85. (if (simple-condition? condition)
  86. (cons (simple-condition-type condition)
  87. (simple-condition-stuff condition))
  88. (let ((message (if (message-condition? condition)
  89. (condition-message condition)
  90. "unknown")))
  91. (cond
  92. ((error? condition)
  93. (list 'error message))
  94. ((warning? condition)
  95. (list 'warning message))
  96. ((note? condition)
  97. (list 'note message))
  98. (else
  99. (list 'unknown message))))))
  100. (define (coerce-to-simple-condition condition)
  101. (if (condition? condition)
  102. (condition->simple-condition condition)
  103. condition))
  104. (sc:define-condition-decoder condition?
  105. (lambda (condition)
  106. (let ((simple-condition (condition->simple-condition condition)))
  107. (values (car simple-condition)
  108. (cdr simple-condition)))))
  109. (define (signal-condition condition)
  110. (really-signal-condition (coerce-to-condition condition)))
  111. (define (make-condition type stuff)
  112. (simple-condition->condition (cons type stuff)))
  113. ; Legacy code, copied verbatim from simple-signal.scm
  114. (define (signal type . stuff)
  115. (signal-condition (make-condition type stuff)))
  116. ; Error
  117. (define (error message . irritants)
  118. (apply signal 'error message irritants))
  119. ; Warn
  120. (define (warn message . irritants)
  121. (signal-condition (make-condition 'warning (cons message irritants))))
  122. ; Note
  123. (define (note message . irritants)
  124. (signal-condition (make-condition 'note (cons message irritants))))
  125. ; Syntax errors
  126. (define (syntax-error message . rest) ; Must return a valid expression.
  127. (signal-condition (make-condition 'syntax-error (cons message rest)))
  128. ''syntax-error)
  129. ; "Call error" - this means that the condition's "stuff" (cdr) is of
  130. ; the form (message procedure . args), and should be displayed appropriately.
  131. ; Proceeding from such an error should return the value that the call
  132. ; to the procedure on the args should have returned.
  133. (define (call-error message proc . args)
  134. (signal-condition (make-condition 'call-error
  135. (cons message (cons proc args)))))