Improve error linenums

This should fix the long-standing issue of having line numbers off-by-one in error messages because the scanner has already looked ahead and found the end-of-line.

The implementation is a bit of a hack, but seems to work well.