node-sql-parser icon indicating copy to clipboard operation
node-sql-parser copied to clipboard

Add a way to find out the original range of characters for an AST node

Open pixelspark opened this issue 5 years ago • 3 comments

Describe the bug I have a use case where I would like to use node-sql-parser for syntax highlighting. For this, I would need to have access to the range of characters in the original string that correspond to parsed AST nodes. It looks like Peg.js makes this information available as a 'location' variable in the parser functions (see https://pegjs.org/documentation).

Adding this should therefore pretty easy. I am not that familiar with the node-sql-parser codebase, but would love to give it a shot if someone can give me some pointers on how this should be implemented. Expected behavior

For each AST node, have a start and end property indicating the character range in the parsed SQL that maps to the AST node.

pixelspark avatar May 22 '20 09:05 pixelspark

This would be really great to have. There are lots of applications for a parser where being able to reference the original source location is of crucial importance.

I'm personally investigating this library for use inside SQL Formatter. There are currently two main blockers for being able to use it:

  • no support for source locations (this issue)
  • no support for comments #525

The latter one I could work around, but for that I would need source locations, to associate locations of comments to their location in AST.

nene avatar Aug 03 '22 16:08 nene

@nene Sorry for the unresolved issues, I will give higher priority to #316, and support it asap.

taozhi8833998 avatar Aug 04 '22 00:08 taozhi8833998

@taozhi8833998 @pixelspark after merging #1680, version 5.0.0 now supports the includeLocations option, which adds a loc object to the last nodes. It's still partial and only in some dialects, but the implementation is easy enough so adding support is a no brainer. You can try it in the bigquery dialect

darzIdo avatar Mar 25 '24 05:03 darzIdo