[ooc-compiler] Position of runtime error
Brought to you by:
mva
|
From: August K. <fus...@co...> - 2005-05-12 00:57:12
|
Hello all,
In runtime error messages, OOC outputs the position where the error
occurred but no info on line (or column) number, for instance:
##
## Runtime error in module Test at pos 171
## Dereference of NIL
##
It's kind of hard to find the position in an editor that lacks Emacs'
`goto-char' function and even for Emacs users it would be more
convenient with a line (and a column) number.
If the function _runtime_error in lib/src/RT0.c made a call to the
function GetLineAndColumn below the error message could be output as for
instance:
##
## Runtime error in module Test at pos 171 (line 11, column 3)
## Dereference of NIL
##
Comments?
Regards,
August
(I suspect that there may be some library function somewhere that does
the same thing as GetLineAndColumn)
/*
* GetLineAndColumn(f, p, &ln, &c) calculates line number ln and
* column number c corresponding to position p in file f .
*
* precondition: p > 0
*
* postcondition: ln > 0 and c >= 0
*/
static void GetLineAndColumn(const char *filename, int pos,
/*@out@*/ int *line, /*@out@*/ int *column)
{
FILE *f;
int i, c, ret;
assert(pos > 0);
f = fopen(filename, "r");
if (f == NULL) { perror(__func__); exit(EXIT_FAILURE); }
*line = 1;
*column = 0;
for (i = 1; i < pos; i++) {
c = fgetc(f);
if (ferror(f)) {
perror(__func__); exit(EXIT_FAILURE);
} else if (feof(f)) {
fprintf(stderr, "%s: Reached end of file\n", __func__);
exit(EXIT_FAILURE);
} else if (c == '\n') {
(*line)++; *column = 0;
} else {
(*column)++;
}
}
ret = fclose(f);
if (ret == EOF) { perror(__func__); exit(EXIT_FAILURE); }
assert((*line > 0) && (*column >= 0));
}
|