exceptions.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ;;; Exception definitions
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Exception constructors for common errors.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot exceptions)
  21. (export &exception simple-exception?
  22. &compound-exception make-compound-exception compound-exception?
  23. compound-exception-components
  24. simple-exceptions make-exception exception?
  25. &message make-exception-with-message exception-with-message?
  26. exception-message
  27. &warning make-warning warning?
  28. &serious make-serious-exception serious-exception?
  29. &error make-error error?
  30. &violation make-violation violation?
  31. &assertion make-assertion-violation assertion-violation?
  32. &arity-violation make-arity-violation arity-violation?
  33. &implementation-restriction make-implementation-restriction-violation
  34. implementation-restriction-violation?
  35. &failed-type-check make-failed-type-check failed-type-check?
  36. failed-type-check-predicate
  37. &non-continuable make-non-continuable-violation
  38. non-continuable-violation?
  39. &irritants make-exception-with-irritants exception-with-irritants?
  40. exception-irritants
  41. &origin make-exception-with-origin exception-with-origin?
  42. exception-origin
  43. &lexical make-lexical-violation lexical-violation?
  44. &i/o make-i/o-error i/o-error?
  45. &i/o-line-and-column make-i/o-line-and-column-error
  46. i/o-line-and-column-error? i/o-error-line i/o-error-column
  47. &i/o-filename make-i/o-filename-error i/o-filename-error?
  48. i/o-error-filename
  49. &i/o-not-seekable make-i/o-not-seekable-error i/o-not-seekable-error?
  50. &i/o-port make-i/o-port-error i/o-port-error? i/o-error-port)
  51. (import (hoot syntax)
  52. (hoot features)
  53. (hoot cond-expand)
  54. (hoot errors)
  55. (hoot pairs)
  56. (hoot lists)
  57. (hoot records)
  58. (hoot match)
  59. (only (hoot primitives) %inline-wasm))
  60. (define-record-type &exception
  61. #:extensible? #t
  62. (make-&exception)
  63. simple-exception?)
  64. (define-record-type &compound-exception
  65. (make-compound-exception components)
  66. compound-exception?
  67. (components compound-exception-components))
  68. (define (simple-exceptions exception)
  69. "Return a list of the simple exceptions that compose the exception
  70. object @var{exception}."
  71. (cond ((compound-exception? exception)
  72. (compound-exception-components exception))
  73. ((simple-exception? exception)
  74. (list exception))
  75. (else
  76. (raise (make-type-error exception 'exception? 'simple-exceptions)))))
  77. (define (make-exception . exceptions)
  78. "Return an exception object composed of @var{exceptions}."
  79. (define (flatten exceptions)
  80. (if (null? exceptions)
  81. '()
  82. (append (simple-exceptions (car exceptions))
  83. (flatten (cdr exceptions)))))
  84. (let ((simple (flatten exceptions)))
  85. (if (and (pair? simple) (null? (cdr simple)))
  86. (car simple)
  87. (make-compound-exception simple))))
  88. (define (exception? obj)
  89. "Return true if @var{obj} is an exception object."
  90. (or (compound-exception? obj) (simple-exception? obj)))
  91. (define-syntax define-exception-type
  92. (lambda (stx)
  93. (syntax-case stx ()
  94. ((define-exception-type exn parent
  95. (make-exn arg ...)
  96. exn?
  97. (field exn-field)
  98. ...)
  99. (with-syntax (((%exn-field ...)
  100. (generate-temporaries #'(exn-field ...))))
  101. #'(begin
  102. (define-record-type exn
  103. #:parent parent #:extensible? #t
  104. (make-exn arg ...)
  105. %exn?
  106. (field %exn-field)
  107. ...)
  108. (define (exn? x)
  109. (or (%exn? x)
  110. (and (compound-exception? x)
  111. (let lp ((simple (compound-exception-components x)))
  112. (match simple
  113. (() #f)
  114. ((x . simple)
  115. (or (%exn? x)
  116. (lp simple))))))))
  117. (define (exn-field x)
  118. (if (%exn? x)
  119. (%exn-field x)
  120. (let lp ((simple (compound-exception-components x)))
  121. (match simple
  122. (() (raise (make-type-error x 'exn-field 'exn?)))
  123. ((x . simple)
  124. (if (%exn? x)
  125. (%exn-field x)
  126. (lp simple)))))))
  127. ...))))))
  128. (define-exception-type &message &exception
  129. (make-exception-with-message message)
  130. exception-with-message?
  131. (message exception-message))
  132. (define-exception-type &warning &exception
  133. (make-warning)
  134. warning?)
  135. (define-exception-type &serious &exception
  136. (make-serious-exception)
  137. serious-exception?)
  138. (define-exception-type &error &serious
  139. (make-error)
  140. error?)
  141. (define-exception-type &violation &serious
  142. (make-violation)
  143. violation?)
  144. (define-exception-type &assertion &violation
  145. (make-assertion-violation)
  146. assertion-violation?)
  147. (define-exception-type &arity-violation &violation
  148. (make-arity-violation)
  149. arity-violation?)
  150. (define-exception-type &implementation-restriction &violation
  151. (make-implementation-restriction-violation)
  152. implementation-restriction-violation?)
  153. (define-exception-type &failed-type-check &assertion
  154. (make-failed-type-check predicate)
  155. failed-type-check?
  156. (predicate failed-type-check-predicate))
  157. (define-exception-type &non-continuable &violation
  158. (make-non-continuable-violation)
  159. non-continuable-violation?)
  160. (define-exception-type &irritants &exception
  161. (make-exception-with-irritants irritants)
  162. exception-with-irritants?
  163. (irritants exception-irritants))
  164. (define-exception-type &origin &exception
  165. (make-exception-with-origin origin)
  166. exception-with-origin?
  167. (origin exception-origin))
  168. (define-exception-type &lexical &violation
  169. (make-lexical-violation)
  170. lexical-violation?)
  171. (define-exception-type &i/o &error
  172. (make-i/o-error)
  173. i/o-error?)
  174. (define-exception-type &i/o-line-and-column &i/o
  175. (make-i/o-line-and-column-error line column)
  176. i/o-line-and-column-error?
  177. (line i/o-error-line)
  178. (column i/o-error-column))
  179. (define-exception-type &i/o-filename &i/o
  180. (make-i/o-filename-error filename)
  181. i/o-filename-error?
  182. (filename i/o-error-filename))
  183. (define-exception-type &i/o-not-seekable &i/o
  184. (make-i/o-not-seekable-error)
  185. i/o-not-seekable-error?)
  186. (define-exception-type &i/o-port &i/o
  187. (make-i/o-port-error port)
  188. i/o-port-error?
  189. (port i/o-error-port))
  190. (cond-expand
  191. (guile-vm)
  192. (hoot-main
  193. (let ()
  194. (define (make-with-irritants exn message origin irritants)
  195. (make-exception exn
  196. (make-exception-with-message message)
  197. (make-exception-with-origin origin)
  198. (make-exception-with-irritants irritants)))
  199. (define-syntax-rule (define-exception-constructor (name arg ...) body ...)
  200. (cond-expand
  201. ((and) (define (name arg ...) body ...))
  202. (else (define (name arg ...) (list arg ...)))))
  203. (define-exception-constructor (make-size-error val max who)
  204. (make-with-irritants (make-error) "size out of range" who (list val)))
  205. (define-exception-constructor (make-index-error val size who)
  206. (make-with-irritants (make-error) "index out of range" who (list val)))
  207. (define-exception-constructor (make-range-error val min max who)
  208. (make-with-irritants (make-error) "value out of range" who (list val)))
  209. (define-exception-constructor (make-start-offset-error val size who)
  210. (make-with-irritants (make-error) "start offset out of range" who
  211. (list val)))
  212. (define-exception-constructor (make-end-offset-error val size who)
  213. (make-with-irritants (make-error) "end offset out of range" who
  214. (list val)))
  215. (define-exception-constructor (make-type-error val who what)
  216. (make-with-irritants (make-failed-type-check what)
  217. "type check failed"
  218. who (list val)))
  219. (define-exception-constructor (make-unimplemented-error who)
  220. (make-exception (make-implementation-restriction-violation)
  221. (make-exception-with-message "unimplemented")
  222. (make-exception-with-origin who)))
  223. (define-exception-constructor (make-assertion-error expr who)
  224. (make-with-irritants (make-assertion-violation) "assertion failed"
  225. who (list expr)))
  226. (define-exception-constructor (make-not-seekable-error port who)
  227. (make-exception (make-i/o-not-seekable-error)
  228. (make-i/o-port-error port)
  229. (make-exception-with-origin who)))
  230. (define-exception-constructor (make-runtime-error-with-message msg)
  231. (make-exception (make-error) (make-exception-with-message msg)))
  232. (define-exception-constructor (make-runtime-error-with-message+irritants
  233. msg irritants)
  234. (make-exception (make-error)
  235. (make-exception-with-message msg)
  236. (make-exception-with-irritants irritants)))
  237. (define-exception-constructor (make-match-error v)
  238. (make-exception (make-assertion-violation)
  239. (make-exception-with-message "value failed to match")
  240. (make-exception-with-irritants (list v))))
  241. (define-exception-constructor (make-arity-error v who)
  242. (define (annotate-with-origin exn)
  243. (if who
  244. (make-exception (make-exception-with-origin who) exn)
  245. exn))
  246. (annotate-with-origin
  247. (make-exception (make-arity-violation)
  248. (make-exception-with-message
  249. "wrong number of arguments")
  250. (make-exception-with-irritants (list v)))))
  251. (define-exception-constructor (make-invalid-keyword-error kw)
  252. (make-exception (make-arity-violation)
  253. (make-exception-with-message
  254. "expected a keyword")
  255. (make-exception-with-irritants (list kw))))
  256. (define-exception-constructor (make-unrecognized-keyword-error kw)
  257. (make-exception (make-arity-violation)
  258. (make-exception-with-message
  259. "unexpected keyword")
  260. (make-exception-with-irritants (list kw))))
  261. (define-exception-constructor (make-missing-keyword-argument-error kw)
  262. (make-exception (make-arity-violation)
  263. (make-exception-with-message
  264. "keyword missing an argument")
  265. (make-exception-with-irritants (list kw))))
  266. (define-syntax-rule (initialize-globals (global type proc) ...)
  267. (%inline-wasm
  268. '(func (param global type) ...
  269. (global.set global (local.get global)) ...)
  270. proc ...))
  271. (define-syntax-rule (initialize-proc-globals (global proc) ...)
  272. (initialize-globals (global (ref $proc) proc) ...))
  273. (initialize-proc-globals
  274. ($make-size-error make-size-error)
  275. ($make-index-error make-index-error)
  276. ($make-range-error make-range-error)
  277. ($make-start-offset-error make-start-offset-error)
  278. ($make-end-offset-error make-end-offset-error)
  279. ($make-type-error make-type-error)
  280. ($make-unimplemented-error make-unimplemented-error)
  281. ($make-assertion-error make-assertion-error)
  282. ($make-not-seekable-error make-not-seekable-error)
  283. ($make-runtime-error-with-message make-runtime-error-with-message)
  284. ($make-runtime-error-with-message+irritants
  285. make-runtime-error-with-message+irritants)
  286. ($make-match-error make-match-error)
  287. ($make-arity-error make-arity-error)
  288. ($make-invalid-keyword-error make-invalid-keyword-error)
  289. ($make-unrecogized-keyword-error make-unrecognized-keyword-error)
  290. ($make-missing-keyword-argument-error
  291. make-missing-keyword-argument-error))))
  292. (else)))