Skip to content

Commit c33b078

Browse files
committed
Add line and column numbers to error diagnostics
The original error() function only showed the raw byte offset and a caret line, this change adds 1-based line and column numbers while leaving everything else unchanged. This is done by reusing start_idx and backtracking through SOURCE to count newlines.
1 parent 60dccd5 commit c33b078

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/globals.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,16 @@ void error(char *msg)
13961396

13971397
strcpy(diagnostic + i, "^ Error occurs here");
13981398

1399-
/* TODO: Implement line/column tracking for precise error location
1400-
* reporting. Current implementation only shows source position offset.
1401-
*/
1402-
printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg,
1403-
SOURCE->size, diagnostic);
1399+
/* Backtrack SOURCE to find line of position */
1400+
int line = 1;
1401+
for (i = 0; i < start_idx; i++) {
1402+
if (SOURCE->elements[i] == '\n')
1403+
line++;
1404+
}
1405+
int column = SOURCE->size - start_idx + 1;
1406+
1407+
printf("[Error]: %s\nOccurs at source location %d:%d.\n%s\n", msg, line,
1408+
column, diagnostic);
14041409
abort();
14051410
}
14061411

0 commit comments

Comments
 (0)