rusty-ecma/RESSA

Feature Request: capture line and column number from parsing

azw413 opened this issue · 3 comments

Now that I'm getting into RESSA, I noticed an omission which could be useful which is to capture the line and column number during parsing. There are several use cases that would require this e.g.

Producing source maps from the parsed files,
Accurate error messages referring back to the source file, etc

One way of doing this might be to have a 'metadata' field on each AST node which can store arbitrary data where the parser could populate the source line and column during parsing. Similarly the writer could populate an output line and column during writing. Both of these things could then be used to create a source map. The metadata is also generally useful for 'decorating' nodes with data that can be used in later traversals etc.

Thanks for the feature request. I absolutely understand why this is desired, currently I have avoided this because calculating the line/column from a byte offset is quite expensive. That being said I have tried to continue to make this something easy to add, currently the Item from ress contains a Location which will provide the byte offset for the start and end. This could be used to calculate the line/column index as needed but would need some extra state in the parser to do so.

I can try and write up more about how this might be doable later this week.

This feature was included with the v0.8.0 release