| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus conditions) |
| 18 |
(export condition? simple-conditions condition-predicate |
| 19 |
condition condition-accessor print-condition |
| 20 |
|
| 21 |
;;; too much junk |
| 22 |
make-message-condition message-condition? |
| 23 |
condition-message make-warning warning? |
| 24 |
make-serious-condition serious-condition? make-error |
| 25 |
error? make-violation violation? make-assertion-violation |
| 26 |
assertion-violation? make-irritants-condition |
| 27 |
irritants-condition? condition-irritants |
| 28 |
make-who-condition who-condition? condition-who |
| 29 |
make-non-continuable-violation non-continuable-violation? |
| 30 |
make-implementation-restriction-violation |
| 31 |
implementation-restriction-violation? |
| 32 |
make-lexical-violation lexical-violation? |
| 33 |
make-syntax-violation syntax-violation? |
| 34 |
syntax-violation-form syntax-violation-subform |
| 35 |
make-undefined-violation undefined-violation? |
| 36 |
make-i/o-error i/o-error? make-i/o-read-error |
| 37 |
i/o-read-error? make-i/o-write-error i/o-write-error? |
| 38 |
make-i/o-invalid-position-error |
| 39 |
i/o-invalid-position-error? i/o-error-position |
| 40 |
make-i/o-filename-error i/o-filename-error? |
| 41 |
i/o-error-filename make-i/o-file-protection-error |
| 42 |
i/o-file-protection-error? make-i/o-file-is-read-only-error |
| 43 |
i/o-file-is-read-only-error? |
| 44 |
make-i/o-file-already-exists-error |
| 45 |
i/o-file-already-exists-error? |
| 46 |
make-i/o-file-does-not-exist-error |
| 47 |
i/o-file-does-not-exist-error? make-i/o-port-error |
| 48 |
i/o-port-error? i/o-error-port make-i/o-decoding-error |
| 49 |
i/o-decoding-error? make-i/o-encoding-error |
| 50 |
i/o-encoding-error? i/o-encoding-error-char |
| 51 |
no-infinities-violation? make-no-infinities-violation |
| 52 |
no-nans-violation? make-no-nans-violation |
| 53 |
interrupted-condition? make-interrupted-condition |
| 54 |
interrupted-signal |
| 55 |
make-source-position-condition source-position-condition? |
| 56 |
source-position-file-name source-position-character |
| 57 |
|
| 58 |
&condition-rtd &condition-rcd &message-rtd &message-rcd |
| 59 |
&warning-rtd &warning-rcd &serious-rtd &serious-rcd |
| 60 |
&error-rtd &error-rcd &violation-rtd &violation-rcd |
| 61 |
&assertion-rtd &assertion-rcd &irritants-rtd |
| 62 |
&irritants-rcd &who-rtd &who-rcd &non-continuable-rtd |
| 63 |
&non-continuable-rcd &implementation-restriction-rtd |
| 64 |
&implementation-restriction-rcd &lexical-rtd &lexical-rcd |
| 65 |
&syntax-rtd &syntax-rcd &undefined-rtd &undefined-rcd |
| 66 |
&i/o-rtd &i/o-rcd &i/o-read-rtd &i/o-read-rcd |
| 67 |
&i/o-write-rtd &i/o-write-rcd &i/o-invalid-position-rtd |
| 68 |
&i/o-invalid-position-rcd &i/o-filename-rtd |
| 69 |
&i/o-filename-rcd &i/o-file-protection-rtd |
| 70 |
&i/o-file-protection-rcd &i/o-file-is-read-only-rtd |
| 71 |
&i/o-file-is-read-only-rcd &i/o-file-already-exists-rtd |
| 72 |
&i/o-file-already-exists-rcd &i/o-file-does-not-exist-rtd |
| 73 |
&i/o-file-does-not-exist-rcd &i/o-port-rtd &i/o-port-rcd |
| 74 |
&i/o-decoding-rtd &i/o-decoding-rcd &i/o-encoding-rtd |
| 75 |
&i/o-encoding-rcd &no-infinities-rtd &no-infinities-rcd |
| 76 |
&no-nans-rtd &no-nans-rcd |
| 77 |
&interrupted-rtd &interrupted-rcd |
| 78 |
&source-position-rtd &source-position-rcd |
| 79 |
) |
| 80 |
(import |
| 81 |
(rnrs records inspection) |
| 82 |
(rnrs records procedural) |
| 83 |
(only (rnrs) record-type-descriptor record-constructor-descriptor record-predicate) |
| 84 |
(only (ikarus records procedural) rtd? rtd-subtype?) |
| 85 |
(except (ikarus) define-condition-type condition? simple-conditions |
| 86 |
condition condition-predicate condition-accessor |
| 87 |
print-condition |
| 88 |
|
| 89 |
;;; more junk |
| 90 |
|
| 91 |
&condition &message &warning &serious &error &violation |
| 92 |
&assertion &irritants &who &non-continuable |
| 93 |
&implementation-restriction &lexical &syntax &undefined |
| 94 |
&i/o &i/o-read &i/o-write &i/o-invalid-position |
| 95 |
&i/o-filename &i/o-file-protection &i/o-file-is-read-only |
| 96 |
&i/o-file-already-exists &i/o-file-does-not-exist |
| 97 |
&i/o-port &i/o-decoding &i/o-encoding &no-infinities |
| 98 |
&no-nans |
| 99 |
|
| 100 |
make-message-condition message-condition? |
| 101 |
condition-message make-warning warning? |
| 102 |
make-serious-condition serious-condition? make-error |
| 103 |
error? make-violation violation? make-assertion-violation |
| 104 |
assertion-violation? make-irritants-condition |
| 105 |
irritants-condition? condition-irritants |
| 106 |
make-who-condition who-condition? condition-who |
| 107 |
make-non-continuable-violation non-continuable-violation? |
| 108 |
make-implementation-restriction-violation |
| 109 |
implementation-restriction-violation? |
| 110 |
make-lexical-violation lexical-violation? |
| 111 |
make-syntax-violation syntax-violation? |
| 112 |
syntax-violation-form syntax-violation-subform |
| 113 |
make-undefined-violation undefined-violation? |
| 114 |
make-i/o-error i/o-error? make-i/o-read-error |
| 115 |
i/o-read-error? make-i/o-write-error i/o-write-error? |
| 116 |
make-i/o-invalid-position-error |
| 117 |
i/o-invalid-position-error? i/o-error-position |
| 118 |
make-i/o-filename-error i/o-filename-error? |
| 119 |
i/o-error-filename make-i/o-file-protection-error |
| 120 |
i/o-file-protection-error? make-i/o-file-is-read-only-error |
| 121 |
i/o-file-is-read-only-error? |
| 122 |
make-i/o-file-already-exists-error |
| 123 |
i/o-file-already-exists-error? |
| 124 |
make-i/o-file-does-not-exist-error |
| 125 |
i/o-file-does-not-exist-error? make-i/o-port-error |
| 126 |
i/o-port-error? i/o-error-port make-i/o-decoding-error |
| 127 |
i/o-decoding-error? make-i/o-encoding-error |
| 128 |
i/o-encoding-error? i/o-encoding-error-char |
| 129 |
no-infinities-violation? make-no-infinities-violation |
| 130 |
no-nans-violation? make-no-nans-violation |
| 131 |
|
| 132 |
interrupted-condition? make-interrupted-condition |
| 133 |
interrupted-signal |
| 134 |
make-source-position-condition source-position-condition? |
| 135 |
source-position-file-name source-position-character |
| 136 |
)) |
| 137 |
|
| 138 |
(define-record-type &condition |
| 139 |
(nongenerative)) |
| 140 |
|
| 141 |
(define &condition-rtd (record-type-descriptor &condition)) |
| 142 |
(define &condition-rcd (record-constructor-descriptor &condition)) |
| 143 |
|
| 144 |
(define-record-type compound-condition |
| 145 |
(nongenerative) |
| 146 |
(fields (immutable components)) |
| 147 |
(sealed #t) |
| 148 |
(opaque #f)) |
| 149 |
|
| 150 |
(define (condition? x) |
| 151 |
(or (&condition? x) |
| 152 |
(compound-condition? x))) |
| 153 |
|
| 154 |
(define condition |
| 155 |
(case-lambda |
| 156 |
[() (make-compound-condition '())] |
| 157 |
[(x) |
| 158 |
(if (condition? x) |
| 159 |
x |
| 160 |
(die 'condition "not a condition type" x))] |
| 161 |
[x* |
| 162 |
(let ([ls |
| 163 |
(let f ([x* x*]) |
| 164 |
(cond |
| 165 |
[(null? x*) '()] |
| 166 |
[(&condition? (car x*)) |
| 167 |
(cons (car x*) (f (cdr x*)))] |
| 168 |
[(compound-condition? (car x*)) |
| 169 |
(append (simple-conditions (car x*)) (f (cdr x*)))] |
| 170 |
[else (die 'condition "not a condition" (car x*))]))]) |
| 171 |
(cond |
| 172 |
[(null? ls) (make-compound-condition '())] |
| 173 |
[(null? (cdr ls)) (car ls)] |
| 174 |
[else (make-compound-condition ls)]))])) |
| 175 |
|
| 176 |
(define (simple-conditions x) |
| 177 |
(cond |
| 178 |
[(compound-condition? x) (compound-condition-components x)] |
| 179 |
[(&condition? x) (list x)] |
| 180 |
[else (die 'simple-conditions "not a condition" x)])) |
| 181 |
|
| 182 |
(define (condition-predicate rtd) |
| 183 |
(unless (rtd? rtd) |
| 184 |
(die 'condition-predicate "not a record type descriptor" rtd)) |
| 185 |
(unless (rtd-subtype? rtd (record-type-descriptor &condition)) |
| 186 |
(die 'condition-predicate "not a descendant of &condition" rtd)) |
| 187 |
(let ([p? (record-predicate rtd)]) |
| 188 |
(lambda (x) |
| 189 |
(or (p? x) |
| 190 |
(and (compound-condition? x) |
| 191 |
(let f ([ls (compound-condition-components x)]) |
| 192 |
(and (pair? ls) |
| 193 |
(or (p? (car ls)) |
| 194 |
(f (cdr ls)))))))))) |
| 195 |
|
| 196 |
(define (condition-accessor rtd proc) |
| 197 |
(unless (rtd? rtd) |
| 198 |
(die 'condition-accessor "not a record type descriptor" rtd)) |
| 199 |
(unless (procedure? proc) |
| 200 |
(die 'condition-accessor "not a procedure" proc)) |
| 201 |
(unless (rtd-subtype? rtd (record-type-descriptor &condition)) |
| 202 |
(die 'condition-accessor "not a descendant of &condition" rtd)) |
| 203 |
(let ([p? (record-predicate rtd)]) |
| 204 |
(lambda (x) |
| 205 |
(cond |
| 206 |
[(p? x) (proc x)] |
| 207 |
[(compound-condition? x) |
| 208 |
(let f ([ls (compound-condition-components x)]) |
| 209 |
(cond |
| 210 |
[(pair? ls) |
| 211 |
(if (p? (car ls)) |
| 212 |
(proc (car ls)) |
| 213 |
(f (cdr ls)))] |
| 214 |
[else |
| 215 |
(die 'condition-accessor "not a condition of correct type" x rtd)]))] |
| 216 |
[else |
| 217 |
(die 'condition-accessor "not a condition of correct type" x rtd)])))) |
| 218 |
|
| 219 |
(define-syntax define-condition-type |
| 220 |
(lambda (x) |
| 221 |
(define (mkname name suffix) |
| 222 |
(datum->syntax name |
| 223 |
(string->symbol |
| 224 |
(string-append |
| 225 |
(symbol->string (syntax->datum name)) |
| 226 |
suffix)))) |
| 227 |
(syntax-case x () |
| 228 |
[(ctxt name super constructor predicate (field* accessor*) ...) |
| 229 |
(and (identifier? #'name) |
| 230 |
(identifier? #'super) |
| 231 |
(identifier? #'constructor) |
| 232 |
(identifier? #'predicate) |
| 233 |
(andmap identifier? #'(field* ...)) |
| 234 |
(andmap identifier? #'(accessor* ...))) |
| 235 |
(with-syntax ([(aux-accessor* ...) (generate-temporaries #'(accessor* ...))] |
| 236 |
[rtd (mkname #'name "-rtd")] |
| 237 |
[rcd (mkname #'name "-rcd")]) |
| 238 |
#'(begin |
| 239 |
(define-record-type (name constructor p?) |
| 240 |
(parent super) |
| 241 |
(fields (immutable field* aux-accessor*) ...) |
| 242 |
(nongenerative) |
| 243 |
(sealed #f) (opaque #f)) |
| 244 |
(define predicate (condition-predicate (record-type-descriptor name))) |
| 245 |
(define accessor* (condition-accessor (record-type-descriptor name) aux-accessor*)) |
| 246 |
... |
| 247 |
(define rtd (record-type-descriptor name)) |
| 248 |
(define rcd (record-constructor-descriptor name))))]))) |
| 249 |
|
| 250 |
(define-condition-type &message &condition |
| 251 |
make-message-condition message-condition? |
| 252 |
(message condition-message)) |
| 253 |
|
| 254 |
(define-condition-type &warning &condition |
| 255 |
make-warning warning?) |
| 256 |
|
| 257 |
(define-condition-type &serious &condition |
| 258 |
make-serious-condition serious-condition?) |
| 259 |
|
| 260 |
(define-condition-type &error &serious |
| 261 |
make-error error?) |
| 262 |
|
| 263 |
(define-condition-type &violation &serious |
| 264 |
make-violation violation?) |
| 265 |
|
| 266 |
(define-condition-type &assertion &violation |
| 267 |
make-assertion-violation assertion-violation?) |
| 268 |
|
| 269 |
(define-condition-type &irritants &condition |
| 270 |
make-irritants-condition irritants-condition? |
| 271 |
(irritants condition-irritants)) |
| 272 |
|
| 273 |
(define-condition-type &who &condition |
| 274 |
make-who-condition who-condition? |
| 275 |
(who condition-who)) |
| 276 |
|
| 277 |
(define-condition-type &non-continuable &violation |
| 278 |
make-non-continuable-violation non-continuable-violation?) |
| 279 |
|
| 280 |
(define-condition-type &implementation-restriction &violation |
| 281 |
make-implementation-restriction-violation |
| 282 |
implementation-restriction-violation?) |
| 283 |
|
| 284 |
(define-condition-type &lexical &violation |
| 285 |
make-lexical-violation lexical-violation?) |
| 286 |
|
| 287 |
(define-condition-type &syntax &violation |
| 288 |
make-syntax-violation syntax-violation? |
| 289 |
(form syntax-violation-form) |
| 290 |
(subform syntax-violation-subform)) |
| 291 |
|
| 292 |
(define-condition-type &undefined &violation |
| 293 |
make-undefined-violation undefined-violation?) |
| 294 |
|
| 295 |
(define-condition-type &i/o &error |
| 296 |
make-i/o-error i/o-error?) |
| 297 |
|
| 298 |
(define-condition-type &i/o-read &i/o |
| 299 |
make-i/o-read-error i/o-read-error?) |
| 300 |
|
| 301 |
(define-condition-type &i/o-write &i/o |
| 302 |
make-i/o-write-error i/o-write-error?) |
| 303 |
|
| 304 |
(define-condition-type &i/o-invalid-position &i/o |
| 305 |
make-i/o-invalid-position-error i/o-invalid-position-error? |
| 306 |
(position i/o-error-position)) |
| 307 |
|
| 308 |
(define-condition-type &i/o-filename &i/o |
| 309 |
make-i/o-filename-error i/o-filename-error? |
| 310 |
(filename i/o-error-filename)) |
| 311 |
|
| 312 |
(define-condition-type &i/o-file-protection &i/o-filename |
| 313 |
make-i/o-file-protection-error i/o-file-protection-error?) |
| 314 |
|
| 315 |
(define-condition-type &i/o-file-is-read-only &i/o-file-protection |
| 316 |
make-i/o-file-is-read-only-error i/o-file-is-read-only-error?) |
| 317 |
|
| 318 |
(define-condition-type &i/o-file-already-exists &i/o-filename |
| 319 |
make-i/o-file-already-exists-error i/o-file-already-exists-error?) |
| 320 |
|
| 321 |
(define-condition-type &i/o-file-does-not-exist &i/o-filename |
| 322 |
make-i/o-file-does-not-exist-error i/o-file-does-not-exist-error?) |
| 323 |
|
| 324 |
(define-condition-type &i/o-port &i/o |
| 325 |
make-i/o-port-error i/o-port-error? |
| 326 |
(port i/o-error-port)) |
| 327 |
|
| 328 |
(define-condition-type &i/o-decoding &i/o-port |
| 329 |
make-i/o-decoding-error i/o-decoding-error?) |
| 330 |
|
| 331 |
(define-condition-type &i/o-encoding &i/o-port |
| 332 |
make-i/o-encoding-error i/o-encoding-error? |
| 333 |
(char i/o-encoding-error-char)) |
| 334 |
|
| 335 |
(define-condition-type &no-infinities &implementation-restriction |
| 336 |
make-no-infinities-violation no-infinities-violation?) |
| 337 |
|
| 338 |
(define-condition-type &no-nans &implementation-restriction |
| 339 |
make-no-nans-violation no-nans-violation?) |
| 340 |
|
| 341 |
;;; ikarus-specific conditions |
| 342 |
(define-condition-type &interrupted &serious |
| 343 |
make-interrupted-condition interrupted-condition? |
| 344 |
(signal interrupted-signal)) |
| 345 |
|
| 346 |
(define-condition-type &source-position &condition |
| 347 |
make-source-position-condition source-position-condition? |
| 348 |
(file-name source-position-file-name) |
| 349 |
(character source-position-character)) |
| 350 |
|
| 351 |
(define print-condition |
| 352 |
(let () |
| 353 |
(define (print-simple-condition x p) |
| 354 |
(let* ([rtd (record-rtd x)] |
| 355 |
[rf (let l ([rtd rtd] [accum '()]) |
| 356 |
(if rtd |
| 357 |
(l (record-type-parent rtd) |
| 358 |
(cons |
| 359 |
(cons rtd (record-type-field-names rtd)) |
| 360 |
accum)) |
| 361 |
(remp (lambda (a) (zero? (vector-length (cdr a)))) |
| 362 |
accum)))] |
| 363 |
[rf-len (apply + (map vector-length |
| 364 |
(map cdr rf)))]) |
| 365 |
(let ([name (record-type-name rtd)]) |
| 366 |
(display name p)) |
| 367 |
(case rf-len |
| 368 |
[(0) (newline p)] |
| 369 |
[(1) |
| 370 |
(display ": " p) |
| 371 |
(write ((record-accessor (caar rf) 0) x) p) |
| 372 |
(newline p)] |
| 373 |
[else |
| 374 |
(display ":\n" p) |
| 375 |
(for-each |
| 376 |
(lambda (a) |
| 377 |
(let f ([i 0] [rtd (car a)] [v (cdr a)]) |
| 378 |
(unless (= i (vector-length v)) |
| 379 |
(display " " p) |
| 380 |
(display (vector-ref v i) p) |
| 381 |
(display ": " p) |
| 382 |
(write ((record-accessor rtd i) x) p) |
| 383 |
(newline p) |
| 384 |
(f (+ i 1) rtd v)))) |
| 385 |
rf)]))) |
| 386 |
(define (print-condition x p) |
| 387 |
(cond |
| 388 |
[(condition? x) |
| 389 |
(let ([ls (simple-conditions x)]) |
| 390 |
(if (null? ls) |
| 391 |
(display "Condition object with no further information\n" p) |
| 392 |
(begin |
| 393 |
(display " Condition components:\n" p) |
| 394 |
(let f ([ls ls] [i 1]) |
| 395 |
(unless (null? ls) |
| 396 |
(display " " p) |
| 397 |
(parameterize ((print-radix 10)) |
| 398 |
(display i p)) |
| 399 |
(display ". " p) |
| 400 |
(print-simple-condition (car ls) p) |
| 401 |
(f (cdr ls) (+ i 1)))))))] |
| 402 |
[else |
| 403 |
(display " Non-condition object: " p) |
| 404 |
(write x p) |
| 405 |
(newline p)])) |
| 406 |
(case-lambda |
| 407 |
[(x) |
| 408 |
(print-condition x (console-output-port))] |
| 409 |
[(x port) |
| 410 |
(if (output-port? port) |
| 411 |
(print-condition x port) |
| 412 |
(die 'print-condition "not an output port" port))]))) |
| 413 |
|
| 414 |
|
| 415 |
) |
| 416 |
|