1 ! Copyright (C) 2004, 2011 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.strings arrays assocs classes
4 classes.builtin classes.tuple classes.tuple.parser combinators
5 combinators.short-circuit compiler.errors compiler.units
6 continuations definitions destructors effects.parser fixups
7 generic generic.math generic.parser generic.single grouping io
8 io.encodings io.styles kernel kernel.private lexer libc make
9 math math.order math.parser math.ratios namespaces parser
10 prettyprint sequences sequences.private slots
11 source-files.errors strings strings.parser summary system vocabs
12 vocabs.loader vocabs.parser words ;
15 GENERIC: error-help ( error -- topic )
17 M: object error-help drop f ;
19 M: tuple error-help class-of ;
21 M: source-file-error error-help error>> error-help ;
23 GENERIC: error. ( error -- )
25 M: object error. short. ;
27 M: string error. print ;
29 : traceback-link. ( continuation -- )
30 "[" write [ "Traceback" ] dip write-object "]" print ;
33 error-continuation get data>> stack. ;
36 error-continuation get retain>> stack. ;
39 error-continuation get call>> callstack. ;
41 : :get ( variable -- value )
42 error-continuation get name>> assoc-stack ;
45 1 - restarts [ nth f ] change-global
46 [ dup no-op-restart = [ drop f ] when ] change-obj
49 : :1 ( -- * ) 1 :res ;
50 : :2 ( -- * ) 2 :res ;
51 : :3 ( -- * ) 3 :res ;
53 : restart. ( restart n -- )
55 1 + dup 3 <= [ ":" % # " " % ] [ # " :res " % ] if
62 "The following restarts are available:" print
64 [ restart. ] each-index
67 : print-error ( error -- )
68 [ error. flush ] curry
69 [ [ "Error in print-error!" print drop ] with-global ]
73 error get print-error ;
75 : print-error-and-restarts ( error -- )
79 "Type :help for debugging help." print flush ;
82 [ print-error-and-restarts ] recover ; inline
84 : expired-error. ( obj -- )
85 "Object did not survive image save/load: " write third . ;
87 : io-error. ( error -- )
88 "I/O error #" write third [ . ] [ strerror print ] bi ;
90 : type-check-error. ( obj -- )
91 "Type check error" print
92 "Object: " write dup fourth short.
93 "Object type: " write dup fourth class-of .
94 "Expected type: " write third type>class . ;
96 : divide-by-zero-error. ( obj -- )
97 "Division by zero" print drop ;
99 HOOK: signal-error. os ( obj -- )
101 : array-size-error. ( obj -- )
102 "Invalid array size: " write dup third .
103 "Maximum: " write fourth 1 - . ;
105 : fixnum-range-error. ( obj -- )
106 "Cannot convert to fixnum: " write third . ;
108 : ffi-error. ( obj -- )
109 "FFI error" print drop ;
111 : find-ffi-error ( string -- error )
112 [ linkage-errors get ] dip
113 '[ nip asset>> name>> _ = ] assoc-find drop nip
114 [ error>> message>> ] [ "none" ] if* ;
116 : undefined-symbol-error. ( obj -- )
117 "Cannot resolve C library function" print
118 "Library: " write dup fourth .
120 [ "Symbol: " write print ]
121 [ "DlError: " write find-ffi-error print ] bi
122 "See https://concatenative.org/wiki/view/Factor/Requirements" print ;
124 : stack-underflow. ( obj name -- )
125 write " stack underflow" print drop ;
127 : stack-overflow. ( obj name -- )
128 write " stack overflow" print drop ;
130 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
131 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
132 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
133 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
134 : callstack-underflow. ( obj -- ) "Call" stack-underflow. ;
135 : callstack-overflow. ( obj -- ) "Call" stack-overflow. ;
137 : memory-error. ( error -- )
138 "Memory protection fault at address " write third .h ;
140 : fp-trap-error. ( error -- )
141 "Floating point trap" print drop ;
143 : interrupt-error. ( error -- )
144 "Interrupt" print drop ;
146 : callback-space-overflow. ( error -- )
147 "Callback space overflow" print drop ;
149 PREDICATE: vm-error < array
150 dup length 2 < [ drop f ] [
152 [ first-unsafe KERNEL-ERROR = ]
153 [ second-unsafe 0 kernel-error-count 1 - between? ]
157 : vm-errors ( error -- n errors )
162 [ type-check-error. ]
163 [ divide-by-zero-error. ]
165 [ array-size-error. ]
166 [ fixnum-range-error. ]
168 [ undefined-symbol-error. ]
169 [ datastack-underflow. ]
170 [ datastack-overflow. ]
171 [ retainstack-underflow. ]
172 [ retainstack-overflow. ]
173 [ callstack-underflow. ]
174 [ callstack-overflow. ]
178 [ callback-space-overflow. ]
181 M: vm-error summary drop "VM error" ;
183 M: vm-error error. dup vm-errors dispatch ;
185 M: vm-error error-help vm-errors nth first ;
187 M: division-by-zero summary
188 drop "Division by zero" ;
191 drop "No suitable method" ;
194 "Generic word " write
196 " does not define a method for the " write
197 dup object>> class-of pprint
199 "Dispatching on object: " write object>> short. ;
201 M: bad-slot-value summary drop "Bad store to specialized slot" ;
203 M: bad-slot-name summary drop "Bad slot name in object literal" ;
205 M: bad-vocab-name summary drop "Vocab name cannot contain ':/\\ \"'" ;
207 M: no-math-method summary
208 drop "No suitable arithmetic method" ;
210 M: no-next-method summary
211 drop "Executing call-next-method from least-specific method" ;
213 M: inconsistent-next-method summary
214 drop "Executing call-next-method with inconsistent parameters" ;
216 M: check-method-error summary
217 drop "Invalid parameters for create-method" ;
219 M: bad-superclass summary
220 drop "Tuple classes can only inherit from non-final tuple classes" ;
222 M: bad-initial-value summary
223 drop "Incompatible initial value" ;
226 drop "Fall-through in cond" ;
229 drop "Fall-through in case" ;
231 M: slice-error summary
232 "Cannot create slice" swap {
233 { [ dup from>> 0 < ] [ ": from < 0" ] }
234 { [ dup [ to>> ] [ seq>> length ] bi > ] [ ": to > length" ] }
235 { [ dup [ from>> ] [ to>> ] bi > ] [ ": from > to" ] }
239 M: bounds-error summary drop "Sequence index out of bounds" ;
241 M: groups-error summary drop "Non positive group size" ;
243 M: condition error. error>> error. ;
245 M: condition summary error>> summary ;
247 M: condition error-help error>> error-help ;
249 M: assert summary drop "Assertion failed" ;
251 M: assert-sequence summary drop "Assertion failed" ;
253 M: assert-sequence error.
254 standard-table-style [
255 [ "=== Expected:" print expected>> stack. ]
256 [ "=== Got:" print got>> stack. ] bi
259 M: immutable summary drop "Sequence is immutable" ;
261 M: redefine-error error.
262 "Re-definition of " write
265 M: undefined-word summary
266 word>> undefined-word?
267 "Cannot execute a deferred word before it has been defined"
268 "Cannot execute a word before it has been compiled"
271 M: no-compilation-unit error.
272 "Attempting to define " write
274 " outside of a compilation unit" print ;
277 drop "Vocabulary does not exist" ;
279 M: encode-error summary drop "Character encoding error" ;
281 M: decode-error summary drop "Character decoding error" ;
283 M: bad-create summary drop "Bad parameters to create" ;
285 M: cannot-be-inline summary drop "This type of word cannot be inlined" ;
287 M: attempt-all-error summary drop "Nothing to attempt" ;
289 M: already-disposed summary drop "Attempting to operate on disposed object" ;
291 M: no-current-vocab-error summary
292 drop "Not in a vocabulary; IN: form required" ;
294 M: no-word-error summary
297 "” found in current vocabulary search path" surround ;
299 M: no-word-error error. summary print ;
301 M: no-word-in-vocab summary
302 [ vocab>> ] [ word>> ] bi
303 [ "No word named “" % % "” found in “" % % "” vocabulary" % ] "" make ;
305 M: no-word-in-vocab error. summary print ;
307 M: ambiguous-use-error summary
309 "The name “" "” resolves to more than one word." surround ;
311 M: ambiguous-use-error error. summary print ;
313 M: staging-violation summary
315 "A parsing word cannot be used in the same file it is defined in." ;
317 M: bad-number summary
318 drop "Bad number literal" ;
320 M: duplicate-slot-names summary
321 drop "Duplicate slot names" ;
323 M: invalid-slot-name summary
324 drop "Invalid slot name" ;
326 M: bad-inheritance summary
327 drop "Circularity in inheritance chain" ;
329 M: not-in-a-method-error summary
330 drop "call-next-method can only be called in a method definition" ;
332 M: version-control-merge-conflict summary
333 drop "Version control merge conflict in source code" ;
335 GENERIC: expected>string ( obj -- str )
337 M: f expected>string drop "end of input" ;
338 M: word expected>string name>> ;
339 M: string expected>string ;
343 dup want>> expected>string write
345 got>> expected>string print ;
347 M: lexer-error error.
348 [ lexer-dump ] [ error>> error. ] bi ;
350 M: lexer-error summary
353 M: lexer-error compute-restarts
354 [ error-continuation get swap compute-fixups ] [ error>> compute-restarts ] bi append ;
356 M: lexer-error error-help
359 M: bad-effect summary
360 drop "Bad stack effect declaration" ;
362 M: invalid-row-variable summary
363 drop "Stack effect row variables can only occur as the first input or output" ;
365 M: row-variable-can't-have-type summary
366 drop "Stack effect row variables cannot have a declared type" ;
369 "Bad escape code: \\" write char>> write nl ;
371 M: bad-literal-tuple summary drop "Bad literal tuple" ;
373 M: not-found-in-roots summary
374 path>> "Cannot resolve vocab: " prepend ;
376 M: wrong-values summary drop "Quotation's stack effect does not match call site" ;
378 M: stack-effect-omits-dashes summary drop "Stack effect must contain “--”" ;
380 M: callsite-not-compiled summary
381 drop "Caller not compiled with the optimizing compiler" ;
383 { "threads" "debugger" } "debugger.threads" require-when
385 os unix? [ "debugger.unix" require ] when