Skip to content

Commit fcfa9fc

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 fcfa9fc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/globals.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,17 @@ 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+
}
1406+
int column = SOURCE->size - start_idx;
1407+
1408+
printf("[Error]: %s\nOccurs at source location %d:%d.\n%s\n", msg,
1409+
line, column, diagnostic);
14041410
abort();
14051411
}
14061412

0 commit comments

Comments
 (0)