Skip to content

update grammar to allow preceding FROM#369

Merged
thomasp85 merged 6 commits intomainfrom
FROM-prefix
Apr 25, 2026
Merged

update grammar to allow preceding FROM#369
thomasp85 merged 6 commits intomainfrom
FROM-prefix

Conversation

@thomasp85
Copy link
Copy Markdown
Collaborator

This PR updates our grammar so that a preceding FROM is allowed in front of VISUALIZE.

This mirrors the syntactic sugar that DuckDB provides

@thomasp85 thomasp85 added this to the 0.3.0 milestone Apr 24, 2026
@thomasp85 thomasp85 requested a review from georgestagg April 24, 2026 11:39
Copy link
Copy Markdown
Collaborator

@georgestagg georgestagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modulo clippy, and some picky comment edit requests, LGTM!

Comment thread tree-sitter-ggsql/grammar.js Outdated
Comment thread tree-sitter-ggsql/grammar.js Outdated
Comment on lines +261 to +263
// Split into FROM + non_from_sql_keyword so other_sql_statement can use
// just the non-FROM variant for its first token (preventing it from
// eating `FROM t VISUALISE ...` which should parse as from_statement).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it, but I see no other way of doing it other than explicitly declaring FROM as a conflict using the tree-sitter conflicts property, which is probably the worse of the two solutions.

Parsing our SQL parts of the query as just a bag of tokens is technically wrong, and we pay for it here (and in that nasty regex above). But the alternative-alternative is to build out the grammar properly and truly parse SQL queries (for all of our dialects), and that's just not going to happen in the short term.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really tried with all my might to make it work with a declared conflict to no avail. It was either this or preprocess the sql query with some even more nasty code

Co-authored-by: George Stagg <georgestagg@gmail.com>
// F is excluded alongside W/S/C/I/U/D/V so the generic-word token can't
// absorb `FROM` — without this, tree-sitter tokenizes FROM as a plain
// word and other_sql_statement wins over from_statement.
const exclude_pattern = /[^\s;(),'"WwSsCcIiUuDdVvFf]+/;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remarking here that the grammar already gives all non-'other_sql_statement's precedence over other_sql_statement, so we wouldn't need the exclude_pattern.
I'm trying to throw it away in #364.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was def needed to get this to work but if you can do away with it then I'd be ecstatic

@thomasp85 thomasp85 merged commit e61463d into main Apr 25, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants