This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/pl/errors.yap

150 lines
3.8 KiB
Plaintext
Raw Normal View History

/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
2018-01-22 13:53:17 +00:00
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
2018-01-22 13:53:17 +00:00
* *
* File: errors.yap *
* comments: error messages for YAP *
* *
* *
* *
*************************************************************************/
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
/** @defgroup YAPErrorHandler Error Handling
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
@ingroup YAPErrors
2018-01-22 13:53:17 +00:00
The error handler is called when there is an execution error or a
warning needs to be displayed. The handlers include a number of hooks
to allow user-control.
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
Errors are terms of the form:
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
- error( domain_error( Domain, Culprit )`
- error( evaluation_error( Expression, Culprit )`
- error( existence_error( Object, Culprit )`
- error( instantiation_error )`
- error( permission_error( Error, Permission, Culprit)`
- error( representation_error( Domain, Culprit )`
- error( resource_error( Resource, Culprit )`
- error( syntax_error( Error )`
- error( system_error( Domain, Culprit )`
- error( type_error( Type, Culprit )`
- error( uninstantiation_error( Culprit )`
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
@{
2018-01-18 14:47:27 +00:00
*/
2018-01-22 13:53:17 +00:00
:- system_module( '$_errors', [system_error/2], ['$Error'/1,
'$do_error'/2,
system_error/3,
system_error/2]).
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
:- use_system_module( '$messages', [file_location/2,
generate_message/3,
translate_message/4]).
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
/**
* @pred system_error( +Error, +Cause)
*
* Generate a system error _Error_, informing the possible cause _Cause_.
*
*/
system_error(Type,Goal) :-
'$do_error'(Type,Goal).
'$do_error'(Type,Goal) :-
% format('~w~n', [Type]),
ancestor_location(Call, Caller),
throw(error(Type, [
[g|g(Goal)],
[p|Call],
[e|Caller]])).
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
/**
* @pred system_error( +Error, +Cause, +Culprit)
*
* Generate a system error _Error_, informing the source goal _Cause_ and a possible _Culprit_.
*
*
* ~~~~~~~~~~
* ~~~~~~~~~~
*
*
*/
system_error(Type,Goal,Culprit) :-
% format('~w~n', [Type]),
ancestor_location(Call, Caller),
throw(error(Type, [
[i|Culprit],
[g|g(Goal)],
[p|Call],
[e|Caller]])).
'$do_pi_error'(type_error(callable,Name/0),Message) :- !,
'$do_error'(type_error(callable,Name),Message).
'$do_pi_error'(Error,Message) :- !,
'$do_error'(Error,Message).
'$Error'(E) :-
'$LoopError'(E,top).
'$LoopError'(_, _) :-
flush_output(user_output),
flush_output(user_error),
2018-01-18 14:47:27 +00:00
fail.
2018-01-22 13:53:17 +00:00
'$LoopError'(Error, Level) :- !,
'$process_error'(Error, Level),
2018-01-18 14:47:27 +00:00
fail.
2018-01-22 13:53:17 +00:00
'$LoopError'(_, _) :-
flush_output,
'$close_error',
2018-01-18 14:47:27 +00:00
fail.
2018-01-22 13:53:17 +00:00
'$process_error'('$forward'(Msg), _) :-
2018-01-18 14:47:27 +00:00
!,
2018-01-22 13:53:17 +00:00
throw( '$forward'(Msg) ).
'$process_error'(abort, Level) :-
2017-03-23 12:28:46 +00:00
!,
2018-01-18 14:47:27 +00:00
(
2018-01-22 13:53:17 +00:00
Level \== top
2018-01-18 14:47:27 +00:00
->
2018-01-22 13:53:17 +00:00
throw( abort )
2018-01-18 14:47:27 +00:00
;
2018-01-22 13:53:17 +00:00
current_prolog_flag(break_level, 0)
2018-01-18 14:47:27 +00:00
->
2018-01-22 13:53:17 +00:00
print_message(informational,abort(user)),
fail
2018-01-18 14:47:27 +00:00
;
2018-01-22 13:53:17 +00:00
current_prolog_flag(break_level, I0),
I is I0-1,
current_prolog_flag(break_level, I),
throw(abort)
2018-01-18 14:47:27 +00:00
).
2018-01-22 13:53:17 +00:00
'$process_error'(error(thread_cancel(_Id), _G),top) :-
!.
'$process_error'(error(thread_cancel(Id), G), _) :-
!,
throw(error(thread_cancel(Id), G)).
'$process_error'(error(permission_error(module,redefined,A),B), Level) :-
Level \= top, !,
throw(error(permission_error(module,redefined,A),B)).
'$process_error'(Error, _Level) :-
functor(Error, Severity, _),
print_message(Severity, Error), !.
%'$process_error'(error(Msg, Where), _) :-
% Print_message(error,error(Msg, [g|Where])), !.
'$process_error'(Throw, _) :-
print_message(error,error(unhandled_exception,Throw)).
%% @}