diff --git a/crates/pgt_completions/src/relevance/filtering.rs b/crates/pgt_completions/src/relevance/filtering.rs index ddbc66eb8..42962782a 100644 --- a/crates/pgt_completions/src/relevance/filtering.rs +++ b/crates/pgt_completions/src/relevance/filtering.rs @@ -1,5 +1,5 @@ use pgt_schema_cache::ProcKind; -use pgt_treesitter::context::{NodeUnderCursor, TreesitterContext, WrappingClause, WrappingNode}; +use pgt_treesitter::context::{TreesitterContext, WrappingClause, WrappingNode}; use super::CompletionRelevanceData; @@ -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)?; @@ -67,23 +71,20 @@ impl CompletionFilter<'_> { } // No autocompletions if there are two identifiers without a separator. - if ctx.node_under_cursor.as_ref().is_some_and(|n| match n { - NodeUnderCursor::TsNode(node) => node.prev_sibling().is_some_and(|p| { - (p.kind() == "identifier" || p.kind() == "object_reference") - && n.kind() == "identifier" - }), - NodeUnderCursor::CustomNode { .. } => false, + if ctx.node_under_cursor.as_ref().is_some_and(|node| { + node.prev_sibling().is_some_and(|p| { + (p.kind() == "any_identifier" || p.kind() == "object_reference") + && node.kind() == "any_identifier" + }) }) { return None; } // no completions if we're right after an asterisk: // `select * {}` - if ctx.node_under_cursor.as_ref().is_some_and(|n| match n { - NodeUnderCursor::TsNode(node) => node - .prev_sibling() - .is_some_and(|p| (p.kind() == "all_fields") && n.kind() == "identifier"), - NodeUnderCursor::CustomNode { .. } => false, + if ctx.node_under_cursor.as_ref().is_some_and(|node| { + node.prev_sibling() + .is_some_and(|p| (p.kind() == "all_fields") && node.kind() == "any_identifier") }) { return None; } @@ -91,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() @@ -99,9 +115,8 @@ impl CompletionFilter<'_> { CompletionRelevanceData::Table(_) => match clause { WrappingClause::From | WrappingClause::Update => true, - WrappingClause::RevokeStatement => { - ctx.matches_ancestor_history(&["revoke_on_table", "object_reference"]) - } + WrappingClause::RevokeStatement | WrappingClause::GrantStatement => ctx + .matches_ancestor_history(&["grantable_on_table", "object_reference"]), WrappingClause::Join { on_node: None } => true, WrappingClause::Join { on_node: Some(on) } => ctx @@ -206,10 +221,12 @@ impl CompletionFilter<'_> { | WrappingClause::Update | WrappingClause::Delete => true, - WrappingClause::RevokeStatement => { - (ctx.matches_ancestor_history(&["revoke_on_table", "object_reference"]) - && ctx.schema_or_alias_name.is_none()) - || ctx.matches_ancestor_history(&["revoke_on_all"]) + WrappingClause::RevokeStatement | WrappingClause::GrantStatement => { + (ctx.matches_ancestor_history(&[ + "grantable_on_table", + "object_reference", + ]) && ctx.schema_or_alias_name.is_none()) + || ctx.matches_ancestor_history(&["grantable_on_all"]) } WrappingClause::Where => { @@ -248,18 +265,17 @@ impl CompletionFilter<'_> { } CompletionRelevanceData::Role(_) => match clause { - WrappingClause::DropRole - | WrappingClause::AlterRole - | WrappingClause::ToRoleAssignment => true, + WrappingClause::DropRole | WrappingClause::AlterRole => true, WrappingClause::SetStatement => ctx .before_cursor_matches_kind(&["keyword_role", "keyword_authorization"]), - WrappingClause::RevokeStatement => { + 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", "keyword_for", ]) diff --git a/crates/pgt_hover/src/hovered_node.rs b/crates/pgt_hover/src/hovered_node.rs index 26a9d2d14..322ca9281 100644 --- a/crates/pgt_hover/src/hovered_node.rs +++ b/crates/pgt_hover/src/hovered_node.rs @@ -32,9 +32,10 @@ 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(&["revoke_on_table", "object_reference"]) => + || ctx + .matches_ancestor_history(&["grantable_on_table", "object_reference"]) => { let num_sibs = ctx.num_siblings(); if ctx.node_under_cursor_is_nth_child(1) && num_sibs > 0 { @@ -51,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!( @@ -72,7 +73,7 @@ impl HoveredNode { } } - "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(), @@ -83,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(), @@ -96,7 +99,7 @@ impl HoveredNode { } } - "identifier" + "any_identifier" if ctx.matches_one_of_ancestors(&[ "alter_role", "policy_to_role", @@ -109,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/grant_parser.rs b/crates/pgt_treesitter/src/context/grant_parser.rs deleted file mode 100644 index c9aebc33b..000000000 --- a/crates/pgt_treesitter/src/context/grant_parser.rs +++ /dev/null @@ -1,418 +0,0 @@ -use pgt_text_size::{TextRange, TextSize}; - -use crate::context::base_parser::{ - CompletionStatementParser, TokenNavigator, WordWithIndex, schema_and_table_name, -}; - -#[derive(Default, Debug, PartialEq, Eq)] -pub(crate) struct GrantContext { - pub table_name: Option, - pub schema_name: Option, - pub node_text: String, - pub node_range: TextRange, - pub node_kind: String, -} - -/// Simple parser that'll turn a policy-related statement into a context object required for -/// completions. -/// The parser will only work if the (trimmed) sql starts with `create policy`, `drop policy`, or `alter policy`. -/// It can only parse policy statements. -pub(crate) struct GrantParser { - navigator: TokenNavigator, - context: GrantContext, - cursor_position: usize, - in_roles_list: bool, -} - -impl CompletionStatementParser for GrantParser { - type Context = GrantContext; - const NAME: &'static str = "GrantParser"; - - fn looks_like_matching_stmt(sql: &str) -> bool { - let lowercased = sql.to_ascii_lowercase(); - let trimmed = lowercased.trim(); - trimmed.starts_with("grant") - } - - fn parse(mut self) -> Self::Context { - while let Some(token) = self.navigator.advance() { - if token.is_under_cursor(self.cursor_position) { - self.handle_token_under_cursor(token); - } else { - self.handle_token(token); - } - } - - self.context - } - - fn make_parser(tokens: Vec, cursor_position: usize) -> Self { - Self { - navigator: tokens.into(), - context: GrantContext::default(), - cursor_position, - in_roles_list: false, - } - } -} - -impl GrantParser { - fn handle_token_under_cursor(&mut self, token: WordWithIndex) { - if self.navigator.previous_token.is_none() { - return; - } - - let previous = self.navigator.previous_token.take().unwrap(); - let current = self - .navigator - .current_token - .as_ref() - .map(|w| w.get_word_without_quotes()); - - match previous - .get_word_without_quotes() - .to_ascii_lowercase() - .as_str() - { - "grant" => { - self.context.node_range = token.get_range(); - self.context.node_kind = "grant_role".into(); - self.context.node_text = token.get_word(); - } - "on" if !matches!(current.as_deref(), Some("table")) => self.handle_table(&token), - - "table" => { - self.handle_table(&token); - } - "to" => { - self.context.node_range = token.get_range(); - self.context.node_kind = "grant_role".into(); - self.context.node_text = token.get_word(); - } - t => { - if self.in_roles_list && t.ends_with(',') { - self.context.node_kind = "grant_role".into(); - } - - self.context.node_range = token.get_range(); - self.context.node_text = token.get_word(); - } - } - } - - fn handle_table(&mut self, token: &WordWithIndex) { - if token.get_word_without_quotes().contains('.') { - let (schema_name, table_name) = schema_and_table_name(token); - - let schema_name_len = schema_name.len(); - self.context.schema_name = Some(schema_name); - - let offset: u32 = schema_name_len.try_into().expect("Text too long"); - let range_without_schema = token - .get_range() - .checked_expand_start( - TextSize::new(offset + 1), // kill the dot as well - ) - .expect("Text too long"); - - self.context.node_range = range_without_schema; - self.context.node_kind = "grant_table".into(); - - // In practice, we should always have a table name. - // The completion sanitization will add a word after a `.` if nothing follows it; - // the token_text will then look like `schema.REPLACED_TOKEN`. - self.context.node_text = table_name.unwrap_or_default(); - } else { - self.context.node_range = token.get_range(); - self.context.node_text = token.get_word(); - self.context.node_kind = "grant_table".into(); - } - } - - fn handle_token(&mut self, token: WordWithIndex) { - match token.get_word_without_quotes().as_str() { - "on" if !self.navigator.next_matches(&[ - "table", - "schema", - "foreign", - "domain", - "sequence", - "database", - "function", - "procedure", - "routine", - "language", - "large", - "parameter", - "schema", - "tablespace", - "type", - ]) => - { - self.table_with_schema() - } - "table" => self.table_with_schema(), - - "to" => { - self.in_roles_list = true; - } - - t => { - if self.in_roles_list && !t.ends_with(',') { - self.in_roles_list = false; - } - } - } - } - - fn table_with_schema(&mut self) { - if let Some(token) = self.navigator.advance() { - if token.is_under_cursor(self.cursor_position) { - self.handle_token_under_cursor(token); - } else if token.get_word_without_quotes().contains('.') { - let (schema, maybe_table) = schema_and_table_name(&token); - self.context.schema_name = Some(schema); - self.context.table_name = maybe_table; - } else { - self.context.table_name = Some(token.get_word()); - } - }; - } -} - -#[cfg(test)] -mod tests { - use pgt_text_size::{TextRange, TextSize}; - - use crate::{ - context::base_parser::CompletionStatementParser, - context::grant_parser::{GrantContext, GrantParser}, - }; - - use pgt_test_utils::QueryWithCursorPosition; - - fn with_pos(query: String) -> (usize, String) { - let mut pos: Option = None; - - for (p, c) in query.char_indices() { - if c == QueryWithCursorPosition::cursor_marker() { - pos = Some(p); - break; - } - } - - ( - pos.expect("Please add cursor position!"), - query - .replace(QueryWithCursorPosition::cursor_marker(), "REPLACED_TOKEN") - .to_string(), - ) - } - - #[test] - fn infers_grant_keyword() { - let (pos, query) = with_pos(format!( - r#" - grant {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: None, - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(19), TextSize::new(33)), - node_kind: "grant_role".into(), - } - ); - } - - #[test] - fn infers_table_name() { - let (pos, query) = with_pos(format!( - r#" - grant select on {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: None, - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(29), TextSize::new(43)), - node_kind: "grant_table".into(), - } - ); - } - - #[test] - fn infers_table_name_with_keyword() { - let (pos, query) = with_pos(format!( - r#" - grant select on table {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: None, - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(35), TextSize::new(49)), - node_kind: "grant_table".into(), - } - ); - } - - #[test] - fn infers_schema_and_table_name() { - let (pos, query) = with_pos(format!( - r#" - grant select on public.{} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: Some("public".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(36), TextSize::new(50)), - node_kind: "grant_table".into(), - } - ); - } - - #[test] - fn infers_schema_and_table_name_with_keyword() { - let (pos, query) = with_pos(format!( - r#" - grant select on table public.{} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: Some("public".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(42), TextSize::new(56)), - node_kind: "grant_table".into(), - } - ); - } - - #[test] - fn infers_role_name() { - let (pos, query) = with_pos(format!( - r#" - grant select on public.users to {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: Some("users".into()), - schema_name: Some("public".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(45), TextSize::new(59)), - node_kind: "grant_role".into(), - } - ); - } - - #[test] - fn determines_table_name_after_schema() { - let (pos, query) = with_pos(format!( - r#" - grant select on public.{} to test_role - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: None, - schema_name: Some("public".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(36), TextSize::new(50)), - node_kind: "grant_table".into(), - } - ); - } - - #[test] - fn infers_quoted_schema_and_table() { - let (pos, query) = with_pos(format!( - r#" - grant select on "MySchema"."MyTable" to {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: Some("MyTable".into()), - schema_name: Some("MySchema".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(53), TextSize::new(67)), - node_kind: "grant_role".into(), - } - ); - } - - #[test] - fn infers_multiple_roles() { - let (pos, query) = with_pos(format!( - r#" - grant select on public.users to alice, {} - "#, - QueryWithCursorPosition::cursor_marker() - )); - - let context = GrantParser::get_context(query.as_str(), pos); - - assert_eq!( - context, - GrantContext { - table_name: Some("users".into()), - schema_name: Some("public".into()), - node_text: "REPLACED_TOKEN".into(), - node_range: TextRange::new(TextSize::new(52), TextSize::new(66)), - node_kind: "grant_role".into(), - } - ); - } -} diff --git a/crates/pgt_treesitter/src/context/mod.rs b/crates/pgt_treesitter/src/context/mod.rs index c373bff9e..84d2b5d06 100644 --- a/crates/pgt_treesitter/src/context/mod.rs +++ b/crates/pgt_treesitter/src/context/mod.rs @@ -2,13 +2,9 @@ use std::{ cmp, collections::{HashMap, HashSet}, }; -mod base_parser; -mod grant_parser; use crate::queries::{self, QueryResult, TreeSitterQueriesExecutor}; -use pgt_text_size::{TextRange, TextSize}; - -use crate::context::{base_parser::CompletionStatementParser, grant_parser::GrantParser}; +use pgt_text_size::TextSize; #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum WrappingClause<'a> { @@ -27,11 +23,11 @@ pub enum WrappingClause<'a> { DropColumn, AlterColumn, RenameColumn, - ToRoleAssignment, SetStatement, AlterRole, DropRole, RevokeStatement, + GrantStatement, CreatePolicy, AlterPolicy, @@ -61,46 +57,6 @@ pub enum WrappingNode { List, } -#[derive(Debug)] -pub enum NodeUnderCursor<'a> { - TsNode(tree_sitter::Node<'a>), - CustomNode { - text: String, - range: TextRange, - kind: String, - previous_node_kind: Option, - }, -} - -impl NodeUnderCursor<'_> { - pub fn start_byte(&self) -> usize { - match self { - NodeUnderCursor::TsNode(node) => node.start_byte(), - NodeUnderCursor::CustomNode { range, .. } => range.start().into(), - } - } - - pub fn end_byte(&self) -> usize { - match self { - NodeUnderCursor::TsNode(node) => node.end_byte(), - NodeUnderCursor::CustomNode { range, .. } => range.end().into(), - } - } - - pub fn kind(&self) -> &str { - match self { - NodeUnderCursor::TsNode(node) => node.kind(), - NodeUnderCursor::CustomNode { kind, .. } => kind.as_str(), - } - } -} - -impl<'a> From> for NodeUnderCursor<'a> { - fn from(node: tree_sitter::Node<'a>) -> Self { - NodeUnderCursor::TsNode(node) - } -} - impl TryFrom<&str> for WrappingNode { type Error = String; @@ -139,7 +95,7 @@ pub struct TreeSitterContextParams<'a> { #[derive(Debug)] pub struct TreesitterContext<'a> { - pub node_under_cursor: Option>, + pub node_under_cursor: Option>, pub tree: &'a tree_sitter::Tree, pub text: &'a str, @@ -194,44 +150,12 @@ impl<'a> TreesitterContext<'a> { mentioned_columns: HashMap::new(), }; - if GrantParser::looks_like_matching_stmt(params.text) { - ctx.gather_grant_context(); - } else { - ctx.gather_tree_context(); - ctx.gather_info_from_ts_queries(); - } + ctx.gather_tree_context(); + ctx.gather_info_from_ts_queries(); ctx } - fn gather_grant_context(&mut self) { - let grant_context = GrantParser::get_context(self.text, self.position); - - self.node_under_cursor = Some(NodeUnderCursor::CustomNode { - text: grant_context.node_text, - range: grant_context.node_range, - kind: grant_context.node_kind.clone(), - previous_node_kind: None, - }); - - if grant_context.node_kind == "grant_table" { - self.schema_or_alias_name = grant_context.schema_name.clone(); - } - - if grant_context.table_name.is_some() { - let mut new = HashSet::new(); - new.insert(grant_context.table_name.unwrap()); - self.mentioned_relations - .insert(grant_context.schema_name, new); - } - - self.wrapping_clause_type = match grant_context.node_kind.as_str() { - "grant_role" => Some(WrappingClause::ToRoleAssignment), - "grant_table" => Some(WrappingClause::From), - _ => None, - }; - } - fn gather_info_from_ts_queries(&mut self) { let stmt_range = self.wrapping_statement_range.as_ref(); let sql = self.text; @@ -320,10 +244,9 @@ impl<'a> TreesitterContext<'a> { } pub fn get_node_under_cursor_content(&self) -> Option { - match self.node_under_cursor.as_ref()? { - NodeUnderCursor::TsNode(node) => self.get_ts_node_content(node), - NodeUnderCursor::CustomNode { text, .. } => Some(text.clone()), - } + self.node_under_cursor + .as_ref() + .and_then(|node| self.get_ts_node_content(node)) } fn gather_tree_context(&mut self) { @@ -375,7 +298,7 @@ impl<'a> TreesitterContext<'a> { // prevent infinite recursion – this can happen with ERROR nodes if current_node_kind == parent_node_kind && ["ERROR", "program"].contains(&parent_node_kind) { - self.node_under_cursor = Some(NodeUnderCursor::from(current_node)); + self.node_under_cursor = Some(current_node); return; } @@ -444,7 +367,7 @@ impl<'a> TreesitterContext<'a> { if current_node.child_count() == 0 || current_node.first_child_for_byte(self.position).is_none() { - self.node_under_cursor = Some(NodeUnderCursor::from(current_node)); + self.node_under_cursor = Some(current_node); return; } @@ -655,6 +578,7 @@ impl<'a> TreesitterContext<'a> { "alter_table" => Some(WrappingClause::AlterTable), "set_statement" => Some(WrappingClause::SetStatement), "revoke_statement" => Some(WrappingClause::RevokeStatement), + "grant_statement" => Some(WrappingClause::RevokeStatement), "column_definitions" => Some(WrappingClause::ColumnDefinitions), "create_policy" => Some(WrappingClause::CreatePolicy), "alter_policy" => Some(WrappingClause::AlterPolicy), @@ -666,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); } } @@ -679,71 +602,52 @@ impl<'a> TreesitterContext<'a> { } pub fn before_cursor_matches_kind(&self, kinds: &[&'static str]) -> bool { - self.node_under_cursor.as_ref().is_some_and(|under_cursor| { - match under_cursor { - NodeUnderCursor::TsNode(node) => { - let mut current = *node; - - // move up to the parent until we're at top OR we have a prev sibling - while current.prev_sibling().is_none() && current.parent().is_some() { - current = current.parent().unwrap(); - } - - current - .prev_sibling() - .is_some_and(|sib| kinds.contains(&sib.kind())) - } + self.node_under_cursor.as_ref().is_some_and(|node| { + let mut current = *node; - NodeUnderCursor::CustomNode { - previous_node_kind, .. - } => previous_node_kind - .as_ref() - .is_some_and(|k| kinds.contains(&k.as_str())), + // move up to the parent until we're at top OR we have a prev sibling + while current.prev_sibling().is_none() && current.parent().is_some() { + current = current.parent().unwrap(); } + + current + .prev_sibling() + .is_some_and(|sib| kinds.contains(&sib.kind())) }) } /// 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 > 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(|under_cursor| match under_cursor { - NodeUnderCursor::TsNode(node) => { - let mut current = Some(*node); - - for &expected_kind in expected_ancestors.iter().rev() { - current = current.and_then(|n| n.parent()); + self.node_under_cursor.as_ref().is_some_and(|node| { + let mut current = Some(*node); - match current { - Some(ancestor) if ancestor.kind() == expected_kind => continue, - _ => return false, - } - } + for &expected_kind in expected_ancestors.iter().rev() { + current = current.and_then(|n| n.parent()); - true + match current { + Some(ancestor) if ancestor.kind() == expected_kind => continue, + _ => return false, } - NodeUnderCursor::CustomNode { .. } => false, - }) + } + + true + }) } /// 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 > 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(|under_cursor| match under_cursor { - NodeUnderCursor::TsNode(node) => node - .parent() - .is_some_and(|p| expected_ancestors.contains(&p.kind())), - NodeUnderCursor::CustomNode { .. } => false, - }) + self.node_under_cursor.as_ref().is_some_and(|node| { + node.parent() + .is_some_and(|p| expected_ancestors.contains(&p.kind())) + }) } /// Checks whether the Node under the cursor is the nth child of the parent. @@ -758,9 +662,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_identifier [14..20] '"auth"' /// * . [20..21] '.' - /// * identifier [21..28] '"users"' + /// * any_identifier [21..28] '"users"' /// */ /// /// if node_under_cursor_is_nth_child(1) { @@ -770,32 +674,24 @@ impl<'a> TreesitterContext<'a> { /// } /// ``` pub fn node_under_cursor_is_nth_child(&self, nth: usize) -> bool { - self.node_under_cursor - .as_ref() - .is_some_and(|under_cursor| match under_cursor { - NodeUnderCursor::TsNode(node) => { - let mut cursor = node.walk(); - node.parent().is_some_and(|p| { - p.children(&mut cursor) - .nth(nth - 1) - .is_some_and(|n| n.id() == node.id()) - }) - } - NodeUnderCursor::CustomNode { .. } => false, + self.node_under_cursor.as_ref().is_some_and(|node| { + let mut cursor = node.walk(); + node.parent().is_some_and(|p| { + p.children(&mut cursor) + .nth(nth - 1) + .is_some_and(|n| n.id() == node.id()) }) + }) } /// Returns the number of siblings of the node under the cursor. pub fn num_siblings(&self) -> usize { self.node_under_cursor .as_ref() - .map(|n| match n { - NodeUnderCursor::TsNode(node) => { - // if there's no parent, we're on the top of the tree, - // where we have 0 siblings. - node.parent().map(|p| p.child_count() - 1).unwrap_or(0) - } - NodeUnderCursor::CustomNode { .. } => 0, + .map(|node| { + // if there's no parent, we're on the top of the tree, + // where we have 0 siblings. + node.parent().map(|p| p.child_count() - 1).unwrap_or(0) }) .unwrap_or(0) } @@ -804,34 +700,31 @@ impl<'a> TreesitterContext<'a> { pub fn node_under_cursor_is_within_field_name(&self, name: &str) -> bool { self.node_under_cursor .as_ref() - .map(|n| match n { - NodeUnderCursor::TsNode(node) => { - // It might seem weird that we have to check for the field_name from the parent, - // but TreeSitter wants it this way, since nodes often can only be named in - // the context of their parents. - let root_node = self.tree.root_node(); - let mut cursor = node.walk(); - let mut parent = node.parent(); - - while let Some(p) = parent { - if p == root_node { - break; - } - - if p.children_by_field_name(name, &mut cursor).any(|c| { - let r = c.range(); - // if the parent range contains the node range, the node is of the field_name. - r.start_byte <= node.start_byte() && r.end_byte >= node.end_byte() - }) { - return true; - } else { - parent = p.parent(); - } + .map(|node| { + // It might seem weird that we have to check for the field_name from the parent, + // but TreeSitter wants it this way, since nodes often can only be named in + // the context of their parents. + let root_node = self.tree.root_node(); + let mut cursor = node.walk(); + let mut parent = node.parent(); + + while let Some(p) = parent { + if p == root_node { + break; } - false + if p.children_by_field_name(name, &mut cursor).any(|c| { + let r = c.range(); + // if the parent range contains the node range, the node is of the field_name. + r.start_byte <= node.start_byte() && r.end_byte >= node.end_byte() + }) { + return true; + } else { + parent = p.parent(); + } } - NodeUnderCursor::CustomNode { .. } => false, + + false }) .unwrap_or(false) } @@ -893,8 +786,6 @@ mod tests { use pgt_test_utils::QueryWithCursorPosition; - use super::NodeUnderCursor; - fn get_tree(input: &str) -> tree_sitter::Tree { let mut parser = tree_sitter::Parser::new(); parser @@ -1126,17 +1017,12 @@ mod tests { let node = ctx.node_under_cursor.as_ref().unwrap(); - match node { - NodeUnderCursor::TsNode(node) => { - assert_eq!(ctx.get_ts_node_content(node), Some("select".into())); + assert_eq!(ctx.get_ts_node_content(node), Some("select".into())); - assert_eq!( - ctx.wrapping_clause_type, - Some(crate::context::WrappingClause::Select) - ); - } - _ => unreachable!(), - } + assert_eq!( + ctx.wrapping_clause_type, + Some(crate::context::WrappingClause::Select) + ); } } @@ -1161,12 +1047,7 @@ mod tests { let node = ctx.node_under_cursor.as_ref().unwrap(); - match node { - NodeUnderCursor::TsNode(node) => { - assert_eq!(ctx.get_ts_node_content(node), Some("from".into())); - } - _ => unreachable!(), - } + assert_eq!(ctx.get_ts_node_content(node), Some("from".into())); } #[test] @@ -1187,13 +1068,8 @@ mod tests { let node = ctx.node_under_cursor.as_ref().unwrap(); - match node { - NodeUnderCursor::TsNode(node) => { - assert_eq!(ctx.get_ts_node_content(node), Some("".into())); - assert_eq!(ctx.wrapping_clause_type, None); - } - _ => unreachable!(), - } + assert_eq!(ctx.get_ts_node_content(node), Some("".into())); + assert_eq!(ctx.wrapping_clause_type, None); } #[test] @@ -1216,13 +1092,8 @@ mod tests { let node = ctx.node_under_cursor.as_ref().unwrap(); - match node { - NodeUnderCursor::TsNode(node) => { - assert_eq!(ctx.get_ts_node_content(node), Some("fro".into())); - assert_eq!(ctx.wrapping_clause_type, Some(WrappingClause::Select)); - } - _ => unreachable!(), - } + assert_eq!(ctx.get_ts_node_content(node), Some("fro".into())); + assert_eq!(ctx.wrapping_clause_type, Some(WrappingClause::Select)); } #[test] @@ -1260,13 +1131,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 "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/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 a137cc8de..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)? - (identifier) + (column_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..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 "." )? - (identifier) @column + (column_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..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 "." )? - (identifier) @column + (column_identifier) @column ) ) ) diff --git a/crates/pgt_treesitter_grammar/grammar.js b/crates/pgt_treesitter_grammar/grammar.js index 9ad689978..4f57b5ef8 100644 --- a/crates/pgt_treesitter_grammar/grammar.js +++ b/crates/pgt_treesitter_grammar/grammar.js @@ -27,7 +27,8 @@ module.exports = grammar({ [$.between_expression, $.binary_expression], [$.time], [$.timestamp], - [$.revoke_on_function, $.revoke_on_table], + [$.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"), @@ -729,7 +732,8 @@ module.exports = grammar({ $.comment_statement, $.set_statement, $.reset_statement, - $.revoke_statement + $.revoke_statement, + $.grant_statement ), _cte: ($) => @@ -778,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( @@ -813,8 +803,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( @@ -870,7 +860,7 @@ module.exports = grammar({ function_argument: ($) => seq( optional($._argmode), - optional($.identifier), + optional($.any_identifier), $.type, optional(seq(choice($.keyword_default, "="), $.literal)) ), @@ -886,7 +876,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 @@ -905,20 +895,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) ), @@ -984,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), @@ -996,7 +990,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 ) ), @@ -1030,7 +1024,7 @@ module.exports = grammar({ seq( $.keyword_create, $.keyword_policy, - $.identifier, + $.any_identifier, $.keyword_on, $.object_reference, optional( @@ -1054,13 +1048,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) ) @@ -1073,7 +1067,7 @@ module.exports = grammar({ $.keyword_to, comma_list( choice( - $.identifier, + $.any_identifier, $.keyword_public, $.keyword_current_user, $.keyword_current_role, @@ -1089,7 +1083,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_policy, optional($._if_exists), - $.identifier + $.any_identifier ), optional( seq( @@ -1152,7 +1146,7 @@ module.exports = grammar({ choice( $.literal, $.keyword_default, - $.identifier, + $.any_identifier, $.keyword_on, $.keyword_off ) @@ -1167,14 +1161,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), @@ -1201,7 +1195,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( @@ -1288,7 +1282,7 @@ module.exports = grammar({ function_declaration: ($) => seq( - $.identifier, + $.any_identifier, $.type, optional( seq( @@ -1348,7 +1342,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: ($) => @@ -1390,7 +1384,7 @@ module.exports = grammar({ _operator_class: ($) => seq( - field("opclass", $.identifier), + field("opclass", $.any_identifier), optional( field( "opclass_parameters", @@ -1406,7 +1400,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))) @@ -1452,21 +1446,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)) ) ), @@ -1475,7 +1469,7 @@ module.exports = grammar({ $.keyword_create, $.keyword_database, optional($._if_not_exists), - $.identifier, + $.any_identifier, optional($.keyword_with), repeat($._with_settings) ), @@ -1484,14 +1478,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, @@ -1520,7 +1514,7 @@ module.exports = grammar({ $.keyword_admin, $.keyword_user ), - comma_list($.identifier, true) + comma_list($.any_identifier, true) ), create_sequence: ($) => @@ -1574,13 +1568,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) @@ -1591,7 +1585,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), @@ -1622,7 +1616,7 @@ module.exports = grammar({ choice($.keyword_old, $.keyword_new), $.keyword_table, optional($.keyword_as), - $.identifier + $.any_identifier ), seq( $.keyword_for, @@ -1630,7 +1624,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)) @@ -1647,7 +1644,7 @@ module.exports = grammar({ $.keyword_insert, seq( $.keyword_update, - optional(seq($.keyword_of, comma_list($.identifier, true))) + optional(seq($.keyword_of, comma_list($.column_identifier, true))) ), $.keyword_delete, $.keyword_truncate @@ -1664,7 +1661,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( @@ -1743,8 +1740,6 @@ module.exports = grammar({ $.add_constraint, $.drop_constraint, $.alter_column, - $.modify_column, - $.change_column, $.drop_column, $.rename_object, $.rename_column, @@ -1767,7 +1762,7 @@ module.exports = grammar({ seq( $.keyword_add, optional($.keyword_constraint), - $.identifier, + $.any_identifier, $.constraint ), @@ -1776,7 +1771,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_constraint, optional($._if_exists), - $.identifier, + $.any_identifier, optional($._drop_behavior) ), @@ -1785,7 +1780,7 @@ module.exports = grammar({ // TODO constraint management $.keyword_alter, optional($.keyword_column), - field("name", $.identifier), + $.column_identifier, choice( seq( choice($.keyword_set, $.keyword_drop), @@ -1823,46 +1818,24 @@ 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", $.identifier), - $.column_definition, - optional($.column_position) - ), - column_position: ($) => - choice( - $.keyword_first, - seq($.keyword_after, field("col_name", $.identifier)) - ), + choice($.keyword_first, seq($.keyword_after, $.column_identifier)), drop_column: ($) => seq( $.keyword_drop, optional($.keyword_column), optional($._if_exists), - field("name", $.identifier) + $.column_identifier ), rename_column: ($) => seq( $.keyword_rename, optional($.keyword_column), - field("old_name", $.identifier), + $.column_identifier, $.keyword_to, - field("new_name", $.identifier) + field("new_name", $.any_identifier) ), alter_view: ($) => @@ -1884,17 +1857,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), @@ -1903,12 +1876,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 + ) ) ) ), @@ -1917,17 +1893,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)) ) ) ) @@ -1936,13 +1912,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 ) @@ -1955,7 +1931,7 @@ module.exports = grammar({ $.keyword_alter, $.keyword_index, optional($._if_exists), - $.identifier, + $.any_identifier, choice( $.rename_object, seq( @@ -1966,13 +1942,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 ) ) @@ -2027,7 +2003,7 @@ module.exports = grammar({ $.keyword_set, choice( choice($.keyword_logged, $.keyword_unlogged), - seq($.keyword_schema, $.identifier) + seq($.keyword_schema, $.any_identifier) ) ) ) @@ -2037,7 +2013,7 @@ module.exports = grammar({ seq( $.keyword_alter, $.keyword_type, - $.identifier, + $.any_identifier, choice( $.change_ownership, $.set_schema, @@ -2045,9 +2021,9 @@ module.exports = grammar({ seq( $.keyword_rename, $.keyword_attribute, - $.identifier, + $.any_identifier, $.keyword_to, - $.identifier, + $.any_identifier, optional($._drop_behavior) ), seq( @@ -2071,23 +2047,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) ) ) @@ -2135,7 +2111,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_schema, optional($._if_exists), - $.identifier, + $.any_identifier, optional($._drop_behavior) ), @@ -2144,7 +2120,7 @@ module.exports = grammar({ $.keyword_drop, $.keyword_database, optional($._if_exists), - $.identifier, + $.any_identifier, optional($.keyword_with), optional($.keyword_force) ), @@ -2154,7 +2130,7 @@ module.exports = grammar({ $.keyword_drop, choice($.keyword_group, $.keyword_role, $.keyword_user), optional($._if_exists), - $.identifier + $.any_identifier ), drop_type: ($) => @@ -2181,7 +2157,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)) ), @@ -2191,7 +2167,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)) ), @@ -2208,9 +2184,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( @@ -2226,14 +2203,18 @@ module.exports = grammar({ object_reference: ($) => choice( seq( - field("database", $.identifier), + field("database", $.any_identifier), ".", - field("schema", $.identifier), + field("schema", $.any_identifier), ".", - field("name", $.identifier) + field("name", $.any_identifier) ), - seq(field("schema", $.identifier), ".", field("name", $.identifier)), - field("name", $.identifier) + seq( + field("schema", $.any_identifier), + ".", + field("name", $.any_identifier) + ), + field("name", $.any_identifier) ), _copy_statement: ($) => @@ -2269,7 +2250,7 @@ module.exports = grammar({ $.keyword_quote, $.keyword_encoding ), - alias($._literal_string, $.identifier) + alias($._literal_string, $.any_identifier) ), seq( choice( @@ -2289,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", $.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( @@ -2325,27 +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($.identifier, alias($._literal_string, $.literal)), + _column: ($) => + choice($.column_identifier, alias($._literal_string, $.literal)), _update_statement: ($) => seq($.update, optional($.returning)), @@ -2376,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) ) ), @@ -2553,9 +2550,6 @@ module.exports = grammar({ ) ), - table_sort: ($) => - seq($.keyword_sort, $.keyword_by, paren_list($.identifier, true)), - table_partition: ($) => seq( choice( @@ -2571,7 +2565,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 ) @@ -2579,7 +2573,7 @@ module.exports = grammar({ _key_value_pair: ($) => seq( - field("key", $.identifier), + field("key", $.any_identifier), "=", field("value", alias($._literal_string, $.literal)) ), @@ -2613,17 +2607,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)) ) ), @@ -2637,7 +2631,7 @@ module.exports = grammar({ column_definition: ($) => seq( - field("name", $._column), + $.any_identifier, field("type", $.type), repeat($._column_constraint) ), @@ -2652,7 +2646,7 @@ module.exports = grammar({ seq( $.keyword_references, $.object_reference, - paren_list($.identifier, true), + paren_list($.column_identifier, true), repeat( seq( $.keyword_on, @@ -2664,7 +2658,7 @@ module.exports = grammar({ seq( $.keyword_set, choice($.keyword_null, $.keyword_default), - optional(paren_list($.identifier, true)) + optional(paren_list($.any_identifier, true)) ) ) ) @@ -2721,7 +2715,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)) ), @@ -2751,13 +2745,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($.column_identifier, true), repeat( seq( $.keyword_on, @@ -2769,7 +2763,7 @@ module.exports = grammar({ seq( $.keyword_set, choice($.keyword_null, $.keyword_default), - optional(paren_list($.identifier, true)) + optional(paren_list($.any_identifier, true)) ) ) ) @@ -2816,10 +2810,10 @@ module.exports = grammar({ $.keyword_end ), - field: ($) => field("name", $.identifier), + field: ($) => field("name", $.column_identifier), _qualified_field: ($) => - seq(optional($.field_qualifier), field("name", $.identifier)), + seq(optional($.field_qualifier), $.column_identifier), field_qualifier: ($) => seq(prec.left(optional_parenthesis($.object_reference)), "."), @@ -2888,7 +2882,7 @@ module.exports = grammar({ field( "start", choice( - $.identifier, + $.any_identifier, $.binary_expression, alias($._literal_string, $.literal), alias($._integer, $.literal) @@ -2901,7 +2895,7 @@ module.exports = grammar({ field( "end", choice( - $.identifier, + $.any_identifier, $.binary_expression, alias($._literal_string, $.literal), alias($._integer, $.literal) @@ -2936,7 +2930,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( @@ -2951,10 +2950,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( @@ -2993,7 +2993,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: ($) => @@ -3033,8 +3033,8 @@ module.exports = grammar({ optional( seq( $.keyword_as, - field("alias", $.identifier), - paren_list($.identifier, false) + field("alias", $.any_identifier), + paren_list($.any_identifier, false) ) ) ) @@ -3057,8 +3057,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, @@ -3073,8 +3073,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) ) ) ), @@ -3118,6 +3118,16 @@ module.exports = grammar({ returning: ($) => seq($.keyword_returning, $.select_expression), + grant_statement: ($) => + seq( + $.keyword_grant, + $._grantable_target_on, + $.keyword_to, + comma_list($.role_specification, true), + optional(seq($.keyword_with, $.keyword_grant, $.keyword_option)), + optional(seq($.keyword_granted, $.keyword_by, $.role_specification)) + ), + // todo: add support for various other revoke statements revoke_statement: ($) => seq( @@ -3134,26 +3144,33 @@ module.exports = grammar({ ) ) ), - choice( - seq( - $.revoke_targets, - choice($.revoke_on_table, $.revoke_on_function, $.revoke_on_all) - ), - $.identifier - ), + $._grantable_target_on, $.keyword_from, comma_list($.role_specification, true), optional(seq($.keyword_granted, $.keyword_by, $.role_specification)), optional(choice($.keyword_cascade, $.keyword_restrict)) ), - revoke_targets: ($) => + _grantable_target_on: ($) => choice( - seq($._revoke_keyword, comma_list($.identifier, false)), - comma_list($.identifier, true) + seq( + $.grantable_targets, + choice( + $.grantable_on_table, + $.grantable_on_function, + $.grantable_on_all + ) + ), + $.any_identifier + ), + + grantable_targets: ($) => + choice( + seq($._grantable, comma_list($.any_identifier, false)), + comma_list($.any_identifier, true) ), - _revoke_keyword: ($) => + _grantable: ($) => choice( comma_list( choice( @@ -3165,14 +3182,15 @@ module.exports = grammar({ $.keyword_references, $.keyword_trigger, $.keyword_maintain, - $.keyword_execute + $.keyword_execute, + $.keyword_references ), true ), seq($.keyword_all, optional($.keyword_privileges)) ), - revoke_on_function: ($) => + grantable_on_function: ($) => seq( $.keyword_on, optional( @@ -3184,7 +3202,7 @@ module.exports = grammar({ ) ), - revoke_on_table: ($) => + grantable_on_table: ($) => prec( 1, seq( @@ -3194,7 +3212,7 @@ module.exports = grammar({ ) ), - revoke_on_all: ($) => + grantable_on_all: ($) => seq( $.keyword_on, $.keyword_all, @@ -3206,12 +3224,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, @@ -3357,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) ) ) ), @@ -3367,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) ) ) ), @@ -3377,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)) ) ) ) @@ -3484,11 +3502,14 @@ 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, + column_identifier: ($) => $._any_identifier, + + _any_identifier: ($) => choice( $._identifier, $._double_quote_string,