Compare commits

...

2 commits

Author SHA1 Message Date
b121cd1fc1
wip 2023-01-06 01:10:59 +03:00
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 // NOTE(chrde): https://github.com/tree-sitter/tree-sitter-javascript/blob/1ddbf1588c353edab37791cdcc9f17e56fb4ea73/grammar.js#L9
extras: ($) => [$.comment, /[\s\uFEFF\u2060\u200B\u00A0]/], extras: ($) => [$.comment, /[\s\uFEFF\u2060\u200B\u00A0]/],
conflicts: ($) => [
[$.op_expression],
[$.select_statement],
[$.from_item],
[$.select_group_by, $._value_expression],
[$.time_expression, $.op_expression],
],
rules: { rules: {
source_file: ($) => source_file: ($) =>
repeat(choice($.psql_statement, seq($._statement, ";"))), repeat(choice($.psql_statement, seq($._statement, ";"))),
@ -160,7 +168,7 @@ module.exports = grammar({
conflict_target: ($) => conflict_target: ($) =>
choice( choice(
seq(kw("on"), kw("constraint"), $.identifier), seq(kw("on"), $._constraint, $.identifier),
seq("(", commaSep($._value_expression), ")") seq("(", commaSep($._value_expression), ")")
), ),
@ -298,7 +306,7 @@ module.exports = grammar({
), ),
seq( seq(
kw("drop"), kw("drop"),
kw("constraint"), $._constraint,
optional($.if_exists), optional($.if_exists),
$.identifier, $.identifier,
optional($.alter_table_fk_ref_action) optional($.alter_table_fk_ref_action)
@ -330,7 +338,7 @@ module.exports = grammar({
table_constraint: ($) => table_constraint: ($) =>
seq( seq(
optional(seq(kw("constraint"), $.identifier)), optional(seq($._constraint, $.identifier)),
$.table_constraint_ty, $.table_constraint_ty,
optional($.constraint_when) optional($.constraint_when)
), ),
@ -379,20 +387,18 @@ module.exports = grammar({
alter_column_type: ($) => alter_column_type: ($) =>
seq($._type, optional(seq(kw("using"), $._value_expression))), seq($._type, optional(seq(kw("using"), $._value_expression))),
alter_table_fk_ref_action: ($) => choice(kw("restrict"), kw("cascade")), alter_table_fk_ref_action: ($) => choice(kw("restrict"), kw("cascade")),
table_column_item: ($) => table_column_item: ($) =>
seq($.identifier, $._type, repeat($.column_constraint)), seq($.identifier, $._type, repeat($.column_constraint)),
column_constraint: ($) => column_constraint: ($) =>
choice(
seq( seq(
kw("constraint"), optional(seq($._constraint, $.identifier)),
$.identifier,
$.column_constraint_ty, $.column_constraint_ty,
optional($.constraint_when) optional($.constraint_when)
), ),
seq($.column_constraint_ty, optional($.constraint_when))
),
column_constraint_ty: ($) => column_constraint_ty: ($) =>
choice( choice(
@ -413,9 +419,12 @@ module.exports = grammar({
kw("to"), kw("to"),
$.identifier $.identifier
), ),
alter_table_rename_constraint: ($) => 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_rename_table: ($) => seq(kw("rename"), kw("to"), $.identifier),
alter_table_change_schema: ($) => alter_table_change_schema: ($) =>
seq(kw("set"), kw("schema"), $.identifier), seq(kw("set"), kw("schema"), $.identifier),
@ -517,7 +526,7 @@ module.exports = grammar({
create_trigger_statement: ($) => create_trigger_statement: ($) =>
seq( seq(
kw("create"), kw("create"),
optional(kw("constraint")), optional($._constraint),
kw("trigger"), kw("trigger"),
$.identifier, $.identifier,
$.trigger_when, $.trigger_when,
@ -663,7 +672,6 @@ module.exports = grammar({
do_block: ($) => seq(kw("do"), $.block), do_block: ($) => seq(kw("do"), $.block),
select_statement: ($) => select_statement: ($) =>
prec.left(
seq( seq(
optional($.with_query), optional($.with_query),
kw("select"), kw("select"),
@ -676,7 +684,6 @@ module.exports = grammar({
optional($.select_order_by), optional($.select_order_by),
optional($._select_limit_offset), optional($._select_limit_offset),
optional($.into) optional($.into)
)
), ),
with_query: ($) => seq(kw("with"), commaSep1($.with_query_item)), with_query: ($) => seq(kw("with"), commaSep1($.with_query_item)),
@ -717,8 +724,6 @@ module.exports = grammar({
// TODO(chrde): rollup, cube, grouping sets // TODO(chrde): rollup, cube, grouping sets
select_group_by: ($) => select_group_by: ($) =>
prec(
1,
seq( seq(
kw("group"), kw("group"),
kw("by"), kw("by"),
@ -726,7 +731,6 @@ module.exports = grammar({
seq("(", commaSep1($._value_expression), ")"), seq("(", commaSep1($._value_expression), ")"),
commaSep1($._value_expression) commaSep1($._value_expression)
) )
)
), ),
select_order_by: ($) => select_order_by: ($) =>
@ -745,12 +749,10 @@ module.exports = grammar({
select_from: ($) => seq(kw("from"), commaSep1($.from_item)), select_from: ($) => seq(kw("from"), commaSep1($.from_item)),
from_item: ($) => from_item: ($) =>
prec.left(
seq( seq(
// TODO(chrde): https://www.postgresql.org/docs/current/sql-select.html // TODO(chrde): https://www.postgresql.org/docs/current/sql-select.html
choice($.from_select, $.from_table, $.from_function), choice($.from_select, $.from_table, $.from_function),
repeat($.join_item) repeat($.join_item)
)
), ),
from_select: ($) => from_select: ($) =>
@ -772,12 +774,10 @@ module.exports = grammar({
), ),
join_item: ($) => join_item: ($) =>
prec.left(
choice( choice(
seq(kw("natural"), $.join_type, $.from_item), seq(kw("natural"), $.join_type, $.from_item),
seq($.join_type, $.from_item, $.join_condition), seq($.join_type, $.from_item, $.join_condition),
seq(kw("cross"), kw("join"), $.from_item) seq(kw("cross"), kw("join"), $.from_item)
)
), ),
join_condition: ($) => join_condition: ($) =>
@ -912,8 +912,7 @@ module.exports = grammar({
type_length: ($) => seq("(", $.number, ")"), type_length: ($) => seq("(", $.number, ")"),
string: ($) => string: ($) => seq("'", repeat(choice(/''/, /[^']/)), "'"),
seq("'", repeat(choice(prec(1, /''/), prec(2, /[^']/))), "'"),
// NOTE(chrde): taken from https://github.com/tree-sitter/tree-sitter-javascript/blob/1ddbf1588c353edab37791cdcc9f17e56fb4ea73/grammar.js#L899 // NOTE(chrde): taken from https://github.com/tree-sitter/tree-sitter-javascript/blob/1ddbf1588c353edab37791cdcc9f17e56fb4ea73/grammar.js#L899
comment: ($) => comment: ($) =>
@ -1002,37 +1001,20 @@ module.exports = grammar({
op_expression: ($) => op_expression: ($) =>
choice( choice(
prec.left(12, seq($._value_expression, $.cast, $._type)), seq($._value_expression, $.cast, $._type),
// array access // array access
prec.right(10, seq(choice($.minus, $.plus), $._value_expression)), seq(choice($.minus, $.plus), $._value_expression),
// ^ // ^
prec.left( seq($._value_expression, choice("*", "/", "%"), $._value_expression),
8, 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),
prec.left( seq($._value_expression, $.comparison_op, $._value_expression),
7, seq($._value_expression, $.comparison_kw, $._value_expression),
seq($._value_expression, choice("-", "+"), $._value_expression) seq($._value_expression, $.comparison_null),
), seq($.not, $._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($.and, $.or), $._value_expression) seq($._value_expression, choice($.and, $.or), $._value_expression)
)
), ),
_list_of_identifiers: ($) => seq("(", commaSep($.identifier), ")"), _list_of_identifiers: ($) => seq("(", commaSep($.identifier), ")"),
@ -1083,5 +1065,8 @@ module.exports = grammar({
_identifier: ($) => /[a-zA-Z0-9_]+(\.?[a-zA-Z0-9_]+)*/, _identifier: ($) => /[a-zA-Z0-9_]+(\.?[a-zA-Z0-9_]+)*/,
// ^ // ^
// |- we dont want to match consecutive dots, e.g: 1..2 consists of 3 tokens // |- 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]" "value": "[oO][nN]"
}, },
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1919,8 +1919,8 @@
"value": "[dD][rR][oO][pP]" "value": "[dD][rR][oO][pP]"
}, },
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
@ -2153,8 +2153,8 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -2488,18 +2488,28 @@
] ]
}, },
"column_constraint": { "column_constraint": {
"type": "SEQ",
"members": [
{
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "identifier"
}
]
},
{
"type": "BLANK"
}
]
}, },
{ {
"type": "SYMBOL", "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": { "column_constraint_ty": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -2660,8 +2647,8 @@
"value": "[rR][eE][nN][aA][mM][eE]" "value": "[rR][eE][nN][aA][mM][eE]"
}, },
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -3457,8 +3444,8 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "PATTERN", "type": "SYMBOL",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]" "name": "_constraint"
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -4307,9 +4294,6 @@
] ]
}, },
"select_statement": { "select_statement": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -4458,7 +4442,6 @@
] ]
} }
] ]
}
}, },
"with_query": { "with_query": {
"type": "SEQ", "type": "SEQ",
@ -4724,9 +4707,6 @@
] ]
}, },
"select_group_by": { "select_group_by": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -4806,7 +4786,6 @@
] ]
} }
] ]
}
}, },
"select_order_by": { "select_order_by": {
"type": "SEQ", "type": "SEQ",
@ -4952,9 +4931,6 @@
] ]
}, },
"from_item": { "from_item": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -4982,7 +4958,6 @@
} }
} }
] ]
}
}, },
"from_select": { "from_select": {
"type": "SEQ", "type": "SEQ",
@ -5132,9 +5107,6 @@
] ]
}, },
"join_item": { "join_item": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -5189,7 +5161,6 @@
] ]
} }
] ]
}
}, },
"join_condition": { "join_condition": {
"type": "CHOICE", "type": "CHOICE",
@ -6014,21 +5985,13 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN", "type": "PATTERN",
"value": "''" "value": "''"
}
}, },
{ {
"type": "PREC",
"value": 2,
"content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^']" "value": "[^']"
} }
}
] ]
} }
}, },
@ -6550,9 +6513,6 @@
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "PREC_LEFT",
"value": 12,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6568,12 +6528,8 @@
"name": "_type" "name": "_type"
} }
] ]
}
}, },
{ {
"type": "PREC_RIGHT",
"value": 10,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6594,12 +6550,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 8,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6628,12 +6580,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 7,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6658,12 +6606,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 6,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6679,12 +6623,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 5,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6700,12 +6640,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 4,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6721,12 +6657,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 3,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6742,12 +6674,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 3,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6759,12 +6687,8 @@
"name": "comparison_null" "name": "comparison_null"
} }
] ]
}
}, },
{ {
"type": "PREC_RIGHT",
"value": 2,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6776,12 +6700,8 @@
"name": "_value_expression" "name": "_value_expression"
} }
] ]
}
}, },
{ {
"type": "PREC_LEFT",
"value": 1,
"content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -6807,7 +6727,6 @@
} }
] ]
} }
}
] ]
}, },
"_list_of_identifiers": { "_list_of_identifiers": {
@ -7044,6 +6963,10 @@
"_identifier": { "_identifier": {
"type": "PATTERN", "type": "PATTERN",
"value": "[a-zA-Z0-9_]+(\\.?[a-zA-Z0-9_]+)*" "value": "[a-zA-Z0-9_]+(\\.?[a-zA-Z0-9_]+)*"
},
"_constraint": {
"type": "PATTERN",
"value": "[cC][oO][nN][sS][tT][rR][aA][iI][nN][tT]"
} }
}, },
"extras": [ "extras": [
@ -7056,7 +6979,25 @@
"value": "[\\s\\uFEFF\\u2060\\u200B\\u00A0]" "value": "[\\s\\uFEFF\\u2060\\u200B\\u00A0]"
} }
], ],
"conflicts": [], "conflicts": [
[
"op_expression"
],
[
"select_statement"
],
[
"from_item"
],
[
"select_group_by",
"_value_expression"
],
[
"time_expression",
"op_expression"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "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 (table_constraint
(identifier) (identifier)
(table_constraint_ty (table_constraint_ty
(identifier))))))) (identifier)
)
)
)
)
)
)
================================================================================ ================================================================================
column drop constraint column drop constraint