exceptions.scm 15 KB

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