Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pixelspark opened this issue May 22, 2020 · 3 comments
Open
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@pixelspark
Copy link

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.

@taozhi8833998 taozhi8833998 added the enhancement New feature or request label May 22, 2020
@nene
Copy link
Contributor

nene commented Aug 3, 2022

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
Copy link
Owner

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

@darzIdo
Copy link
Contributor

darzIdo commented Mar 25, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants