Skip to content

Commit 4ef3dc9

Browse files
committed
feat: TSQL WHILE with blocks
1 parent c9e6547 commit 4ef3dc9

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed

grammar.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,14 @@ module.exports = grammar({
704704
while_statement: $ => seq(
705705
$.keyword_while,
706706
optional_parenthesis($._expression),
707-
$.statement,
707+
choice(
708+
$.statement,
709+
seq(
710+
$.keyword_begin,
711+
repeat($.statement),
712+
$.keyword_end,
713+
),
714+
),
708715
),
709716

710717
var_declarations: $ => seq($.keyword_declare, repeat1($.var_declaration)),
@@ -1491,7 +1498,7 @@ module.exports = grammar({
14911498
field('value', choice($.identifier, alias($._single_quote_string, $.literal))),
14921499
),
14931500

1494-
create_database: $ => seq(
1501+
create_database: $ => prec.left(seq(
14951502
$.keyword_create,
14961503
$.keyword_database,
14971504
optional($._if_not_exists),
@@ -1500,9 +1507,9 @@ module.exports = grammar({
15001507
repeat(
15011508
$._with_settings
15021509
),
1503-
),
1510+
)),
15041511

1505-
create_role: $ => seq(
1512+
create_role: $ => prec.left(seq(
15061513
$.keyword_create,
15071514
choice(
15081515
$.keyword_user,
@@ -1517,7 +1524,7 @@ module.exports = grammar({
15171524
$._role_options,
15181525
),
15191526
),
1520-
),
1527+
)),
15211528

15221529
_role_options: $ => choice(
15231530
field("option", $.identifier),
@@ -1578,7 +1585,7 @@ module.exports = grammar({
15781585
),
15791586
),
15801587

1581-
create_extension: $ => seq(
1588+
create_extension: $ => prec.left(seq(
15821589
$.keyword_create,
15831590
$.keyword_extension,
15841591
optional($._if_not_exists),
@@ -1587,7 +1594,7 @@ module.exports = grammar({
15871594
optional(seq($.keyword_schema, $.identifier)),
15881595
optional(seq($.keyword_version, choice($.identifier, alias($._literal_string, $.literal)))),
15891596
optional($.keyword_cascade),
1590-
),
1597+
)),
15911598

15921599
create_trigger: $ => seq(
15931600
$.keyword_create,
@@ -1651,7 +1658,7 @@ module.exports = grammar({
16511658
$.keyword_truncate,
16521659
),
16531660

1654-
create_type: $ => seq(
1661+
create_type: $ => prec.left(seq(
16551662
$.keyword_create,
16561663
$.keyword_type,
16571664
$.object_reference,
@@ -1682,7 +1689,7 @@ module.exports = grammar({
16821689
),
16831690
),
16841691
),
1685-
),
1692+
)),
16861693

16871694
enum_elements: $ => seq(
16881695
paren_list(field("enum_element", alias($._literal_string, $.literal))),
@@ -1956,7 +1963,7 @@ module.exports = grammar({
19561963
),
19571964
),
19581965

1959-
alter_role: $ => seq(
1966+
alter_role: $ => prec.left(seq(
19601967
$.keyword_alter,
19611968
choice(
19621969
$.keyword_role,
@@ -1983,7 +1990,7 @@ module.exports = grammar({
19831990
),
19841991
)
19851992
),
1986-
),
1993+
)),
19871994

19881995
set_configuration: $ => seq(
19891996
field("option", $.identifier),
@@ -2163,14 +2170,14 @@ module.exports = grammar({
21632170
optional($._drop_behavior)
21642171
),
21652172

2166-
drop_database: $ => seq(
2173+
drop_database: $ => prec.left(seq(
21672174
$.keyword_drop,
21682175
$.keyword_database,
21692176
optional($._if_exists),
21702177
$.identifier,
21712178
optional($.keyword_with),
21722179
optional($.keyword_force),
2173-
),
2180+
)),
21742181

21752182
drop_role: $ => seq(
21762183
$.keyword_drop,
@@ -2458,7 +2465,7 @@ module.exports = grammar({
24582465
repeat1($.when_clause)
24592466
),
24602467

2461-
when_clause: $ => seq(
2468+
when_clause: $ => prec.left(seq(
24622469
$.keyword_when,
24632470
optional($.keyword_not),
24642471
$.keyword_matched,
@@ -2481,7 +2488,7 @@ module.exports = grammar({
24812488
),
24822489
optional($.where)
24832490
)
2484-
),
2491+
)),
24852492

24862493
_optimize_statement: $ => choice(
24872494
$._compute_stats,
@@ -2490,7 +2497,7 @@ module.exports = grammar({
24902497
),
24912498

24922499
// Compute stats for Impala and Hive
2493-
_compute_stats: $ => choice(
2500+
_compute_stats: $ => prec.left(choice(
24942501
// Hive
24952502
seq(
24962503
$.keyword_analyze,
@@ -2528,7 +2535,7 @@ module.exports = grammar({
25282535
)
25292536
)
25302537
),
2531-
),
2538+
)),
25322539

25332540
_optimize_table: $ => choice(
25342541
// Athena/Iceberg
@@ -2558,14 +2565,14 @@ module.exports = grammar({
25582565
),
25592566
),
25602567

2561-
_vacuum_table: $ => seq(
2568+
_vacuum_table: $ => prec.left(seq(
25622569
$.keyword_vacuum,
25632570
optional($._vacuum_option),
25642571
$.object_reference,
25652572
optional(
25662573
paren_list($.field)
25672574
),
2568-
),
2575+
)),
25692576

25702577
_vacuum_option: $ => choice(
25712578
seq($.keyword_full, optional(choice($.keyword_true, $.keyword_false))),
@@ -2749,11 +2756,11 @@ module.exports = grammar({
27492756
')',
27502757
),
27512758

2752-
column_definition: $ => seq(
2759+
column_definition: $ => prec.left(seq(
27532760
field('name', $._column),
27542761
field('type', $._type),
27552762
repeat($._column_constraint),
2756-
),
2763+
)),
27572764

27582765
_column_comment: $ => seq(
27592766
$.keyword_comment,

queries/highlights.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
(keyword_storage)
321321
(keyword_compression)
322322
(keyword_duplicate)
323+
(keyword_while)
323324
] @keyword
324325

325326
[

test/corpus/while.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,79 @@ WHILE (
7373
(relation
7474
(object_reference
7575
(identifier))))))))
76+
77+
================================================================================
78+
While in TSQL with SELECT many statements
79+
================================================================================
80+
81+
WHILE (
82+
SELECT AVG(ListPrice)
83+
FROM Production.Product
84+
) < $300
85+
BEGIN
86+
UPDATE Production.Product
87+
SET ListPrice = ListPrice * 2
88+
89+
SELECT MAX(ListPrice)
90+
FROM Production.Product
91+
END
92+
93+
--------------------------------------------------------------------------------
94+
95+
(program
96+
(statement
97+
(while_statement
98+
(keyword_while)
99+
(binary_expression
100+
(subquery
101+
(select
102+
(keyword_select)
103+
(select_expression
104+
(term
105+
(invocation
106+
(object_reference
107+
(identifier))
108+
(term
109+
(field
110+
(identifier)))))))
111+
(from
112+
(keyword_from)
113+
(relation
114+
(object_reference
115+
(identifier)
116+
(identifier)))))
117+
(parameter))
118+
(keyword_begin)
119+
(statement
120+
(update
121+
(keyword_update)
122+
(relation
123+
(object_reference
124+
(identifier)
125+
(identifier)))
126+
(keyword_set)
127+
(assignment
128+
(field
129+
(identifier))
130+
(binary_expression
131+
(field
132+
(identifier))
133+
(literal)))))
134+
(statement
135+
(select
136+
(keyword_select)
137+
(select_expression
138+
(term
139+
(invocation
140+
(object_reference
141+
(identifier))
142+
(term
143+
(field
144+
(identifier)))))))
145+
(from
146+
(keyword_from)
147+
(relation
148+
(object_reference
149+
(identifier)
150+
(identifier)))))
151+
(keyword_end))))

0 commit comments

Comments
 (0)