In SLY, the recommended way to get column information for a token is to use its stored offset, find the offset of the last newline, and use that to compute the column.
While coding a scanner, I realized there is a simpler/cheaper way to get column information for each token.
If you instead store the offset of the start of a line at the same time you update the line number information, and copy that line start offset into each token together with the line number, the token has sufficient information to compute its offset by itself.
Abstracting this to "a position-information" object makes this even simpler. Instead of copying a line number, copy an object named position or something similar. Create a new Position object with line number and line start offset each time a newline is found, and done.
In SLY, the recommended way to get column information for a token is to use its stored offset, find the offset of the last newline, and use that to compute the column.
While coding a scanner, I realized there is a simpler/cheaper way to get column information for each token.
If you instead store the offset of the start of a line at the same time you update the line number information, and copy that line start offset into each token together with the line number, the token has sufficient information to compute its offset by itself.
Abstracting this to "a position-information" object makes this even simpler. Instead of copying a line number, copy an object named
position
or something similar. Create a newPosition
object with line number and line start offset each time a newline is found, and done.