I’m currently working on improving the error reporting for my language, of which a major part is printing the line containing the error (with the usual arrows to specify a particular span).
However, I’m unsure of which direction to go with in terms of actually getting that line, assuming I know the line number. I’ve considered the following approaches.
Store an array of the file split into lines. I used this previously with another interpreter and it worked very well, though I’m worried about it possibly taking up excessive memory for larger files.
Iterate/search through the file content string by jumping across newline terminators to find the n-th line, then making a view object across that line. This works well but searching may be quite slow for later line numbers.
Read the file line by line, stopping at the n-th line and using it to report the error. Likely the worst solution out of the 3 since it involves file I/O and rereads the file again. However, it would still be a straightforward solution.
Any advice or suggestions on this? How did others here approach this problem?