DerekStride / tree-sitter-sql

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

Non visible conflict on column definition and table constraints #201

Open antoineB opened 1 year ago

antoineB commented 1 year ago

According to postgres and sqlite doc this sql is valid (key is a column name)

CREATE TABLE public.test (
    id bigint NOT NULL,
    key char(255) NOT NULL
);

But key is a keyword so the rule $._key_constraint is choosed instead of $.column_definition.

I add to change the grammar.js to :

modified   grammar.js
@@ -12,6 +12,7 @@ module.exports = grammar({
     [$.object_reference, $._qualified_field],
     [$.object_reference],
     [$.between_expression, $.binary_expression],
+    [$._column, $._key_constraint],
   ],

   precedences: $ => [
@@ -1918,6 +1919,7 @@ module.exports = grammar({

     _column_list: $ => paren_list(alias($._column, $.column), true),
     _column: $ => choice(
+      $.keyword_key,
       $.identifier,
       alias($._literal_string, $.literal),
     ),

The conflict was mentioned at compile time when I added the $.keyword_key to $._column.

In SQL many keyword are not reserved (https://www.postgresql.org/docs/current/sql-keywords-appendix.html) and could be used as identifier, should we add :

  _non_reserved_keyword: $ => choice(
      $.keyword_key,
      $.keyword_delete
      ...
      ),
  _identifier_keyword: $ => choice(
      $._identifier,
      $._non_reserved_keyword,
  ),

and replace $._identifier by $._identifier_keyword ?

Maybe there is a better solution with word ?