Skip to content

Commit 252dd66

Browse files
committed
feat: TSQL WHILE with blocks
1 parent e7891e1 commit 252dd66

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)),
@@ -1501,7 +1508,7 @@ module.exports = grammar({
15011508
field('value', choice($.identifier, alias($._single_quote_string, $.literal))),
15021509
),
15031510

1504-
create_database: $ => seq(
1511+
create_database: $ => prec.left(seq(
15051512
$.keyword_create,
15061513
$.keyword_database,
15071514
optional($._if_not_exists),
@@ -1510,9 +1517,9 @@ module.exports = grammar({
15101517
repeat(
15111518
$._with_settings
15121519
),
1513-
),
1520+
)),
15141521

1515-
create_role: $ => seq(
1522+
create_role: $ => prec.left(seq(
15161523
$.keyword_create,
15171524
choice(
15181525
$.keyword_user,
@@ -1527,7 +1534,7 @@ module.exports = grammar({
15271534
$._role_options,
15281535
),
15291536
),
1530-
),
1537+
)),
15311538

15321539
_role_options: $ => choice(
15331540
field("option", $.identifier),
@@ -1588,7 +1595,7 @@ module.exports = grammar({
15881595
),
15891596
),
15901597

1591-
create_extension: $ => seq(
1598+
create_extension: $ => prec.left(seq(
15921599
$.keyword_create,
15931600
$.keyword_extension,
15941601
optional($._if_not_exists),
@@ -1597,7 +1604,7 @@ module.exports = grammar({
15971604
optional(seq($.keyword_schema, $.identifier)),
15981605
optional(seq($.keyword_version, choice($.identifier, alias($._literal_string, $.literal)))),
15991606
optional($.keyword_cascade),
1600-
),
1607+
)),
16011608

16021609
create_trigger: $ => seq(
16031610
$.keyword_create,
@@ -1661,7 +1668,7 @@ module.exports = grammar({
16611668
$.keyword_truncate,
16621669
),
16631670

1664-
create_type: $ => seq(
1671+
create_type: $ => prec.left(seq(
16651672
$.keyword_create,
16661673
$.keyword_type,
16671674
$.object_reference,
@@ -1692,7 +1699,7 @@ module.exports = grammar({
16921699
),
16931700
),
16941701
),
1695-
),
1702+
)),
16961703

16971704
enum_elements: $ => seq(
16981705
paren_list(field("enum_element", alias($._literal_string, $.literal))),
@@ -1966,7 +1973,7 @@ module.exports = grammar({
19661973
),
19671974
),
19681975

1969-
alter_role: $ => seq(
1976+
alter_role: $ => prec.left(seq(
19701977
$.keyword_alter,
19711978
choice(
19721979
$.keyword_role,
@@ -1993,7 +2000,7 @@ module.exports = grammar({
19932000
),
19942001
)
19952002
),
1996-
),
2003+
)),
19972004

19982005
set_configuration: $ => seq(
19992006
field("option", $.identifier),
@@ -2173,14 +2180,14 @@ module.exports = grammar({
21732180
optional($._drop_behavior)
21742181
),
21752182

2176-
drop_database: $ => seq(
2183+
drop_database: $ => prec.left(seq(
21772184
$.keyword_drop,
21782185
$.keyword_database,
21792186
optional($._if_exists),
21802187
$.identifier,
21812188
optional($.keyword_with),
21822189
optional($.keyword_force),
2183-
),
2190+
)),
21842191

21852192
drop_role: $ => seq(
21862193
$.keyword_drop,
@@ -2468,7 +2475,7 @@ module.exports = grammar({
24682475
repeat1($.when_clause)
24692476
),
24702477

2471-
when_clause: $ => seq(
2478+
when_clause: $ => prec.left(seq(
24722479
$.keyword_when,
24732480
optional($.keyword_not),
24742481
$.keyword_matched,
@@ -2491,7 +2498,7 @@ module.exports = grammar({
24912498
),
24922499
optional($.where)
24932500
)
2494-
),
2501+
)),
24952502

24962503
_optimize_statement: $ => choice(
24972504
$._compute_stats,
@@ -2500,7 +2507,7 @@ module.exports = grammar({
25002507
),
25012508

25022509
// Compute stats for Impala and Hive
2503-
_compute_stats: $ => choice(
2510+
_compute_stats: $ => prec.left(choice(
25042511
// Hive
25052512
seq(
25062513
$.keyword_analyze,
@@ -2538,7 +2545,7 @@ module.exports = grammar({
25382545
)
25392546
)
25402547
),
2541-
),
2548+
)),
25422549

25432550
_optimize_table: $ => choice(
25442551
// Athena/Iceberg
@@ -2568,14 +2575,14 @@ module.exports = grammar({
25682575
),
25692576
),
25702577

2571-
_vacuum_table: $ => seq(
2578+
_vacuum_table: $ => prec.left(seq(
25722579
$.keyword_vacuum,
25732580
optional($._vacuum_option),
25742581
$.object_reference,
25752582
optional(
25762583
paren_list($.field)
25772584
),
2578-
),
2585+
)),
25792586

25802587
_vacuum_option: $ => choice(
25812588
seq($.keyword_full, optional(choice($.keyword_true, $.keyword_false))),
@@ -2759,11 +2766,11 @@ module.exports = grammar({
27592766
')',
27602767
),
27612768

2762-
column_definition: $ => seq(
2769+
column_definition: $ => prec.left(seq(
27632770
field('name', $._column),
27642771
field('type', $._type),
27652772
repeat($._column_constraint),
2766-
),
2773+
)),
27672774

27682775
_column_comment: $ => seq(
27692776
$.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)