taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
805 stars 180 forks source link

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

Open pixelspark opened 4 years ago

pixelspark commented 4 years ago

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.

nene commented 2 years ago

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:

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.

taozhi8833998 commented 2 years ago

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

darzIdo commented 7 months ago

@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