Compare commits

...

2 Commits

Author SHA1 Message Date
Dmitriy Pleshevskiy b121cd1fc1
wip 2023-01-06 01:10:59 +03:00
Dmitriy Pleshevskiy dc314830d2
grammar: move constraint to hidden rule 2023-01-06 01:01:38 +03:00
4 changed files with 57153 additions and 67358 deletions

View File

@ -27,6 +27,14 @@ module.exports = grammar({
// NOTE(chrde): https://github.com/tree-sitter/tree-sitter-javascript/blob/1ddbf1588c353edab37791cdcc9f17e56fb4ea73/grammar.js#L9
extras: ($) => [$.comment, /[\s\uFEFF\u2060\u200B\u00A0]/],
conflicts: ($) => [
[$.op_expression],
[$.select_statement],
[$.from_item],
[$.select_group_by, $._value_expression],
[$.time_expression, $.op_expression],
],
rules: {
source_file: ($) =>
repeat(choice($.psql_statement, seq($._statement, ";"))),
@ -160,7 +168,7 @@ module.exports = grammar({
conflict_target: ($) =>
choice(
seq(kw("on"), kw("constraint"), $.identifier),
seq(kw("on"), $._constraint, $.identifier),
seq("(", commaSep($._value_expression), ")")
),
@ -298,7 +306,7 @@ module.exports = grammar({
),
seq(
kw("drop"),
kw("constraint"),
$._constraint,
optional($.if_exists),
$.identifier,
optional($.alter_table_fk_ref_action)
@ -330,7 +338,7 @@ module.exports = grammar({
table_constraint: ($) =>
seq(
optional(seq(kw("constraint"), $.identifier)),
optional(seq($._constraint, $.identifier)),
$.table_constraint_ty,
optional($.constraint_when)
),
@ -379,20 +387,18 @@ module.exports = grammar({
alter_column_type: ($) =>
seq($._type, optional(seq(kw("using"), $._value_expression))),
alter_table_fk_ref_action: ($) => choice(kw("restrict"), kw("cascade")),
table_column_item: ($) =>
seq($.identifier, $._type, repeat($.column_constraint)),
column_constraint: ($) =>
choice(
seq(
kw("constraint"),
$.identifier,
optional(seq($._constraint, $.identifier)),
$.column_constraint_ty,
optional($.constraint_when)
),
seq($.column_constraint_ty, optional($.constraint_when))
),
column_constraint_ty: ($) =>
choice(
@ -413,9 +419,12 @@ module.exports = grammar({
kw("to"),
$.identifier
),
alter_table_rename_constraint: ($) =>
seq(kw("rename"), kw("constraint"), $.identifier, kw("to"), $.identifier),
seq(kw("rename"), $._constraint, $.identifier, kw("to"), $.identifier),
alter_table_rename_table: ($) => seq(kw("rename"), kw("to"), $.identifier),
alter_table_change_schema: ($) =>
seq(kw("set"), kw("schema"), $.identifier),
@ -517,7 +526,7 @@ module.exports = grammar({
create_trigger_statement: ($) =>
seq(
kw("create"),
optional(kw("constraint")),
optional($._constraint),
kw("trigger"),
$.identifier,
$.trigger_when,
@ -663,7 +672,6 @@ module.exports = grammar({
do_block: ($) => seq(kw("do"), $.block),
select_statement: ($) =>
prec.left(
seq(
optional($.with_query),
kw("select"),
@ -676,7 +684,6 @@ module.exports = grammar({
optional($.select_order_by),
optional($._select_limit_offset),
optional($.into)
)
),
with_query: ($) => seq(kw("with"), commaSep1($.with_query_item)),
@ -717,8 +724,6 @@ module.exports = grammar({
// TODO(chrde): rollup, cube, grouping sets
select_group_by: ($) =>
prec(
1,
seq(
kw("group"),
kw("by"),
@ -726,7 +731,6 @@ module.exports = grammar({
seq("(", commaSep1($._value_expression), ")"),
commaSep1($._value_expression)
)
)
),
select_order_by: ($) =>
@ -745,12 +749,10 @@ module.exports = grammar({
select_from: ($) => seq(kw("from"), commaSep1($.from_item)),
from_item: ($) =>
prec.left(
seq(
// TODO(chrde): https://www.postgresql.org/docs/current/sql-select.html
choice($.from_select, $.from_table, $.from_function),
repeat($.join_item)
)
),
from_select: ($) =>
@ -772,12 +774,10 @@ module.exports = grammar({
),
join_item: ($) =>
prec.left(
choice(
seq(kw("natural"), $.join_type, $.from_item),
seq($.join_type, $.from_item, $.join_condition),
seq(kw("cross"), kw("join"), $.from_item)
)
),
join_condition: ($) =>
@ -912,8 +912,7 @@ module.exports = grammar({
type_length: ($) => seq("(", $.number, ")"),
string: ($) =>
seq("'", repeat(choice(prec(1, /''/), prec(2, /[^']/))), "'"),
string: ($) => seq("'", repeat(choice(/''/, /[^']/)), "'"),
// NOTE(chrde): taken from https://github.com/tree-sitter/tree-sitter-javascript/blob/1ddbf1588c353edab37791cdcc9f17e56fb4ea73/grammar.js#L899
comment: ($) =>
@ -1002,37 +1001,20 @@ module.exports = grammar({
op_expression: ($) =>
choice(
prec.left(12, seq($._value_expression, $.cast, $._type)),
seq($._value_expression, $.cast, $._type),
// array access
prec.right(10, seq(choice($.minus, $.plus), $._value_expression)),
seq(choice($.minus, $.plus), $._value_expression),
// ^
prec.left(
8,
seq($._value_expression, choice("*", "/", "%"), $._value_expression)
),
prec.left(
7,
seq($._value_expression, choice("-", "+"), $._value_expression)
),
prec.left(6, seq($._value_expression, $.other_op, $._value_expression)),
prec.left(
5,
seq($._value_expression, $.contains_op, $._value_expression)
),
prec.left(
4,
seq($._value_expression, $.comparison_op, $._value_expression)
),
prec.left(
3,
seq($._value_expression, $.comparison_kw, $._value_expression)
),
prec.left(3, seq($._value_expression, $.comparison_null)),
prec.right(2, seq($.not, $._value_expression)),
prec.left(
1,
seq($._value_expression, choice("*", "/", "%"), $._value_expression),
seq($._value_expression, choice("-", "+"), $._value_expression),
seq($._value_expression, $.other_op, $._value_expression),
seq($._value_expression, $.contains_op, $._value_expression),
seq($._value_expression, $.comparison_op, $._value_expression),
seq($._value_expression, $.comparison_kw, $._value_expression),
seq($._value_expression, $.comparison_null),
seq($.not, $._value_expression),
seq($._value_expression, choice($.and, $.or), $._value_expression)
)
),
_list_of_identifiers: ($) => seq("(", commaSep($.identifier), ")"),
@ -1083,5 +1065,8 @@ module.exports = grammar({
_identifier: ($) => /[a-zA-Z0-9_]+(\.?[a-zA-Z0-9_]+)*/,
// ^
// |- we dont want to match consecutive dots, e.g: 1..2 consists of 3 tokens
//--------------------------
_constraint: ($) => kw("constraint"),
},
});

View File

@ -955,8 +955,8 @@
"value": "[oO][nN]"
},
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "SYMBOL",
@ -1919,8 +1919,8 @@
"value": "[dD][rR][oO][pP]"
},
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "CHOICE",
@ -2153,8 +2153,8 @@
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "SYMBOL",
@ -2488,18 +2488,28 @@
]
},
"column_constraint": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
@ -2519,29 +2529,6 @@
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "column_constraint_ty"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "constraint_when"
},
{
"type": "BLANK"
}
]
}
]
}
]
},
"column_constraint_ty": {
"type": "CHOICE",
"members": [
@ -2660,8 +2647,8 @@
"value": "[rR][eE][nN][aA][mM][eE]"
},
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "SYMBOL",
@ -3457,8 +3444,8 @@
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
"type": "SYMBOL",
"name": "_constraint"
},
{
"type": "BLANK"
@ -4307,9 +4294,6 @@
]
},
"select_statement": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
@ -4458,7 +4442,6 @@
]
}
]
}
},
"with_query": {
"type": "SEQ",
@ -4724,9 +4707,6 @@
]
},
"select_group_by": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
@ -4806,7 +4786,6 @@
]
}
]
}
},
"select_order_by": {
"type": "SEQ",
@ -4952,9 +4931,6 @@
]
},
"from_item": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
@ -4982,7 +4958,6 @@
}
}
]
}
},
"from_select": {
"type": "SEQ",
@ -5132,9 +5107,6 @@
]
},
"join_item": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
@ -5189,7 +5161,6 @@
]
}
]
}
},
"join_condition": {
"type": "CHOICE",
@ -6014,21 +5985,13 @@
"type": "CHOICE",
"members": [
{
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "''"
}
},
{
"type": "PREC",
"value": 2,
"content": {
"type": "PATTERN",
"value": "[^']"
}
}
]
}
},
@ -6550,9 +6513,6 @@
"type": "CHOICE",
"members": [
{
"type": "PREC_LEFT",
"value": 12,
"content": {
"type": "SEQ",
"members": [
{
@ -6568,12 +6528,8 @@
"name": "_type"
}
]
}
},
{
"type": "PREC_RIGHT",
"value": 10,
"content": {
"type": "SEQ",
"members": [
{
@ -6594,12 +6550,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 8,
"content": {
"type": "SEQ",
"members": [
{
@ -6628,12 +6580,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 7,
"content": {
"type": "SEQ",
"members": [
{
@ -6658,12 +6606,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 6,
"content": {
"type": "SEQ",
"members": [
{
@ -6679,12 +6623,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 5,
"content": {
"type": "SEQ",
"members": [
{
@ -6700,12 +6640,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 4,
"content": {
"type": "SEQ",
"members": [
{
@ -6721,12 +6657,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 3,
"content": {
"type": "SEQ",
"members": [
{
@ -6742,12 +6674,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 3,
"content": {
"type": "SEQ",
"members": [
{
@ -6759,12 +6687,8 @@
"name": "comparison_null"
}
]
}
},
{
"type": "PREC_RIGHT",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
@ -6776,12 +6700,8 @@
"name": "_value_expression"
}
]
}
},
{
"type": "PREC_LEFT",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
@ -6807,7 +6727,6 @@
}
]
}
}
]
},
"_list_of_identifiers": {
@ -7044,6 +6963,10 @@
"_identifier": {
"type": "PATTERN",
"value": "[a-zA-Z0-9_]+(\\.?[a-zA-Z0-9_]+)*"
},
"_constraint": {
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
}
},
"extras": [
@ -7056,7 +6979,25 @@
"value": "[\\s\\uFEFF\\u2060\\u200B\\u00A0]"
}
],
"conflicts": [],
"conflicts": [
[
"op_expression"
],
[
"select_statement"
],
[
"from_item"
],
[
"select_group_by",
"_value_expression"
],
[
"time_expression",
"op_expression"
]
],
"precedences": [],
"externals": [],
"inline": [],

123217
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -223,7 +223,13 @@ alter table foo add constraint u_bar unique(bar);
(table_constraint
(identifier)
(table_constraint_ty
(identifier)))))))
(identifier)
)
)
)
)
)
)
================================================================================
column drop constraint