dabeaz / sly

Sly Lex Yacc
Other
817 stars 108 forks source link

Cheaper/simpler way to get column information out-of-the-box #48

Open alberth opened 4 years ago

alberth commented 4 years ago

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.

alberth commented 4 years ago

Affects #44