blynn / nex

Lexer for Go
http://cs.stanford.edu/~blynn/nex/
GNU General Public License v3.0
416 stars 47 forks source link

Size parameter should also be returned #49

Open purpleidea opened 6 years ago

purpleidea commented 6 years ago

Description

In the source there is func (yylex *Lexer) Line() int func (yylex *Lexer) Column() int. I think it would also be useful to have func (yylex *Lexer) Size() int which returns position in bytes relative to the start of the files.

Current approach

I'd like to lex and parse a number of different files together. The approach I'm attempting is the concatenate them together with a https://golang.org/pkg/io/#MultiWriter. I also store a list of cumulative file.Size() offsets for each file. I get this via Stat when opening each file for the MultiWriter.

At the end of lexing/parsing if I get an error, I work backwards from position of error to line number and file.

Problem

The problem is that I'm doing the "math" with line numbers instead of Size offsets. This means that I need to loop through each file (before lexing/parsing) and count all the newlines. If Size was available in addition to Column and Row, then this would be much more direct.

I haven't yet worked out how I'd get from the correct size offset to Column and Line, but I figure that's doable somehow.

Help

Help is appreciated if anyone can contribute. This currently works with line number alone, but as I mentioned a Size offset might be preferable. If there's an alternative technique for lexing/parsing multiple files together as one, please let me know!

Thanks!