1 | | % (c) 2009-2024 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, |
2 | | % Heinrich Heine Universitaet Duesseldorf |
3 | | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) |
4 | | |
5 | | |
6 | | :- module(error_manager,[init_error_manager/0, |
7 | | add_warning/2, add_warning/3, add_warning/4, |
8 | | reset_errors/0, clear_wd_errors/0, |
9 | | add_error/2, add_error/3, add_error/4, |
10 | | add_error_and_fail/3, add_error_and_fail/2, |
11 | | add_error_fail/3, |
12 | | add_failed_call_error/1, add_internal_error/2, add_internal_error/4, |
13 | | register_optional_error_or_warning/1, reset_optional_errors_or_warnings/0, |
14 | | assert_true/1, |
15 | | add_debug_message/3, add_debug_message/4, |
16 | | add_message/2, add_message/3, add_message/4, |
17 | | add_all_perrors/1, add_all_perrors/2, add_all_perrors/3, |
18 | | no_real_perror_occurred/1, |
19 | | get_all_errors/1, tcltk_get_all_errors/4, |
20 | | get_tk_table_position_info/2, |
21 | | get_all_errors_and_reset/1, % call if you want to start with a fresh top-level error scope |
22 | | get_all_errors_with_span_info_and_reset/1, % ditto + obtain location info; excludes messages |
23 | | get_all_errors_and_clear/1, |
24 | | get_error/2, get_error_with_span/3, |
25 | | get_warning/2, check_error_occured/2, check_warning_occured/2, |
26 | | get_error_span_linecol/5, get_error_span_linecol/4, |
27 | | simplify_span/2, |
28 | | tk_get_error_span_linecol_for_main_file/4, |
29 | | register_error_span/2, |
30 | | check_error_span_file_linecol/6, |
31 | | position_is_not_in_main_file/1, |
32 | | error_or_warning_occured/0, |
33 | | real_error_occurred/0, reset_real_error_occurred/0, assert_real_error_occurred/1, |
34 | | warning_occurred/0, % true if a warning was raised |
35 | | wd_error_occured/0, |
36 | | test_error_occurred/1, count_errors_occurred/2, |
37 | | get_total_number_of_errors/1, |
38 | | reset_error_spans/0, |
39 | | |
40 | | backup_errors/0, backed_up_error/4, |
41 | | |
42 | | set_error_context/1, clear_error_context/0, |
43 | | get_error_context/1, restore_error_context/1, |
44 | | print_error_context/0, |
45 | | |
46 | | extract_span_description/2, extract_symbolic_label_pos/2, extract_symbolic_label/2, |
47 | | extract_line_col/5, extract_file_line_col/6, |
48 | | extract_all_line_col/5, |
49 | | extract_line_col_for_main_file/5, |
50 | | extract_subsidiary_tail_file_name/2, extract_tail_file_name/2, |
51 | | extract_file_number_and_name/3, |
52 | | extract_additional_description/2, extract_pos_context/4, |
53 | | print_error_span/1, print_message_span/1, |
54 | | |
55 | | turn_show_error_source_on/0, turn_show_error_source_off/0, |
56 | | |
57 | | enter_new_error_scope/1, enter_new_error_scope/2, |
58 | | enter_new_clean_error_scope/1, |
59 | | get_current_error_scope/1, |
60 | | exit_error_scope/2, exit_error_scope/3, |
61 | | error_occurred_at_level/1, specific_event_occurred_at_level/2, |
62 | | %throw_errors_in_scope/3, |
63 | | add_new_event_in_error_scope/1, add_new_event_in_error_scope/2, |
64 | | event_occurred_in_error_scope/1, error_occurred_in_error_scope/0, |
65 | | clear_events_in_error_scope/2, clear_events_in_current_error_scope/1, |
66 | | clear_enumeration_warning_in_error_scope/1, |
67 | | clear_all_errors_in_error_scope/1, |
68 | | call_in_fresh_error_scope_for_one_solution/1, |
69 | | enumeration_warning_occured_in_error_scope/0, |
70 | | is_enumeration_warning_exception/1, |
71 | | abort_error_occured_in_error_scope/0, |
72 | | on_enumeration_warning/2, on_enumeration_warning_with_continue/3, |
73 | | throw_enumeration_warnings_in_current_scope/0, |
74 | | throw_enumeration_warnings_in_scope/3, |
75 | | catch_enumeration_warning_exceptions/2, catch_enumeration_warning_exceptions/3, |
76 | | catch_enumeration_warning_exceptions/4, catch_enumeration_warning_exceptions_and_wd_failures/5, |
77 | | call_with_enumeration_warning/1, |
78 | | observe_enumeration_warnings/2, |
79 | | critical_enumeration_warning_occured_in_error_scope/4, |
80 | | critical_enumeration_warning_occured_in_error_scope/0, |
81 | | virtual_time_out_occured_in_error_scope/1, % variation which returns a virtual_time_out(.) term |
82 | | clear_enumeration_warnings/0, |
83 | | translate_events_in_current_scope_to_warnings/2, |
84 | | time_out_with_enum_warning_one_solution/3, % use this for finding one solution |
85 | | time_out_with_enum_warning_one_solution/4, |
86 | | time_out_with_enum_warning_one_solution_no_new_error_scope/3, |
87 | | is_time_out_result/1, |
88 | | time_out_with_enum_warning_for_findall/3, % use this if you cycle through all solutions |
89 | | time_out_with_enum_warning_for_findall_in_current_error_scope/4, |
90 | | get_virtual_time_out_from_exception/2, % used by time_out_with_enum_warning_for_findall |
91 | | clpfd_overflow_msg/1, |
92 | | is_clpfd_overflow_representation_error/2, is_clpfd_overflow_representation_error_msg/1, |
93 | | logged_error/4, |
94 | | display_error_statistics/0, |
95 | | set_error_manager_silent_mode/1 |
96 | | ]). |
97 | | |
98 | | % meta_predicate annotations should appear before loading any code: |
99 | | :- meta_predicate add_failed_call_error(0). |
100 | | :- meta_predicate add_internal_error(-,:). |
101 | | :- meta_predicate add_new_event_in_error_scope(-,2). |
102 | | |
103 | | :- meta_predicate on_enumeration_warning(0,0). |
104 | | :- meta_predicate on_enumeration_warning_with_continue(0,0,0). |
105 | | |
106 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0). |
107 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0,-). |
108 | | :- meta_predicate catch_enumeration_warning_exceptions(0,0,-,-). |
109 | | :- meta_predicate catch_enumeration_warning_exceptions_and_wd_failures(0,0,-,0,-). |
110 | | |
111 | | :- meta_predicate observe_enumeration_warnings(0,0). |
112 | | |
113 | | :- meta_predicate time_out_with_enum_warning_one_solution(0,-,-). |
114 | | :- meta_predicate time_out_with_enum_warning_one_solution(0,-,-,-). |
115 | | |
116 | | :- meta_predicate call_in_fresh_error_scope_for_one_solution(0). |
117 | | |
118 | | :- meta_predicate call_with_enumeration_warning(0). |
119 | | |
120 | | :- meta_predicate time_out_with_enum_warning_one_solution_no_new_error_scope(0,-,-). |
121 | | |
122 | | :- meta_predicate time_out_with_enum_warning_for_findall(0,-,-). |
123 | | :- meta_predicate time_out_with_enum_warning_for_findall2(-,0,-,-). |
124 | | |
125 | | :- meta_predicate time_out_with_enum_warning_for_findall_in_current_error_scope(-,0,-,-). |
126 | | |
127 | | :- meta_predicate assert_true(0). |
128 | | :- meta_predicate catch_wd(-,0,-). |
129 | | |
130 | | % ------------ |
131 | | |
132 | | :- use_module(library(lists)). |
133 | | |
134 | | :- use_module(tools,[get_tail_filename/2]). |
135 | | :- use_module(tools_lists,[count_occurences/2]). |
136 | | :- use_module(tools_printing,[print_error/1,print_message_on_stream/3,format_error_with_nl/2,format_with_colour/4]). |
137 | | :- use_module(tools_strings,[ajoin/2, string_concatenate/3]). |
138 | | |
139 | | :- use_module(module_information,[module_info/2]). |
140 | | :- module_info(group,infrastructure). |
141 | | :- module_info(description,'The error_manager controls error messages. Error messages can be added, retrieved, counted,etc. The module also takes care of extracting source spans from error messages.'). |
142 | | |
143 | | %:- use_module(translate). |
144 | | :- use_module(debug, [debug_mode/1, debug_println/1, debug_println/2]). |
145 | | :- use_module(preferences,[get_preference/2,reset_temporary_preference/2, |
146 | | temporary_set_preference/3]). |
147 | | |
148 | | |
149 | | :- use_module(extension('counter/counter'), |
150 | | [counter_init/0, new_counter/1, get_counter/2, inc_counter/2, set_counter/2]). |
151 | | |
152 | | % cur_block_level is now stored as a C counter, which needs to be initialised |
153 | | init_error_manager :- counter_init, new_counter(cur_block_level). |
154 | | |
155 | | |
156 | | :- dynamic optional_error_or_warning/1. |
157 | | register_optional_error_or_warning(Type) :- |
158 | | (optional_error_or_warning(Type) -> true ; assertz(optional_error_or_warning(Type))). |
159 | | reset_optional_errors_or_warnings :- retractall(optional_error_or_warning(_)). |
160 | | |
161 | | % INTERNAL ERRORS |
162 | | |
163 | | |
164 | | add_failed_call_error(Call) :- !, |
165 | | add_internal_error('Internal Error; call failed: ',Call). |
166 | | |
167 | | add_internal_error(Module,Msg,Term,Span) :- |
168 | | add_error(internal_error(Module),Msg,Term,Span). |
169 | | add_internal_error(Msg,Module:Call) :- !, |
170 | | add_error(internal_error(Module),Msg,Call). |
171 | | add_internal_error(Msg,Call) :- |
172 | | add_error(internal_error(unknown_module),Msg,Call). |
173 | | |
174 | | |
175 | | % ERROR SCOPING |
176 | | |
177 | | %:- dynamic cur_block_level/1. |
178 | | %cur_block_level(0). % no realised as a C counter |
179 | | :- volatile error_occurred_at_level/1. |
180 | | :- dynamic error_occurred_at_level/1. |
181 | | :- volatile throw_error/3. |
182 | | :- dynamic throw_error/3. |
183 | | |
184 | | |
185 | | get_current_error_scope(Level) :- get_counter(cur_block_level,Level). |
186 | | |
187 | | enter_new_error_scope(Level,_PP) :- |
188 | | enter_new_error_scope(Level). %, tools_printing:print_term_summary(enter(Level,PP)),nl. |
189 | | enter_new_error_scope(Level) :- |
190 | | enter_new_clean_error_scope(Level), |
191 | | L1 is Level - 1, |
192 | | copy_throw_errors(L1,Level). |
193 | | copy_throw_errors(L1,L) :- |
194 | ? | throw_error(L1,Error,Span), %print(copy(throw(L1,L,Error))),nl, |
195 | | assertz(throw_error(L,Error,Span)),fail. |
196 | | copy_throw_errors(_,_). |
197 | | enter_new_clean_error_scope(Level) :- |
198 | | inc_counter(cur_block_level,L1), |
199 | | (retract(throw_error(L1,_,_)) -> retractall(throw_error(L1,_,_)) ; true), % DO WE NEED THIS ?? |
200 | | % print(enter(L1)),nl,trace, |
201 | | Level=L1. |
202 | | |
203 | | |
204 | | % mark that everything unifying with Error should be thrown rather than collected |
205 | | throw_errors_in_scope(Level,Error,Span) :- |
206 | ? | (\+ throw_error(Level,Error,Span) % TO DO: check that we are an instance of existing error ! |
207 | | -> asserta(throw_error(Level,Error,Span)) |
208 | | ; true). |
209 | | |
210 | | clear_all_errors_in_error_scope(Level) :- %print(clearing(Level)),nl, |
211 | | clear_events_in_error_scope(Level,_), |
212 | | error_occurred_at_level(L1), L1>Level, %print(clearing(L1)),nl, |
213 | | clear_events_in_error_scope(Level,_),fail. |
214 | | clear_all_errors_in_error_scope(_). |
215 | | |
216 | | % useful in call_cleanup; when error block may already be exited |
217 | | exit_error_scope_if_necessary(Level,PP) :- get_counter(cur_block_level,CurLevel), |
218 | | (Level =< CurLevel |
219 | | -> exit_error_scope(Level,_,PP) %,print(exit(Level,CurLevel,PP)),nl |
220 | | ; true). |
221 | | |
222 | | exit_error_scope(Level,ErrorOcc) :- exit_error_scope(Level,ErrorOcc,unknown). |
223 | | exit_error_scope(Level,ErrorOcc,PP) :- get_counter(cur_block_level,CurL), |
224 | | % print(exit(Level,CurL,PP)),nl, |
225 | | (CurL = Level -> NewL is CurL-1 |
226 | | ; Level < CurL -> NewL is Level-1, |
227 | | %print_error_scopes, |
228 | | format('*** exiting multiple error blocks (~w -> ~w) for ~w ***~n',[CurL,NewL,PP]) |
229 | | ; format('*** block ~w already exited (currently at ~w) for ~w ***~n',[Level,CurL,PP]), |
230 | | NewL is CurL |
231 | | ), set_counter(cur_block_level,NewL), |
232 | | copy_error_scope_events(CurL,NewL), |
233 | ? | (retract(throw_error(CurL,_,_)) -> retractall(throw_error(CurL,_,_)) ; true), |
234 | | (retract(error_occurred_at_level(CurL)) |
235 | | -> ErrorOcc=true, |
236 | | (error_occurred_at_level(NewL) -> true ; assertz(error_occurred_at_level(NewL))) |
237 | | ; ErrorOcc=false). |
238 | | |
239 | | |
240 | | % versions of enter_new_error_scope and exit_error_scope which re_enter blocks and clean up upon backtracking |
241 | | %enter_new_error_scope_bt(Level,PP) :- enter_new_error_scope(Level,PP), exit_upon_fail(Level,PP). |
242 | | %exit_upon_fail(_,_). |
243 | | %exit_upon_fail(Level,PP) :- print(exit_upon_backtrack(Level,PP)),nl, |
244 | | % exit_error_scope(Level,_,PP),fail. |
245 | | % |
246 | | %exit_error_scope_bt(Level,ErrorOcc,PP) :- exit_error_scope(Level,ErrorOcc,PP). |
247 | | %exit_error_scope_bt(Level,_ErrorOcc,PP) :- print(re_enter_block(Level,PP)),nl, |
248 | | % enter_new_error_scope(Level2,PP), |
249 | | % (Level2=Level -> true ; print(mismatch_when_reentering_block(Level,PP)),nl), |
250 | | % fail. |
251 | | |
252 | | % --------------------------- |
253 | | |
254 | | |
255 | | |
256 | | assert_real_error_in_current_block(Source) :- get_counter(cur_block_level,L),L>0, |
257 | | %print(assert_real_error_in_current_block(L)),nl, |
258 | | (specific_event_occurred_at_level(L,Source) -> true |
259 | | ; assertz(specific_event_occurred_at_level(L,Source))), % ensures that clear_events_in_current_error_scope does not remove error_occurred_at_level if used for enumeration warnings, ... |
260 | | \+ error_occurred_at_level(L), assertz(error_occurred_at_level(L)),!. |
261 | | assert_real_error_in_current_block(_). |
262 | | |
263 | | |
264 | | reset_error_scopes :- |
265 | | (set_counter(cur_block_level,0) -> true |
266 | | ; format(user_error,'~n*** set_counter(cur_block_level,0) failed!!~n',[]),fail |
267 | | ), % try debug random gitlab failures; something really bad happened |
268 | | retractall(error_occurred_at_level(_)), |
269 | | retractall(specific_event_occurred_at_level(_,_)). |
270 | | |
271 | | % for debugging / portraying error scopes: |
272 | | % portray_error_scopes |
273 | | :- public print_error_scopes/0. |
274 | | print_error_scopes :- |
275 | | if(get_counter(cur_block_level,L), |
276 | | format(' Cur Block Level = ~w~n',[L]), |
277 | | format(' *** NO Cur Block Level Information !!',[]) |
278 | | ), |
279 | | fail. |
280 | | print_error_scopes :- |
281 | | findall(L,error_occurred_at_level(L),LL), |
282 | | format(' Errors occured at those levels: ~w~n',[LL]), |
283 | | format(' Specific events at levels:~n',[]), |
284 | | specific_event_occurred_at_level(Level,Err), |
285 | | format(' ~w --> ~w~n',[Level,Err]), |
286 | | fail. |
287 | | print_error_scopes. |
288 | | |
289 | | % call a predicate in fresh error scope (assuming one solution is searched for only; failure is also ok) |
290 | | % and then discard all errors |
291 | | call_in_fresh_error_scope_for_one_solution(Call) :- |
292 | | enter_new_error_scope(Level,call_in_fresh_error_scope_for_one_solution), |
293 | | call_cleanup(Call, |
294 | | (clear_all_errors_in_error_scope(_), |
295 | | exit_error_scope(Level,_,call_in_fresh_error_scope_for_one_solution))). |
296 | | |
297 | | :- volatile specific_event_occurred_at_level/2. |
298 | | :- dynamic specific_event_occurred_at_level/2. |
299 | | |
300 | | % can be used to keep track of whether specific events occurred within an error_scope |
301 | | add_new_event_in_error_scope(Event) :- |
302 | | add_new_event_in_error_scope(Event,do_nothing). |
303 | | do_nothing(_,_). |
304 | | add_new_event_in_error_scope(Event,PrintMessageCall) :- |
305 | | get_counter(cur_block_level,L), |
306 | ? | (specific_event_occurred_at_level(L,Event) -> fail |
307 | ? | ; (throw_error(L,Event,Span) |
308 | | -> (debug_mode(off) -> true ; print_message(informational,throwing(Event))), % trace, % <--- |
309 | | call(PrintMessageCall,throwing,Span), |
310 | | % Note: event is not asserted ! See e.g. tests 1089, 1519, 1522, 1661, 1199, 1291, ... |
311 | | throw(Event) |
312 | | ; assertz(specific_event_occurred_at_level(L,Event)), |
313 | | call(PrintMessageCall,not_throwing,unknown) |
314 | | ) |
315 | | ). |
316 | | |
317 | | % call if you just want to add the error information; without throwing |
318 | | assert_event_in_error_scope_without_throw(Event) :- get_counter(cur_block_level,L), |
319 | | (specific_event_occurred_at_level(L,Event) -> true |
320 | | ; assertz(specific_event_occurred_at_level(L,Event))). |
321 | | |
322 | | error_occurred_in_error_scope :- get_counter(cur_block_level,L),error_occurred_at_level(L). |
323 | ? | event_occurred_in_error_scope(Event) :- get_counter(cur_block_level,L),specific_event_occurred_at_level(L,Event). |
324 | | |
325 | ? | copy_error_scope_events(FromLevel,ToLevel) :- retract(specific_event_occurred_at_level(FromLevel,Event)), |
326 | ? | \+ specific_event_occurred_at_level(ToLevel,Event), |
327 | | % print(copying_event(FromLevel,ToLevel,Event)),nl, |
328 | | assertz(specific_event_occurred_at_level(ToLevel,Event)), % should we throw if this level demands it ? |
329 | | (throw_error(ToLevel,Event,_Span) |
330 | | -> debug_println(20,throw_copied_event(Event)), throw(Event)), |
331 | | fail. |
332 | | copy_error_scope_events(_,_). |
333 | | |
334 | | clear_events_in_current_error_scope(Event) :- get_counter(cur_block_level,L),clear_events_in_error_scope(L,Event). |
335 | | |
336 | | %clear_events_in_error_scope(L,Event) :-retract(specific_event_occurred_at_level(L,Event)), print(retract(L,Event)),nl,fail. |
337 | | |
338 | | clear_enumeration_warning_in_error_scope(Level) :- |
339 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)). |
340 | | |
341 | | clear_events_in_error_scope(L,Event) :- |
342 | ? | (specific_event_occurred_at_level(L,_) |
343 | | -> retractall(specific_event_occurred_at_level(L,Event)), |
344 | ? | (specific_event_occurred_at_level(L,_) -> true % we have not removed all events |
345 | | ; retract(error_occurred_at_level(L)) -> true |
346 | | ; true |
347 | | ) |
348 | | ; true). % no errors can have been there |
349 | | |
350 | | throw_enumeration_warnings_in_current_scope :- |
351 | | get_counter(cur_block_level,L), throw_enumeration_warnings_in_scope(L,_,unknown). |
352 | | |
353 | | throw_enumeration_warnings_in_scope(Level,Critical,Span) :- |
354 | | simplify_span(Span,SimplifiedSpan), % relevant for test 1562 |
355 | | throw_errors_in_scope(Level,enumeration_warning(_,_,_,_,Critical),SimplifiedSpan). |
356 | | |
357 | | on_enumeration_warning(Call, BackupCall) :- |
358 | ? | on_enumeration_warning_with_continue(Call, BackupCall, true). |
359 | | % sets up a new error scope and forces enumeration_warnings to be thrown |
360 | | % if an enumeration_warning happens the BackupCall is executed |
361 | | on_enumeration_warning_with_continue(Call, _, _) :- is_true_call(Call),!. % happens e.g., for card_for_specific_closure3 |
362 | | on_enumeration_warning_with_continue(Call, BackupCall, TrueContinuation) :- |
363 | | enter_new_error_scope(Level,on_enumeration_warning_with_continue), |
364 | | throw_enumeration_warnings_in_scope(Level,_,unknown), |
365 | ? | call_cleanup( |
366 | | catch( |
367 | | if(Call, |
368 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_true), |
369 | | % TODO: we need a version that re-instates error_scope upon backtracking?! |
370 | | call(TrueContinuation)), |
371 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_false),fail) |
372 | | ), |
373 | | Exc, |
374 | | (exit_error_scope(Level,_,on_enumeration_warning_with_continue_exception), % We do not catch timeouts |
375 | | (Exc=enumeration_warning(_,_,_,_,_) -> call(BackupCall) ; throw(Exc)) |
376 | | ) |
377 | | ), |
378 | | exit_error_scope_if_necessary(Level,on_enumeration_warning_with_continue_timeout) % TIMEOUT |
379 | | ). |
380 | | |
381 | | % check if we are simply calling on_enumeration_warning with trivial true call; avoid call_cleanup, error_scope. |
382 | | is_true_call(_:X) :- !, nonvar(X), is_true_call(X). |
383 | | is_true_call(call(X)) :- !, is_true_call(X). |
384 | | is_true_call(true). |
385 | | |
386 | | catch_enumeration_warning_exceptions(Call, BackupCall) :- |
387 | ? | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,false,fail,unknown). |
388 | | catch_enumeration_warning_exceptions(Call, BackupCall, AddAsEventError) :- |
389 | | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,AddAsEventError,fail,unknown). |
390 | | catch_enumeration_warning_exceptions(Call, BackupCall, AddAsEventError,Source) :- |
391 | ? | catch_enumeration_warning_exceptions_and_wd_failures(Call,BackupCall,AddAsEventError,fail,Source). |
392 | | |
393 | | % can be used to check if an exception is an enumeration warning |
394 | | is_enumeration_warning_exception(enumeration_warning(_Enumerating,_Type,_Range,_RestrRange,_Critical)). |
395 | | |
396 | | % AddAsEventError = true means that caught exceptions are added as event_errors in scope |
397 | | % (this can be important if we later want to decide whether an enumeration warning occured) |
398 | | % WDCall: this gets called when a well_definedness_error is raised by the Call |
399 | | catch_enumeration_warning_exceptions_and_wd_failures(Call, BackupCall,AddAsEventError,WDCall,Source) :- |
400 | | enter_new_error_scope(Level,catch_enumeration_warning_exceptions), |
401 | | % print(enter(Level)),nl, |
402 | | % difference: we do not force to throw_errors_in_scope |
403 | ? | catch( |
404 | | call_cleanup(if(Call,true,catch_wd(Level,WDCall,Source)), |
405 | | (%print(exit(Level)),nl, |
406 | | exit_error_scope(Level,_,catch_enumeration_warning_exceptions))), |
407 | | % call_cleanup can be tricky in case of success of Call: the error scope may not be exited immediately; TO DO: maybe rewrite using same style as above (using if + catch) |
408 | | enumeration_warning(A,B,C,D,E), |
409 | | ((AddAsEventError=false |
410 | | -> (Source=ignore(Src) |
411 | | -> formatsilentmsg(user_output,'%%% Ignoring unbounded enumeration warning (source:~w).~n',[Src]) |
412 | | ; true) |
413 | | % inform that enum warning was not serious: there is a fall-back solution |
414 | | ; assert_event_in_error_scope_without_throw(enumeration_warning(A,B,C,D,E))), |
415 | | BackupCall)). |
416 | | |
417 | | catch_wd(Level,WDCall,Source) :- |
418 | | WDCall \= fail, |
419 | | % print(check_wd(Level,Source)),nl, portray_events_in_error_scope,nl, |
420 | | specific_event_occurred_at_level(Level,well_definedness_error),!, |
421 | | formatsilentmsg(user_output,'%%% well_definedness_error (source:~w).~n',[Source]), |
422 | | call(WDCall). |
423 | | catch_wd(_,_,_) :- fail. |
424 | | |
425 | | |
426 | | % execute some code when enumeration warning occurs; and then throw exception again |
427 | | observe_enumeration_warnings(Call,ObserverCallToExecute) :- |
428 | | catch(Call, enumeration_warning(A,B,C,D,E), |
429 | | call_cleanup(ObserverCallToExecute, throw(enumeration_warning(A,B,C,D,E)))). |
430 | | |
431 | | |
432 | | % sets up a new error scope and forces enumeration_warnings to be thrown |
433 | | % the exceptions are not caught ! |
434 | | call_with_enumeration_warning(Call) :- |
435 | | enter_new_error_scope(Level,call_with_enumeration_warning), |
436 | | throw_enumeration_warnings_in_scope(Level,_,unknown), |
437 | | call_cleanup( |
438 | | (Call, % the assumption is that Call will only have one solution |
439 | | exit_error_scope(Level,_,call_with_enumeration_warning_true)), |
440 | | exit_error_scope_if_necessary(Level,call_with_enumeration_warning_timeout) % failure, TIMEOUT or Exception |
441 | | ). |
442 | | |
443 | | |
444 | | % a version of time_out which treats enumeration_warnings like time-outs |
445 | | % and also checks enumeration_warnings in case of failure |
446 | | % assumes we only look for one solution (otherwise error scope cleanup has to be changed !! ) |
447 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes,ClearEnumWarnings) :- |
448 | | enter_new_error_scope(Level,time_out_with_enum_warning_one_solution), |
449 | | call_cleanup( |
450 | | catch( |
451 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,ClearEnumWarnings,perform_cut), |
452 | | enumeration_warning(A,B,C,D,E), |
453 | | % no need to clear: we leave error_scope below |
454 | | TimeOutRes=virtual_time_out(thrown_enumeration_warning(A,B,C,D,E))), |
455 | | exit_error_scope(Level,_,time_out_with_enum_warning_one_solution)). |
456 | | |
457 | | % here enumeration warnings are not cleared and propagated to outer error scope |
458 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes) :- |
459 | | time_out_with_enum_warning_one_solution(Call,TO,TimeOutRes,no_clear). |
460 | | |
461 | | |
462 | | enumeration_warning_occured_in_error_scope :- |
463 | | event_occurred_in_error_scope(enumeration_warning(_,_,_,_,_)). |
464 | | |
465 | | abort_error_occured_in_error_scope :- |
466 | | event_occurred_in_error_scope(abort_error(_)). % is this still being generated?? |
467 | | abort_error_occured_in_error_scope :- |
468 | | event_occurred_in_error_scope(well_definedness_error). |
469 | | |
470 | | |
471 | | % just like above; but does not set up new error_scope (for performance) and clears enumeration warnings in case of failure and success |
472 | | time_out_with_enum_warning_one_solution_no_new_error_scope(Call,TO,TimeOutRes) :- |
473 | | (enumeration_warning_occured_in_error_scope |
474 | | -> %trace, |
475 | | format('Events in error block:~n',[]),portray_events_in_error_scope, |
476 | | add_internal_error('Error block already has enumeration warnings',time_out_with_enum_warning_one_solution_no_new_error_scope(Call,TO,TimeOutRes)) |
477 | | ; true), |
478 | | % Note: will call safe_time_out which will not call time_out when time_out_preference_disabled / disable_time_out |
479 | | catch( |
480 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,clear_errors,perform_cut), % clear errors so we can continue using same error scope |
481 | | enumeration_warning(A,B,C,D,E), |
482 | | %% (print(enum_warning),nl,TimeOutRes=time_out)). %% |
483 | | TimeOutRes=virtual_time_out(thrown_enumeration_warning(A,B,C,D,E))). |
484 | | |
485 | | |
486 | | % check if a result is a time_out: |
487 | | is_time_out_result(time_out). |
488 | | is_time_out_result(virtual_time_out(_)). |
489 | | |
490 | | % a version which is to be used not for a single call but for backtracking |
491 | | % generate virtual time_out at the end if enumeration warning occured |
492 | | |
493 | | time_out_with_enum_warning_for_findall(Call,TO,TimeOutRes) :- |
494 | | enter_new_error_scope(Level,time_out_with_enum_warning_for_findall), |
495 | ? | time_out_with_enum_warning_for_findall2(Level,Call,TO,TimeOutRes). |
496 | | |
497 | | time_out_with_enum_warning_for_findall2(_Level,Call,TO,TimeOutRes) :- %print(enter(_Level)),nl, |
498 | ? | catch( |
499 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,no_clear,no_cut), |
500 | | Exc, |
501 | | get_virtual_time_out_from_exception(Exc,TimeOutRes)). % virtual time_out |
502 | | time_out_with_enum_warning_for_findall2(Level,_Call,_TO,_TimeOutRes) :- % print(exit(Level)),nl, |
503 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)), |
504 | | exit_error_scope(Level,_,time_out_with_enum_warning_for_findall2),fail. |
505 | | |
506 | | % a version of time_out_with_enum_warning_for_findall where you manually have to enter_new_error_scope before |
507 | | % and exit_error_scope afterwards; useful if you repeatedly call this predicate and want to set up the block only once |
508 | | time_out_with_enum_warning_for_findall_in_current_error_scope(_Level,Call,TO,TimeOutRes) :- %print(enter(_Level)),nl, |
509 | ? | catch( |
510 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,no_clear,no_cut), |
511 | | Exc, |
512 | | get_virtual_time_out_from_exception(Exc,TimeOutRes)). % virtual time_out |
513 | | time_out_with_enum_warning_for_findall_in_current_error_scope(Level,_Call,_TO,_TimeOutRes) :- |
514 | | % does not exit level |
515 | | clear_events_in_error_scope(Level,enumeration_warning(_,_,_,_,_)),fail. |
516 | | |
517 | | % ------------ |
518 | | |
519 | | % see if an exception corresponds to a virtual_timeout in the form of an enumeration warning |
520 | | % also treats a CLPFD overflow as a virtual time-out in the sense that computation was aborted |
521 | | get_virtual_time_out_from_exception(enumeration_warning(Cause,_,_,_,_),TimeOutRes) :- !, |
522 | | get_virtual_time_out_res(Cause,TimeOutRes). |
523 | | get_virtual_time_out_from_exception(error(_ErrMsg,ErrTerm),TimeOutRes) :- |
524 | | is_clpfd_overflow_representation_error(ErrTerm,Cause),!, |
525 | | clpfd_overflow_msg(Msg), |
526 | | add_warning(clpfd_overflow,Msg), |
527 | | TimeOutRes = virtual_time_out(Cause). |
528 | | get_virtual_time_out_from_exception(Exc,_) :- % not detected as virtual time-out: throw again |
529 | | throw(Exc). |
530 | | |
531 | | get_virtual_time_out_res(kodkod_timeout,Res) :- !, Res= virtual_time_out(kodkod_timeout). |
532 | | get_virtual_time_out_res(_,virtual_time_out(exception)). |
533 | | |
534 | | |
535 | | is_clpfd_overflow_representation_error(representation_error(_Constraint,_,CauseMsg),CauseMsg) :- |
536 | | is_clpfd_overflow_representation_error_msg(CauseMsg). |
537 | | is_clpfd_overflow_representation_error_msg('CLPFD integer overflow'). |
538 | | is_clpfd_overflow_representation_error_msg('max_clpfd_integer'). |
539 | | is_clpfd_overflow_representation_error_msg('min_clpfd_integer'). |
540 | | |
541 | | clpfd_overflow_msg(M) :- clpfd_interface:is_64_bit_system, !, |
542 | | M = 'A CLPFD integer overflow occurred.\nSet CLPFD preference to FALSE (Animation Preferences).'. |
543 | | clpfd_overflow_msg('A CLPFD integer overflow occurred.\nSet CLPFD preference to FALSE (Animation Preferences) or use a 64 bit version of ProB.'). |
544 | | |
545 | | % ----------------- |
546 | | |
547 | | :- use_module(tools_meta,[safe_time_out/3]). |
548 | | % a version of time_out which upon failure checks if enumeration warnings were generated |
549 | | time_out_check_enum_warning_for_failure(Call,TO,TimeOutRes,ClearErrors,CutAfterFirstSol) :- |
550 | ? | safe_time_out(Call,TO,TimeOutRes), % a safe version which checks that time-out value TO is not too large |
551 | | %call(Call), TimeOutRes=ok, |
552 | | (TimeOutRes = time_out -> ! %, print(real_timeout),nl |
553 | | ; (ClearErrors=clear_errors -> clear_enumeration_warnings ; true), |
554 | | (CutAfterFirstSol=perform_cut -> ! ; true) |
555 | | %,print(sol),nl |
556 | | ). |
557 | | time_out_check_enum_warning_for_failure(_Call,_TO,TimeOutRes,ClearErrors,_) :- % print(failure),nl, |
558 | ? | virtual_time_out_occured_in_error_scope(TimeOutRes), |
559 | | (ClearErrors=clear_errors -> clear_enumeration_warnings ; true). |
560 | | |
561 | | virtual_time_out_occured_in_error_scope(TimeOutRes) :- |
562 | ? | event_occurred_in_error_scope(enumeration_warning(A,B,C,D,critical)), % print(virtual),nl, |
563 | | TimeOutRes = virtual_time_out(failure_enumeration_warning(A,B,C,D,critical)). % we have not investigated all possibilities; generate virtual time_out |
564 | | |
565 | | critical_enumeration_warning_occured_in_error_scope :- |
566 | ? | critical_enumeration_warning_occured_in_error_scope(_,_,_,_). |
567 | | critical_enumeration_warning_occured_in_error_scope(A,B,C,D) :- |
568 | ? | event_occurred_in_error_scope(enumeration_warning(A,B,C,D,critical)). |
569 | | |
570 | | clear_enumeration_warnings :- |
571 | | clear_events_in_current_error_scope(enumeration_warning(_,_,_,_,_)). |
572 | | |
573 | | % retract events and raise real warnings |
574 | | % Src and Msg used if these are not enumeration warnings |
575 | | translate_events_in_current_scope_to_warnings(Src,Msg) :- |
576 | | get_counter(cur_block_level,L), |
577 | ? | retract(specific_event_occurred_at_level(L,Event)), |
578 | | (translate_enumeration_warning(Event,EMsg,Span) |
579 | | -> add_warning(enumeration_warning,EMsg,'',Span) |
580 | | ; add_warning(Src,Msg,Event)), |
581 | | fail. |
582 | | translate_events_in_current_scope_to_warnings(_,_). |
583 | | |
584 | | % see also: print_enum_warning |
585 | | translate_enumeration_warning(enumeration_warning(Enumerating,Type,Range,RestrictedRange,_Throwing), Msg,Span) :- |
586 | | translate_enumeration_info(Enumerating,Info,Span), |
587 | | ajoin(['Unbounded enumeration of ',Info,':',Type,', narrowing range: ',Range,' ---> ',RestrictedRange],Msg). |
588 | | % Throwing=throwing -> print_pending_abort_info ? |
589 | | |
590 | | translate_enumeration_info(enumerating(Info),Res,Span) :- !, translate_enumeration_info(Info,Res,Span). % TO DO: call get_trigger_info |
591 | | translate_enumeration_info(b(identifier(Id),_,Info),Res,Span) :- !, Res=Id,Span=Info. |
592 | | translate_enumeration_info(I,I,I). |
593 | | |
594 | | % show all events in current error scope |
595 | | portray_events_in_error_scope :- event_occurred_in_error_scope(E), |
596 | | portray_event(E),fail. |
597 | | portray_events_in_error_scope. |
598 | | |
599 | | portray_event(enumeration_warning(Source,Type,Range,RestrictedRange,Critical)) :- |
600 | | translate_enumeration_info(Source,Res,_Span), !, |
601 | | format(' *** Enumeration Warning (~w): Source ~w, Type: ~w, Range reduction: ~w ---> ~w~n', |
602 | | [Critical, Res, Type, Range, RestrictedRange]). |
603 | | portray_event(E) :- format(' *** ~w~n',[E]). |
604 | | |
605 | | % ----------------------- |
606 | | |
607 | | :- volatile logged_error_hash/5, real_error_occurred/0, logged_error_spans/2, saved_subsidiary_span_pos/4. |
608 | | :- dynamic logged_error_hash/5. |
609 | | :- dynamic logged_message/4. |
610 | | :- dynamic real_error_occurred/0, warning_occurred/0. |
611 | | :- dynamic logged_error_spans/2. |
612 | | :- dynamic saved_subsidiary_span_pos/4. % for Tcl/Tk |
613 | | |
614 | | |
615 | | reset_real_error_occurred :- |
616 | | (retract(real_error_occurred) -> true ; true), |
617 | | (retract(warning_occurred) -> true ; true). |
618 | | |
619 | | reset_errors :- |
620 | | (reset_errors_but_not_scopes -> true |
621 | | ; format(user_error,'~n*** reset_errors_but_not_scopes failed!!~n',[]), |
622 | | fail % something really bad happened |
623 | | ), |
624 | | reset_error_limit_reached, |
625 | | reset_error_scopes. % call if you want to start again at a fresh top-level, without error scopes |
626 | | reset_errors_but_not_scopes :- |
627 | | reset_real_error_occurred, |
628 | | retractall(logged_error_hash(_,_,_,_,_)), |
629 | | retractall(logged_message(_,_,_,_)), |
630 | | reset_error_spans, |
631 | | clear_error_context. |
632 | | |
633 | | reset_error_span_if_required :- |
634 | ? | (logged_error_hash(_,_,_,_,_) -> true |
635 | | ; retractall(logged_error_spans(_,_))). |
636 | | |
637 | | reset_error_spans :- |
638 | | retractall(logged_error_spans(_,_)), |
639 | | retractall(saved_subsidiary_span_pos(_,_,_,_)). |
640 | | |
641 | | wd_error_occured :- |
642 | | test_error_occurred(well_definedness_error). |
643 | | |
644 | | clear_wd_errors :- |
645 | | clear_errors_with_source(well_definedness_error). |
646 | | clear_errors_with_source(Source) :- |
647 | | retractall(logged_error_spans(Source,_)), |
648 | | retractall(logged_error_hash(_,Source,_S1,_TContext1,_Span1)), |
649 | | retractall(logged_message(Source,_,_,_)), |
650 | | (logged_error_hash(_,_Source2,_S2,_TContext2,_Span2) -> true |
651 | | ; reset_real_error_occurred). |
652 | | |
653 | | print_error_span(Span) :- print_error_span(Span,error). |
654 | | print_error_span(unknown,_) :- !. |
655 | | print_error_span(none,_) :- !. % from Rodin |
656 | | print_error_span([],_) :- !. % empty info list |
657 | | print_error_span(Span,Kind) :- |
658 | | extract_span_description(Span,PosMsg), !, % also detects info lists |
659 | | print_error(Kind,PosMsg). |
660 | | print_error_span(b(E,T,I),Kind) :- |
661 | | translate_bexpr(b(E,T,I),S),!, |
662 | | print_error(Kind,S). |
663 | | print_error_span(span_predicate(P,_,_),Kind) :- translate_bexpr(P,S),!, |
664 | | % can happen in Event-B when no position info can be extracted |
665 | | print_error(Kind,S). |
666 | | print_error_span(List,Kind) :- List = [_|_], % info list which was not recognised; probably Event-B mode |
667 | | debug_mode(off), |
668 | | !, |
669 | | include(useful_info,List,UsefulList), % only show useful infos |
670 | | (UsefulList = [] -> true |
671 | | ; print_error(Kind,UsefulList)). |
672 | | print_error_span(Span,Kind) :- print_error(Kind,Span). % span not recognized ! |
673 | | |
674 | | useful_info(section(_)). |
675 | | useful_info(used_ids(_)). |
676 | | |
677 | | |
678 | | translate_bexpr(P,S) :- |
679 | | (debug_mode(on) -> Lim=3000 ; Lim=1000), |
680 | | translate:translate_subst_or_bexpr_with_limit(P,Lim,S). |
681 | | |
682 | | % the same, but not using user_error and red and only printing when span recognised |
683 | | % more detailed than translate:print_span |
684 | | print_message_span(unknown) :- !. |
685 | | print_message_span(Span) :- |
686 | | extract_span_description(Span,PosMsg), !, print(PosMsg). |
687 | | print_message_span(_). % span not recognized ! |
688 | | |
689 | | % try and extract a string (aka atom) describing the position |
690 | | extract_span_description(span_predicate(P,_,_),PosMsg) :- !, |
691 | | extract_span_description(P,PosMsg). |
692 | | extract_span_description(b(E,_,I),PosMsg) :- !, |
693 | | (extract_span_description(I,PosMsg) -> true |
694 | | ; first_sub_expr(E,A) -> extract_span_description(A,PosMsg)). |
695 | | extract_span_description(Span,PosMsg) :- |
696 | ? | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol),!, |
697 | | (empty_line_col(StartLine,StartCol,EndLine,EndCol) |
698 | | -> POS = ['Unknown position'|POS2] |
699 | | ; POS = ['Line: ',StartLine,' Column: ',StartCol, |
700 | | ' until Line: ',EndLine,' Column: ',EndCol|POS2] |
701 | | ), |
702 | | (extract_file_name(Span,Filename) |
703 | | -> POS2 = [' in file: ',Filename|POS3] ; POS2 = POS3), |
704 | ? | (extract_additional_description(Span,ContextMsg) |
705 | | -> POS3 = [' within ',ContextMsg] ; POS3=[]), |
706 | | ajoin(POS,PosMsg). |
707 | | extract_span_description(Span,PosMsg) :- |
708 | | extract_symbolic_label_pos(Span,Symbolic),!, |
709 | | % TO DO: we could try and find section for Rodin |
710 | | ajoin(['@label: ',Symbolic],PosMsg). |
711 | | extract_span_description(Span,PosMsg) :- |
712 | | % try if we have an unknown position with a known context (e.g., pos_context(unknown,call_stack(...)) |
713 | | extract_additional_description(Span,ContextMsg),!, |
714 | | %POS = ['Unknown position within ',ContextMsg], |
715 | | POS = ['Within ',ContextMsg], |
716 | | ajoin(POS,PosMsg). |
717 | | % Note: Atom Plugin is quite sensitive to above error message and capitalization |
718 | | % (https://github.com/wysiib/language-b-eventb) |
719 | | |
720 | | |
721 | | |
722 | | |
723 | | % detect if a position is empty; Line 0, Column -1 .. Line 0, Column -2 used for non available Library Definition files |
724 | | % happens, e.g., when typing CHOOSE({1}\{1}) in REPL |
725 | | empty_line_col(0,N1,0,N2) :- integer(N1), integer(N2), N1>N2. |
726 | | |
727 | | assert_true(Code) :- |
728 | | (call(Code) -> true ; add_internal_error('Assertion failed: ',Code)). |
729 | | |
730 | | add_error_fail(Source,ErrMsg, ErrTerm) :- add_error_and_fail(Source,ErrMsg, ErrTerm). |
731 | | add_error_and_fail(Source,ErrMsg, ErrTerm) :- add_error(Source,ErrMsg, ErrTerm),fail. |
732 | | add_error_and_fail(Source,ErrMsg) :- add_error(Source,ErrMsg),fail. |
733 | | |
734 | | % add_error/2, add_error/3 and add_error/4 |
735 | | add_error(Source,ErrMsg) :- add_error4(Source,ErrMsg, '',unknown). |
736 | | add_error(Source,ErrMsg, ErrTerm) :- add_error4(Source,ErrMsg, ErrTerm,unknown). |
737 | | add_error(Source,ErrMsg,ErrTerm,Span) :- add_error4(Source,ErrMsg,ErrTerm,Span). |
738 | | |
739 | | add_error4(Source,ErrMsg,ErrTerm,Span) :- |
740 | | assert_real_error_occurred(Source), |
741 | | combine_msg_and_error_term(ErrMsg,ErrTerm,Span,S), |
742 | | add_error_or_warning(Source,S,Span). |
743 | | |
744 | | %:- use_module(translate, [translate_error_term/2, translate_context/2]). |
745 | | combine_msg_and_error_term(Msg,ErrTerm,_,Res) :- ErrTerm=='', !, Res=Msg. |
746 | | combine_msg_and_error_term(ErrMsg,ErrTerm,Span,S) :- |
747 | | string_concatenate(ErrMsg,' ',ST), |
748 | | safe_translate_error_term(ErrTerm,Span,RString), |
749 | | string_concatenate(ST,RString,S). |
750 | | |
751 | | |
752 | | :- dynamic show_error_source/0. |
753 | | show_error_source. % True if error source and span to be printed |
754 | | turn_show_error_source_on :- (show_error_source -> true ; assertz(show_error_source)). |
755 | | turn_show_error_source_off :- retractall(show_error_source). |
756 | | |
757 | | trace_if_user_wants_it :- (preferences:get_preference(trace_upon_error,true) -> safe_trace ; true). |
758 | | |
759 | | % try Prolog's trace; if this fails (compiled code), then ask user for input. |
760 | | safe_trace :- translate:install_b_portray_hook, |
761 | | catch(trace, error(existence_error(_,_),_), type_to_continue). |
762 | | |
763 | | :- dynamic do_not_interrupt_user_anymore/0. |
764 | | |
765 | | type_to_continue :- do_not_interrupt_user_anymore,!. |
766 | | type_to_continue :- format(user_output,'(type <RETURN> to continue, z<RETURN> to zip) ===> ',[]), |
767 | | read_line(user_input,X), |
768 | | (zip(X) -> assertz(do_not_interrupt_user_anymore) ; true). |
769 | | |
770 | | zip([122|_]). %starts with z |
771 | | |
772 | | |
773 | | print_error_source(well_definedness_error) :- !,print_error('A well-definedness error occured !'). |
774 | | print_error_source(warning(Source)) :- show_error_source,!, |
775 | | format_error_with_nl('~n! A warning occurred (source: ~w) !',[Source]). |
776 | | print_error_source(internal_error(Source)) :- show_error_source,!, |
777 | | flush_output, % internal error should not happen; be sure to flush all other pending outputs so that we can see where it happened |
778 | | format_error_with_nl('~n! An internal error occurred (source: ~w) !',[Source]). |
779 | | print_error_source(Source) :- show_error_source,!, |
780 | | format_error_with_nl('~n! An error occurred (source: ~w) !',[Source]). |
781 | | print_error_source(_). |
782 | | |
783 | | print_error_context(Kind,Context) :- ((Context = '' ; \+show_error_source) -> true ; print_error(Kind,Context)). |
784 | | |
785 | | print_error(message,Term) :- !, print_message_on_stream(user_output,[blue],Term). |
786 | | print_error(_,Term) :- print_error(Term). |
787 | | |
788 | | safe_translate_error_term(ErrTerm,Span,RString) :- |
789 | | temporary_set_preference(expand_avl_upto,5,CHNG), |
790 | | translate:translate_error_term(ErrTerm,Span,RString), |
791 | | reset_temporary_preference(expand_avl_upto,CHNG). |
792 | | |
793 | | safe_translate_context(Result) :- |
794 | | % avoid infinite loop when error occurs inside translate_context |
795 | | current_context(Context), |
796 | | !, |
797 | | set_error_context(translate_context), |
798 | | temporary_set_preference(expand_avl_upto,4,CHNG), |
799 | | (translate:translate_context(Context,TContext) |
800 | | -> true ; TContext=Context), |
801 | | reset_temporary_preference(expand_avl_upto,CHNG), |
802 | | restore_error_context(Context), |
803 | | Result=TContext. |
804 | | safe_translate_context(''). % no context |
805 | | |
806 | | is_optional_error_or_warning(warning(Src)) :- !, optional_error_or_warning(Src). |
807 | | is_optional_error_or_warning(Src) :- optional_error_or_warning(Src). |
808 | | |
809 | | add_error_or_warning(Source,ErrMsg,Span) :- |
810 | | safe_translate_context(Context), |
811 | | reset_error_span_if_required, |
812 | | simplify_span(Span,SSpan), % for better hashing, assertz, retract |
813 | | assertz_logged_error_if_new(Source,ErrMsg,Context,SSpan,New), |
814 | | log_error_or_msg(Source,ErrMsg,SSpan), |
815 | | (New=false -> true |
816 | | ; is_optional_error_or_warning(Source) -> |
817 | | (extract_span_description(SSpan,PosMsg) -> true ; PosMsg=''), |
818 | | formatsilentmsg(user_output,'% Optional error ~w occured: ~w ~w~n',[Source,ErrMsg,PosMsg]), |
819 | | register_error_span(Source,SSpan,_) |
820 | | ; register_error_span(Source,SSpan,NewSrcSpan), |
821 | | (NewSrcSpan=false, |
822 | | error_limit_reached, |
823 | | Source \= internal_error(_) % always show internal errors |
824 | | -> true % this is not a new Source,SSpan combination and we have already several errors; |
825 | | ; print_error_source(Source), |
826 | | print_error(ErrMsg), |
827 | | print_error_context(error,Context), |
828 | | print_error_span(SSpan) |
829 | | ), |
830 | | trace_if_user_wants_it |
831 | | ). |
832 | | |
833 | | % optionally log errors in to a ndsjon logfile |
834 | | log_error_or_msg(Source,ErrMsg,Span) :- get_preference(error_log_file,LogFile), |
835 | | LogFile \= '', |
836 | | decompose_error_source(Source,Type,Src), |
837 | | !, |
838 | | ndjson_log(Type,Src,ErrMsg,Span,LogFile). |
839 | | log_error_or_msg(_,_,_). |
840 | | |
841 | | % log error, warning or message to NDJSON log file |
842 | | % Type is warning, error, info |
843 | | :- use_module(tools_io,[intelligent_open/4]). |
844 | | ndjson_log(Type,Src,ErrMsg,Span,LogFile) :- |
845 | | force_extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol), |
846 | | catch(intelligent_open(LogFile,append,S,[encoding(utf8)]), E, |
847 | | (format(user_error,'~n*** Cannot open error log file: ~w~n*** Exception: ~w~n',[LogFile,E]),fail)), |
848 | | !, |
849 | | ndjson_write(S,Type,ErrMsg,Src,Filename,StartLine,StartCol,EndLine,EndCol), |
850 | | (extract_additional_pos_and_descr(Span,SubPos,_Desc), |
851 | | extract_position_from_span(SubPos,Filename2,StartLine2,StartCol2,EndLine2,EndCol2), |
852 | | ndjson_write(S,Type,ErrMsg,Src,Filename2,StartLine2,StartCol2,EndLine2,EndCol2), |
853 | | fail |
854 | | ; |
855 | | true |
856 | | ), |
857 | | close(S). |
858 | | ndjson_log(_,_,_,_,_). |
859 | | |
860 | | % assign position to main file if we cannot extract the precise position |
861 | | force_extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
862 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol),!. |
863 | | force_extract_position_from_span(_Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
864 | | bmachine:b_get_main_filenumber(MainFN), |
865 | | get_filename(MainFN,Filename),!, |
866 | | % generate a virtual position, so that global errors without position are shown in Atom, VSCode,... |
867 | | StartLine=1,StartCol=1,EndLine=1,EndCol=1. |
868 | | force_extract_position_from_span(_Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
869 | | specfile:currently_opened_file_status(Filename,_), |
870 | | StartLine='-1',StartCol='-1',EndLine='-1',EndCol='-1'. |
871 | | |
872 | | %ndjson format http://ndjson.org |
873 | | % one JSON term per line |
874 | | :- use_module(tools_portability, [exists_source/1]). |
875 | | :- if(\+ exists_source(library(json))). |
876 | | % library(json) not available, e. g. SICStus before 4.5 or other Prolog systems |
877 | | ndjson_write(S,Type,ErrMsg,Src,Filename,StartLine,StartCol,EndLine,EndCol) :- |
878 | | EscFile=Filename, EscErrMsg=ErrMsg, |
879 | | format(S,'{"type" : "~w", "details": {"message": "~w", "type": "~w", "file": "~w", "start": {"line": ~w, "col": ~w}, "end": {"line": ~w, "col": ~w} }}~n', |
880 | | [Type,EscErrMsg,Src,EscFile,StartLine,StartCol,EndLine,EndCol]). |
881 | | :- else. |
882 | | :- use_module(library(json),[json_write/3]). |
883 | | ndjson_write(S,Type,ErrMsg,Src,Filename,StartLine,StartCol,EndLine,EndCol) :- |
884 | | version:version_str(Version), |
885 | | Term = json([type=Type, % Type is warning, error, info |
886 | | message=ErrMsg, |
887 | | reason=Src, |
888 | | file=Filename, |
889 | | start=json([line=StartLine,col=StartCol]), |
890 | | end=json([line=EndLine,col=EndCol]), |
891 | | version=Version |
892 | | ]), |
893 | | json_write(S,Term,[compact(true)]), nl(S). |
894 | | :- endif. |
895 | | |
896 | | decompose_error_source(warning(Source),warning,Res) :- !, Res=Source. |
897 | | decompose_error_source(internal_error(Source),internal_error,Res) :- !, Res=Source. |
898 | | decompose_error_source(message(Source),information,Res) :- !, Res=Source. % mainly for NDJSON |
899 | | decompose_error_source(Msg,error,Msg). |
900 | | |
901 | | |
902 | | % the total number of errors since start; no way to reset this; useful for validation reports |
903 | | % TO DO: maybe use counter extension; but tricky to get dependency and initialisation right |
904 | | get_total_number_of_errors(X) :- |
905 | | (bb_get(total_number_of_errors,R) -> X=R |
906 | | ; X=0). |
907 | | |
908 | | inc_total_number_of_errors :- |
909 | | (bb_get(total_number_of_errors,R) |
910 | | -> R1 is R+1, bb_put(total_number_of_errors,R1) |
911 | | ; bb_put(total_number_of_errors,1)). |
912 | | |
913 | | |
914 | | total_number_of_errors_at_last_reset(Res) :- |
915 | | (bb_get(total_number_of_errors_at_last_reset,R) -> Res=R ; Res=0). |
916 | | |
917 | | reset_error_limit_reached :- |
918 | | bb_put(error_limit_reached_printed,false), |
919 | | get_total_number_of_errors(T), |
920 | | bb_put(total_number_of_errors_at_last_reset,T). |
921 | | |
922 | | error_limit_reached :- bb_get(error_limit_reached_printed,true),!. |
923 | | error_limit_reached :- get_total_number_of_errors(Nr), Nr >= 100, |
924 | | total_number_of_errors_at_last_reset(Last), Delta is Nr-Last, Delta >= 100, |
925 | | format_error_with_nl('Error limit reached (total: ~w, delta: ~w); only printing new error source and locations!',[Nr,Delta]), |
926 | | bb_put(error_limit_reached_printed,true). |
927 | | |
928 | | assert_warning_occured :- |
929 | | (preferences:get_preference(strict_raise_warnings,true) |
930 | | -> assert_real_error_occurred(warning) |
931 | | ; warning_occurred -> true |
932 | | ; assertz(warning_occurred)). |
933 | | assert_real_error_occurred(Source) :- |
934 | | (real_error_occurred -> true ; assertz(real_error_occurred)), |
935 | | inc_total_number_of_errors, |
936 | | assert_real_error_in_current_block(Source). |
937 | | |
938 | | :- use_module(hashing,[term_depth_hash/3]). |
939 | | assertz_logged_error_if_new(Source,S,TContext,Span,New) :- |
940 | | % limit depth mainly for TContext/Span; avoiding generating many similar error messages with same msg |
941 | | % however, for WD errors often the msg varies (e.g. argument for function application); so this will not prevent a lot of errors |
942 | | term_depth_hash(logged_error_hash(Source,S,TContext,Span),4,Hash), |
943 | | (logged_error_hash(Hash,Source,S,TContext,Span) |
944 | | -> debug_println(repeated_error(Hash)),New=false |
945 | | ; %print(new(Hash,Source,S,TContext,Span)),nl, |
946 | | assertz(logged_error_hash(Hash,Source,S,TContext,Span)), |
947 | | New=true). |
948 | | |
949 | ? | logged_error(Source,S,TContext,Span) :- logged_error_hash(_Hash,Source,S,TContext,Span). |
950 | | |
951 | | add_warning(Source,ErrMsg, ErrTerm) :- |
952 | | add_warning(Source,ErrMsg, ErrTerm,unknown). |
953 | | add_warning(Source,ErrMsg, ErrTerm,Span) :- |
954 | | safe_translate_error_term(ErrTerm,Span,RString), |
955 | | string_concatenate(ErrMsg,RString,S), |
956 | | assert_warning_occured, |
957 | | add_error_or_warning(warning(Source),S,Span). |
958 | | add_warning(Source,Message) :- |
959 | | assert_warning_occured, |
960 | | add_error_or_warning(warning(Source),Message,unknown). |
961 | | |
962 | | % MESSAGES |
963 | | |
964 | | % add message in debug mode |
965 | | add_debug_message(Source,ErrMsg, ErrTerm) :- |
966 | | add_debug_message(Source,ErrMsg, ErrTerm,unknown). |
967 | | add_debug_message(Source,ErrMsg, ErrTerm, Span) :- |
968 | | debug_mode(on),!, |
969 | | add_message(Source,ErrMsg, ErrTerm, Span). |
970 | | add_debug_message(_,_,_,_). |
971 | | |
972 | | |
973 | | add_message(Source,ErrMsg, ErrTerm) :- /* same as add_error but does not add err msg */ |
974 | | add_message(Source,ErrMsg, ErrTerm,unknown). |
975 | | |
976 | | add_message(_,_, _,unknown) :- error_manager_silent_mode(on),!. |
977 | | add_message(Source,ErrMsg, ErrTerm,Span) :- /* same as add_error but does not add err msg */ |
978 | | safe_translate_error_term(ErrTerm,Span,RString), |
979 | | string_concatenate(ErrMsg,RString,S), |
980 | | add_message_direct(Source, S), |
981 | | safe_translate_context(Context), |
982 | | print_error_context(message,Context), |
983 | | print_error_span(Span,message), |
984 | | log_error_or_msg(message(Source),S,Span), |
985 | | assertz(logged_message(Source,S,Context,Span)). |
986 | | |
987 | | add_message(_,_) :- error_manager_silent_mode(on),!. |
988 | | add_message(Source,ErrMsg) :- add_message_direct(Source,ErrMsg). |
989 | | |
990 | | add_message_direct(Source,ErrMsg) :- |
991 | | (show_error_source -> format_with_colour(user_output,[blue],'! Message (source: ~w):~n',[Source]) ; true), |
992 | | print_error(message,ErrMsg). |
993 | | |
994 | | |
995 | | % adds a list of errors with position information |
996 | | % [error(Msg,Pos)] or [warning(Msg,Pos)...] |
997 | | no_real_perror_occurred([]). |
998 | | no_real_perror_occurred([warning(_,_)|T]) :- no_real_perror_occurred(T). |
999 | | |
1000 | | add_all_perrors(Errors) :- add_all_perrors(Errors,[],bmachine). |
1001 | | add_all_perrors(Errors,Files) :- add_all_perrors(Errors,Files,bmachine). |
1002 | | add_all_perrors(Errors,Files,Context) :- add_all_perrors(Errors,Files,Context,0,100). |
1003 | | |
1004 | | add_all_perrors([],_,_,_,_). |
1005 | | add_all_perrors([H|Rest],_Filenames,ContextOfError,NrOfErrors,MaxErrors) :- |
1006 | | NrOfErrors >= MaxErrors,!, |
1007 | | length([H|Rest],Len), |
1008 | | add_error(ContextOfError,'Reached maximium number of errors; number of errors not reported: ',Len). |
1009 | | add_all_perrors([Error|Rest],Filenames,ContextOfError,NrOfErrors,MaxErrors) :- |
1010 | | get_perror(Error,ErrOrWarn,Msg,Pos), |
1011 | | extract_perror_pos(Pos,Filenames,NewPos), |
1012 | | (ErrOrWarn == warning -> add_warning(ContextOfError,Msg,'',NewPos) |
1013 | | ; ErrOrWarn = internal_error -> add_internal_error(ContextOfError,Msg,'',NewPos) |
1014 | | ; add_error(ContextOfError,Msg,'',NewPos)), |
1015 | | N1 is NrOfErrors+1, |
1016 | | add_all_perrors(Rest,Filenames,ContextOfError,N1,MaxErrors). |
1017 | | |
1018 | | extract_perror_pos(Pos,Filenames,ResPOS) :- |
1019 | | get_position_row_cols(Pos,Srow,Scol,Erow,Ecol), |
1020 | | get_position_filenumber(Pos,Filenumber),!, |
1021 | | ( get_filename(Filenumber,Filenames,Filename) |
1022 | | %Ecol1 is Ecol+1, |
1023 | | -> ResPOS = src_span(Filenumber,Filename,Srow,Scol,Erow,Ecol,[],[]) |
1024 | | % ; Filenumber = -1, Filenames=[FN] -> % just a single filename, e.g., in VisB parsing |
1025 | | % % TODO: we need to adjust line and column numbers using a reference position |
1026 | | % POS = src_position_with_filename_and_ec(Srow,Scol,Erow,Ecol,FN) |
1027 | | ; ResPOS = src_span(Srow,Scol,Erow,Ecol,[],[]) |
1028 | | ). |
1029 | | extract_perror_pos(pos(Srow,Scol,Filename),Files,POS) :- !, % deprecated ? |
1030 | | adapt_null_file_name(Filename,Files,Filename2), |
1031 | | S1 is Scol+0, % zero width span |
1032 | | POS = src_position_with_filename_and_ec(Srow,Scol,Srow,S1,Filename2). |
1033 | | extract_perror_pos(pos(Srow,Scol,Erow,Ecol,Filename),Files,POS) :- !, |
1034 | | adapt_null_file_name(Filename,Files,Filename2), |
1035 | | %Ecol1 is Ecol+1, |
1036 | | POS = src_position_with_filename_and_ec(Srow,Scol,Erow,Ecol,Filename2). |
1037 | | extract_perror_pos(pos(Srow,Scol),_,POS) :- !, print(deprecated_pos(Srow,Scol)),nl, |
1038 | | POS = src_position(Srow,Scol,none,1). |
1039 | | extract_perror_pos(Pos,_,Pos). |
1040 | | |
1041 | | % adapt filename |
1042 | | adapt_null_file_name(null,[File],Res) :- |
1043 | | !, % the error is not associated to a given file, but we have a file list with just a single file; |
1044 | | % occurs e.g. when parsing VisB JSON files |
1045 | | Res=File. |
1046 | | adapt_null_file_name(F,_,F). |
1047 | | |
1048 | | get_perror(error(Msg,Pos),error,Msg,Pos) :- !. |
1049 | | get_perror(internal_error(Msg,Pos),internal_error,Msg,Pos) :- !. |
1050 | | get_perror(warning(Msg,Pos),warning,Msg,Pos) :- !. |
1051 | | get_perror(M,_,M,unknown) :- format(user_error,'*** unknown perror: ~w~n',[M]). |
1052 | | |
1053 | | % version without list of filenames |
1054 | | get_filename(Filenumber,Filename) :- |
1055 | | bmachine:b_get_all_used_filenames(FN), |
1056 | | get_filename(Filenumber,FN,Filename). |
1057 | | |
1058 | | get_filename(Filenumber,Filenames,Filename) :- |
1059 | | number(Filenumber),!, |
1060 | | nth1(Filenumber,Filenames,Filename). |
1061 | | get_filename(N,F,R) :- |
1062 | | add_internal_error('Cannot get filename: ',get_filename(N,F,R)), |
1063 | | N=1,F=[R|_]. % return first file if possible |
1064 | | |
1065 | | % check if error/warning occured and retract matching error/warning |
1066 | | get_error_with_span(Source,ErrMsg,Span) :- |
1067 | ? | retract(logged_error_hash(_Hash,Source,ErrMsg,_Context,Span)). |
1068 | | get_error(Source,ErrMsg) :- |
1069 | ? | retract(logged_error_hash(_,Source,ErrMsg,_,_)). |
1070 | | get_warning(Source,ErrMsg) :- |
1071 | | retract(logged_error_hash(_,warning(Source),ErrMsg,_,_)). |
1072 | | |
1073 | | % now the same without actually retracting the error/warning: |
1074 | ? | check_error_occured(Source,ErrMsg) :- logged_error(Source,ErrMsg,_,_). |
1075 | | check_warning_occured(Source,ErrMsg) :- logged_error(warning(Source),ErrMsg,_,_). |
1076 | | |
1077 | | get_next_error_or_warning(Msg) :- get_error_or_message(_Src,Type,Msg), |
1078 | | Type \= message. % retract message, but do not return it |
1079 | | get_error_or_message(Source,Type,Msg) :- |
1080 | | retract_logged_error_or_message(Source,Type,ErrMsg,Context,Span), |
1081 | | (extract_line_col(Span,StartLine,StartCol,EndLine,EndCol) |
1082 | | -> POS = ['\n ### Line: ',StartLine,', Column: ',StartCol|POS1], |
1083 | | ((StartLine,StartCol) \= (EndLine,EndCol) |
1084 | | -> (StartLine=EndLine -> POS1 = [' until ',EndCol|POS2] |
1085 | | ; POS1 = ['\n ### until Line: ',EndLine,', Column: ',EndCol|POS2] |
1086 | | ) |
1087 | | ; POS1 = POS2 |
1088 | | ), |
1089 | | (extract_additional_description(Span,CMsg) |
1090 | | -> POS2 = ['\n ### within: ',CMsg,'\n\n'] |
1091 | | ; POS2 = ['\n\n'] |
1092 | | ), |
1093 | | (extract_relative_file_name(Span,Filename) |
1094 | | -> PosMsg = ['\n ### File: ',Filename | POS] |
1095 | | ; PosMsg = POS |
1096 | | ), |
1097 | | tools:ajoin(PosMsg,EndStr) |
1098 | | ; EndStr = '\n\n' |
1099 | | ), |
1100 | | ((Context='' ; Context = '???') |
1101 | | -> string_concatenate(ErrMsg,EndStr,S0) |
1102 | | ; safe_translate_error_term(Context,Span,CString), |
1103 | | string_concatenate(CString,EndStr,CString2), |
1104 | | string_concatenate(': Context = ',CString2,CString3), |
1105 | | string_concatenate(ErrMsg,CString3,S0) |
1106 | | ), |
1107 | | (debug_mode(off) -> Msg = S0 |
1108 | | ; decompose_error_source(Source,_,SrcAtom), |
1109 | | string_concatenate(SrcAtom,': ',S1), |
1110 | | string_concatenate(S1,S0,Msg) |
1111 | | ). |
1112 | | |
1113 | | % retract an error |
1114 | | retract_logged_error_or_message(Source,Type,ErrMsg,Context,Span) :- Type \== message, |
1115 | | retract(logged_error_hash(_Hash,Source,ErrMsg,Context,Span)), |
1116 | | (Source = warning(_) -> Type=warning |
1117 | | ; Source = internal_error(_) -> Type=internal_error |
1118 | | ; Type=error). |
1119 | | retract_logged_error_or_message(message(Source),message,ErrMsg,Context,Span) :- |
1120 | | retract(logged_message(Source,ErrMsg,Context,Span)). |
1121 | | |
1122 | | |
1123 | | get_error_with_span_info(ErrMsg,Type,ErrLocations) :- |
1124 | | retract_logged_error_or_message(_Source,Type,ErrMsg,_Context,Span), |
1125 | | findall( error_span(Filename,StartLine,StartCol,EndLine,EndCol), |
1126 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol), |
1127 | | ErrLocations). |
1128 | | |
1129 | | extract_position_from_span(Span,Filename,StartLine,StartCol,EndLine,EndCol) :- |
1130 | | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol), |
1131 | | (extract_file_name(Span,FName) -> Filename=FName ; Filename=''). |
1132 | | |
1133 | | % Tcl/Tk interface of SICStus cannot deal with certain strange Unicode symbols |
1134 | | % see prob_examples/public_examples/B/Puzzles/LogicQuestion.mch where code 55349 appears |
1135 | | tk_escape(Atom,EAtom) :- atom(Atom),!, |
1136 | | atom_codes(Atom,C), |
1137 | | maplist(tk_escape_code,C,EC), |
1138 | | atom_codes(EAtom,EC). |
1139 | | tk_escape(list(L),list(EL)) :- !, maplist(tk_escape,L,EL). |
1140 | | tk_escape(X,X). |
1141 | | |
1142 | | tk_escape_code(StrangeUnicode,Res) :- |
1143 | | StrangeUnicode > 55000,!, % not sure what the limit should be here |
1144 | | Res = 9633. % white box |
1145 | | tk_escape_code(Code,Code). |
1146 | | |
1147 | | tcltk_get_all_errors(Limit,list(ERes),NrErrorsShown,NrErrors) :- |
1148 | | get_all_errors(All), |
1149 | | length(All,NrErrors), |
1150 | | (NrErrors =< Limit -> Res = All, NrErrorsShown = NrErrors |
1151 | | ; prefix_length(All,Res,Limit), % truncate |
1152 | | NrErrorsShown = Limit |
1153 | | ), |
1154 | | tk_escape(list(Res),list(ERes)). |
1155 | | |
1156 | | |
1157 | | get_all_errors(All) :- |
1158 | | findall(Err,get_next_error_or_warning(Err),All), |
1159 | | All \= [], |
1160 | | clear_error_context. |
1161 | | |
1162 | | get_all_errors_and_clear(All) :- |
1163 | | findall(Err,get_next_error_or_warning(Err),All), |
1164 | | reset_errors_but_not_scopes, |
1165 | | All \= []. |
1166 | | get_all_errors_and_reset(All) :- |
1167 | | findall(Err,get_next_error_or_warning(Err),All), |
1168 | | reset_errors, % fresh top-level |
1169 | | All \= []. |
1170 | | get_all_errors_with_span_info_and_reset(All) :- |
1171 | | findall(error(ErrMsg,ErrType,ErrLocations),get_error_with_span_info(ErrMsg,ErrType,ErrLocations),All), |
1172 | | reset_errors. |
1173 | | |
1174 | | error_or_warning_occured :- logged_error(_Source,_,_,_),!. |
1175 | ? | test_error_occurred(Source) :- logged_error(Source,_,_,_),real_error_occurred,!. |
1176 | | |
1177 | | %:- use_module(library(lists),[length/2]). length is builtin |
1178 | | |
1179 | | count_errors_occurred(Source,NrOfErrors) :- |
1180 | | findall(1,logged_error(Source,_,_,_),Ls), |
1181 | | length(Ls,NrOfErrors). |
1182 | | |
1183 | | display_error_statistics :- findall(Source,logged_error(Source,_,_,_),L), |
1184 | | count_occurences(L,Occ), |
1185 | | format('Error manager summary: ~w~n',[Occ]). |
1186 | | |
1187 | | |
1188 | | |
1189 | | |
1190 | | % ERROR CONTEXTS |
1191 | | % -------------- |
1192 | | |
1193 | | /* add context information; will be added to any error messages */ |
1194 | | % Note: Each Prolog module maintains its own blackboard for bb_get/bb_put |
1195 | | set_error_context(Ctx) :- bb_put(error_manager_context,Ctx). |
1196 | | |
1197 | | clear_error_context :- (bb_delete(error_manager_context,_) -> true ; true). |
1198 | | % retractall with dynamic predicate seems to slow down |
1199 | | |
1200 | | current_context(C) :- bb_get(error_manager_context,C). |
1201 | | |
1202 | | get_error_context(C) :- (current_context(C) -> true ; C='???'). |
1203 | | print_error_context :- (safe_translate_context(R), R\='' -> print_error(error,R) ; true). |
1204 | | |
1205 | | % restore an error context obtained by get_error_context |
1206 | | restore_error_context('???') :- !, clear_error_context. |
1207 | | restore_error_context(C) :- set_error_context(C). |
1208 | | |
1209 | | |
1210 | | % ----------------- |
1211 | | |
1212 | | get_error_span(Source,Span) :- |
1213 | | retract(logged_error_spans(Source,Span)). |
1214 | | |
1215 | | % a way for external modules to register error locations, e.g., for display by Tcl/Tk HighlightSyntaxErrors |
1216 | | register_error_span(Source,Span) :- register_error_span(Source,Span,_). |
1217 | | register_error_span(_,unknown,New) :- !,New=false. |
1218 | | register_error_span(Source,Span,New) :- logged_error_spans(Source,Span),!,New=false. |
1219 | | register_error_span(Source,Span,New) :- assertz(logged_error_spans(Source,Span)),New=true. |
1220 | | |
1221 | | get_error_span_linecol(Line,Col,EndLine,EndCol) :- get_error_span_linecol(_Source,Line,Col,EndLine,EndCol). |
1222 | | get_error_span_linecol(Source,Line,Col,EndLine,EndCol) :- |
1223 | | get_error_span(Source,Span), % looks at logged_error_spans and retracts |
1224 | | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1225 | | |
1226 | | % gets repeatedly called from Tcl/Tk to get all error locations |
1227 | | tk_get_error_span_linecol_for_main_file(Line,Col,EndLine,EndCol) :- |
1228 | | retract(saved_subsidiary_span_pos(Line,Col,EndLine,EndCol)). |
1229 | | tk_get_error_span_linecol_for_main_file(Line,Col,EndLine,EndCol) :- |
1230 | | get_error_span(_Source,Span), % looks at logged_error_spans and retracts even if not in main file ! |
1231 | | (extract_primary_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) |
1232 | | -> (extract_additional_pos_and_descr(Span,SubPos,_), |
1233 | | extract_line_col_for_main_file(SubPos,SLine,SCol,SEndLine,SEndCol), |
1234 | | assertz(saved_subsidiary_span_pos(SLine,SCol,SEndLine,SEndCol)), % store for later lookup |
1235 | | fail |
1236 | | ; true) |
1237 | | ; % try finding subsidiary position which is in main file: |
1238 | | extract_additional_pos_and_descr(Span,SubPos,_), |
1239 | | extract_line_col_for_main_file(SubPos,Line,Col,EndLine,EndCol) |
1240 | | ). |
1241 | | |
1242 | | |
1243 | | % just like get_error_span_linecol but does not retract and gets Filename |
1244 | | check_error_span_file_linecol(Source, FullFilename,Line,Col,EndLine,EndCol) :- |
1245 | ? | logged_error_spans(Source,Span), |
1246 | | (extract_file_line_col(Span,FullFilename,Line,Col,EndLine,EndCol) |
1247 | | ; |
1248 | ? | extract_additional_pos_and_descr(Span,SubPos,_), |
1249 | | extract_file_line_col(SubPos,FullFilename,Line,Col,EndLine,EndCol)). |
1250 | | |
1251 | | extract_file_line_col(Span,FullFilename,Line,Col,EndLine,EndCol) :- |
1252 | | (extract_file_number_and_name(Span,_,FullFilename) |
1253 | | -> true |
1254 | | ; FullFilename = unknown), % e.g. something like src_span/6; should we return unknown('?') |
1255 | ? | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1256 | | |
1257 | | % try and extract symbolic (Rodin label) information |
1258 | | extract_symbolic_label_pos(span_predicate(P,_,_),PosMsg) :- !, |
1259 | | extract_symbolic_label_pos(P,PosMsg). |
1260 | | extract_symbolic_label_pos(b(E,_,I),PosMsg) :- !, |
1261 | | (extract_symbolic_label_pos(I,PosMsg) -> true |
1262 | | ; first_sub_expr(E,A) -> extract_symbolic_label_pos(A,PosMsg)). |
1263 | | extract_symbolic_label_pos([],_) :- !,fail. |
1264 | | extract_symbolic_label_pos([H|T],SymbolicPos) :- !, |
1265 | | posinfo_member(N,[H|T]), |
1266 | | extract_symbolic_label_pos(N,SymbolicPos),!. |
1267 | | extract_symbolic_label_pos(rodinpos(Label,_),Label). % see also get_texpr_labels in bsyntaxtree |
1268 | | extract_symbolic_label_pos(rodinpos(Model,Name,_),Label) :- ajoin([Model,':',Name],Label). |
1269 | | extract_symbolic_label_pos(rodin_derived_context_pos(Model,Context,LabelName),Label) :- |
1270 | | ajoin([Model,'.',Context,':',LabelName],Label). |
1271 | | extract_symbolic_label_pos(label(Label),Label). |
1272 | | |
1273 | | % just extract label, without model |
1274 | | extract_symbolic_label(span_predicate(P,_,_),PosMsg) :- !, |
1275 | | extract_symbolic_label(P,PosMsg). |
1276 | | extract_symbolic_label(b(E,_,I),PosMsg) :- !, |
1277 | | (extract_symbolic_label(I,PosMsg) -> true |
1278 | | ; first_sub_expr(E,A) -> extract_symbolic_label(A,PosMsg)). |
1279 | | extract_symbolic_label([H|T],SymbolicPos) :- !, |
1280 | | posinfo_member(N,[H|T]), |
1281 | | extract_symbolic_label(N,SymbolicPos),!. |
1282 | | extract_symbolic_label(rodinpos(Label,_),Label) :- Label \= []. % see also get_texpr_labels in bsyntaxtree |
1283 | | extract_symbolic_label(rodinpos(_Model,Label,_),Label) :- Label \= []. |
1284 | | extract_symbolic_label(rodin_derived_context_pos(_Model,_Context,Label),Label) :- Label \= []. |
1285 | | extract_symbolic_label(label(Label),Label) :- Label \= []. |
1286 | | |
1287 | | % quickly try and simplify a span, e.g., before asserting it in a fact (as it may become big) |
1288 | | simplify_span(b(E,_,Infos),Res) :- !, |
1289 | ? | (definite_posinfo_member(N,Infos) -> Res=N |
1290 | | ; first_sub_expr(E,A) -> simplify_span(A,Res) |
1291 | | ; Res=unknown). |
1292 | | simplify_span(Infos,Res) :- Infos = [_|_],!, |
1293 | ? | (definite_posinfo_member(N,Infos) -> Res=N ; Res=unknown). |
1294 | | simplify_span(span_predicate(P,_,_),Res) :- !, simplify_span(P,Res). |
1295 | | simplify_span(S,S). |
1296 | | |
1297 | | % choose first sub-expression in case top-expression has no position info (it would be better if extract_info/3 in bsyntaxtree merges position info) |
1298 | | first_sub_expr(negation(A,_),A). |
1299 | | first_sub_expr(conjunct(A,_),A). |
1300 | | first_sub_expr(disjunct(A,_),A). |
1301 | | first_sub_expr(implication(A,_),A). |
1302 | | first_sub_expr(equivalence(A,_),A). |
1303 | | first_sub_expr(exists([A|_],_),A). |
1304 | | first_sub_expr(forall([A|_],_),A). |
1305 | | first_sub_expr(let_predicate([A|_],_,_),A). |
1306 | | first_sub_expr(let_expression([A|_],_,_),A). |
1307 | | first_sub_expr(let_expression_global([A|_],_,_),A). |
1308 | | first_sub_expr(sequence([A|_]),A). |
1309 | | first_sub_expr(parallel([A|_]),A). |
1310 | | first_sub_expr(assign([A|_],_),A). |
1311 | | first_sub_expr(var([A|_],_),A). |
1312 | | first_sub_expr(equal(A,_),A). |
1313 | | first_sub_expr(not_equal(A,_),A). |
1314 | | first_sub_expr(function(A,_),A). |
1315 | | first_sub_expr(less(A,_),A). |
1316 | | first_sub_expr(less_equal(A,_),A). |
1317 | | % TO DO: use bsyntaxtree:syntaxtraversion |
1318 | | |
1319 | | :- use_module(tools_positions,[get_position_filenumber/2, get_position_row_cols/5]). |
1320 | | |
1321 | | % extract_line_col(S,_,_,_,_) :- print(extract(S)),nl,fail. |
1322 | | extract_line_col(Var,_,_,_,_) :- var(Var),!, write(variable_span(Var)),nl, |
1323 | | fail. |
1324 | | extract_line_col([],_,_,_,_) :- !,fail. |
1325 | | extract_line_col([H|T],Srow,Scol,Erow,Ecol) :- !, |
1326 | | posinfo_member(N,[H|T]), |
1327 | | extract_line_col(N,Srow,Scol,Erow,Ecol),!. |
1328 | | extract_line_col(closure(_,_,B),Srow,Scol,Erow,Ecol) :- !, |
1329 | | extract_line_col(B,Srow,Scol,Erow,Ecol). |
1330 | | extract_line_col(b(E,_,Infos),Srow,Scol,Erow,Ecol) :- !, |
1331 | | (extract_line_col(Infos,Srow,Scol,Erow,Ecol) -> true |
1332 | | ; first_sub_expr(E,A) -> extract_line_col(A,Srow,Scol,Erow,Ecol)). |
1333 | | extract_line_col(span_predicate(P,_,_),Srow,Scol,Erow,Ecol) :- !, |
1334 | | extract_line_col(P,Srow,Scol,Erow,Ecol). |
1335 | | extract_line_col(PosTerm,Srow,S1,Erow,E1) :- |
1336 | | get_position_row_cols(PosTerm,Srow,Scol,Erow,Ecol), |
1337 | | number(Srow), |
1338 | | % _Filenumber is an index into list provided by b_get_all_used_filenames |
1339 | | number(Scol), number(Erow), number(Ecol), |
1340 | | !, |
1341 | | (Scol>0 -> S1 is Scol-1 ; S1 = Scol), % the BParser starts column numbering at 1 |
1342 | | (Ecol>0 -> E1 is Ecol-1 ; E1 = Ecol). |
1343 | | %E1=Ecol. %Somehow the Ecol info is ok for Tcl |
1344 | | extract_line_col(unknown,_,_,_,_) :- !,fail. |
1345 | | extract_line_col(pos_context(A,_,_),StartLine,StartCol,EndLine,EndCol) :- !, |
1346 | | extract_line_col(A,StartLine,StartCol,EndLine,EndCol). |
1347 | | extract_line_col(src_span(_Filenr,_Filename,StartLine,SC,EndLine,EC,Offset,LenBytes),StartLine,StartCol,EndLine,EndCol) |
1348 | | :- !, |
1349 | | extract_line_col(src_span(StartLine,SC,EndLine,EC,Offset,LenBytes),StartLine,StartCol,EndLine,EndCol). |
1350 | | extract_line_col(src_span(SL,SC,EL,EC,_Off,_LenBytes),StartLine,StartCol,EndLine,EndCol) :- !, |
1351 | | StartLine=SL, StartCol is SC-1, |
1352 | | EndLine = EL, EndCol is EC-1. |
1353 | | %:- !, StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine = StartLine. %% fix because new library generates erroneous EndLine/EndCol info |
1354 | | % EndCol is EC-1. |
1355 | ? | extract_line_col(span_info(_Info,Span),SL,SC,EL,EC) :- !, extract_line_col(Span,SL,SC,EL,EC). |
1356 | | extract_line_col(src_span_operator(SP1,SP2),SL,SC,EL,EC) :- !, |
1357 | | (extract_line_col(SP1,SL,SC,EL,EC) ; extract_line_col(SP2,SL,SC,EL,EC)). |
1358 | | extract_line_col(multi_span(SL,SC,EL,EC,_Off,_Len,TAIL),StartLine,StartCol,EndLine,EndCol) :- !, |
1359 | | % TO DO: replace by multi_span/2 |
1360 | | ( StartLine = SL, StartCol is SC-1, |
1361 | | EndLine = EL, EndCol is EC-1 |
1362 | | ; extract_line_col(TAIL,StartLine,StartCol,EndLine,EndCol)). |
1363 | | %extract_line_col(multi_span(A,B),StartLine,StartCol,EndLine,EndCol) :- !, |
1364 | | % (extract_line_col(A,StartLine,StartCol,EndLine,EndCol) ; |
1365 | | % extract_line_col(B,StartLine,StartCol,EndLine,EndCol)). |
1366 | | extract_line_col(src_position(StartLine,SC,_Off,LenBytes),StartLine,StartCol,EndLine,EndCol) :- !, |
1367 | | StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine is StartLine. |
1368 | | extract_line_col(src_position_with_filename(StartLine,SC,LenBytes,_Filename),StartLine,StartCol,EndLine,EndCol) :- |
1369 | | !, |
1370 | | StartCol is SC-1, EndCol is StartCol+LenBytes, EndLine is StartLine. |
1371 | | extract_line_col(src_position_with_filename_and_ec(SL,SC,EL,EC,_Fname),StartLine,StartCol,EndLine,EndCol) :- |
1372 | | !, |
1373 | | StartLine=SL,StartCol is SC-1,EndLine = EL, EndCol is EC-1. |
1374 | | extract_line_col(lineCol(StartLine,StartCol),StartLine,StartCol,EndLine,EndCol) :- !, |
1375 | | EndLine=StartLine, EndCol=StartCol. |
1376 | | extract_line_col(span_context(Span,_),StartLine,StartCol,EndLine,EndCol) :- !, |
1377 | | extract_line_col(Span,StartLine,StartCol,EndLine,EndCol). |
1378 | | extract_line_col(Pos,_,_,_,_) :- is_symbolic_position(Pos),!,fail. % cannot extract line/col infos |
1379 | | %extract_line_col(Struct,StartLine,StartCol,EndLine,EndCol) :- |
1380 | | % is_argument_of_compound_term(Struct,Arg), |
1381 | | % functor(Arg,FF,NN),functor(Struct,FS,NS), write(detected(FF,NN,in,FS,NS)),nl,trace, |
1382 | | % /* recursively look for error spans inside */ |
1383 | | % extract_line_col(Arg,StartLine,StartCol,EndLine,EndCol),!. |
1384 | | %extract_line_col(S,_,_,_,_) :- print('COULD NOT EXTRACT LINE/COL INFO: '), print(S), |
1385 | | % functor(S,F,N), print(' '),print(F/N),nl,nl,fail. |
1386 | | |
1387 | | %:- use_module(library(between),[between/3]). |
1388 | | %is_argument_of_compound_term(Term,Arg) :- functor(Term,_,Arity), |
1389 | | % between(1,Arity,ArgNr), arg(ArgNr,Term,Arg), |
1390 | | % % this is quite dangerous, and possibly expensive |
1391 | | % ground(Arg). |
1392 | | |
1393 | | is_symbolic_position(rodinpos(_,_)). |
1394 | | is_symbolic_position(rodinpos(_,_,_)). |
1395 | | is_symbolic_position(rodin_derived_context_pos(_,_,_)). |
1396 | | is_symbolic_position(label(_)). |
1397 | | |
1398 | | |
1399 | | /* For TCL TK: extract all line_col information in one go and in 4 lists */ |
1400 | | extract_all_line_col(Span, StartLineList,StartColList,EndLineList,EndColList) :- |
1401 | | findall(lc(SL,SC,EL,EC), extract_line_col(Span,SL,SC,EL,EC),All), |
1402 | | %print(all(All,Span)),nl, |
1403 | | split_lc(All,StartLineList,StartColList,EndLineList,EndColList). |
1404 | | |
1405 | | split_lc([], [],[],[],[]). |
1406 | | split_lc([lc(SL,SC,EL,EC)|T],[SL|T1],[SC|T2],[EL|T3],[EC|T4]) :- |
1407 | | split_lc(T,T1,T2,T3,T4). |
1408 | | |
1409 | | % TO DO: merge with line_col and in Tcl/Tk use info to highlight proper file ! |
1410 | | |
1411 | | |
1412 | | % TODO: maybe add a preference to always get full filename path (cf always_print_filename in translate) |
1413 | | % TODO: do not show filename if there is only one file ? |
1414 | | extract_relative_file_name(Pos,RelFilename) :- |
1415 | | extract_file_name(Pos,Filename), |
1416 | | (specfile:currently_opened_file_status(Main,_), |
1417 | | tools:get_parent_directory(Main,MainDir), |
1418 | | tools:gen_relative_path(Filename,MainDir,Rel) |
1419 | | -> RelFilename = Rel |
1420 | | ; RelFilename = Filename). |
1421 | | extract_file_name(Pos,Filename) :- |
1422 | | extract_valid_file_number_and_name(Pos,_,Filename), |
1423 | | Filename \=null. % Filename = null occurs in eval_strings !? |
1424 | | extract_tail_file_name(Pos,Filename) :- |
1425 | | extract_valid_file_number_and_name(Pos,_,FullFilename), |
1426 | | tools:get_tail_filename(FullFilename,Filename). |
1427 | | extract_subsidiary_tail_file_name(Pos,Filename) :- |
1428 | | extract_valid_file_number_and_name(Pos,Nr,FullFilename), |
1429 | | not_main_file(Nr,FullFilename), |
1430 | | tools:get_tail_filename(FullFilename,Filename). |
1431 | | |
1432 | | extract_valid_file_number_and_name(Pos,Nr,Filename) :- |
1433 | | extract_file_number_and_name(Pos,Nr,Filename), |
1434 | | valid_filename(Filename). |
1435 | | valid_filename(F) :- atom(F). % not unknown(-1) which can come from REPL |
1436 | | % should we detect null here? |
1437 | | |
1438 | | % a version of extract_line_col which fails if the primary error location is not in the main file |
1439 | | extract_primary_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) :- |
1440 | | \+((extract_file_number_and_name(Span,Nr,FullFilename), |
1441 | | not_main_file(Nr,FullFilename))), |
1442 | | extract_line_col(Span,Line,Col,EndLine,EndCol). |
1443 | | |
1444 | | % a version of extract_line_col which fails if the error or subsidiary location is not in the main file |
1445 | | extract_line_col_for_main_file(Span,Line,Col,EndLine,EndCol) :- |
1446 | | (extract_file_number_and_name(Span,Nr,FullFilename), |
1447 | | not_main_file(Nr,FullFilename) |
1448 | | -> extract_additional_pos_and_descr(Span,SubPos,_), |
1449 | | extract_line_col_for_main_file(SubPos,Line,Col,EndLine,EndCol) |
1450 | | ; extract_line_col(Span,Line,Col,EndLine,EndCol) |
1451 | | ). |
1452 | | |
1453 | | extract_file_number_and_name(V,_,_) :- var(V),!,fail. |
1454 | | extract_file_number_and_name(value(_),_,_) :- !,fail. |
1455 | | extract_file_number_and_name(pos_context(A,_,_), Filenumber,Filename) :- !, |
1456 | | extract_file_number_and_name(A, Filenumber,Filename). |
1457 | | extract_file_number_and_name(Pos, ResFileNr,Res) :- |
1458 | | get_position_filenumber(Pos,Filenumber),!, |
1459 | | (get_filename(Filenumber,Filename) |
1460 | | -> ResFileNr=Filenumber,Res=Filename |
1461 | | ; ResFileNr=Filenumber,Res=unknown(Filenumber) % unknown(-1) which can come from REPL |
1462 | | ). |
1463 | | extract_file_number_and_name(src_span(_,Filename,_,_,_,_,_,_), ResFileNr, Res) :- !,ResFileNr=unknown, Res=Filename. |
1464 | | extract_file_number_and_name(src_span(_,_,_, _,_,_), _ResFileNr, _Res) :- !,fail. % src_span/6, No file stored |
1465 | | extract_file_number_and_name(src_position_with_filename(_,_,_,Filename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=Filename. |
1466 | | extract_file_number_and_name(src_position_with_filename_and_ec(_,_,_,_,Filename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=Filename. |
1467 | | extract_file_number_and_name(section(RodinFilename), ResFileNr, Res) :- !,ResFileNr=unknown,Res=RodinFilename. |
1468 | | extract_file_number_and_name([H|T],Filenumber,Filename) :- !, |
1469 | | posinfo_member(Arg,[H|T]), |
1470 | | extract_file_number_and_name(Arg,Filenumber,Filename),!. |
1471 | | extract_file_number_and_name(closure(_,_,B),Filenumber,Filename) :- !, |
1472 | | extract_file_number_and_name(B,Filenumber,Filename). |
1473 | | extract_file_number_and_name(b(E,_,Infos),Filenumber,Filename) :- !, |
1474 | | (extract_file_number_and_name(Infos,Filenumber,Filename) -> true |
1475 | | ; first_sub_expr(E,A) -> extract_file_number_and_name(A,Filenumber,Filename)). |
1476 | | extract_file_number_and_name(span_context(Span,_),Filenumber,Filename) :- !, |
1477 | | extract_file_number_and_name(Span,Filenumber,Filename). |
1478 | | %extract_file_number_and_name(Struct,Filenumber,Filename) :- |
1479 | | % /* recursively look for error spans inside */ |
1480 | | % is_argument_of_compound_term(Struct,Arg), |
1481 | | % extract_file_number_and_name(Arg,Filenumber,Filename),!. |
1482 | | |
1483 | ? | definite_posinfo_member(POS,Args) :- member(nodeid(POS),Args). |
1484 | | posinfo_member(Arg,Args) :- % look for a nodeid(.) member |
1485 | ? | (member(nodeid(POS),Args) -> Arg=POS). % no longer allow other members in lists ; member(Arg,Args)). |
1486 | | |
1487 | | %:- use_module(bmachine,[b_get_all_used_filenames/1,b_get_main_filenumber/1]). |
1488 | | position_is_not_in_main_file(Pos) :- |
1489 | | get_position_filenumber(Pos,Filenumber),!, % B position info |
1490 | | not_main_filenumber(Filenumber). |
1491 | | position_is_not_in_main_file(src_span(_,Filename,_,_,_,_,_,_)) :- !, |
1492 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1493 | | position_is_not_in_main_file(src_position_with_filename(_,_,_,Filename)) :- !, |
1494 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1495 | | position_is_not_in_main_file(src_position_with_filename_ec(_,_,_,_,Filename)) :- !, |
1496 | | specfile:currently_opened_file_status(Main,_), Main \= Filename. |
1497 | | position_is_not_in_main_file(Struct) :- \+ atomic(Struct), Struct =.. [_|Args], |
1498 | | /* recursively look for error spans inside */ |
1499 | ? | member(Arg,Args), position_is_not_in_main_file(Arg),!. |
1500 | | |
1501 | | |
1502 | | not_main_file(Filenumber,_) :- number(Filenumber),!, % B Position info |
1503 | | not_main_filenumber(Filenumber). |
1504 | | not_main_file(_,Filename) :- % probably CSP position info; or unknown |
1505 | | specfile:currently_opened_file_status(Main,_), |
1506 | | \+ same_file_name(Main,Filename). %Main \= Filename. |
1507 | | |
1508 | | :- use_module(tools,[get_tail_filename/2]). |
1509 | | same_file_name(F1,F2) :- |
1510 | | (F1=F2 -> true |
1511 | | ; atom(F1), atom(F2), % we can have a non-atomic name like package(event_b_project) |
1512 | | get_tail_filename(F1,T1), |
1513 | | get_tail_filename(F2,T2), T1=T2). |
1514 | | |
1515 | | not_main_filenumber(Filenumber) :- |
1516 | | bmachine:b_get_main_filenumber(MainFN), |
1517 | | MainFN \= Filenumber. |
1518 | | |
1519 | | % --------------------------------- |
1520 | | |
1521 | | |
1522 | | |
1523 | | % find a sub-position matching a given target like context_pos |
1524 | | find_sub_position_match(Pos,Target) :- Target=Pos,!. |
1525 | | find_sub_position_match(b(E,_,Infos),Target) :- !, |
1526 | | (find_sub_position_match(Infos,Target) -> true |
1527 | | ; first_sub_expr(E,A),find_sub_position_match(A,Target)). |
1528 | | find_sub_position_match(span_predicate(P,_,_),Target) :- !, |
1529 | | find_sub_position_match(P,Target). |
1530 | | find_sub_position_match([H|T],Target) :- !, |
1531 | | posinfo_member(N,[H|T]), |
1532 | | find_sub_position_match(N,Target). |
1533 | | find_sub_position_match(closure(_,_,B),Target) :- !, |
1534 | | find_sub_position_match(B,Target). |
1535 | | |
1536 | | extract_pos_context(Span,MainPos,Context,CtxtPos) :- |
1537 | | find_sub_position_match(Span,pos_context(M,C,P)),!, |
1538 | | MainPos=M, Context=C, CtxtPos=P. |
1539 | | |
1540 | | % extract additional information about error span, e.g., Definition call context, other spans,... |
1541 | | extract_additional_description(Span,FullDesc) :- |
1542 | ? | extract_additional_pos_and_descr(Span,_Position,FullDesc). |
1543 | | |
1544 | | extract_additional_pos_and_descr(Span,CtxtPos,FullDesc) :- |
1545 | | extract_pos_context(Span,MainPos,Context,CtxtPos), |
1546 | | %nl,print(pos_context(MainPos,Context,CtxtPos)),nl, |
1547 | ? | extract_context_msg(Context,MainPos,CMsg), |
1548 | | (extract_span_description(CtxtPos,PosMsg) |
1549 | | -> ajoin([CMsg,' at ',PosMsg],FullDesc) |
1550 | | ; FullDesc=CMsg). % no position info available |
1551 | | |
1552 | | extract_context_msg(call_stack(Stack),MainPos,Msg) :- !, |
1553 | ? | translate:translate_call_stack([top_level_call(MainPos)|Stack],Msg). % dynamic call stack; we add subsidiary DEFINITION calls to the Call stack |
1554 | | % see result e.g., for public_examples/B/FeatureChecks/DEFINITIONS/DefCallStackDisplay.mch |
1555 | | % TODO: should we also add_inner_context_description for use-cases like Atom editor |
1556 | | extract_context_msg(definition_call(Name),MainPos,Msg) :- !, % static Definition macro expansion call stack |
1557 | | ajoin(['DEFINITION call of ',Name],OuterMsg), |
1558 | | add_inner_context_description(MainPos,OuterMsg,Msg). |
1559 | | extract_context_msg(C,MainPos,Msg) :- ajoin(['CONTEXT: ',C],OuterMsg), |
1560 | | add_inner_context_description(MainPos,OuterMsg,Msg). |
1561 | | |
1562 | | |
1563 | | add_inner_context_description(Span,OuterCMsg,FullDesc) :- |
1564 | | % used to have this check: Span = pos_context(MainPos,Context,CtxtPos), |
1565 | | extract_additional_pos_and_descr(Span,_Pos,InnerCMsg),!, |
1566 | | ajoin([InnerCMsg,' within ',OuterCMsg],FullDesc). |
1567 | | add_inner_context_description(_,FullDesc,FullDesc). |
1568 | | |
1569 | | |
1570 | | |
1571 | | % --------------------------------- |
1572 | | |
1573 | | % Get Source Position Info string for use in Tcl/Tk tables (column must be called 'Source'): |
1574 | | get_tk_table_position_info(Span,PosStr) :- |
1575 | | extract_line_col_for_main_file(Span,StartRow,StartCol,EndRow,EndCol), !, |
1576 | | ajoin([' at line ',StartRow,':',StartCol,' - ', EndRow, ':', EndCol],PosStr). % recognised by Tk Table viewer |
1577 | | get_tk_table_position_info(Span,PosStr) :- extract_span_description(Span,PosStr),!. |
1578 | | get_tk_table_position_info(_,''). |
1579 | | |
1580 | | |
1581 | | % --------------------------------- |
1582 | | |
1583 | | :- dynamic backed_up_error/4. |
1584 | | |
1585 | | % copy current error facts into backed_up_error facts |
1586 | | backup_errors :- |
1587 | | retractall(backed_up_error(_,_,_,_)), |
1588 | | logged_error(Source,S,TContext,Span), |
1589 | | assertz(backed_up_error(Source,S,TContext,Span)), |
1590 | | fail. |
1591 | | backup_errors. |
1592 | | |
1593 | | % --------------------------------- |
1594 | | |
1595 | | :- dynamic error_manager_silent_mode/1. |
1596 | | % separate from debug:silent_mode to avoid dependency to debug module |
1597 | | error_manager_silent_mode(off). |
1598 | | |
1599 | | set_error_manager_silent_mode(X) :- retractall(error_manager_silent_mode(_)), |
1600 | | assertz(error_manager_silent_mode(X)). |
1601 | | |
1602 | | |
1603 | | formatsilentmsg(Stream,FS,Args) :- error_manager_silent_mode(off) -> format(Stream,FS,Args) ; true. |
1604 | | |
1605 | | % ------------------------------------------- |
1606 | | |
1607 | | :- use_module(eventhandling,[register_event_listener/3]). |
1608 | | :- register_event_listener(reset_prob,reset_errors, |
1609 | | 'Reset Error Manager just like after starup_prob (except total_number_of_errors)'). |