google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.3k stars 209 forks source link

syntax: support setting initial line/col for scanner #349

Closed alandonovan closed 3 years ago

alandonovan commented 3 years ago

The new FilePortion type, which may be provided to the scanner, parser, or ExecFile functions, combines a piece of text with its start line/column numbers, for applications that extract a Starlark expression from the middle of a larger file.

Fixes https://github.com/google/starlark-go/issues/346

alandonovan commented 3 years ago

Another approach would be to support #line filename:line:column comments in the scanner. That would be more general, but more complicated of course.

That's a good point, though my experience of line directives in several languages is that they create a great deal of complexity: some clients want the raw line numbers, others want the cooked. They also break the invariant that all the statements within a given file have the same filename, but I suppose that's fixable by making them line-and-column directives instead of file-and-line directives.