grammar: add table constraint

This commit is contained in:
Dmitriy Pleshevskiy 2023-01-08 16:17:16 +03:00
parent acda125c15
commit de04a49b20
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 2432 additions and 2024 deletions

View file

@ -37,17 +37,13 @@ module.exports = grammar({
seq(
"(",
optional(
commaSepRepeat1(choice($.column_definition /*, $.table_constraint*/))
commaSepRepeat1(choice($.column_definition, $.table_constraint))
),
")"
),
column_definition: ($) =>
seq(
field("name", $.identifier),
field("datatype", $._type),
repeat($.column_constraint)
),
seq(field("name", $.identifier), $.type, repeat($.column_constraint)),
column_constraint: ($) =>
seq(
@ -57,31 +53,52 @@ module.exports = grammar({
$.keyword_null,
seq($.keyword_default, $._expression),
// TODO: add index_parameters in UNIQUE, PRIMARY KEY
seq(
$.keyword_unique,
optional(
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
)
),
$._unique_constraint,
$._primary_key,
seq(
$.keyword_references,
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
optional(
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
)
$._foreign_key_references
// TODO: CHECK
// TODO: GENERATED
)
// TODO: DEFERRABLE
),
table_constraint: ($) =>
seq(
optional(seq($.keyword_constraint, field("name", $.identifier))),
choice(
seq($._unique_constraint, $.column_list),
seq($._primary_key, $.column_list),
seq($._foreign_key, $.column_list, $._foreign_key_references)
// TODO: CHECK
// TODO: EXCLUDE
)
// TODO: DEFERRABLE
),
column_list: ($) =>
seq("(", commaSepRepeat1(field("name", $.identifier)), ")"),
_unique_constraint: ($) =>
seq(
$.keyword_unique,
optional(
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct)
)
),
_foreign_key_references: ($) =>
seq(
$.keyword_references,
$.table_reference,
optional(seq("(", field("refcolumn", $.identifier), ")")),
optional($._foreign_key_match),
optional(
choice(
seq($._foreign_key_on_delete, $._foreign_key_on_update),
seq($._foreign_key_on_update, $._foreign_key_on_delete)
)
)
),
_foreign_key_match: ($) =>
seq(
$.keyword_match,
@ -126,8 +143,9 @@ module.exports = grammar({
),
// References: https://www.postgresql.org/docs/15/datatype.html
_type: ($) =>
type: ($) =>
choice(
field("name", $.identifier),
$._type_numeric,
$._type_character,
$._type_datetime,

View file

@ -126,6 +126,10 @@
{
"type": "SYMBOL",
"name": "column_definition"
},
{
"type": "SYMBOL",
"name": "table_constraint"
}
]
},
@ -144,6 +148,10 @@
{
"type": "SYMBOL",
"name": "column_definition"
},
{
"type": "SYMBOL",
"name": "table_constraint"
}
]
}
@ -175,12 +183,8 @@
}
},
{
"type": "FIELD",
"name": "datatype",
"content": {
"type": "SYMBOL",
"name": "_type"
}
"type": "SYMBOL",
"name": "type"
},
{
"type": "REPEAT",
@ -244,97 +248,168 @@
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_unique"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_nulls"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "keyword_not"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "keyword_distinct"
}
]
},
{
"type": "BLANK"
}
]
}
]
"type": "SYMBOL",
"name": "_unique_constraint"
},
{
"type": "SYMBOL",
"name": "_primary_key"
},
{
"type": "SYMBOL",
"name": "_foreign_key_references"
}
]
}
]
},
"table_constraint": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_references"
"name": "keyword_constraint"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_unique_constraint"
},
{
"type": "SYMBOL",
"name": "table_reference"
"name": "column_list"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_primary_key"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "refcolumn",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
"type": "SYMBOL",
"name": "column_list"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key"
},
{
"type": "SYMBOL",
"name": "column_list"
},
{
"type": "SYMBOL",
"name": "_foreign_key_references"
}
]
}
]
}
]
},
"column_list": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
]
}
]
}
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"_unique_constraint": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_unique"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_nulls"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_match"
"name": "keyword_not"
},
{
"type": "BLANK"
@ -342,45 +417,106 @@
]
},
{
"type": "CHOICE",
"type": "SYMBOL",
"name": "keyword_distinct"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"_foreign_key_references": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "keyword_references"
},
{
"type": "SYMBOL",
"name": "table_reference"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "refcolumn",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_match"
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
}
]
}
]
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
},
{
"type": "BLANK"
"type": "SYMBOL",
"name": "_foreign_key_on_update"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_foreign_key_on_update"
},
{
"type": "SYMBOL",
"name": "_foreign_key_on_delete"
}
]
}
]
},
{
"type": "BLANK"
}
]
}
@ -612,9 +748,17 @@
}
]
},
"_type": {
"type": {
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "SYMBOL",
"name": "_type_numeric"

View file

@ -146,104 +146,6 @@
"type": "column_definition",
"named": true,
"fields": {
"datatype": {
"multiple": false,
"required": true,
"types": [
{
"type": "char",
"named": true
},
{
"type": "decimal",
"named": true
},
{
"type": "double",
"named": true
},
{
"type": "keyword_bigint",
"named": true
},
{
"type": "keyword_bigserial",
"named": true
},
{
"type": "keyword_boolean",
"named": true
},
{
"type": "keyword_bytea",
"named": true
},
{
"type": "keyword_date",
"named": true
},
{
"type": "keyword_datetime",
"named": true
},
{
"type": "keyword_integer",
"named": true
},
{
"type": "keyword_money",
"named": true
},
{
"type": "keyword_real",
"named": true
},
{
"type": "keyword_serial",
"named": true
},
{
"type": "keyword_smallint",
"named": true
},
{
"type": "keyword_smallserial",
"named": true
},
{
"type": "keyword_text",
"named": true
},
{
"type": "keyword_time",
"named": true
},
{
"type": "keyword_timestamp",
"named": true
},
{
"type": "keyword_timestamptz",
"named": true
},
{
"type": "keyword_uuid",
"named": true
},
{
"type": "keyword_xml",
"named": true
},
{
"type": "numeric",
"named": true
},
{
"type": "varchar",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
@ -257,11 +159,15 @@
},
"children": {
"multiple": true,
"required": false,
"required": true,
"types": [
{
"type": "column_constraint",
"named": true
},
{
"type": "type",
"named": true
}
]
}
@ -277,10 +183,30 @@
{
"type": "column_definition",
"named": true
},
{
"type": "table_constraint",
"named": true
}
]
}
},
{
"type": "column_list",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "create_table",
"named": true,
@ -554,6 +480,114 @@
]
}
},
{
"type": "table_constraint",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"refcolumn": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "column_list",
"named": true
},
{
"type": "keyword_constraint",
"named": true
},
{
"type": "keyword_delete",
"named": true
},
{
"type": "keyword_distinct",
"named": true
},
{
"type": "keyword_foreign",
"named": true
},
{
"type": "keyword_full",
"named": true
},
{
"type": "keyword_key",
"named": true
},
{
"type": "keyword_match",
"named": true
},
{
"type": "keyword_not",
"named": true
},
{
"type": "keyword_nulls",
"named": true
},
{
"type": "keyword_on",
"named": true
},
{
"type": "keyword_partial",
"named": true
},
{
"type": "keyword_primary",
"named": true
},
{
"type": "keyword_references",
"named": true
},
{
"type": "keyword_simple",
"named": true
},
{
"type": "keyword_unique",
"named": true
},
{
"type": "keyword_update",
"named": true
},
{
"type": "referencial_action",
"named": true
},
{
"type": "table_reference",
"named": true
}
]
}
},
{
"type": "table_reference",
"named": true,
@ -580,6 +614,120 @@
}
}
},
{
"type": "type",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "char",
"named": true
},
{
"type": "decimal",
"named": true
},
{
"type": "double",
"named": true
},
{
"type": "keyword_bigint",
"named": true
},
{
"type": "keyword_bigserial",
"named": true
},
{
"type": "keyword_boolean",
"named": true
},
{
"type": "keyword_bytea",
"named": true
},
{
"type": "keyword_date",
"named": true
},
{
"type": "keyword_datetime",
"named": true
},
{
"type": "keyword_integer",
"named": true
},
{
"type": "keyword_money",
"named": true
},
{
"type": "keyword_real",
"named": true
},
{
"type": "keyword_serial",
"named": true
},
{
"type": "keyword_smallint",
"named": true
},
{
"type": "keyword_smallserial",
"named": true
},
{
"type": "keyword_text",
"named": true
},
{
"type": "keyword_time",
"named": true
},
{
"type": "keyword_timestamp",
"named": true
},
{
"type": "keyword_timestamptz",
"named": true
},
{
"type": "keyword_uuid",
"named": true
},
{
"type": "keyword_xml",
"named": true
},
{
"type": "numeric",
"named": true
},
{
"type": "varchar",
"named": true
}
]
}
},
{
"type": "varchar",
"named": true,

File diff suppressed because it is too large Load diff

View file

@ -135,33 +135,33 @@ create table foo (
(column_definitions
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
)
(column_definition
name: (identifier)
datatype: (keyword_boolean)
(type (keyword_boolean))
)
(column_definition
name: (identifier)
datatype: (numeric
(type (numeric
(keyword_numeric)
precision: (number)
)
))
)
(column_definition
name: (identifier)
datatype: (keyword_timestamptz)
(type (keyword_timestamptz))
)
(column_definition
name: (identifier)
datatype: (varchar
(type (varchar
(keyword_varchar)
size: (number)
)
))
)
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(type (keyword_uuid))
)
)
)
@ -190,7 +190,7 @@ create table foo (
(column_definitions
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(type (keyword_uuid))
(column_constraint
(keyword_primary)
(keyword_key)
@ -198,7 +198,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_not)
(keyword_null)
@ -206,14 +206,14 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_null)
)
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_not)
(keyword_null)
@ -250,7 +250,7 @@ create table foo (
(column_definitions
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(type (keyword_uuid))
(column_constraint
(keyword_primary)
(keyword_key)
@ -258,7 +258,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(type (keyword_uuid))
(column_constraint
(keyword_null)
)
@ -309,7 +309,7 @@ create table foo (
(column_definitions
(column_definition
name: (identifier)
datatype: (keyword_uuid)
(type (keyword_uuid))
(column_constraint
(keyword_constraint)
name: (identifier)
@ -319,7 +319,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_constraint)
name: (identifier)
@ -329,7 +329,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_constraint)
name: (identifier)
@ -338,7 +338,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_constraint)
name: (identifier)
@ -356,7 +356,7 @@ create table foo (
)
(column_definition
name: (identifier)
datatype: (keyword_text)
(type (keyword_text))
(column_constraint
(keyword_constraint)
name: (identifier)

Binary file not shown.