vb6parse 1.2.4

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use vb6parse::parsers::cst::ConcreteSyntaxTree;

use std::fmt::Write;
const SNAPSHOT_PATH: &str = "../../snapshots/parsers/cst/edge_cases/recursion_limits";

/// Test deeply nested If statements
/// This tests indirect recursion through `parse_statement_list()` and `parse_if_statement()`.
///
/// Note: This test currently uses a moderate depth (50) to test parser behavior.
/// Once recursion depth limits are implemented (see recursion.md, Strategy 5),
/// this test should be updated to verify proper error handling at the limit.
#[test]
fn deeply_nested_if_statements() {
    const DEPTH: usize = 50;

    let mut source = String::from("Sub Test()\n");

    // Generate deeply nested If statements
    for i in 0..DEPTH {
        let _ = writeln!(source, "{}If x{} Then", "    ".repeat(i + 1), i);
    }

    // Add innermost statement
    let _ = writeln!(source, "{}y = 1", "    ".repeat(DEPTH + 1));

    // Close all If statements
    for i in (0..DEPTH).rev() {
        let _ = writeln!(source, "{}End If", "    ".repeat(i + 1));
    }

    let _ = writeln!(source, "End Sub");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for deeply_nested_if_statements (depth={DEPTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("deeply_nested_if_statements_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("deeply_nested_if_statements_failures", failure_messages);
}

/// Test deeply nested For loops
/// This tests indirect recursion through `parse_statement_list()` and `parse_for_statement()`.
///
/// Note: Uses moderate depth (30) for testing. Once recursion limits are implemented,
/// this should verify proper error handling at `MAX_STATEMENT_DEPTH`.
#[test]
fn deeply_nested_for_loops() {
    const DEPTH: usize = 30;

    let mut source = String::from("Sub Test()\n");

    // Generate deeply nested For loops
    for i in 0..DEPTH {
        let _ = writeln!(source, "{}For i{i} = 1 To 10", "    ".repeat(i + 1));
    }

    // Add innermost statement
    let _ = writeln!(source, "{}x = x + 1", "    ".repeat(DEPTH + 1));

    // Close all For loops
    for i in (0..DEPTH).rev() {
        let _ = writeln!(source, "{}Next i{i}", "    ".repeat(i + 1));
    }

    let _ = writeln!(source, "End Sub");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for deeply_nested_for_loops (depth={DEPTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("deeply_nested_for_loops_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("deeply_nested_for_loops_failures", failure_messages);
}

/// Test deeply nested parenthesized expressions
/// This tests direct recursion in `parse_expression_with_binding_power()`.
///
/// Note: Uses moderate depth (100) for testing. Once recursion limits are implemented
/// (Strategy 5, `MAX_EXPRESSION_DEPTH` = 500), this should verify proper error handling.
#[test]
fn deeply_nested_parentheses() {
    const DEPTH: usize = 100;

    let mut source = String::from("Sub Test()\n    result = ");

    // Generate deeply nested parentheses
    for _ in 0..DEPTH {
        source.push('(');
    }

    source.push('x');

    for _ in 0..DEPTH {
        source.push(')');
    }

    let _ = writeln!(source);

    let _ = writeln!(source, "End Sub");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for deeply_nested_parentheses (depth={DEPTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("deeply_nested_parentheses_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("deeply_nested_parentheses_failures", failure_messages);
}

/// Test long chain of binary operations
/// This tests expression parsing with many infix operators
///
/// Note: Uses moderate length (200) for testing. Once recursion limits are implemented,
/// this should verify the parser handles long expression chains correctly.
#[test]
fn long_binary_operation_chain() {
    const LENGTH: usize = 200;

    let mut source = String::from("Sub Test()\n    result = ");

    // Generate long chain of additions
    for i in 0..LENGTH {
        if i > 0 {
            source.push_str(" + ");
        }
        let _ = write!(source, "x{i}");
    }

    source.push_str("\nEnd Sub\n");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for long_binary_operation_chain (length={LENGTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("long_binary_operation_chain_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("long_binary_operation_chain_failures", failure_messages);
}

/// Test mixed nested control flow (combination of If, For, While, Do, Select)
/// This tests the mutual recursion between various control flow statements
///
/// Note: Uses moderate depth (25) with mixed constructs. Once recursion limits
/// are implemented, verify proper handling of complex nested control flow.
#[test]
fn mixed_nested_control_flow() {
    let source = r"
Sub Test()
    If a Then
        For i = 1 To 10
            If b Then
                While c
                    Do
                        If d Then
                            For j = 1 To 5
                                Select Case e
                                    Case 1
                                        If f Then
                                            While g
                                                Do While h
                                                    If i Then
                                                        For k = 1 To 3
                                                            x = x + 1
                                                        Next k
                                                    End If
                                                Loop
                                            Wend
                                        End If
                                    Case 2
                                        x = 2
                                End Select
                            Next j
                        End If
                    Loop Until j
                Wend
            End If
        Next i
    End If
End Sub
";

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();

    eprintln!("=== Failures for mixed_nested_control_flow ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("mixed_nested_control_flow_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("mixed_nested_control_flow_failures", failure_messages);
}

/// Test complex nested boolean expression
/// This tests expression recursion with multiple levels of And/Or operations and parentheses
///
/// Note: Once recursion limits are implemented, this should verify proper handling
/// of complex boolean expressions.
#[test]
fn complex_nested_boolean_expression() {
    let source = r"
Sub Test()
    result = ((a And b) Or (c And d)) And _
             ((e Or f) And (g Or h)) Or _
             (((i And j) Or (k And l)) And _
              ((m Or n) And (o Or p))) And _
             ((q And r) Or ((s And t) And (u Or v)))
End Sub
";

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();

    eprintln!("=== Failures for complex_nested_boolean_expression ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("complex_nested_boolean_expression_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!(
        "complex_nested_boolean_expression_failures",
        failure_messages
    );
}

/// Test deeply nested Select Case statements
/// This tests another form of control flow recursion
///
/// Note: Uses moderate depth (20) for testing. Once recursion limits are implemented,
/// verify proper error handling for deeply nested Select statements.
#[test]
fn deeply_nested_select_case() {
    const DEPTH: usize = 20;

    let mut source = String::from("Sub Test()\n");

    // Generate deeply nested Select Case statements
    for i in 0..DEPTH {
        let _ = writeln!(source, "{}Select Case x{i}", "    ".repeat(i + 1));
        let _ = writeln!(source, "{}Case 1", "    ".repeat(i + 2));
    }

    // Add innermost statement
    let _ = writeln!(source, "{}y = 1", "    ".repeat(DEPTH + 2));

    // Close all Select Case statements
    for i in (0..DEPTH).rev() {
        let _ = writeln!(source, "{}End Select", "    ".repeat(i + 1));
    }

    source.push_str("End Sub\n");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for deeply_nested_select_case (depth={DEPTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("deeply_nested_select_case_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("deeply_nested_select_case_failures", failure_messages);
}

/// Test deeply nested With blocks
/// This tests another recursion pattern in statement list parsing
///
/// Note: Uses moderate depth (25) for testing. Once recursion limits are implemented,
/// verify proper handling of nested With blocks.
#[test]
fn deeply_nested_with_blocks() {
    const DEPTH: usize = 25;

    let mut source = String::from("Sub Test()\n");

    // Generate deeply nested With blocks
    for i in 0..DEPTH {
        let _ = writeln!(source, "{}With obj{i}", "    ".repeat(i + 1));
    }

    // Add innermost statement
    let _ = writeln!(source, "{}.Property = 1", "    ".repeat(DEPTH + 1));

    // Close all With blocks
    for i in (0..DEPTH).rev() {
        let _ = writeln!(source, "{}End With", "    ".repeat(i + 1));
    }

    let _ = writeln!(source, "End Sub");

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", &source).unpack();

    eprintln!("=== Failures for deeply_nested_with_blocks (depth={DEPTH}) ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("deeply_nested_with_blocks_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!("deeply_nested_with_blocks_failures", failure_messages);
}

/// Test combination of nested expressions and statements
/// This tests interaction between expression recursion and statement recursion
///
/// Note: Tests realistic scenario with reasonable nesting. Once recursion limits
/// are implemented, verify both expression and statement depth tracking work correctly.
#[test]
fn combined_expression_and_statement_nesting() {
    let source = r"
Sub Test()
    If ((a + b) * (c + d)) > ((e - f) / (g - h)) Then
        For i = (x * y) To ((z + w) * 2)
            If (((p And q) Or (r And s)) And ((t Or u) And (v Or w))) Then
                result = ((a + (b * (c + (d * e)))) - ((f * (g + h)) / i))
                While (x > ((y * z) + (w / 2)))
                    Do
                        x = x - ((a + b) * (c + d))
                    Loop Until (x < ((y + z) / 2))
                Wend
            End If
        Next i
    End If
End Sub
";

    let (cst_opt, failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();

    eprintln!("=== Failures for combined_expression_and_statement_nesting ===");
    eprintln!("Number of failures: {}", failures.len());
    for failure in &failures {
        failure.eprint();
    }
    eprintln!("=== End Failures ===");

    let cst = cst_opt.expect("CST should be present even with syntax errors");
    let tree = cst.to_serializable();

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path(SNAPSHOT_PATH);
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();

    insta::assert_yaml_snapshot!("combined_expression_and_statement_nesting_cst", tree);

    let failure_messages: Vec<String> = failures.iter().map(|f| format!("{f:?}")).collect();
    insta::assert_yaml_snapshot!(
        "combined_expression_and_statement_nesting_failures",
        failure_messages
    );
}