Skip to content

Commit

Permalink
feat(sets): Dedicated node with optional parens for set operations
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStride committed Jul 5, 2023
1 parent 55ceeea commit 899321d
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 196 deletions.
75 changes: 31 additions & 44 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,11 @@ module.exports = grammar({
),

_dml_read: $ => seq(
choice(
seq(
optional(
$._cte
),
optional($._cte),
optional_parenthesis(
choice(
$._select_statement,
$.set_operation,
),
),
),
Expand All @@ -623,25 +622,30 @@ module.exports = grammar({
')',
),

_select_statement: $ => seq(
$.select,
optional($.from),
repeat(
set_operation: $ => seq(
$._select_statement,
repeat1(
seq(
choice(
seq(
$.keyword_union,
optional($.keyword_all),
field(
"operation",
choice(
seq($.keyword_union, optional($.keyword_all)),
$.keyword_except,
$.keyword_intersect,
),
$.keyword_except,
$.keyword_intersect,
),
$.select,
optional($.from),
$._select_statement,
),
),
),

_select_statement: $ => optional_parenthesis(
seq(
$.select,
optional($.from),
),
),

select: $ => seq(
$.keyword_select,
seq(
Expand Down Expand Up @@ -759,33 +763,7 @@ module.exports = grammar({
),
),

create_query: $ => choice(
$._select_statement,
seq(
$._cte,
$._select_statement,
),
seq(
$._inner_create_query,
repeat(
seq(
$.keyword_union,
optional($.keyword_all),
$._inner_create_query,
),
)
),
),

_inner_create_query: $ => choice(
seq( '(', $._select_statement, ')'),
seq(
'(',
$._cte,
$._select_statement,
')',
),
),
create_query: $ => $._dml_read,

create_view: $ => prec.right(
seq(
Expand Down Expand Up @@ -2496,6 +2474,15 @@ function unsigned_type($, type) {
)
}

function optional_parenthesis(node) {
return prec.right(
choice(
node,
seq("(", node, ")"),
),
)
}

function parametric_type($, type, params = ['size']) {
return prec.right(1,
choice(
Expand Down
6 changes: 3 additions & 3 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,9 @@
(keyword_to)
(keyword_schema)
(keyword_owner)
(keyword_union)
(keyword_all)
(keyword_any)
(keyword_some)
(keyword_except)
(keyword_intersect)
(keyword_returning)
(keyword_begin)
(keyword_commit)
Expand Down Expand Up @@ -314,6 +311,9 @@
(keyword_by)
(keyword_on)
(keyword_do)
(keyword_union)
(keyword_except)
(keyword_intersect)
] @keyword.operator

[
Expand Down
33 changes: 17 additions & 16 deletions test/corpus/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1232,22 +1232,23 @@ UNION ALL
(identifier))
(keyword_as)
(create_query
(select
(keyword_select)
(select_expression
(term
(literal)
(keyword_as)
(identifier))))
(keyword_union)
(keyword_all)
(select
(keyword_select)
(select_expression
(term
(literal)
(keyword_as)
(identifier))))))))
(set_operation
(select
(keyword_select)
(select_expression
(term
(literal)
(keyword_as)
(identifier))))
(keyword_union)
(keyword_all)
(select
(keyword_select)
(select_expression
(term
(literal)
(keyword_as)
(identifier)))))))))

================================================================================
Create view as select with cte
Expand Down
Loading

0 comments on commit 899321d

Please sign in to comment.