1 ! Copyright (C) 2004, 2008 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 prettyprint prettyprint.config sequences
5 assocs sequences.private strings io.styles io.files vectors
6 words system splitting math.parser classes.tuple continuations
7 continuations.private combinators generic.math classes.builtin
8 classes compiler.units generic.standard vocabs init
9 kernel.private io.encodings accessors math.order
10 destructors source-files parser classes.tuple.parser
11 effects.parser lexer compiler.errors generic.parser
15 GENERIC: error. ( error -- )
16 GENERIC: error-help ( error -- topic )
19 M: object error-help drop f ;
21 M: tuple error-help class ;
23 M: string error. print ;
26 error-continuation get data>> stack. ;
29 error-continuation get retain>> stack. ;
32 error-continuation get call>> callstack. ;
34 : :get ( variable -- value )
35 error-continuation get name>> assoc-stack ;
38 1- restarts get-global nth f restarts set-global restart ;
40 : :1 ( -- * ) 1 :res ;
41 : :2 ( -- * ) 2 :res ;
42 : :3 ( -- * ) 3 :res ;
44 : restart. ( restart n -- )
46 1+ dup 3 <= [ ":" % # " " % ] [ # " :res " % ] if
53 "The following restarts are available:" print
55 [ restart. ] each-index
58 : print-error ( error -- )
59 [ error. flush ] curry
60 [ global [ "Error in print-error!" print drop ] bind ]
63 : print-error-and-restarts ( error -- )
67 "Type :help for debugging help." print flush ;
70 [ print-error-and-restarts ] recover ;
72 M: relative-underflow summary
73 drop "Too many items removed from data stack" ;
75 M: relative-overflow summary
76 drop "Superfluous items pushed to data stack" ;
78 : expired-error. ( obj -- )
79 "Object did not survive image save/load: " write third . ;
81 : io-error. ( error -- )
82 "I/O error: " write third print ;
84 : type-check-error. ( obj -- )
85 "Type check error" print
86 "Object: " write dup fourth short.
87 "Object type: " write dup fourth class .
88 "Expected type: " write third type>class . ;
90 : divide-by-zero-error. ( obj -- )
91 "Division by zero" print drop ;
93 : signal-error. ( obj -- )
94 "Operating system signal " write third . ;
96 : array-size-error. ( obj -- )
97 "Invalid array size: " write dup third .
98 "Maximum: " write fourth 1- . ;
100 : c-string-error. ( obj -- )
101 "Cannot convert to C string: " write third . ;
103 : ffi-error. ( obj -- )
105 dup third [ write ": " write ] when*
108 : heap-scan-error. ( obj -- )
109 "Cannot do next-object outside begin/end-scan" print drop ;
111 : undefined-symbol-error. ( obj -- )
112 "The image refers to a library or symbol that was not found"
113 " at load time" append print drop ;
115 : stack-underflow. ( obj name -- )
116 write " stack underflow" print drop ;
118 : stack-overflow. ( obj name -- )
119 write " stack overflow" print drop ;
121 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
122 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
123 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
124 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
126 : memory-error. ( error -- )
127 "Memory protection fault at address " write third .h ;
129 : primitive-error. ( error -- )
130 "Unimplemented primitive" print drop ;
132 PREDICATE: kernel-error < array
134 { [ dup empty? ] [ drop f ] }
135 { [ dup first "kernel-error" = not ] [ drop f ] }
136 [ second 0 15 between? ]
139 : kernel-errors ( error -- n errors )
141 { 0 [ expired-error. ] }
143 { 2 [ primitive-error. ] }
144 { 3 [ type-check-error. ] }
145 { 4 [ divide-by-zero-error. ] }
146 { 5 [ signal-error. ] }
147 { 6 [ array-size-error. ] }
148 { 7 [ c-string-error. ] }
150 { 9 [ heap-scan-error. ] }
151 { 10 [ undefined-symbol-error. ] }
152 { 11 [ datastack-underflow. ] }
153 { 12 [ datastack-overflow. ] }
154 { 13 [ retainstack-underflow. ] }
155 { 14 [ retainstack-overflow. ] }
156 { 15 [ memory-error. ] }
159 M: kernel-error error. dup kernel-errors case ;
161 M: kernel-error error-help kernel-errors at first ;
164 drop "No suitable method" ;
167 "Generic word " write
169 " does not define a method for the " write
170 dup object>> class pprint
172 "Dispatching on object: " write object>> short. ;
174 M: bad-slot-value summary drop "Bad store to specialized slot" ;
176 M: no-math-method summary
177 drop "No suitable arithmetic method" ;
179 M: no-next-method summary
180 drop "Executing call-next-method from least-specific method" ;
182 M: inconsistent-next-method summary
183 drop "Executing call-next-method with inconsistent parameters" ;
185 M: check-method summary
186 drop "Invalid parameters for create-method" ;
188 M: not-a-tuple summary
191 M: bad-superclass summary
192 drop "Tuple classes can only inherit from other tuple classes" ;
194 M: no-initial-value summary
195 drop "Initial value must be provided for slots specialized to this class" ;
197 M: bad-initial-value summary
198 drop "Incompatible initial value" ;
201 drop "Fall-through in cond" ;
204 drop "Fall-through in case" ;
206 M: slice-error error.
207 "Cannot create slice because " write
210 M: bounds-error summary drop "Sequence index out of bounds" ;
212 M: condition error. error>> error. ;
214 M: condition summary error>> summary ;
216 M: condition error-help error>> error-help ;
218 M: assert summary drop "Assertion failed" ;
221 "Assertion failed" print
222 standard-table-style [
225 [ expect>> [ [ "Expect:" write ] with-cell pprint-cell ] with-row ]
226 [ got>> [ [ "Got:" write ] with-cell pprint-cell ] with-row ] bi
229 M: immutable summary drop "Sequence is immutable" ;
231 M: redefine-error error.
232 "Re-definition of " write
236 drop "Calling a deferred word before it has been defined" ;
238 M: no-compilation-unit error.
239 "Attempting to define " write
241 " outside of a compilation unit" print ;
244 drop "Vocabulary does not exist" ;
246 M: encode-error summary drop "Character encoding error" ;
248 M: decode-error summary drop "Character decoding error" ;
250 M: bad-create summary drop "Bad parameters to create" ;
252 M: attempt-all-error summary drop "Nothing to attempt" ;
254 M: already-disposed summary drop "Attempting to operate on disposed object" ;
256 M: no-current-vocab summary
257 drop "Not in a vocabulary; IN: form required" ;
259 M: no-word-error summary
260 drop "Word not found in current vocabulary search path" ;
262 M: staging-violation summary
264 "A parsing word cannot be used in the same file it is defined in." ;
266 M: bad-number summary
267 drop "Bad number literal" ;
269 M: duplicate-slot-names summary
270 drop "Duplicate slot names" ;
272 M: invalid-slot-name summary
273 drop "Invalid slot name" ;
275 : file. ( file -- ) path>> <pathname> . ;
277 M: source-file-error error.
278 [ file>> file. ] [ error>> error. ] bi ;
280 M: source-file-error summary
283 M: source-file-error compute-restarts
284 error>> compute-restarts ;
286 M: source-file-error error-help
289 M: not-in-a-method-error summary
290 drop "call-next-method can only be called in a method definition" ;
292 GENERIC: expected>string ( obj -- str )
294 M: f expected>string drop "end of input" ;
295 M: word expected>string name>> ;
296 M: string expected>string ;
300 dup want>> expected>string write
302 got>> expected>string print ;
304 M: lexer-error error.
305 [ lexer-dump ] [ error>> error. ] bi ;
307 M: lexer-error summary
310 M: lexer-error compute-restarts
311 error>> compute-restarts ;
313 M: lexer-error error-help
316 M: object compiler-error. ( error word -- )
318 "While compiling " write pprint ": " print
322 M: bad-effect summary
323 drop "Bad stack effect declaration" ;
325 M: bad-escape summary drop "Bad escape code" ;