Re: Capturing error line on exception (SWI)



On 2005-06-30, Mauro Di Nuzzo <picorna@xxxxxxxxx> wrote:
> Hi,
>
> does anybody know how to get the error line of an error in swi prolog?
> Before trying to explain I give you an example:
>
> go :-
> ...,
> ensure_loaded('myfile.pl'),
> ...
>
> if an error occurs in 'myfile.pl' an error message is printed which tell me
> the error line no.
> But suppose I am currently use catch/3 or call_cleanup/3...

I'm not sure I get what you're after. There is no point putting a
catch/3 around ensure_loaded/1 to catch errors while loading the file.
As far as I know ensure_loaded/1 only throws an exception of the
filename argument is of the wrong type or the file cannot be opened.
Once the file is open the compilation loop itself uses catch/3 and
print_message/2 to catch and print error messages. This is done to
prevent the compiler from stopping after the first error. The line
number is part of the exception context (i.e. the 2nd argument of the
error/2 tuple).

You can intercept and act upon printing error messages by defining
message_hook/3. That might solve your problem.

> how can I get the error line number?
> I firstly tried to access the stream_property/2 (with $stream_position)
> using current_input/1, but I realized that the stream associated with
> 'myfile.pl' is suddenly closed after an error occurs (ie. it does no longer

And anyway, the Prolog compiler doesn't read from current input, but from
an explicitely passed input stream to avoid trouble with directives that
may modify the current input stream and do really weird things. You can
get the current stream through prolog_load_context/2.

> Does any way to get the error line number exist, when we consult external
> files?

I hope the above suffices. Otherwise elaborate a bit on what you exactly
want to do.

Cheers --- Jan
.



Relevant Pages

  • Re: Capturing error line on exception (SWI)
    ... > print_message/2 to catch and print error messages. ... > prevent the compiler from stopping after the first error. ... > an explicitely passed input stream to avoid trouble with directives that ... > may modify the current input stream and do really weird things. ...
    (comp.lang.prolog)
  • Re: Parsing mulitple files
    ... first flatten my input stream, but then error messages aren't very helpful. ... It depends somewhat on your language, ... Thus is bad government born! ...
    (comp.compilers.tools.javacc)
  • Re: FORTH: BEGIN as compile-time and no run-time?!?!?!?!? Help
    ... > of the compiler for your own purposes. ... Forth reads the input stream ... was 3 distinct entities, the inner interpreter, the outer interpreter ... When the compiler encounters an IMMEDIATE word it executes ...
    (comp.lang.asm.x86)
  • Parsing mulitple files
    ... The language I'm parsing has 'include' statements for files. ... first flatten my input stream (pre-process to insert include files), ... How can I switch between input files and have error messages state ...
    (comp.compilers.tools.javacc)