Repository: DerekStride/tree-sitter-sql (language: python)

Issue:
Missing Statments
Following statement types are still missing:

- [ ] `GRANT`
- [ ] `REVOKE`
- [x] `COMMENT`

https://www.postgresql.org/docs/15/sql-grant.html
https://www.postgresql.org/docs/15/sql-revoke.html
https://www.postgresql.org/docs/15/sql-comment.html


Discussion / hints:

Fix patch:
diff --git a/Makefile b/Makefile
index 7ce409d7..1e73950b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,19 +1,39 @@
-ts := $(shell which tree-sitter 2> /dev/null)
-ifeq (, ${ts})
-	ts := $(shell which tree-sitter-cli 2> /dev/null)
+CC ?= gcc
+
+TS := $(shell which tree-sitter 2>/dev/null)
+ifeq (, ${TS})
+	TS := $(shell which tree-sitter-cli 2>/dev/null)
 endif
+TSFLAGS ?=
+
+.PHONY: all
+all: compile
+
+.PHONY: clean
+clean:
+	rm src/grammar.json src/parser.c
+
+.PHONY: generate
+generate: src/grammar.json src/parser.c
+src/grammar.json src/parser.c: grammar.js queries/highlights.scm queries/indents.scm
+	${TS} generate ${TSFLAGS}
 
-generate:
-	${ts} generate
+.PHONY: regenerate
+regenerate: clean generate
 
-test: generate
-	${ts} test
+.PHONY: test
+test: src/grammar.json
+	${TS} test
 
-format: generate
-	${ts} test --update
+.PHONY: format
+format: src/grammar.json
+	${TS} test --update
 
-compile: generate
-	gcc -shared -o target/parser.so -fPIC src/parser.c -I./src
+.PHONY: compile
+compile: target/parser.so
+target/parser.so: src/parser.c
+	${CC} -shared -o target/parser.so -fPIC src/parser.c -I./src
 
-check_keywords:
-	$(shell bash scripts/test-keywords.sh)
+.PHONY: check_keywords
+check_keywords: src/grammar.json queries/highlights.scm scripts/test-keywords.sh
+	@bash scripts/test-keywords.sh
diff --git a/grammar.js b/grammar.js
index 27516544..0c4a75be 100644
--- a/grammar.js
+++ b/grammar.js
@@ -643,6 +643,7 @@ module.exports = grammar({
       $._rename_statement,
       $._optimize_statement,
       $._merge_statement,
+      $.comment_statement,
     ),
 
     _cte: $ => seq(
@@ -723,6 +724,57 @@ module.exports = grammar({
       ),
     ),
 
+    comment_statement: $ => seq(
+      $.keyword_comment,
+      $.keyword_on,
+      $._comment_target,
+      $.keyword_is,
+      choice(
+        $.keyword_null,
+        alias($._literal_string, $.literal),
+      ),
+    ),
+
+    _comment_target: $ => choice(
+      // TODO: access method
+      // TODO: aggregate
+      $.cast,
+      // TODO: collation
+      seq($.keyword_column, alias($._qualified_field, $.object_reference)),
+      // TODO: constraint (on domain)
+      // TODO: conversion
+      seq($.keyword_database, $.identifier),
+      // TODO: domain
+      // TODO: extension
+      // TODO: event trigger
+      // TODO: foreign data wrapper
+      // TODO: foreign table
+      // TODO: function
+      seq($.keyword_index, $.object_reference),
+      // TODO: large object
+      seq($.keyword_materialized, $.keyword_view, $.object_reference),
+      // TODO: operator (|class|family)
+      // TODO: policy
+      // TODO: (procedural) language
+      // TODO: procedure
+      // TODO: publication
+      seq($.keyword_role, $.identifier),
+      // TODO: routine
+      // TODO: rule
+      seq($.keyword_schema, $.identifier),
+      seq($.keyword_sequence, $.object_reference),
+      // TODO: server
+      // TODO: statistics
+      // TODO: subscription
+      seq($.keyword_table, $.object_reference),
+      seq($.keyword_tablespace, $.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_view, $.object_reference),
+    ),
+
     select: $ => seq(
       $.keyword_select,
       seq(


Test patch:
diff --git a/scripts/test-keywords.sh b/scripts/test-keywords.sh
index 3c3a73e4..2b027840 100755
--- a/scripts/test-keywords.sh
+++ b/scripts/test-keywords.sh
@@ -18,3 +18,5 @@ if [[ "$keywords" ]]; then
   echo "$keywords"
   exit 1
 fi
+
+echo "OK"
diff --git a/test/corpus/comment_stat.txt b/test/corpus/comment_stat.txt
new file mode 100644
index 00000000..bae3baf6
--- /dev/null
+++ b/test/corpus/comment_stat.txt
@@ -0,0 +1,104 @@
+================================================================================
+Comment on table
+================================================================================
+
+COMMENT ON TABLE my_schema.my_table IS "this table is a test";
+
+--------------------------------------------------------------------------------
+
+(program
+	(statement
+		(comment_statement
+			(keyword_comment)
+			(keyword_on)
+			(keyword_table)
+			(object_reference
+				schema: (identifier)
+		    	name: (identifier))
+			(keyword_is)
+			(literal))))
+
+================================================================================
+Comment on column is null
+================================================================================
+
+COMMENT ON COLUMN my_schema.my_table.my_column IS NULL;
+
+--------------------------------------------------------------------------------
+
+(program
+	(statement
+		(comment_statement
+			(keyword_comment)
+			(keyword_on)
+			(keyword_column)
+			(object_reference
+				(object_reference
+					(identifier)
+					(identifier))
+				(identifier))
+			(keyword_is)
+			(keyword_null))))
+
+================================================================================
+Comment on cast
+================================================================================
+
+COMMENT ON CAST (varchar AS text) IS "convert varchar to text";
+
+--------------------------------------------------------------------------------
+
+(program
+	(statement
+		(comment_statement
+			(keyword_comment)
+			(keyword_on)
+			(cast
+				(keyword_cast)
+				(field
+					(identifier))
+				(keyword_as)
+				(keyword_text))
+			(keyword_is)
+			(literal))))
+
+================================================================================
+Comment on materialized view
+================================================================================
+
+COMMENT ON MATERIALIZED VIEW matview IS "this view is materialized";
+
+--------------------------------------------------------------------------------
+
+(program
+	(statement
+		(comment_statement
+			(keyword_comment)
+			(keyword_on)
+			(keyword_materialized)
+			(keyword_view)
+			(object_reference
+				(identifier))
+			(keyword_is)
+			(literal))))
+
+================================================================================
+Comment on trigger
+================================================================================
+
+COMMENT ON TRIGGER on_insert ON users IS "new user has been added";
+
+--------------------------------------------------------------------------------
+
+(program
+	(statement
+		(comment_statement
+			(keyword_comment)
+			(keyword_on)
+			(keyword_trigger)
+			(identifier)
+			(keyword_on)
+			(object_reference
+				name: (identifier))
+			(keyword_is)
+			(literal))))