From d82f7b34db5e593fae8308f611ad5cc3f1d16e6e Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 25 Oct 2025 10:45:36 +0200 Subject: [PATCH 1/4] ok --- .../src/relevance/filtering.rs | 8 +- crates/pgt_hover/src/hovered_node.rs | 14 +- crates/pgt_treesitter/src/context/mod.rs | 6 +- crates/pgt_treesitter_grammar/grammar.js | 264 ++++++++++-------- 4 files changed, 159 insertions(+), 133 deletions(-) diff --git a/crates/pgt_completions/src/relevance/filtering.rs b/crates/pgt_completions/src/relevance/filtering.rs index e5935292c..916384ef8 100644 --- a/crates/pgt_completions/src/relevance/filtering.rs +++ b/crates/pgt_completions/src/relevance/filtering.rs @@ -69,8 +69,8 @@ impl CompletionFilter<'_> { // No autocompletions if there are two identifiers without a separator. if ctx.node_under_cursor.as_ref().is_some_and(|node| { node.prev_sibling().is_some_and(|p| { - (p.kind() == "identifier" || p.kind() == "object_reference") - && node.kind() == "identifier" + (p.kind() == "any_identifier" || p.kind() == "object_reference") + && node.kind() == "any_identifier" }) }) { return None; @@ -80,7 +80,7 @@ impl CompletionFilter<'_> { // `select * {}` if ctx.node_under_cursor.as_ref().is_some_and(|node| { node.prev_sibling() - .is_some_and(|p| (p.kind() == "all_fields") && node.kind() == "identifier") + .is_some_and(|p| (p.kind() == "all_fields") && node.kind() == "any_identifier") }) { return None; } @@ -254,7 +254,7 @@ impl CompletionFilter<'_> { WrappingClause::RevokeStatement | WrappingClause::GrantStatement => { ctx.matches_ancestor_history(&["role_specification"]) || ctx.node_under_cursor.as_ref().is_some_and(|k| { - k.kind() == "identifier" + k.kind() == "any_identifier" && ctx.before_cursor_matches_kind(&[ "keyword_grant", "keyword_revoke", diff --git a/crates/pgt_hover/src/hovered_node.rs b/crates/pgt_hover/src/hovered_node.rs index 7a7e388ac..bc13d07c1 100644 --- a/crates/pgt_hover/src/hovered_node.rs +++ b/crates/pgt_hover/src/hovered_node.rs @@ -32,7 +32,7 @@ impl HoveredNode { let under_cursor = ctx.node_under_cursor.as_ref()?; match under_cursor.kind() { - "identifier" + "any_identifier" if ctx.matches_ancestor_history(&["relation", "object_reference"]) || ctx .matches_ancestor_history(&["grantable_on_table", "object_reference"]) => @@ -52,7 +52,7 @@ impl HoveredNode { } } - "identifier" + "any_identifier" if ctx.matches_ancestor_history(&["object_reference"]) && ctx.wrapping_clause_type.as_ref().is_some_and(|clause| { matches!( @@ -73,7 +73,7 @@ impl HoveredNode { } } - "identifier" if ctx.matches_ancestor_history(&["field"]) => { + "any_identifier" if ctx.matches_ancestor_history(&["field"]) => { if let Some(table_or_alias) = ctx.schema_or_alias_name.as_ref() { Some(HoveredNode::Column(NodeIdentification::SchemaAndName(( table_or_alias.clone(), @@ -84,7 +84,9 @@ impl HoveredNode { } } - "identifier" if ctx.matches_ancestor_history(&["invocation", "object_reference"]) => { + "any_identifier" + if ctx.matches_ancestor_history(&["invocation", "object_reference"]) => + { if let Some(schema) = ctx.schema_or_alias_name.as_ref() { Some(HoveredNode::Function(NodeIdentification::SchemaAndName(( schema.clone(), @@ -97,7 +99,7 @@ impl HoveredNode { } } - "identifier" + "any_identifier" if ctx.matches_one_of_ancestors(&[ "alter_role", "policy_to_role", @@ -110,7 +112,7 @@ impl HoveredNode { Some(HoveredNode::Role(NodeIdentification::Name(node_content))) } - "identifier" + "any_identifier" if ( // hover over custom type in `create table` or `returns` (ctx.matches_ancestor_history(&["type", "object_reference"]) diff --git a/crates/pgt_treesitter/src/context/mod.rs b/crates/pgt_treesitter/src/context/mod.rs index e87984da4..e8017d223 100644 --- a/crates/pgt_treesitter/src/context/mod.rs +++ b/crates/pgt_treesitter/src/context/mod.rs @@ -620,7 +620,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > identifier` and the "identifier" is a leaf node, + /// If the tree shows `relation > object_reference > identifier` and the "any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_ancestor_history(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -642,7 +642,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > identifier` and the "identifier" is a leaf node, + /// If the tree shows `relation > object_reference > identifier` and the "any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -1138,7 +1138,7 @@ mod tests { identifier [40..43] '@id' @ [40..41] '@' - You can see that the '@' is a child of the "identifier" but has a range smaller than its parent's. + You can see that the '@' is a child of the "any_identifier" but has a range smaller than its parent's. This would crash our context parsing because, at position 42, we weren't at the leaf node but also couldn't go to a child on that position. */ diff --git a/crates/pgt_treesitter_grammar/grammar.js b/crates/pgt_treesitter_grammar/grammar.js index 07a135e52..23875fcd2 100644 --- a/crates/pgt_treesitter_grammar/grammar.js +++ b/crates/pgt_treesitter_grammar/grammar.js @@ -814,8 +814,8 @@ module.exports = grammar({ cte: ($) => seq( - $.identifier, - optional(paren_list(field("argument", $.identifier), false)), + $.any_identifier, + optional(paren_list(field("argument", $.any_identifier), false)), $.keyword_as, optional(seq(optional($.keyword_not), $.keyword_materialized)), wrapped_in_parenthesis( @@ -871,7 +871,7 @@ module.exports = grammar({ function_argument: ($) => seq( optional($._argmode), - optional($.identifier), + optional($.any_identifier), $.type, optional(seq(choice($.keyword_default, "="), $.literal)) ), @@ -887,7 +887,7 @@ module.exports = grammar({ seq($.keyword_column, alias($._qualified_field, $.object_reference)), // TODO: constraint (on domain) // TODO: conversion - seq($.keyword_database, $.identifier), + seq($.keyword_database, $.any_identifier), // TODO: domain seq($.keyword_extension, $.object_reference), // TODO: event trigger @@ -906,20 +906,25 @@ module.exports = grammar({ // TODO: (procedural) language // TODO: procedure // TODO: publication - seq($.keyword_role, $.identifier), + seq($.keyword_role, $.any_identifier), // TODO: routine // TODO: rule - seq($.keyword_schema, $.identifier), + seq($.keyword_schema, $.any_identifier), seq($.keyword_sequence, $.object_reference), // TODO: server // TODO: statistics // TODO: subscription seq($.keyword_table, $.object_reference), - seq($.keyword_tablespace, $.identifier), + seq($.keyword_tablespace, $.any_identifier), // TODO: text search (configuration|dictionary|parser|template) // TODO: transform for - seq($.keyword_trigger, $.identifier, $.keyword_on, $.object_reference), - seq($.keyword_type, $.identifier), + seq( + $.keyword_trigger, + $.any_identifier, + $.keyword_on, + $.object_reference + ), + seq($.keyword_type, $.any_identifier), seq($.keyword_view, $.object_reference) ), @@ -997,7 +1002,7 @@ module.exports = grammar({ seq( $.keyword_with, paren_list( - seq($.identifier, optional(seq("=", choice($.literal, $.array)))), + seq($.any_identifier, optional(seq("=", choice($.literal, $.array)))), true ) ), @@ -1031,7 +1036,7 @@ module.exports = grammar({ seq( $.keyword_create, $.keyword_policy, - $.identifier, + $.any_identifier, $.keyword_on, $.object_reference, optional( @@ -1055,13 +1060,13 @@ module.exports = grammar({ alter_policy: ($) => seq( - seq($.keyword_alter, $.keyword_policy, $.identifier), + seq($.keyword_alter, $.keyword_policy, $.any_identifier), optional( seq( $.keyword_on, $.object_reference, choice( - seq($.keyword_rename, $.keyword_to, $.identifier), + seq($.keyword_rename, $.keyword_to, $.any_identifier), $.policy_to_role, optional($.check_or_using_clause) ) @@ -1074,7 +1079,7 @@ module.exports = grammar({ $.keyword_to, comma_list( choice( - $.identifier, + $.any_identifier, $.keyword_public, $.keyword_current_user, $.keyword_current_role, @@ -1090,7 +1095,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_policy, optional($._if_exists), - $.identifier + $.any_identifier ), optional( seq( @@ -1153,7 +1158,7 @@ module.exports = grammar({ choice( $.literal, $.keyword_default, - $.identifier, + $.any_identifier, $.keyword_on, $.keyword_off ) @@ -1168,14 +1173,14 @@ module.exports = grammar({ seq( $.keyword_session, $.keyword_authorization, - choice($.identifier, $.keyword_default) + choice($.any_identifier, $.keyword_default) ), - seq($.keyword_role, choice($.identifier, $.keyword_none)) + seq($.keyword_role, choice($.any_identifier, $.keyword_none)) ) ), seq( $.keyword_constraints, - choice($.keyword_all, comma_list($.identifier, true)), + choice($.keyword_all, comma_list($.any_identifier, true)), choice($.keyword_deferred, $.keyword_immediate) ), seq($.keyword_transaction, $._transaction_mode), @@ -1202,7 +1207,7 @@ module.exports = grammar({ $.keyword_view, optional($._if_not_exists), $.object_reference, - optional(paren_list($.identifier, false)), + optional(paren_list($.any_identifier, false)), $.keyword_as, $.create_query, optional( @@ -1289,7 +1294,7 @@ module.exports = grammar({ function_declaration: ($) => seq( - $.identifier, + $.any_identifier, $.type, optional( seq( @@ -1349,7 +1354,7 @@ module.exports = grammar({ // regard to the defined language to match either sql, plsql or // plpgsql. Currently the function_body_statement support only sql. And // maybe for other language the function_body should be a string. - $.identifier + $.any_identifier ), function_volatility: ($) => @@ -1391,7 +1396,7 @@ module.exports = grammar({ _operator_class: ($) => seq( - field("opclass", $.identifier), + field("opclass", $.any_identifier), optional( field( "opclass_parameters", @@ -1407,7 +1412,7 @@ module.exports = grammar({ field("function", $.invocation), field("column", $._column) ), - optional(seq($.keyword_collate, $.identifier)), + optional(seq($.keyword_collate, $.any_identifier)), optional($._operator_class), optional($.direction), optional(seq($.keyword_nulls, choice($.keyword_first, $.keyword_last))) @@ -1453,21 +1458,21 @@ module.exports = grammar({ choice( seq( optional($._if_not_exists), - $.identifier, - optional(seq($.keyword_authorization, $.identifier)) + $.any_identifier, + optional(seq($.keyword_authorization, $.any_identifier)) ), - seq($.keyword_authorization, $.identifier) + seq($.keyword_authorization, $.any_identifier) ) ) ), _with_settings: ($) => seq( - field("name", $.identifier), + field("name", $.any_identifier), optional("="), field( "value", - choice($.identifier, alias($._single_quote_string, $.literal)) + choice($.any_identifier, alias($._single_quote_string, $.literal)) ) ), @@ -1476,7 +1481,7 @@ module.exports = grammar({ $.keyword_create, $.keyword_database, optional($._if_not_exists), - $.identifier, + $.any_identifier, optional($.keyword_with), repeat($._with_settings) ), @@ -1485,14 +1490,14 @@ module.exports = grammar({ seq( $.keyword_create, choice($.keyword_user, $.keyword_role, $.keyword_group), - $.identifier, + $.any_identifier, optional($.keyword_with), repeat(choice($._user_access_role_config, $._role_options)) ), _role_options: ($) => choice( - field("option", $.identifier), + field("option", $.any_identifier), seq( $.keyword_valid, $.keyword_until, @@ -1521,7 +1526,7 @@ module.exports = grammar({ $.keyword_admin, $.keyword_user ), - comma_list($.identifier, true) + comma_list($.any_identifier, true) ), create_sequence: ($) => @@ -1575,13 +1580,13 @@ module.exports = grammar({ $.keyword_create, $.keyword_extension, optional($._if_not_exists), - $.identifier, + $.any_identifier, optional($.keyword_with), - optional(seq($.keyword_schema, $.identifier)), + optional(seq($.keyword_schema, $.any_identifier)), optional( seq( $.keyword_version, - choice($.identifier, alias($._literal_string, $.literal)) + choice($.any_identifier, alias($._literal_string, $.literal)) ) ), optional($.keyword_cascade) @@ -1592,7 +1597,7 @@ module.exports = grammar({ $.keyword_create, optional($._or_replace), // mariadb - optional(seq($.keyword_definer, "=", $.identifier)), + optional(seq($.keyword_definer, "=", $.any_identifier)), optional($.keyword_constraint), // sqlite optional($._temporary), @@ -1623,7 +1628,7 @@ module.exports = grammar({ choice($.keyword_old, $.keyword_new), $.keyword_table, optional($.keyword_as), - $.identifier + $.any_identifier ), seq( $.keyword_for, @@ -1631,7 +1636,10 @@ module.exports = grammar({ choice($.keyword_row, $.keyword_statement), // mariadb optional( - seq(choice($.keyword_follows, $.keyword_precedes), $.identifier) + seq( + choice($.keyword_follows, $.keyword_precedes), + $.any_identifier + ) ) ), seq($.keyword_when, wrapped_in_parenthesis($._expression)) @@ -1648,7 +1656,7 @@ module.exports = grammar({ $.keyword_insert, seq( $.keyword_update, - optional(seq($.keyword_of, comma_list($.identifier, true))) + optional(seq($.keyword_of, comma_list($.any_identifier, true))) ), $.keyword_delete, $.keyword_truncate @@ -1665,7 +1673,7 @@ module.exports = grammar({ seq( $.keyword_as, $.column_definitions, - optional(seq($.keyword_collate, $.identifier)) + optional(seq($.keyword_collate, $.any_identifier)) ), seq($.keyword_as, $.keyword_enum, $.enum_elements), seq( @@ -1768,7 +1776,7 @@ module.exports = grammar({ seq( $.keyword_add, optional($.keyword_constraint), - $.identifier, + $.any_identifier, $.constraint ), @@ -1777,7 +1785,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_constraint, optional($._if_exists), - $.identifier, + $.any_identifier, optional($._drop_behavior) ), @@ -1786,7 +1794,7 @@ module.exports = grammar({ // TODO constraint management $.keyword_alter, optional($.keyword_column), - field("name", $.identifier), + field("name", $.any_identifier), choice( seq( choice($.keyword_set, $.keyword_drop), @@ -1838,7 +1846,7 @@ module.exports = grammar({ $.keyword_change, optional($.keyword_column), optional($._if_exists), - field("old_name", $.identifier), + field("old_name", $.any_identifier), $.column_definition, optional($.column_position) ), @@ -1846,7 +1854,7 @@ module.exports = grammar({ column_position: ($) => choice( $.keyword_first, - seq($.keyword_after, field("col_name", $.identifier)) + seq($.keyword_after, field("col_name", $.any_identifier)) ), drop_column: ($) => @@ -1854,16 +1862,16 @@ module.exports = grammar({ $.keyword_drop, optional($.keyword_column), optional($._if_exists), - field("name", $.identifier) + field("name", $.any_identifier) ), rename_column: ($) => seq( $.keyword_rename, optional($.keyword_column), - field("old_name", $.identifier), + field("old_name", $.any_identifier), $.keyword_to, - field("new_name", $.identifier) + field("new_name", $.any_identifier) ), alter_view: ($) => @@ -1885,17 +1893,17 @@ module.exports = grammar({ seq( $.keyword_alter, $.keyword_schema, - $.identifier, + $.any_identifier, choice($.keyword_rename, $.keyword_owner), $.keyword_to, - $.identifier + $.any_identifier ), alter_database: ($) => seq( $.keyword_alter, $.keyword_database, - $.identifier, + $.any_identifier, optional($.keyword_with), choice( seq($.rename_object), @@ -1904,12 +1912,15 @@ module.exports = grammar({ $.keyword_reset, choice( $.keyword_all, - field("configuration_parameter", $.identifier) + field("configuration_parameter", $.any_identifier) ) ), seq( $.keyword_set, - choice(seq($.keyword_tablespace, $.identifier), $.set_configuration) + choice( + seq($.keyword_tablespace, $.any_identifier), + $.set_configuration + ) ) ) ), @@ -1918,17 +1929,17 @@ module.exports = grammar({ seq( $.keyword_alter, choice($.keyword_role, $.keyword_group, $.keyword_user), - choice($.identifier, $.keyword_all), + choice($.any_identifier, $.keyword_all), choice( $.rename_object, seq(optional($.keyword_with), repeat($._role_options)), seq( - optional(seq($.keyword_in, $.keyword_database, $.identifier)), + optional(seq($.keyword_in, $.keyword_database, $.any_identifier)), choice( seq($.keyword_set, $.set_configuration), seq( $.keyword_reset, - choice($.keyword_all, field("option", $.identifier)) + choice($.keyword_all, field("option", $.any_identifier)) ) ) ) @@ -1937,13 +1948,13 @@ module.exports = grammar({ set_configuration: ($) => seq( - field("option", $.identifier), + field("option", $.any_identifier), choice( seq($.keyword_from, $.keyword_current), seq( choice($.keyword_to, "="), choice( - field("parameter", $.identifier), + field("parameter", $.any_identifier), $.literal, $.keyword_default ) @@ -1956,7 +1967,7 @@ module.exports = grammar({ $.keyword_alter, $.keyword_index, optional($._if_exists), - $.identifier, + $.any_identifier, choice( $.rename_object, seq( @@ -1967,13 +1978,13 @@ module.exports = grammar({ $.keyword_statistics, alias($._natural_number, $.literal) ), - seq($.keyword_reset, paren_list($.identifier, false)), + seq($.keyword_reset, paren_list($.any_identifier, false)), seq( $.keyword_set, choice( - seq($.keyword_tablespace, $.identifier), + seq($.keyword_tablespace, $.any_identifier), paren_list( - seq($.identifier, "=", field("value", $.literal)), + seq($.any_identifier, "=", field("value", $.literal)), false ) ) @@ -2028,7 +2039,7 @@ module.exports = grammar({ $.keyword_set, choice( choice($.keyword_logged, $.keyword_unlogged), - seq($.keyword_schema, $.identifier) + seq($.keyword_schema, $.any_identifier) ) ) ) @@ -2038,7 +2049,7 @@ module.exports = grammar({ seq( $.keyword_alter, $.keyword_type, - $.identifier, + $.any_identifier, choice( $.change_ownership, $.set_schema, @@ -2046,9 +2057,9 @@ module.exports = grammar({ seq( $.keyword_rename, $.keyword_attribute, - $.identifier, + $.any_identifier, $.keyword_to, - $.identifier, + $.any_identifier, optional($._drop_behavior) ), seq( @@ -2072,23 +2083,23 @@ module.exports = grammar({ ), seq( choice( - seq($.keyword_add, $.keyword_attribute, $.identifier, $.type), + seq($.keyword_add, $.keyword_attribute, $.any_identifier, $.type), seq( $.keyword_drop, $.keyword_attribute, optional($._if_exists), - $.identifier + $.any_identifier ), seq( $.keyword_alter, $.keyword_attribute, - $.identifier, + $.any_identifier, optional(seq($.keyword_set, $.keyword_data)), $.keyword_type, $.type ) ), - optional(seq($.keyword_collate, $.identifier)), + optional(seq($.keyword_collate, $.any_identifier)), optional($._drop_behavior) ) ) @@ -2136,7 +2147,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_schema, optional($._if_exists), - $.identifier, + $.any_identifier, optional($._drop_behavior) ), @@ -2145,7 +2156,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_database, optional($._if_exists), - $.identifier, + $.any_identifier, optional($.keyword_with), optional($.keyword_force) ), @@ -2155,7 +2166,7 @@ module.exports = grammar({ $.keyword_drop, choice($.keyword_group, $.keyword_role, $.keyword_user), optional($._if_exists), - $.identifier + $.any_identifier ), drop_type: ($) => @@ -2182,7 +2193,7 @@ module.exports = grammar({ $.keyword_index, optional($.keyword_concurrently), optional($._if_exists), - field("name", $.identifier), + field("name", $.any_identifier), optional($._drop_behavior), optional(seq($.keyword_on, $.object_reference)) ), @@ -2192,7 +2203,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_extension, optional($._if_exists), - comma_list($.identifier, true), + comma_list($.any_identifier, true), optional(choice($.keyword_cascade, $.keyword_restrict)) ), @@ -2209,9 +2220,10 @@ module.exports = grammar({ seq($.keyword_rename, $.keyword_to, $.object_reference), set_schema: ($) => - seq($.keyword_set, $.keyword_schema, field("schema", $.identifier)), + seq($.keyword_set, $.keyword_schema, field("schema", $.any_identifier)), - change_ownership: ($) => seq($.keyword_owner, $.keyword_to, $.identifier), + change_ownership: ($) => + seq($.keyword_owner, $.keyword_to, $.any_identifier), object_id: ($) => seq( @@ -2227,14 +2239,18 @@ module.exports = grammar({ object_reference: ($) => choice( seq( - field("database", $.identifier), + field("database", $.any_identifier), + ".", + field("schema", $.any_identifier), ".", - field("schema", $.identifier), + field("name", $.any_identifier) + ), + seq( + field("schema", $.any_identifier), ".", - field("name", $.identifier) + field("name", $.any_identifier) ), - seq(field("schema", $.identifier), ".", field("name", $.identifier)), - field("name", $.identifier) + field("name", $.any_identifier) ), _copy_statement: ($) => @@ -2270,7 +2286,7 @@ module.exports = grammar({ $.keyword_quote, $.keyword_encoding ), - alias($._literal_string, $.identifier) + alias($._literal_string, $.any_identifier) ), seq( choice( @@ -2307,7 +2323,7 @@ module.exports = grammar({ ), $.object_reference, optional($.table_partition), // Spark SQL - optional(seq($.keyword_as, field("alias", $.identifier))), + optional(seq($.keyword_as, field("alias", $.any_identifier))), // TODO we need a test for `insert...set` choice($._insert_values, $._set_values), optional(choice($._on_conflict, $._on_duplicate_key_update)) @@ -2346,7 +2362,8 @@ module.exports = grammar({ _set_values: ($) => seq($.keyword_set, comma_list($.assignment, true)), _column_list: ($) => paren_list(alias($._column, $.column), true), - _column: ($) => choice($.identifier, alias($._literal_string, $.literal)), + _column: ($) => + choice($.any_identifier, alias($._literal_string, $.literal)), _update_statement: ($) => seq($.update, optional($.returning)), @@ -2555,7 +2572,7 @@ module.exports = grammar({ ), table_sort: ($) => - seq($.keyword_sort, $.keyword_by, paren_list($.identifier, true)), + seq($.keyword_sort, $.keyword_by, paren_list($.any_identifier, true)), table_partition: ($) => seq( @@ -2572,7 +2589,7 @@ module.exports = grammar({ $.keyword_partition ), choice( - paren_list($.identifier, false), // postgres & Impala (CTAS) + paren_list($.any_identifier, false), // postgres & Impala (CTAS) $.column_definitions, // impala/hive external tables paren_list($._key_value_pair, true) // Spark SQL ) @@ -2580,7 +2597,7 @@ module.exports = grammar({ _key_value_pair: ($) => seq( - field("key", $.identifier), + field("key", $.any_identifier), "=", field("value", alias($._literal_string, $.literal)) ), @@ -2614,17 +2631,17 @@ module.exports = grammar({ $.keyword_default, $.keyword_character, $.keyword_set, - $.identifier + $.any_identifier ), - seq($.keyword_collate, $.identifier), + seq($.keyword_collate, $.any_identifier), field("name", $.keyword_default), seq( field( "name", - choice($.keyword_engine, $.identifier, $._literal_string) + choice($.keyword_engine, $.any_identifier, $._literal_string) ), "=", - field("value", choice($.identifier, $._literal_string)) + field("value", choice($.any_identifier, $._literal_string)) ) ), @@ -2653,7 +2670,7 @@ module.exports = grammar({ seq( $.keyword_references, $.object_reference, - paren_list($.identifier, true), + paren_list($.any_identifier, true), repeat( seq( $.keyword_on, @@ -2665,7 +2682,7 @@ module.exports = grammar({ seq( $.keyword_set, choice($.keyword_null, $.keyword_default), - optional(paren_list($.identifier, true)) + optional(paren_list($.any_identifier, true)) ) ) ) @@ -2722,7 +2739,7 @@ module.exports = grammar({ _constraint_literal: ($) => seq( $.keyword_constraint, - field("name", $.identifier), + field("name", $.any_identifier), choice(seq($._primary_key, $.ordered_columns), seq($._check_constraint)) ), @@ -2752,13 +2769,13 @@ module.exports = grammar({ ), $.keyword_index ), - optional(field("name", $.identifier)), + optional(field("name", $.any_identifier)), $.ordered_columns, optional( seq( $.keyword_references, $.object_reference, - paren_list($.identifier, true), + paren_list($.any_identifier, true), repeat( seq( $.keyword_on, @@ -2770,7 +2787,7 @@ module.exports = grammar({ seq( $.keyword_set, choice($.keyword_null, $.keyword_default), - optional(paren_list($.identifier, true)) + optional(paren_list($.any_identifier, true)) ) ) ) @@ -2817,10 +2834,10 @@ module.exports = grammar({ $.keyword_end ), - field: ($) => field("name", $.identifier), + field: ($) => field("name", $.any_identifier), _qualified_field: ($) => - seq(optional($.field_qualifier), field("name", $.identifier)), + seq(optional($.field_qualifier), field("name", $.any_identifier)), field_qualifier: ($) => seq(prec.left(optional_parenthesis($.object_reference)), "."), @@ -2889,7 +2906,7 @@ module.exports = grammar({ field( "start", choice( - $.identifier, + $.any_identifier, $.binary_expression, alias($._literal_string, $.literal), alias($._integer, $.literal) @@ -2902,7 +2919,7 @@ module.exports = grammar({ field( "end", choice( - $.identifier, + $.any_identifier, $.binary_expression, alias($._literal_string, $.literal), alias($._integer, $.literal) @@ -2937,7 +2954,12 @@ module.exports = grammar({ ), window_clause: ($) => - seq($.keyword_window, $.identifier, $.keyword_as, $.window_specification), + seq( + $.keyword_window, + $.any_identifier, + $.keyword_as, + $.window_specification + ), window_specification: ($) => wrapped_in_parenthesis( @@ -2952,10 +2974,11 @@ module.exports = grammar({ seq( $.invocation, $.keyword_over, - choice($.identifier, $.window_specification) + choice($.any_identifier, $.window_specification) ), - _alias: ($) => seq(optional($.keyword_as), field("alias", $.identifier)), + _alias: ($) => + seq(optional($.keyword_as), field("alias", $.any_identifier)), from: ($) => seq( @@ -2994,7 +3017,7 @@ module.exports = grammar({ choice($.keyword_force, $.keyword_use, $.keyword_ignore), $.keyword_index, optional(seq($.keyword_for, $.keyword_join)), - wrapped_in_parenthesis(field("index_name", $.identifier)) + wrapped_in_parenthesis(field("index_name", $.any_identifier)) ), join: ($) => @@ -3034,8 +3057,8 @@ module.exports = grammar({ optional( seq( $.keyword_as, - field("alias", $.identifier), - paren_list($.identifier, false) + field("alias", $.any_identifier), + paren_list($.any_identifier, false) ) ) ) @@ -3058,8 +3081,8 @@ module.exports = grammar({ choice($.invocation, $.subquery), optional( choice( - seq($.keyword_as, field("alias", $.identifier)), - field("alias", $.identifier) + seq($.keyword_as, field("alias", $.any_identifier)), + field("alias", $.any_identifier) ) ), $.keyword_on, @@ -3074,8 +3097,8 @@ module.exports = grammar({ choice($.invocation, $.subquery), optional( choice( - seq($.keyword_as, field("alias", $.identifier)), - field("alias", $.identifier) + seq($.keyword_as, field("alias", $.any_identifier)), + field("alias", $.any_identifier) ) ) ), @@ -3162,13 +3185,13 @@ module.exports = grammar({ $.grantable_on_all ) ), - $.identifier + $.any_identifier ), grantable_targets: ($) => choice( - seq($._grantable, comma_list($.identifier, false)), - comma_list($.identifier, true) + seq($._grantable, comma_list($.any_identifier, false)), + comma_list($.any_identifier, true) ), _grantable: ($) => @@ -3225,12 +3248,12 @@ module.exports = grammar({ ), $.keyword_in, $.keyword_schema, - comma_list($.identifier, true) + comma_list($.any_identifier, true) ), role_specification: ($) => choice( - seq(optional($.keyword_group), $.identifier), + seq(optional($.keyword_group), $.any_identifier), $.keyword_public, $.keyword_current_role, $.keyword_current_user, @@ -3503,11 +3526,12 @@ module.exports = grammar({ ), _bit_string: ($) => seq(/[bBxX]'([^']|'')*'/, repeat(/'([^']|'')*'/)), // The identifier should be followed by a string (no parenthesis allowed) - _string_casting: ($) => seq($.identifier, $._single_quote_string), + _string_casting: ($) => seq($.any_identifier, $._single_quote_string), bang: (_) => "!", - identifier: ($) => + any_identifier: ($) => $._any_identifier, + _any_identifier: ($) => choice( $._identifier, $._double_quote_string, From ce2f679fa90eb23857fcba2c01e9d1238fdadcf1 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 25 Oct 2025 10:50:45 +0200 Subject: [PATCH 2/4] refactor: rename to any_identifier --- crates/pgt_treesitter/src/context/mod.rs | 14 +++++++------- crates/pgt_treesitter/src/queries/parameters.rs | 2 +- crates/pgt_treesitter/src/queries/relations.rs | 12 ++++++------ .../pgt_treesitter/src/queries/select_columns.rs | 2 +- crates/pgt_treesitter/src/queries/table_aliases.rs | 6 +++--- crates/pgt_treesitter/src/queries/where_columns.rs | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/pgt_treesitter/src/context/mod.rs b/crates/pgt_treesitter/src/context/mod.rs index e8017d223..09fe45517 100644 --- a/crates/pgt_treesitter/src/context/mod.rs +++ b/crates/pgt_treesitter/src/context/mod.rs @@ -620,7 +620,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > identifier` and the "any_identifier" is a leaf node, + /// If the tree shows `relation > object_reference > any_any_identifier` and the "any_any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_ancestor_history(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -642,7 +642,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > identifier` and the "any_identifier" is a leaf node, + /// If the tree shows `relation > object_reference > any_any_identifier` and the "any_any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -663,9 +663,9 @@ impl<'a> TreesitterContext<'a> { /// * keyword_from [9..13] 'from' /// * relation [14..28] '"auth"."users"' /// * object_reference [14..28] '"auth"."users"' - /// * identifier [14..20] '"auth"' + /// * any_any_identifier [14..20] '"auth"' /// * . [20..21] '.' - /// * identifier [21..28] '"users"' + /// * any_any_identifier [21..28] '"users"' /// */ /// /// if node_under_cursor_is_nth_child(1) { @@ -1132,13 +1132,13 @@ mod tests { keyword_where [29..34] 'where' binary_expression [35..43] 'id = @id' field [35..37] 'id' - identifier [35..37] 'id' + any_identifier [35..37] 'id' = [38..39] '=' field [40..43] '@id' - identifier [40..43] '@id' + any_identifier [40..43] '@id' @ [40..41] '@' - You can see that the '@' is a child of the "any_identifier" but has a range smaller than its parent's. + You can see that the '@' is a child of the "any_any_identifier" but has a range smaller than its parent's. This would crash our context parsing because, at position 42, we weren't at the leaf node but also couldn't go to a child on that position. */ diff --git a/crates/pgt_treesitter/src/queries/parameters.rs b/crates/pgt_treesitter/src/queries/parameters.rs index a137cc8de..fdfb98a56 100644 --- a/crates/pgt_treesitter/src/queries/parameters.rs +++ b/crates/pgt_treesitter/src/queries/parameters.rs @@ -11,7 +11,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { [ (field (field_qualifier)? - (identifier) + (any_identifier) ) @reference (parameter) @parameter diff --git a/crates/pgt_treesitter/src/queries/relations.rs b/crates/pgt_treesitter/src/queries/relations.rs index 74a51dcbd..4f94d677c 100644 --- a/crates/pgt_treesitter/src/queries/relations.rs +++ b/crates/pgt_treesitter/src/queries/relations.rs @@ -11,17 +11,17 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (relation (object_reference . - (identifier) @schema_or_table + (any_identifier) @schema_or_table "."? - (identifier)? @table + (any_identifier)? @table )+ ) (insert (object_reference . - (identifier) @schema_or_table + (any_identifier) @schema_or_table "."? - (identifier)? @table + (any_identifier)? @table )+ ) (alter_table @@ -29,9 +29,9 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (keyword_table) (object_reference . - (identifier) @schema_or_table + (any_identifier) @schema_or_table "."? - (identifier)? @table + (any_identifier)? @table )+ ) "#; diff --git a/crates/pgt_treesitter/src/queries/select_columns.rs b/crates/pgt_treesitter/src/queries/select_columns.rs index d8fa1d16a..5b8ee6dfa 100644 --- a/crates/pgt_treesitter/src/queries/select_columns.rs +++ b/crates/pgt_treesitter/src/queries/select_columns.rs @@ -14,7 +14,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (object_reference) @alias "." )? - (identifier) @column + (any_identifier) @column ) ) ","? diff --git a/crates/pgt_treesitter/src/queries/table_aliases.rs b/crates/pgt_treesitter/src/queries/table_aliases.rs index 9d771bf71..6c39b2e5e 100644 --- a/crates/pgt_treesitter/src/queries/table_aliases.rs +++ b/crates/pgt_treesitter/src/queries/table_aliases.rs @@ -10,12 +10,12 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (relation (object_reference . - (identifier) @schema_or_table + (any_identifier) @schema_or_table "."? - (identifier)? @table + (any_identifier)? @table ) (keyword_as)? - (identifier) @alias + (any_identifier) @alias ) "#; tree_sitter::Query::new(&pgt_treesitter_grammar::LANGUAGE.into(), QUERY_STR) diff --git a/crates/pgt_treesitter/src/queries/where_columns.rs b/crates/pgt_treesitter/src/queries/where_columns.rs index b3371518c..62e9e0a5c 100644 --- a/crates/pgt_treesitter/src/queries/where_columns.rs +++ b/crates/pgt_treesitter/src/queries/where_columns.rs @@ -16,7 +16,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (object_reference) @alias "." )? - (identifier) @column + (any_identifier) @column ) ) ) From 2eca24d6471233dca2904030a21263dd47e277f5 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 25 Oct 2025 10:55:41 +0200 Subject: [PATCH 3/4] ups --- crates/pgt_treesitter/src/context/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/pgt_treesitter/src/context/mod.rs b/crates/pgt_treesitter/src/context/mod.rs index 09fe45517..381c06b62 100644 --- a/crates/pgt_treesitter/src/context/mod.rs +++ b/crates/pgt_treesitter/src/context/mod.rs @@ -620,7 +620,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > any_any_identifier` and the "any_any_identifier" is a leaf node, + /// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_ancestor_history(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -642,7 +642,7 @@ impl<'a> TreesitterContext<'a> { /// Verifies whether the node_under_cursor has the passed in ancestors in the right order. /// Note that you need to pass in the ancestors in the order as they would appear in the tree: /// - /// If the tree shows `relation > object_reference > any_any_identifier` and the "any_any_identifier" is a leaf node, + /// If the tree shows `relation > object_reference > any_identifier` and the "any_identifier" is a leaf node, /// you need to pass `&["relation", "object_reference"]`. pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool { self.node_under_cursor.as_ref().is_some_and(|node| { @@ -663,9 +663,9 @@ impl<'a> TreesitterContext<'a> { /// * keyword_from [9..13] 'from' /// * relation [14..28] '"auth"."users"' /// * object_reference [14..28] '"auth"."users"' - /// * any_any_identifier [14..20] '"auth"' + /// * any_identifier [14..20] '"auth"' /// * . [20..21] '.' - /// * any_any_identifier [21..28] '"users"' + /// * any_identifier [21..28] '"users"' /// */ /// /// if node_under_cursor_is_nth_child(1) { @@ -1138,7 +1138,7 @@ mod tests { any_identifier [40..43] '@id' @ [40..41] '@' - You can see that the '@' is a child of the "any_any_identifier" but has a range smaller than its parent's. + You can see that the '@' is a child of the "any_identifier" but has a range smaller than its parent's. This would crash our context parsing because, at position 42, we weren't at the leaf node but also couldn't go to a child on that position. */ From 586b3a2871866fbcd9fddf83e0a0bc8ad12fc757 Mon Sep 17 00:00:00 2001 From: Julian Domke <68325451+juleswritescode@users.noreply.github.com> Date: Tue, 28 Oct 2025 12:09:35 +0100 Subject: [PATCH 4/4] [5] refactor(treesitter): use declarative node names instead of context checking (#571) --- .../src/relevance/filtering.rs | 21 ++- crates/pgt_hover/src/hovered_node.rs | 2 +- crates/pgt_treesitter/src/context/mod.rs | 3 +- .../src/queries/insert_columns.rs | 10 +- .../pgt_treesitter/src/queries/parameters.rs | 2 +- .../src/queries/select_columns.rs | 2 +- .../src/queries/where_columns.rs | 2 +- crates/pgt_treesitter_grammar/grammar.js | 174 ++++++++---------- 8 files changed, 103 insertions(+), 113 deletions(-) diff --git a/crates/pgt_completions/src/relevance/filtering.rs b/crates/pgt_completions/src/relevance/filtering.rs index 916384ef8..42962782a 100644 --- a/crates/pgt_completions/src/relevance/filtering.rs +++ b/crates/pgt_completions/src/relevance/filtering.rs @@ -17,7 +17,11 @@ impl<'a> From> for CompletionFilter<'a> { impl CompletionFilter<'_> { pub fn is_relevant(&self, ctx: &TreesitterContext) -> Option<()> { self.completable_context(ctx)?; - self.check_clause(ctx)?; + + self.check_node_type(ctx) + // we want to rely on treesitter more, so checking the clause is a fallback + .or_else(|| self.check_clause(ctx))?; + self.check_invocation(ctx)?; self.check_mentioned_schema_or_alias(ctx)?; @@ -88,6 +92,21 @@ impl CompletionFilter<'_> { Some(()) } + fn check_node_type(&self, ctx: &TreesitterContext) -> Option<()> { + let kind = ctx.node_under_cursor.as_ref().map(|n| n.kind())?; + + let is_allowed = match kind { + "column_identifier" => { + matches!(self.data, CompletionRelevanceData::Column(_)) + && !ctx.matches_ancestor_history(&["insert_values", "field"]) + && !ctx.node_under_cursor_is_within_field_name("binary_expr_right") + } + _ => false, + }; + + if is_allowed { Some(()) } else { None } + } + fn check_clause(&self, ctx: &TreesitterContext) -> Option<()> { ctx.wrapping_clause_type .as_ref() diff --git a/crates/pgt_hover/src/hovered_node.rs b/crates/pgt_hover/src/hovered_node.rs index bc13d07c1..322ca9281 100644 --- a/crates/pgt_hover/src/hovered_node.rs +++ b/crates/pgt_hover/src/hovered_node.rs @@ -73,7 +73,7 @@ impl HoveredNode { } } - "any_identifier" if ctx.matches_ancestor_history(&["field"]) => { + "column_identifier" => { if let Some(table_or_alias) = ctx.schema_or_alias_name.as_ref() { Some(HoveredNode::Column(NodeIdentification::SchemaAndName(( table_or_alias.clone(), diff --git a/crates/pgt_treesitter/src/context/mod.rs b/crates/pgt_treesitter/src/context/mod.rs index 381c06b62..84d2b5d06 100644 --- a/crates/pgt_treesitter/src/context/mod.rs +++ b/crates/pgt_treesitter/src/context/mod.rs @@ -590,8 +590,7 @@ impl<'a> TreesitterContext<'a> { // `node.child_by_field_id(..)` does not work as expected let mut on_node = None; for child in node.children(cursor) { - // 28 is the id for "keyword_on" - if child.kind_id() == 28 { + if child.kind() == "keyword_on" { on_node = Some(child); } } diff --git a/crates/pgt_treesitter/src/queries/insert_columns.rs b/crates/pgt_treesitter/src/queries/insert_columns.rs index e80718321..a46ab7b86 100644 --- a/crates/pgt_treesitter/src/queries/insert_columns.rs +++ b/crates/pgt_treesitter/src/queries/insert_columns.rs @@ -7,14 +7,8 @@ use super::QueryTryFrom; static TS_QUERY: LazyLock = LazyLock::new(|| { static QUERY_STR: &str = r#" - (insert - (object_reference) - (list - "("? - (column) @column - ","? - ")"? - ) + (insert_columns + (column_identifier) @column ) "#; tree_sitter::Query::new(&pgt_treesitter_grammar::LANGUAGE.into(), QUERY_STR) diff --git a/crates/pgt_treesitter/src/queries/parameters.rs b/crates/pgt_treesitter/src/queries/parameters.rs index fdfb98a56..cab7b11eb 100644 --- a/crates/pgt_treesitter/src/queries/parameters.rs +++ b/crates/pgt_treesitter/src/queries/parameters.rs @@ -11,7 +11,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { [ (field (field_qualifier)? - (any_identifier) + (column_identifier) ) @reference (parameter) @parameter diff --git a/crates/pgt_treesitter/src/queries/select_columns.rs b/crates/pgt_treesitter/src/queries/select_columns.rs index 5b8ee6dfa..c1835fe32 100644 --- a/crates/pgt_treesitter/src/queries/select_columns.rs +++ b/crates/pgt_treesitter/src/queries/select_columns.rs @@ -14,7 +14,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (object_reference) @alias "." )? - (any_identifier) @column + (column_identifier) @column ) ) ","? diff --git a/crates/pgt_treesitter/src/queries/where_columns.rs b/crates/pgt_treesitter/src/queries/where_columns.rs index 62e9e0a5c..bf9fda4a4 100644 --- a/crates/pgt_treesitter/src/queries/where_columns.rs +++ b/crates/pgt_treesitter/src/queries/where_columns.rs @@ -16,7 +16,7 @@ static TS_QUERY: LazyLock = LazyLock::new(|| { (object_reference) @alias "." )? - (any_identifier) @column + (column_identifier) @column ) ) ) diff --git a/crates/pgt_treesitter_grammar/grammar.js b/crates/pgt_treesitter_grammar/grammar.js index 23875fcd2..4f57b5ef8 100644 --- a/crates/pgt_treesitter_grammar/grammar.js +++ b/crates/pgt_treesitter_grammar/grammar.js @@ -28,6 +28,7 @@ module.exports = grammar({ [$.time], [$.timestamp], [$.grantable_on_function, $.grantable_on_table], + [$.any_identifier, $.column_identifier], ], precedences: ($) => [ @@ -263,6 +264,8 @@ module.exports = grammar({ keyword_storage: (_) => make_keyword("storage"), keyword_compression: (_) => make_keyword("compression"), + keyword_overriding: () => make_keyword("overriding"), + keyword_system: () => make_keyword("system"), keyword_policy: (_) => make_keyword("policy"), keyword_permissive: (_) => make_keyword("permissive"), keyword_restrictive: (_) => make_keyword("restrictive"), @@ -779,21 +782,7 @@ module.exports = grammar({ ), _show_statement: ($) => - seq( - $.keyword_show, - choice( - $._show_create, - $.keyword_all, // Postgres - $._show_tables // trino/presto - ) - ), - - _show_tables: ($) => - seq( - $.keyword_tables, - optional(seq($.keyword_from, $._qualified_field)), - optional(seq($.keyword_like, $._expression)) - ), + seq($.keyword_show, choice($._show_create, $.keyword_all)), _show_create: ($) => seq( @@ -990,7 +979,6 @@ module.exports = grammar({ $.table_partition, $.stored_as, $.storage_location, - $.table_sort, $.row_format, seq($.keyword_tblproperties, paren_list($.table_option, true)), seq($.keyword_without, $.keyword_oids), @@ -1656,7 +1644,7 @@ module.exports = grammar({ $.keyword_insert, seq( $.keyword_update, - optional(seq($.keyword_of, comma_list($.any_identifier, true))) + optional(seq($.keyword_of, comma_list($.column_identifier, true))) ), $.keyword_delete, $.keyword_truncate @@ -1752,8 +1740,6 @@ module.exports = grammar({ $.add_constraint, $.drop_constraint, $.alter_column, - $.modify_column, - $.change_column, $.drop_column, $.rename_object, $.rename_column, @@ -1794,7 +1780,7 @@ module.exports = grammar({ // TODO constraint management $.keyword_alter, optional($.keyword_column), - field("name", $.any_identifier), + $.column_identifier, choice( seq( choice($.keyword_set, $.keyword_drop), @@ -1832,44 +1818,22 @@ module.exports = grammar({ ) ), - modify_column: ($) => - seq( - $.keyword_modify, - optional($.keyword_column), - optional($._if_exists), - $.column_definition, - optional($.column_position) - ), - - change_column: ($) => - seq( - $.keyword_change, - optional($.keyword_column), - optional($._if_exists), - field("old_name", $.any_identifier), - $.column_definition, - optional($.column_position) - ), - column_position: ($) => - choice( - $.keyword_first, - seq($.keyword_after, field("col_name", $.any_identifier)) - ), + choice($.keyword_first, seq($.keyword_after, $.column_identifier)), drop_column: ($) => seq( $.keyword_drop, optional($.keyword_column), optional($._if_exists), - field("name", $.any_identifier) + $.column_identifier ), rename_column: ($) => seq( $.keyword_rename, optional($.keyword_column), - field("old_name", $.any_identifier), + $.column_identifier, $.keyword_to, field("new_name", $.any_identifier) ), @@ -2306,33 +2270,42 @@ module.exports = grammar({ insert: ($) => seq( - choice($.keyword_insert, $.keyword_replace), + $.keyword_insert, + $.keyword_into, + $.object_reference, + optional($._alias), + optional($.insert_columns), optional( - choice( - $.keyword_low_priority, - $.keyword_delayed, - $.keyword_high_priority + seq( + $.keyword_overriding, + choice($.keyword_user, $.keyword_system), + $.keyword_value ) ), - optional($.keyword_ignore), - optional( - choice( - $.keyword_into, - $.keyword_overwrite // Spark SQL - ) + choice( + seq($.keyword_default, $.keyword_values), + $.insert_values, + $._select_statement ), - $.object_reference, - optional($.table_partition), // Spark SQL - optional(seq($.keyword_as, field("alias", $.any_identifier))), - // TODO we need a test for `insert...set` - choice($._insert_values, $._set_values), - optional(choice($._on_conflict, $._on_duplicate_key_update)) + optional($._on_conflict) ), + insert_values: ($) => + comma_list( + seq( + $.keyword_values, + paren_list(choice($._expression, $.keyword_default), true) + ), + true + ), + + insert_columns: ($) => paren_list($.column_identifier, true), + _on_conflict: ($) => seq( $.keyword_on, $.keyword_conflict, + // todo: conflict target seq( $.keyword_do, choice( @@ -2342,28 +2315,13 @@ module.exports = grammar({ ) ), - _on_duplicate_key_update: ($) => - seq( - $.keyword_on, - $.keyword_duplicate, - $.keyword_key, - $.keyword_update, - $.assignment_list - ), - assignment_list: ($) => seq($.assignment, repeat(seq(",", $.assignment))), - _insert_values: ($) => - seq( - optional(alias($._column_list, $.list)), - choice(seq($.keyword_values, comma_list($.list, true)), $._dml_read) - ), - _set_values: ($) => seq($.keyword_set, comma_list($.assignment, true)), _column_list: ($) => paren_list(alias($._column, $.column), true), _column: ($) => - choice($.any_identifier, alias($._literal_string, $.literal)), + choice($.column_identifier, alias($._literal_string, $.literal)), _update_statement: ($) => seq($.update, optional($.returning)), @@ -2394,10 +2352,31 @@ module.exports = grammar({ ), $.keyword_then, choice( - $.keyword_delete, + // merge_insert + seq( + $.keyword_insert, + optional(paren_list($.column_identifier, true)), + optional( + seq( + $.keyword_overriding, + choice($.keyword_system, $.keyword_user), + $.keyword_value + ) + ), + choice( + seq($.keyword_default, $.keyword_values), + seq( + $.keyword_values, + paren_list(choice($._expression, $.keyword_default), true) + ) + ) + ), + // merge_update seq($.keyword_update, $._set_values), - seq($.keyword_insert, $._insert_values), - optional($.where) + // merge_delete + $.keyword_delete, + + seq($.keyword_do, $.keyword_nothing) ) ), @@ -2571,9 +2550,6 @@ module.exports = grammar({ ) ), - table_sort: ($) => - seq($.keyword_sort, $.keyword_by, paren_list($.any_identifier, true)), - table_partition: ($) => seq( choice( @@ -2655,7 +2631,7 @@ module.exports = grammar({ column_definition: ($) => seq( - field("name", $._column), + $.any_identifier, field("type", $.type), repeat($._column_constraint) ), @@ -2670,7 +2646,7 @@ module.exports = grammar({ seq( $.keyword_references, $.object_reference, - paren_list($.any_identifier, true), + paren_list($.column_identifier, true), repeat( seq( $.keyword_on, @@ -2775,7 +2751,7 @@ module.exports = grammar({ seq( $.keyword_references, $.object_reference, - paren_list($.any_identifier, true), + paren_list($.column_identifier, true), repeat( seq( $.keyword_on, @@ -2834,10 +2810,10 @@ module.exports = grammar({ $.keyword_end ), - field: ($) => field("name", $.any_identifier), + field: ($) => field("name", $.column_identifier), _qualified_field: ($) => - seq(optional($.field_qualifier), field("name", $.any_identifier)), + seq(optional($.field_qualifier), $.column_identifier), field_qualifier: ($) => seq(prec.left(optional_parenthesis($.object_reference)), "."), @@ -3399,9 +3375,9 @@ module.exports = grammar({ prec.left( precedence, seq( - field("left", $._expression), - field("operator", operator), - field("right", $._expression) + field("binary_expr_left", $._expression), + field("binary_expr_operator", operator), + field("binary_expr_right", $._expression) ) ) ), @@ -3409,9 +3385,9 @@ module.exports = grammar({ prec.left( precedence, seq( - field("left", $._expression), - field("operator", operator), - field("right", $._expression) + field("binary_expr_left", $._expression), + field("binary_expr_operator", operator), + field("binary_expr_right", $._expression) ) ) ), @@ -3419,9 +3395,9 @@ module.exports = grammar({ prec.left( precedence, seq( - field("left", $._expression), - field("operator", operator), - field("right", choice($.list, $.subquery)) + field("binary_expr_left", $._expression), + field("binary_expr_operator", operator), + field("binary_expr_right", choice($.list, $.subquery)) ) ) ) @@ -3531,6 +3507,8 @@ module.exports = grammar({ bang: (_) => "!", any_identifier: ($) => $._any_identifier, + column_identifier: ($) => $._any_identifier, + _any_identifier: ($) => choice( $._identifier,