DerekStride / tree-sitter-sql

SQL grammar for tree-sitter
http://derek.stride.host/tree-sitter-sql/
MIT License
147 stars 47 forks source link

Improve partial query handling #261

Closed jonnyso closed 2 months ago

jonnyso commented 2 months ago

Its a common application for SQL to have arbitrary pieces of the script appear individually if the query is being built dynamically. Currently, I suspect that the parser is checking whether certain keywords appear in the right spot, i.e. the WHERE keyword can only appear after a SELECT .. FROM .. sequence and not at the start of the script, which would naturally not work properly when concatenating bits and pieces of the SQL together.

Is it possible to make it so that if a keyword appears in the beginning of the script it assumes that its probably in the right spot ? Or even better, detect somehow if its an injection, or a partial, rather than the whole statement ?

An example bellow of how it breaks syntax highlighting when I tried to apply the injection in Rust on Neovim.

image

matthias-Q commented 2 months ago

AFAIK partial parsing is not possible in the way you described it with treesitter.

Ping @DerekStride : Am I wrong here or is that correct?

DerekStride commented 2 months ago

Am I wrong here or is that correct?

That's correct, treesitter will try it's best to parse incorrect syntax but it's not perfect at it and unfortunately there's no good way to ensure it builds the best "partial" tree.

It will insert "error" nodes and attempt to recover (you can see this in the NOT IN (?, ?, ?) condition. The parser was able to recover and partially parse the IN keyword but it wasn't able to figure out what to do with the NOT. To make it better we could improve the parser algorithm in treesitter directly but I don't think it'd be an easy to make partial parsing much better than it currently is.