/
condition-printer.nmosh.ss
221 lines (203 loc) · 5.72 KB
/
condition-printer.nmosh.ss
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
(library (nmosh condition-printer)
(export condition-printer
debug-format
with-condition-printer with-condition-printer/raise)
(import (rnrs) (nmosh conditions)
(only (mosh) format)
(nmosh pathutils)
(primitives id-name id-debug id-maybe-library))
; almost as syntax->datum but allows symbol in l
(define (strip l)
(cond
((list? l) (map strip l))
((vector? l) (vector-map strip l))
((identifier? l) (id-name l))
(else l)))
; -> (nmosh debug)
(define (debug-format debug)
(define (extract-name name)
(let ((l (string->list name)))
(let* ((r (reverse l))
(a (car r))
(b (cadr r)))
(if (char=? #\> a b) ; IT WON'T WORK (when supplied filename contains spc)
(let loop ((cur '())
(s (cddr r)))
(if (char=? #\space (car s))
(make-simple-path (list->string cur))
(loop (cons (car s) cur) (cdr s))))
name))))
(cond
((and (list? debug) (= 2 (length debug)))
(let ((name (car debug))
(line (cadr debug)))
(format "~a:~d" (if (string? name)
(extract-name name)
"INVALID")
line)))
(else #f)))
; STUB!
(define (id->debuginfo e)
(let ((name (id-name e))
(library (id-maybe-library e))
(debug (id-debug e)))
(if debug
(debug-format debug)
#f)))
(define (gather l)
(define (next cur e)
(if (member e cur)
cur
(if e (cons e cur) cur)))
(define (itr cur e)
(cond
((list? e) (fold-left itr cur e))
((vector? e) (fold-left itr cur (vector->list e)))
((identifier? e) (next cur (id->debuginfo e)))
(else cur)))
(fold-left itr '() l))
(define-syntax with-condition-printer/raise
(syntax-rules ()
((_ arg ...)
(guard (e
(#t
(if (condition? e)
(begin
(condition-printer e (current-error-port))
(raise e))
(display (list 'UNKNOWN-DATUM! e) (current-error-port)))))
arg ...))))
(define-syntax with-condition-printer
(syntax-rules ()
((_ arg ...)
(guard (e
(#t ;always handles
(if (condition? e)
(condition-printer e (current-error-port))
(display (list 'UNKNOWN-DATUM! e) (current-error-port)))))
arg ...))))
(define (safe-condition-who e) ; when who is #f... e.g.) eval (#f . #f)
(guard (c (#t #f))
(condition-who e)))
(define (ptake x l) ;; permissive SRFI-1 take
;; (ptake N #f) => #f
;; (ptake N x) => '()
(cond
((not l)
#f)
((not (pair? x))
'())
((= x 1)
(car x))
(else
(cons (car x) (ptake (- x 1) (cdr l))))))
(define (syntax-trace-printer e port)
(define (tab)
(display " " port))
(let ((who (safe-condition-who e))
(message (condition-message e))
(form (syntax-violation-form e))
(subform (syntax-violation-subform e))
(trace (ptake 5 (condition-syntax-trace e))))
(display " Syntax error")
(newline port)
(display " who : " port)
(display who port)
(newline port)
(display " message : " port)
(display message port)
(newline port)
(when form
(display " form : " port)
(display form port)
(newline port))
(when subform
(display " subform : " port)
(display subform port)
(newline port))
(when (pair? trace)
(display " around : " port)
(for-each (lambda (e)
(display e port)
(newline port)
(display " " port))
(gather trace))
(newline port)
(display " trace : \n" port)
(for-each (lambda (e)
(tab)
(write (strip e) port)
(newline port)
(newline port)) trace))))
(define (condition-printer e port)
(cond
((syntax-trace-condition? e)
(syntax-trace-printer e port))
(else (condition-printer/base e port))))
;-------------------------------------------------------------------------
; from mosh's psyntax (see lib/psyntax/psyntax/main.ss)
;-------------------------------------------------------------------------
(define (rpad str pad n)
(let ([rest (- n (string-length (format "~a" str)))])
(let loop ([rest rest]
[ret (format "~a" str)])
(if (<= rest 0)
ret
(loop (- rest 1) (string-append ret pad))))))
(define (for-each-with-index proc lst)
(do ((i 1 (+ i 1)) ; start with 1
(lst lst (cdr lst)))
((null? lst))
(proc i (car lst))))
(define (map-with-index proc lst)
(let loop ([i 0]
[lst lst]
[ret '()])
(if (null? lst)
(reverse ret)
(loop (+ i 1) (cdr lst) (cons (proc i (car lst)) ret)))))
(define (record->field-alist r)
(define (ref rtd i x)
(let ([val ((record-accessor rtd i) x)])
val)); NMOSH: do not ungensym here
(let loop ([ret '()]
[rtd (record-rtd r)])
(cond
[rtd
(loop (append ret
(map-with-index
(lambda (i field)
(cons field (ref rtd i r)))
(vector->list (record-type-field-names rtd)))) (record-type-parent rtd))]
[else ret])))
(define (condition-printer/base e port)
(define max-condition-len
(apply max (map (lambda (c) (string-length
(symbol->string (record-type-name (record-rtd c)))))
(simple-conditions e))))
(display " Condition components:\n" port)
(for-each-with-index
(lambda (i x)
(let ([rtd (record-rtd x)]
[fields-alist (record->field-alist x)])
(format port " ~d. ~a" i (rpad (symbol->string (record-type-name rtd)) " " max-condition-len))
(when (null? fields-alist)
(newline port))
(let loop ([first #t]
[fields-alist fields-alist])
(cond
[(null? fields-alist) '()]
[else
(let ([field (car fields-alist)])
(unless first
(display (rpad "" " " (+ 4 max-condition-len)) port))
(display " " port)
(display (car field) port)
(display ": " port)
(write (cdr field) port)
(newline port)
(loop #f (cdr fields-alist)))
]
))))
(simple-conditions e)))
)