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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! `If`/`Then`/`Else`/`ElseIf` statement parsing for VB6 CST.
//!
//! This module handles parsing of VB6 conditional statements:
//! - `If`/`Then`/`Else` statements (both single-line and multi-line)
//! - `ElseIf` clauses
//! - `Else` clauses
use crate::language::Token;
use crate::parsers::SyntaxKind;
use super::Parser;
impl Parser<'_> {
/// Parse an `If` statement: `If` condition `Then` ... `End If`
/// Handles both single-line and multi-line `If` statements.
///
/// If a compiler directive prefix (`#`) is detected before `If`,
/// delegates to `parse_compiler_directive` instead.
///
/// `IfStatement`
/// ├─ `If` keyword
/// ├─ condition tokens
/// ├─ `Then` keyword
/// ├─ body tokens
/// ├─ `ElseIfClause` (if present)
/// │ ├─ `ElseIf` keyword
/// │ ├─ condition tokens
/// │ ├─ `Then` keyword
/// │ └─ body tokens
/// ├─ `ElseClause` (if present)
/// │ ├─ `Else` keyword
/// │ └─ body tokens
/// ├─ `End` keyword
/// └─ `If` keyword
///
pub(crate) fn parse_if_statement(&mut self) {
if self.at_compiler_directive_keyword(Token::IfKeyword) {
self.parse_compiler_directive();
return;
}
self.parsing_header = false;
self.builder.start_node(SyntaxKind::IfStatement.to_raw());
self.consume_whitespace();
self.consume_token(); // If
self.consume_whitespace();
self.parse_expression();
self.consume_whitespace();
if self.at_token(Token::ThenKeyword) {
self.consume_token();
}
self.consume_whitespace();
// Skip trailing comment on the Then line — a comment after Then
// does not constitute a single-line If body.
while self.at_token(Token::EndOfLineComment) || self.at_token(Token::RemComment) {
self.consume_token();
self.consume_whitespace();
}
// Check if single-line If
let is_single_line = !self.at_token(Token::Newline) && !self.is_at_end();
if is_single_line {
self.parse_single_line_if_statement();
return;
}
// Multi-line If: parse body and ElseIf/Else clauses
if self.at_token(Token::Newline) {
self.consume_token();
}
// Parse If body - the recursive call here is now safe because
// parse_statement_list handles control flow iteratively
self.parse_statement_list(|parser| {
(parser.at_token(Token::EndKeyword)
&& parser.peek_next_keyword() == Some(Token::IfKeyword))
|| parser.at_compiler_end_if_directive()
|| parser.at_token(Token::ElseIfKeyword)
|| parser.at_compiler_directive_keyword(Token::ElseIfKeyword)
|| parser.at_token(Token::ElseKeyword)
|| parser.at_compiler_directive_keyword(Token::ElseKeyword)
});
// Handle ElseIf and Else clauses
while !self.is_at_end() {
if self.at_compiler_directive_keyword(Token::ElseIfKeyword) {
self.consume_compiler_directive_prefix();
}
if self.at_token(Token::ElseIfKeyword) {
self.builder.start_node(SyntaxKind::ElseIfClause.to_raw());
self.consume_token(); // ElseIf
self.consume_whitespace();
self.parse_expression();
self.consume_whitespace();
if self.at_token(Token::ThenKeyword) {
self.consume_token();
}
self.consume_whitespace();
if self.at_token(Token::Newline) {
self.consume_token();
}
// Parse ElseIf body
self.parse_statement_list(|parser| {
parser.at_token(Token::ElseIfKeyword)
|| parser.at_compiler_directive_keyword(Token::ElseIfKeyword)
|| parser.at_token(Token::ElseKeyword)
|| parser.at_compiler_directive_keyword(Token::ElseKeyword)
|| (parser.at_token(Token::EndKeyword)
&& parser.peek_next_keyword() == Some(Token::IfKeyword))
|| parser.at_compiler_end_if_directive()
});
self.builder.finish_node(); // ElseIfClause
} else if self.at_compiler_directive_keyword(Token::ElseKeyword) {
self.consume_compiler_directive_prefix();
} else if self.at_token(Token::ElseKeyword) {
self.builder.start_node(SyntaxKind::ElseClause.to_raw());
self.consume_token(); // Else
self.consume_whitespace();
if self.at_token(Token::Newline) {
self.consume_token();
}
// Parse Else body
self.parse_statement_list(|parser| {
parser.at_token(Token::EndKeyword)
&& parser.peek_next_keyword() == Some(Token::IfKeyword)
|| parser.at_compiler_end_if_directive()
});
self.builder.finish_node(); // ElseClause
} else {
break;
}
}
// Consume "End If"
if self.at_compiler_end_if_directive() {
self.consume_compiler_directive_prefix();
}
if self.at_token(Token::EndKeyword) {
self.consume_token();
self.consume_whitespace();
self.consume_token(); // If
self.consume_until_after(Token::Newline);
}
self.builder.finish_node(); // IfStatement
}
/// Parse a compiler directive (`#If ... [#ElseIf ...] [#Else ...] #End If`).
///
/// Creates a `CompilerDirective` with:
/// - `CompilerIfClause` wrapping the `#If` condition line
/// - `StatementList` for the if-body
/// - optional `CompilerElseIfClause` + `StatementList`
/// - optional `CompilerElseClause` + `StatementList`
/// - `CompilerEndIfClause` wrapping `#End If`
///
/// The body `StatementList` uses stop conditions for `#ElseIf`, `#Else`,
/// and `#End If`. When those tokens appear inside a nested regular
/// `IfStatement` frame, the iterative engine consumes them via its own
/// stop logic (preserving existing behaviour). In contexts where no
/// regular `IfStatement` consumes them (e.g. module-level `#If`),
/// the body stops at the directive token and this function handles the
/// clause.
///
/// Caller MUST have verified `at_compiler_directive_keyword(Token::IfKeyword)` first.
pub(crate) fn parse_compiler_directive(&mut self) {
self.parsing_header = false;
self.builder
.start_node(SyntaxKind::CompilerDirective.to_raw());
// ---- CompilerIfClause ----
self.builder
.start_node(SyntaxKind::CompilerIfClause.to_raw());
self.consume_token(); // Octothorpe
self.consume_whitespace();
self.consume_token(); // If
self.consume_whitespace();
self.parse_expression();
self.consume_whitespace();
if self.at_token(Token::ThenKeyword) {
self.consume_token();
}
self.consume_whitespace();
while self.at_token(Token::EndOfLineComment) || self.at_token(Token::RemComment) {
self.consume_token();
self.consume_whitespace();
}
if self.at_token(Token::Newline) {
self.consume_token();
}
self.builder.finish_node(); // CompilerIfClause
// ---- Body / ElseIf / Else clauses ----
loop {
// Parse body (stops at #ElseIf, #Else, #End If, or end-of-scope)
self.parse_statement_list(|parser| {
parser.at_compiler_directive_keyword(Token::ElseIfKeyword)
|| parser.at_compiler_directive_keyword(Token::ElseKeyword)
|| parser.at_compiler_end_if_directive()
});
// Check what stopped us and handle if it's a directive clause
if self.at_compiler_directive_keyword(Token::ElseIfKeyword) {
// CompilerElseIfClause
self.builder
.start_node(SyntaxKind::CompilerElseIfClause.to_raw());
self.consume_token(); // Octothorpe
self.consume_whitespace();
self.consume_token(); // ElseIf
self.consume_whitespace();
self.parse_expression();
self.consume_whitespace();
if self.at_token(Token::ThenKeyword) {
self.consume_token();
}
self.consume_whitespace();
if self.at_token(Token::Newline) {
self.consume_token();
}
self.builder.finish_node(); // CompilerElseIfClause
// Continue loop to parse elsif body
continue;
}
if self.at_compiler_directive_keyword(Token::ElseKeyword) {
// CompilerElseClause
self.builder
.start_node(SyntaxKind::CompilerElseClause.to_raw());
self.consume_token(); // Octothorpe
self.consume_whitespace();
self.consume_token(); // Else
self.consume_whitespace();
if self.at_token(Token::Newline) {
self.consume_token();
}
self.builder.finish_node(); // CompilerElseClause
// Continue loop to parse else body
continue;
}
if self.at_compiler_end_if_directive() {
// CompilerEndIfClause
self.builder
.start_node(SyntaxKind::CompilerEndIfClause.to_raw());
self.consume_token(); // Octothorpe
self.consume_whitespace();
self.consume_token(); // End
self.consume_whitespace();
self.consume_token(); // If
self.consume_until_after(Token::Newline);
self.builder.finish_node(); // CompilerEndIfClause
}
break;
}
self.builder.finish_node(); // CompilerDirective
}
/// Parse a single-line If statement.
///
/// Single-line If statements have the form:
/// `If` condition `Then` statement [ `Else` statement ]
/// The statement(s) can be any valid VB6 statement, including procedure calls,
/// assignments, and even another single-line If.
fn parse_single_line_if_statement(&mut self) {
// Single-line If: parse inline statements.
// Statement parsers (e.g. parse_assignment_statement) consume the
// trailing newline. We must detect that and stop the loop, otherwise
// the single-line If would keep parsing onto subsequent lines.
while !self.is_at_end() && !self.at_token(Token::Newline) {
if self.at_token(Token::ElseKeyword) {
break;
}
let pos_before = self.pos;
if self.is_control_flow_keyword() {
self.parse_control_flow_statement();
} else if self.is_library_statement_keyword() {
self.parse_library_statement();
} else if self.is_variable_declaration_keyword() {
self.parse_array_statement();
} else if self.is_statement_keyword() {
self.parse_statement();
} else {
match self.current_token() {
Some(
Token::Whitespace
| Token::EndOfLineComment
| Token::RemComment
| Token::ColonOperator,
) => {
self.consume_token();
}
_ => {
if self.at_token(Token::LetKeyword) {
self.parse_let_statement();
} else if self.at_token(Token::PeriodOperator) {
// Handle dot-prefixed member access in With blocks
if self.is_at_with_member_assignment() {
self.parse_assignment_statement();
} else {
self.parse_procedure_call();
}
} else if self.is_at_assignment() {
self.parse_assignment_statement();
} else {
self.consume_token();
}
}
}
}
// If a statement parser consumed a newline as part of the
// statement, we've reached the end of this single line.
if self.pos > pos_before
&& self.tokens[pos_before..self.pos]
.iter()
.any(|(_, t)| *t == Token::Newline)
{
break;
}
}
if self.at_token(Token::Newline) {
self.consume_token();
}
self.builder.finish_node();
// IfStatement
}
}