File: condition.scm

package info (click to toggle)
scheme48 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,232 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (305 lines) | stat: -rw-r--r-- 8,280 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
; Part of Scheme 48 1.9.  See file COPYING for notices and license.

; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber

; Sort-of the R6RS condition library.

(define-record-type &condition
  (make-simple-condition)
  simple-condition?)

(define-record-type &compound-condition
  (make-compound-condition components)
  compound-condition?
  (components explode-condition))

(define-record-discloser &compound-condition
  (lambda (r)
    (cons 'compound-condition
	  (explode-condition r))))

(define (simple-conditions con)
  (cond
   ((simple-condition? con)
    (list con))
   ((compound-condition? con)
    (explode-condition con))
   (else
    (assertion-violation 'simple-conditions
			 "not a condition"
			 con))))

(define (condition? thing)
  (or (simple-condition? thing)
      (compound-condition? thing)))

(define (condition . components)
  (make-compound-condition
   (apply append
	  (map (lambda (component)
		 (cond
		  ((simple-condition? component)
		   (list component))
		  ((compound-condition? component)
		   (explode-condition component))
		  (else
		   (assertion-violation 'condition
					"component wasn't a condition"
					component))))
	       components))))

(define (condition-predicate type)
  (if (not (record-type<=? type &condition))
      (assertion-violation 'condition-predicate
			   "not a subtype of &condition"
			   type))
  (let ((simple-pred (record-predicate type)))
    (lambda (con)
      (cond
       ((simple-condition? con)
	(simple-pred con))
       ((compound-condition? con)
	(any? simple-pred (explode-condition con)))
       (else #f)))))

(define (condition-accessor type simple-access)
  (if (not (record-type<=? type &condition))
      (assertion-violation 'condition-predicate
			   "not a subtype of &condition"
			   type))
  (let ((simple-pred (record-predicate type)))
    (lambda (con)
      (cond
       ((simple-condition? con)
	(simple-access con))
       ((compound-condition? con)
	(cond
	 ((first simple-pred (explode-condition con))
	  => simple-access)
	 (else
	  (assertion-violation '<condition-accessor>
			       "condition isn't of type"
			       con type))))
       (else
	(assertion-violation '<condition-accessor>
			     "condition isn't of type"
			     con type))))))

(define-syntax define-condition-type
  (syntax-rules ()
    ((define-condition-type ?name ?supertype ?constructor ?predicate
       (?field1 ?accessor1) ...)
     (define-condition-type-helper
       ?name ?supertype ?constructor ?predicate
       ((?field1 ?accessor1) ...)
       ()))))

(define-syntax define-condition-type-helper
  (syntax-rules ()
    ((define-condition-type-helper
       ?name ?supertype ?constructor ?predicate
       ((?field1 ?accessor1) ?rest ...)
       (?spec1 ...))
     (define-condition-type-helper
       ?name ?supertype ?constructor ?predicate
       (?rest ...)
       (?spec1 ... (?field1 ?accessor1 temp-condition-accessor))))
    ((define-condition-type-helper
       ?name ?supertype ?constructor ?predicate
       ()
       ((?field1 ?accessor1 ?condition-accessor1) ...))
     (begin
       (define ?name (make-record-type '?name '(?field1 ...) ?supertype))

       (define-record-discloser ?name
	 (lambda (r)
	   (list '?name (?condition-accessor1 r) ...)))

       (define ?constructor (record-standard-constructor ?name))
       
       (define ?predicate (condition-predicate ?name))

       (define ?condition-accessor1 (record-accessor ?name '?field1))
       ...
       (define ?accessor1
	 (condition-accessor ?name ?condition-accessor1))
       ...))))
       
;; Utilities, defined locally to avoid having to load SRFI 1

;; (These need to come before the standard condition types below.)

(define (first pred list)
  (let loop ((list list))
    (cond ((null? list)
	   #f)
          ((pred (car list))
	   (car list))
          (else
	   (loop (cdr list))))))

(define (any? proc list)
  (let loop ((list list))
    (cond ((null? list)
	   #f)
          ((proc (car list))
	   #t)
          (else
	   (loop (cdr list))))))

;; Standard condition types

(define-condition-type &message &condition 
  make-message-condition message-condition?
  (message condition-message))

(define-condition-type &warning &condition
  make-warning warning?)

(define-condition-type &serious &condition
  make-serious-condition serious-condition?)

(define-condition-type &error &serious
  make-error error?)

(define-condition-type &violation &serious
  make-violation violation?)

(define-condition-type &non-continuable &violation
  make-noncontinuable-violation non-continuable-violation?)

(define-condition-type &implementation-restriction &violation
  make-implementation-restriction-violation implementation-restriction-violation?)

(define-condition-type &lexical &violation
  make-lexical-violation lexical-violation?)

(define-condition-type &syntax &violation
  make-syntax-violation syntax-violation?
  (form syntax-violation-form)
  (subform syntax-violation-subform))

(define-condition-type &undefined &violation
  make-undefined-violation undefined-violation?)

(define-condition-type &assertion &violation
  make-assertion-violation assertion-violation?)

(define-condition-type &irritants &condition
  make-irritants-condition irritants-condition?
  (irritants condition-irritants))

(define-condition-type &who &condition
  make-who-condition who-condition?
  (who condition-who))

; Scheme-48-specific condition types

; VM Exceptions

(define-condition-type &vm-exception &serious
  make-vm-exception vm-exception?
  (opcode vm-exception-opcode) ; number
  (reason vm-exception-reason) ; symbol
  )

; OS errors (errno or Windows error codes)

(define-condition-type &os-error &error
  make-os-error os-error?
  (code os-error-code))

; I/O errors

(define-condition-type &i/o-error &error
  make-i/o-error i/o-error?)

(define-condition-type &i/o-port-error &i/o-error
  make-i/o-port-error i/o-port-error?
  (port i/o-error-port))

; Decoding errors

(define-condition-type &decoding-error &error
  make-decoding-error decoding-error?
  (encoding-name decoding-error-encoding-name)
  (bytes decoding-error-bytes)
  (start decoding-error-start))

; Notes

(define-condition-type &note &condition
  make-note note?)

; Interrupts

(define-condition-type &interrupt &condition
  make-interrupt-condition interrupt-condition?
  (source interrupt-source))

; Decoding a condition for printing

;; Return a symbol describing the type,
;; a symbol or string describing the source of the problem, an error
;; message or #f, and a list of other objects describing the
;; problem.  Valid type symbols include:

;; ERROR, ASSERTION-VIOLATION, SYNTAX-VIOLATION, VM-EXCEPTION,
;; WARNING, SERIOUS, NOTE, INTERRUPT

(define (decode-condition con)
  (let ((type
	 (cond
	  ((error? con) 'error)
	  ((assertion-violation? con) 'assertion-violation)
	  ((syntax-violation? con) 'syntax-violation)
	  ((vm-exception? con) 'vm-exception)
	  ((serious-condition? con) 'serious)
	  ((interrupt-condition? con) 'interrupt)
	  ((warning? con) 'warning)
	  ((note? con) 'note)
	  (else 'unknown)))
	(who
	 (and (who-condition? con)
	      (condition-who con)))
	(message
	 (and (message-condition? con)
	      (condition-message con)))
	(stuff (if (irritants-condition? con)
		   (condition-irritants con)
		   '()))
	(syntax-stuff
	 (if (syntax-violation? con)
	     (let ((form (syntax-violation-form con))
		   (subform (syntax-violation-subform con)))
	       (if subform
		   (list form subform)
		   (list form)))
	     '()))
	(more-stuff
	 (delete-first
	  (lambda (con)	; make sure interesting subtypes still get printed
	    (memq (record-type con)
		  *covered-condition-txpes*))
	  ;; we don't expect interesting subtypes here
	  (delete-first
	   vm-exception?
	   (delete-first
	    message-condition?
	    (delete-first
	     who-condition?
	     (delete-first
	      irritants-condition?
	      (simple-conditions con))))))))
    (values type who message (append stuff syntax-stuff more-stuff))))

(define (delete-first pred? l)
  (let recur ((l l))
    (cond
     ((null? l) l)
     ((pred? (car l)) (cdr l))
     (else
      (cons (car l) (recur (cdr l)))))))

(define *covered-condition-txpes*
  (list &syntax &warning &note &interrupt &error &assertion &serious))