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,19 +387,17 @@ 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,
$.column_constraint_ty,
optional($.constraint_when)
),
seq($.column_constraint_ty, optional($.constraint_when))
seq(
optional(seq($._constraint, $.identifier)),
$.column_constraint_ty,
optional($.constraint_when)
),
column_constraint_ty: ($) =>
@ -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,20 +672,18 @@ module.exports = grammar({
do_block: ($) => seq(kw("do"), $.block),
select_statement: ($) =>
prec.left(
seq(
optional($.with_query),
kw("select"),
commaSep($.select_item),
optional($.into),
optional($.select_from),
optional($.select_where),
optional($.select_group_by),
optional($.select_having),
optional($.select_order_by),
optional($._select_limit_offset),
optional($.into)
)
seq(
optional($.with_query),
kw("select"),
commaSep($.select_item),
optional($.into),
optional($.select_from),
optional($.select_where),
optional($.select_group_by),
optional($.select_having),
optional($.select_order_by),
optional($._select_limit_offset),
optional($.into)
),
with_query: ($) => seq(kw("with"), commaSep1($.with_query_item)),
@ -717,15 +724,12 @@ module.exports = grammar({
// TODO(chrde): rollup, cube, grouping sets
select_group_by: ($) =>
prec(
1,
seq(
kw("group"),
kw("by"),
choice(
seq("(", commaSep1($._value_expression), ")"),
commaSep1($._value_expression)
)
seq(
kw("group"),
kw("by"),
choice(
seq("(", commaSep1($._value_expression), ")"),
commaSep1($._value_expression)
)
),
@ -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)
)
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)
)
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($.and, $.or), $._value_expression)
)
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"),
},
});

File diff suppressed because it is too large Load Diff

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