exceptions.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. define-exception-type
  25. simple-exceptions make-exception exception?
  26. &message make-exception-with-message exception-with-message?
  27. exception-message
  28. &warning make-warning warning?
  29. &serious make-serious-exception serious-exception?
  30. &error make-error error?
  31. &violation make-violation violation?
  32. &assertion make-assertion-violation assertion-violation?
  33. &arity-violation make-arity-violation arity-violation?
  34. &implementation-restriction make-implementation-restriction-violation
  35. implementation-restriction-violation?
  36. &failed-type-check make-failed-type-check failed-type-check?
  37. failed-type-check-predicate
  38. &non-continuable make-non-continuable-violation
  39. non-continuable-violation?
  40. &irritants make-exception-with-irritants exception-with-irritants?
  41. exception-irritants
  42. &origin make-exception-with-origin exception-with-origin?
  43. exception-origin
  44. &source make-exception-with-source exception-with-source?
  45. exception-source-file exception-source-line
  46. exception-source-column
  47. &lexical make-lexical-violation lexical-violation?
  48. &i/o make-i/o-error i/o-error?
  49. &i/o-line-and-column make-i/o-line-and-column-error
  50. i/o-line-and-column-error? i/o-error-line i/o-error-column
  51. &i/o-filename make-i/o-filename-error i/o-filename-error?
  52. i/o-error-filename
  53. &i/o-not-seekable make-i/o-not-seekable-error i/o-not-seekable-error?
  54. &i/o-port make-i/o-port-error i/o-port-error? i/o-error-port
  55. &syntax make-syntax-error syntax-error?
  56. &invalid-syntax make-invalid-syntax-error invalid-syntax-error?
  57. invalid-syntax-form invalid-syntax-subform)
  58. (import (hoot syntax)
  59. (hoot features)
  60. (hoot cond-expand)
  61. (hoot errors)
  62. (hoot eq)
  63. (hoot inline-wasm)
  64. (hoot pairs)
  65. (hoot lists)
  66. (hoot values)
  67. (hoot records)
  68. (hoot syntax-objects)
  69. (only (hoot primitives) procedure-property)
  70. (hoot match))
  71. (define-record-type &exception
  72. #:extensible? #t
  73. (make-&exception)
  74. simple-exception?)
  75. (define-record-type &compound-exception
  76. (make-compound-exception components)
  77. compound-exception?
  78. (components compound-exception-components))
  79. (define (simple-exceptions exception)
  80. "Return a list of the simple exceptions that compose the exception
  81. object @var{exception}."
  82. (cond ((compound-exception? exception)
  83. (compound-exception-components exception))
  84. ((simple-exception? exception)
  85. (list exception))
  86. (else
  87. (raise (make-type-error exception 'exception? 'simple-exceptions)))))
  88. (define (make-exception . exceptions)
  89. "Return an exception object composed of @var{exceptions}."
  90. (define (flatten exceptions)
  91. (if (null? exceptions)
  92. '()
  93. (append (simple-exceptions (car exceptions))
  94. (flatten (cdr exceptions)))))
  95. (let ((simple (flatten exceptions)))
  96. (if (and (pair? simple) (null? (cdr simple)))
  97. (car simple)
  98. (make-compound-exception simple))))
  99. (define (exception? obj)
  100. "Return true if @var{obj} is an exception object."
  101. (or (compound-exception? obj) (simple-exception? obj)))
  102. (define-syntax define-exception-type
  103. (lambda (stx)
  104. (define (parent-fields parent)
  105. (let-values (((kind value) (syntax-local-binding parent)))
  106. (datum->syntax
  107. parent
  108. (or (and (eq? kind 'macro)
  109. (procedure-property value 'record-type?)
  110. (procedure-property value 'fields))
  111. '()))))
  112. (syntax-case stx ()
  113. ((define-exception-type exn parent
  114. make-exn
  115. exn?
  116. (field exn-field)
  117. ...)
  118. (with-syntax (((pfield ...) (parent-fields #'parent))
  119. ((%exn-field ...)
  120. (generate-temporaries #'(exn-field ...))))
  121. #'(begin
  122. (define-record-type exn
  123. #:parent parent #:extensible? #t
  124. (make-exn pfield ... field ...)
  125. %exn?
  126. (field %exn-field)
  127. ...)
  128. (define (exn? x)
  129. (or (%exn? x)
  130. (and (compound-exception? x)
  131. (let lp ((simple (compound-exception-components x)))
  132. (match simple
  133. (() #f)
  134. ((x . simple)
  135. (or (%exn? x)
  136. (lp simple))))))))
  137. (define (exn-field x)
  138. (if (%exn? x)
  139. (%exn-field x)
  140. (let lp ((simple (compound-exception-components x)))
  141. (match simple
  142. (() (raise (make-type-error x 'exn-field 'exn?)))
  143. ((x . simple)
  144. (if (%exn? x)
  145. (%exn-field x)
  146. (lp simple)))))))
  147. ...))))))
  148. (define-exception-type &message &exception
  149. make-exception-with-message
  150. exception-with-message?
  151. (message exception-message))
  152. (define-exception-type &warning &exception
  153. make-warning
  154. warning?)
  155. (define-exception-type &serious &exception
  156. make-serious-exception
  157. serious-exception?)
  158. (define-exception-type &error &serious
  159. make-error
  160. error?)
  161. (define-exception-type &violation &serious
  162. make-violation
  163. violation?)
  164. (define-exception-type &assertion &violation
  165. make-assertion-violation
  166. assertion-violation?)
  167. (define-exception-type &arity-violation &violation
  168. make-arity-violation
  169. arity-violation?)
  170. (define-exception-type &implementation-restriction &violation
  171. make-implementation-restriction-violation
  172. implementation-restriction-violation?)
  173. (define-exception-type &failed-type-check &assertion
  174. make-failed-type-check
  175. failed-type-check?
  176. (predicate failed-type-check-predicate))
  177. (define-exception-type &non-continuable &violation
  178. make-non-continuable-violation
  179. non-continuable-violation?)
  180. (define-exception-type &irritants &exception
  181. make-exception-with-irritants
  182. exception-with-irritants?
  183. (irritants exception-irritants))
  184. (define-exception-type &origin &exception
  185. make-exception-with-origin
  186. exception-with-origin?
  187. (origin exception-origin))
  188. (define-exception-type &source &exception
  189. make-exception-with-source
  190. exception-with-source?
  191. (file exception-source-file)
  192. (line exception-source-line)
  193. (column exception-source-column))
  194. (define-exception-type &lexical &violation
  195. make-lexical-violation
  196. lexical-violation?)
  197. (define-exception-type &i/o &error
  198. make-i/o-error
  199. i/o-error?)
  200. (define-exception-type &i/o-line-and-column &i/o
  201. make-i/o-line-and-column-error
  202. i/o-line-and-column-error?
  203. (line i/o-error-line)
  204. (column i/o-error-column))
  205. (define-exception-type &i/o-filename &i/o
  206. make-i/o-filename-error
  207. i/o-filename-error?
  208. (filename i/o-error-filename))
  209. (define-exception-type &i/o-not-seekable &i/o
  210. make-i/o-not-seekable-error
  211. i/o-not-seekable-error?)
  212. (define-exception-type &i/o-port &i/o
  213. make-i/o-port-error
  214. i/o-port-error?
  215. (port i/o-error-port))
  216. (define-exception-type &syntax &violation
  217. make-syntax-error
  218. syntax-error?)
  219. (define-exception-type &invalid-syntax &syntax
  220. make-invalid-syntax-error
  221. invalid-syntax-error?
  222. (form invalid-syntax-form)
  223. (subform invalid-syntax-subform))
  224. (cond-expand
  225. (guile-vm)
  226. (hoot-main
  227. (let ()
  228. (define (make-with-irritants exn message origin irritants)
  229. (make-exception exn
  230. (make-exception-with-message message)
  231. (make-exception-with-origin origin)
  232. (make-exception-with-irritants irritants)))
  233. (define-syntax-rule (define-exception-constructor (name arg ...) body ...)
  234. (cond-expand
  235. ((and) (define (name arg ...) body ...))
  236. (else (define (name arg ...) (list arg ...)))))
  237. (define-exception-constructor (make-size-error val max who)
  238. (make-with-irritants (make-error) "size out of range" who (list val)))
  239. (define-exception-constructor (make-index-error val size who)
  240. (make-with-irritants (make-error) "index out of range" who (list val)))
  241. (define-exception-constructor (make-range-error val min max who)
  242. (make-with-irritants (make-error) "value out of range" who (list val)))
  243. (define-exception-constructor (make-start-offset-error val size who)
  244. (make-with-irritants (make-error) "start offset out of range" who
  245. (list val)))
  246. (define-exception-constructor (make-end-offset-error val size who)
  247. (make-with-irritants (make-error) "end offset out of range" who
  248. (list val)))
  249. (define-exception-constructor (make-type-error val who what)
  250. (make-with-irritants (make-failed-type-check what)
  251. "type check failed"
  252. who (list val)))
  253. (define-exception-constructor (make-unimplemented-error who)
  254. (make-exception (make-implementation-restriction-violation)
  255. (make-exception-with-message "unimplemented")
  256. (make-exception-with-origin who)))
  257. (define-exception-constructor (make-assertion-error expr who)
  258. (make-with-irritants (make-assertion-violation) "assertion failed"
  259. who (list expr)))
  260. (define-exception-constructor (make-not-seekable-error port who)
  261. (make-exception (make-i/o-not-seekable-error)
  262. (make-i/o-port-error port)
  263. (make-exception-with-origin who)))
  264. (define-exception-constructor (make-runtime-error-with-message msg)
  265. (make-exception (make-error) (make-exception-with-message msg)))
  266. (define-exception-constructor (make-runtime-error-with-message+irritants
  267. msg irritants)
  268. (make-exception (make-error)
  269. (make-exception-with-message msg)
  270. (make-exception-with-irritants irritants)))
  271. (define-exception-constructor (make-match-error v)
  272. (make-exception (make-assertion-violation)
  273. (make-exception-with-message "value failed to match")
  274. (make-exception-with-irritants (list v))))
  275. (define-exception-constructor (make-arity-error v who)
  276. (define (annotate-with-origin exn)
  277. (if who
  278. (make-exception (make-exception-with-origin who) exn)
  279. exn))
  280. (annotate-with-origin
  281. (make-exception (make-arity-violation)
  282. (make-exception-with-message
  283. "wrong number of arguments")
  284. (make-exception-with-irritants (list v)))))
  285. (define-exception-constructor (make-invalid-keyword-error kw)
  286. (make-exception (make-arity-violation)
  287. (make-exception-with-message
  288. "expected a keyword")
  289. (make-exception-with-irritants (list kw))))
  290. (define-exception-constructor (make-unrecognized-keyword-error kw)
  291. (make-exception (make-arity-violation)
  292. (make-exception-with-message
  293. "unexpected keyword")
  294. (make-exception-with-irritants (list kw))))
  295. (define-exception-constructor (make-missing-keyword-argument-error kw)
  296. (make-exception (make-arity-violation)
  297. (make-exception-with-message
  298. "keyword missing an argument")
  299. (make-exception-with-irritants (list kw))))
  300. (define-exception-constructor (make-syntax-violation who message form
  301. subform)
  302. (make-exception (if form
  303. (make-invalid-syntax-error form subform)
  304. (make-syntax-error))
  305. (make-exception-with-message message)
  306. (make-exception-with-origin who)))
  307. (define (annotate-with-source exn file line column)
  308. (if (exception? exn)
  309. (make-exception exn (make-exception-with-source file line column))
  310. exn))
  311. (define-syntax-rule (initialize-globals (global type proc) ...)
  312. (%inline-wasm
  313. '(func (param global type) ...
  314. (global.set global (local.get global)) ...)
  315. proc ...))
  316. (define-syntax-rule (initialize-proc-globals (global proc) ...)
  317. (initialize-globals (global (ref $proc) proc) ...))
  318. (initialize-proc-globals
  319. ($make-size-error make-size-error)
  320. ($make-index-error make-index-error)
  321. ($make-range-error make-range-error)
  322. ($make-start-offset-error make-start-offset-error)
  323. ($make-end-offset-error make-end-offset-error)
  324. ($make-type-error make-type-error)
  325. ($make-unimplemented-error make-unimplemented-error)
  326. ($make-assertion-error make-assertion-error)
  327. ($make-not-seekable-error make-not-seekable-error)
  328. ($make-runtime-error-with-message make-runtime-error-with-message)
  329. ($make-runtime-error-with-message+irritants
  330. make-runtime-error-with-message+irritants)
  331. ($make-match-error make-match-error)
  332. ($make-arity-error make-arity-error)
  333. ($make-invalid-keyword-error make-invalid-keyword-error)
  334. ($make-unrecognized-keyword-error make-unrecognized-keyword-error)
  335. ($make-missing-keyword-argument-error
  336. make-missing-keyword-argument-error)
  337. ($make-syntax-violation make-syntax-violation)
  338. ($annotate-with-source annotate-with-source))))
  339. (else)))