1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Snowflake Scripting lowering (Phase 8): blocks, branches, loops, and exception handlers —
//! keyword lines flush, statement-list bodies indented.
use sql_dialect_fmt_syntax::{SyntaxKind, SyntaxNode};
use SyntaxKind::*;
use crate::doc::{concat, empty, hard_line, indent, text, Doc};
use super::Lowerer;
impl Lowerer {
/// A `STMT_LIST` (a block / branch / loop / handler body): each statement on its own line with a
/// synthesized terminating `;`, the whole indented one level. Returns the indented body only —
/// the caller emits the surrounding keyword lines (`BEGIN`/`END`, `THEN`, `DO`, …).
fn lower_block_body(&mut self, list: &SyntaxNode) -> Doc {
let mut parts = Vec::new();
for stmt in list.children() {
parts.push(hard_line());
self.reset();
parts.push(self.lower_node(&stmt));
parts.push(text(";"));
}
indent(concat(parts))
}
/// `[DECLARE …] BEGIN <body> [EXCEPTION …] END [label]` — keyword lines flush, bodies indented.
pub(super) fn lower_block(&mut self, node: &SyntaxNode) -> Doc {
let mut parts = Vec::new();
let mut started = false;
for child in node.children_with_tokens() {
if let Some(token) = child.as_token() {
if token.kind().is_trivia() {
continue;
}
match token.kind() {
BEGIN_KW => {
if started {
parts.push(hard_line());
}
self.reset();
parts.push(self.token(token));
started = true;
}
END_KW => {
parts.push(hard_line());
self.reset();
parts.push(self.token(token));
}
_ => parts.push(self.token(token)),
}
} else if let Some(node) = child.as_node() {
match node.kind() {
DECLARE_SECTION => {
parts.push(self.lower_declare_section(node));
started = true;
}
STMT_LIST => parts.push(self.lower_block_body(node)),
EXCEPTION_SECTION => {
parts.push(hard_line());
self.reset();
parts.push(self.lower_exception_section(node));
}
_ => parts.push(self.lower_node(node)), // END label
}
}
}
concat(parts)
}
/// `DECLARE` then each declaration on its own indented line with a synthesized `;`.
pub(super) fn lower_declare_section(&mut self, node: &SyntaxNode) -> Doc {
let mut head = empty();
let mut body = Vec::new();
for child in node.children_with_tokens() {
if let Some(token) = child.as_token() {
if token.kind().is_trivia() {
continue;
}
if token.kind() == DECLARE_KW {
self.reset();
head = self.token(token);
}
} else if let Some(node) = child.as_node() {
body.push(hard_line());
self.reset();
body.push(self.lower_node(node));
body.push(text(";"));
}
}
concat(vec![head, indent(concat(body))])
}
/// `IF <cond> THEN … [ELSEIF … THEN …] [ELSE …] END IF` — branch keywords flush, bodies indented.
pub(super) fn lower_if(&mut self, node: &SyntaxNode) -> Doc {
self.reset();
self.lower_keyword_block(node, |kind| matches!(kind, ELSEIF_KW | ELSE_KW | END_KW))
}
/// `CASE [operand] WHEN … THEN … [ELSE …] END [CASE]` — arms are indented one level, with each
/// arm body using the same statement-list layout as `IF` branches.
pub(super) fn lower_case_stmt(&mut self, node: &SyntaxNode) -> Doc {
let mut parts = Vec::new();
let mut first_case = true;
let mut pending_else = false;
self.reset();
for child in node.children_with_tokens() {
if let Some(token) = child.as_token() {
if token.kind().is_trivia() {
continue;
}
match token.kind() {
CASE_KW if first_case => {
first_case = false;
parts.push(self.token(token));
}
ELSE_KW => {
self.reset();
parts.push(indent(concat(vec![hard_line(), self.token(token)])));
pending_else = true;
}
END_KW => {
parts.push(hard_line());
self.reset();
parts.push(self.token(token));
pending_else = false;
}
_ => parts.push(self.token(token)), // trailing CASE in END CASE
}
} else if let Some(node) = child.as_node() {
match node.kind() {
CASE_STMT_WHEN => {
self.reset();
parts.push(indent(concat(vec![
hard_line(),
self.lower_case_stmt_when(node),
])));
pending_else = false;
}
STMT_LIST if pending_else => {
parts.push(indent(self.lower_block_body(node)));
pending_else = false;
}
_ => parts.push(self.lower_node(node)), // simple CASE operand
}
}
}
concat(parts)
}
/// One `WHEN <test> THEN <body>` arm of a procedural CASE statement.
fn lower_case_stmt_when(&mut self, node: &SyntaxNode) -> Doc {
self.lower_keyword_block(node, |_| false)
}
/// `FOR/WHILE … DO … END`, `LOOP … END LOOP`, `REPEAT … UNTIL … END REPEAT` — body indented.
pub(super) fn lower_loop(&mut self, node: &SyntaxNode) -> Doc {
self.reset();
self.lower_keyword_block(node, |kind| matches!(kind, END_KW | UNTIL_KW))
}
/// `EXCEPTION` then each `WHEN … THEN <body>` handler indented.
pub(super) fn lower_exception_section(&mut self, node: &SyntaxNode) -> Doc {
let mut head = empty();
let mut body = Vec::new();
for child in node.children_with_tokens() {
if let Some(token) = child.as_token() {
if token.kind().is_trivia() {
continue;
}
if token.kind() == EXCEPTION_KW {
self.reset();
head = self.token(token);
}
} else if let Some(node) = child.as_node() {
body.push(hard_line());
self.reset();
body.push(self.lower_exception_when(node));
}
}
concat(vec![head, indent(concat(body))])
}
/// `WHEN <exc> THEN <body>` — the `WHEN … THEN` line then the handler body indented.
fn lower_exception_when(&mut self, node: &SyntaxNode) -> Doc {
self.lower_keyword_block(node, |_| false)
}
fn lower_keyword_block(
&mut self,
node: &SyntaxNode,
break_before: impl Fn(SyntaxKind) -> bool,
) -> Doc {
let mut parts = Vec::new();
for child in node.children_with_tokens() {
if let Some(token) = child.as_token() {
if token.kind().is_trivia() {
continue;
}
if break_before(token.kind()) {
parts.push(hard_line());
self.reset();
}
parts.push(self.token(token));
} else if let Some(node) = child.as_node() {
if node.kind() == STMT_LIST {
parts.push(self.lower_block_body(node));
} else {
parts.push(self.lower_node(node));
}
}
}
concat(parts)
}
}