]> gitweb.factorcode.org Git - factor.git/blob - basis/debugger/debugger.factor
Merge branch 'master' into smarter_error_list
[factor.git] / basis / debugger / debugger.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: slots arrays definitions generic hashtables summary io
4 kernel math namespaces make prettyprint prettyprint.config
5 sequences assocs sequences.private strings io.styles
6 io.pathnames vectors words system splitting math.parser
7 classes.mixin classes.tuple continuations continuations.private
8 combinators generic.math classes.builtin classes compiler.units
9 generic.standard vocabs init kernel.private io.encodings
10 accessors math.order destructors source-files parser
11 classes.tuple.parser effects.parser lexer
12 generic.parser strings.parser vocabs.loader vocabs.parser see
13 source-files.errors ;
14 IN: debugger
15
16 GENERIC: error. ( error -- )
17 GENERIC: error-help ( error -- topic )
18
19 M: object error. . ;
20 M: object error-help drop f ;
21
22 M: tuple error-help class ;
23
24 M: string error. print ;
25
26 : :s ( -- )
27     error-continuation get data>> stack. ;
28
29 : :r ( -- )
30     error-continuation get retain>> stack. ;
31
32 : :c ( -- )
33     error-continuation get call>> callstack. ;
34
35 : :get ( variable -- value )
36     error-continuation get name>> assoc-stack ;
37
38 : :res ( n -- * )
39     1- restarts get-global nth f restarts set-global restart ;
40
41 : :1 ( -- * ) 1 :res ;
42 : :2 ( -- * ) 2 :res ;
43 : :3 ( -- * ) 3 :res ;
44
45 : restart. ( restart n -- )
46     [
47         1+ dup 3 <= [ ":" % # "    " % ] [ # " :res  " % ] if
48         name>> %
49     ] "" make print ;
50
51 : restarts. ( -- )
52     restarts get [
53         nl
54         "The following restarts are available:" print
55         nl
56         [ restart. ] each-index
57     ] unless-empty ;
58
59 : print-error ( error -- )
60     [ error. flush ] curry
61     [ global [ "Error in print-error!" print drop ] bind ]
62     recover ;
63
64 : :error ( -- )
65     error get print-error ;
66
67 : print-error-and-restarts ( error -- )
68     print-error
69     restarts.
70     nl
71     "Type :help for debugging help." print flush ;
72
73 : try ( quot -- )
74     [ print-error-and-restarts ] recover ; inline
75
76 : expired-error. ( obj -- )
77     "Object did not survive image save/load: " write third . ;
78
79 : io-error. ( error -- )
80     "I/O error: " write third print ;
81
82 : type-check-error. ( obj -- )
83     "Type check error" print
84     "Object: " write dup fourth short.
85     "Object type: " write dup fourth class .
86     "Expected type: " write third type>class . ;
87
88 : divide-by-zero-error. ( obj -- )
89     "Division by zero" print drop ;
90
91 : signal-error. ( obj -- )
92     "Operating system signal " write third . ;
93
94 : array-size-error. ( obj -- )
95     "Invalid array size: " write dup third .
96     "Maximum: " write fourth 1- . ;
97
98 : c-string-error. ( obj -- )
99     "Cannot convert to C string: " write third . ;
100
101 : ffi-error. ( obj -- )
102     "FFI: " write
103     dup third [ write ": " write ] when*
104     fourth print ;
105
106 : heap-scan-error. ( obj -- )
107     "Cannot do next-object outside begin/end-scan" print drop ;
108
109 : undefined-symbol-error. ( obj -- )
110     "The image refers to a library or symbol that was not found"
111     " at load time" append print drop ;
112
113 : stack-underflow. ( obj name -- )
114     write " stack underflow" print drop ;
115
116 : stack-overflow. ( obj name -- )
117     write " stack overflow" print drop ;
118
119 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
120 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
121 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
122 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
123
124 : memory-error. ( error -- )
125     "Memory protection fault at address " write third .h ;
126
127 : primitive-error. ( error -- ) 
128     "Unimplemented primitive" print drop ;
129
130 PREDICATE: kernel-error < array
131     {
132         { [ dup empty? ] [ drop f ] }
133         { [ dup first "kernel-error" = not ] [ drop f ] }
134         [ second 0 15 between? ]
135     } cond ;
136
137 : kernel-errors ( error -- n errors )
138     second {
139         { 0  [ expired-error.          ] }
140         { 1  [ io-error.               ] }
141         { 2  [ primitive-error.        ] }
142         { 3  [ type-check-error.       ] }
143         { 4  [ divide-by-zero-error.   ] }
144         { 5  [ signal-error.           ] }
145         { 6  [ array-size-error.       ] }
146         { 7  [ c-string-error.         ] }
147         { 8  [ ffi-error.              ] }
148         { 9  [ heap-scan-error.        ] }
149         { 10 [ undefined-symbol-error. ] }
150         { 11 [ datastack-underflow.    ] }
151         { 12 [ datastack-overflow.     ] }
152         { 13 [ retainstack-underflow.  ] }
153         { 14 [ retainstack-overflow.   ] }
154         { 15 [ memory-error.           ] }
155     } ; inline
156
157 M: kernel-error error. dup kernel-errors case ;
158
159 M: kernel-error error-help kernel-errors at first ;
160
161 M: no-method summary
162     drop "No suitable method" ;
163
164 M: no-method error.
165     "Generic word " write
166     dup generic>> pprint
167     " does not define a method for the " write
168     dup object>> class pprint
169     " class." print
170     "Dispatching on object: " write object>> short. ;
171
172 M: bad-slot-value summary drop "Bad store to specialized slot" ;
173
174 M: no-math-method summary
175     drop "No suitable arithmetic method" ;
176
177 M: no-next-method summary
178     drop "Executing call-next-method from least-specific method" ;
179
180 M: inconsistent-next-method summary
181     drop "Executing call-next-method with inconsistent parameters" ;
182
183 M: check-method summary
184     drop "Invalid parameters for create-method" ;
185
186 M: not-a-tuple summary
187     drop "Not a tuple" ;
188
189 M: bad-superclass summary
190     drop "Tuple classes can only inherit from other tuple classes" ;
191
192 M: no-initial-value summary
193     drop "Initial value must be provided for slots specialized to this class" ;
194
195 M: bad-initial-value summary
196     drop "Incompatible initial value" ;
197
198 M: no-cond summary
199     drop "Fall-through in cond" ;
200
201 M: no-case summary
202     drop "Fall-through in case" ;
203
204 M: slice-error summary
205     drop "Cannot create slice" ;
206
207 M: bounds-error summary drop "Sequence index out of bounds" ;
208
209 M: condition error. error>> error. ;
210
211 M: condition summary error>> summary ;
212
213 M: condition error-help error>> error-help ;
214
215 M: assert summary drop "Assertion failed" ;
216
217 M: assert-sequence summary drop "Assertion failed" ;
218
219 M: assert-sequence error.
220     standard-table-style [
221         [ "=== Expected:" print expected>> stack. ]
222         [ "=== Got:" print got>> stack. ] bi
223     ] tabular-output ;
224
225 M: immutable summary drop "Sequence is immutable" ;
226
227 M: redefine-error error.
228     "Re-definition of " write
229     def>> . ;
230
231 M: undefined summary
232     drop "Calling a deferred word before it has been defined" ;
233
234 M: no-compilation-unit error.
235     "Attempting to define " write
236     definition>> pprint
237     " outside of a compilation unit" print ;
238
239 M: no-vocab summary
240     drop "Vocabulary does not exist" ;
241
242 M: encode-error summary drop "Character encoding error" ;
243
244 M: decode-error summary drop "Character decoding error" ;
245
246 M: bad-create summary drop "Bad parameters to create" ;
247
248 M: attempt-all-error summary drop "Nothing to attempt" ;
249
250 M: already-disposed summary drop "Attempting to operate on disposed object" ;
251
252 M: no-current-vocab summary
253     drop "Not in a vocabulary; IN: form required" ;
254
255 M: no-word-error error.
256     "No word named ``" write name>> write "'' found in current vocabulary search path" print ;
257
258 M: staging-violation summary
259     drop
260     "A parsing word cannot be used in the same file it is defined in." ;
261
262 M: bad-number summary
263     drop "Bad number literal" ;
264
265 M: duplicate-slot-names summary
266     drop "Duplicate slot names" ;
267
268 M: invalid-slot-name summary
269     drop "Invalid slot name" ;
270
271 M: source-file-error summary
272     error>> summary ;
273
274 M: source-file-error compute-restarts
275     error>> compute-restarts ;
276
277 M: source-file-error error-help
278     error>> error-help ;
279
280 M: not-in-a-method-error summary
281     drop "call-next-method can only be called in a method definition" ;
282
283 GENERIC: expected>string ( obj -- str )
284
285 M: f expected>string drop "end of input" ;
286 M: word expected>string name>> ;
287 M: string expected>string ;
288
289 M: unexpected error.
290     "Expected " write
291     dup want>> expected>string write
292     " but got " write
293     got>> expected>string print ;
294
295 M: lexer-error error.
296     [ lexer-dump ] [ error>> error. ] bi ;
297
298 M: lexer-error summary
299     error>> summary ;
300
301 M: lexer-error compute-restarts
302     error>> compute-restarts ;
303
304 M: lexer-error error-help
305     error>> error-help ;
306
307 M: source-file-error error.
308     [
309         [
310             [
311                 [ file>> [ % ": " % ] when* ]
312                 [ line#>> [ # ": " % ] when* ]
313                 [ summary % ] tri
314             ] "" make
315         ] [
316             [
317                 presented set
318                 bold font-style set
319             ] H{ } make-assoc
320         ] bi format nl
321     ] [ error>> error. ] bi ;
322
323 M: bad-effect summary
324     drop "Bad stack effect declaration" ;
325
326 M: bad-escape summary drop "Bad escape code" ;
327
328 M: bad-literal-tuple summary drop "Bad literal tuple" ;
329
330 M: check-mixin-class summary drop "Not a mixin class" ;
331
332 M: not-found-in-roots summary drop "Cannot resolve vocab: path" ;
333
334 M: wrong-values summary drop "Quotation called with wrong stack effect" ;