perl-parser-core 0.13.3

Core parser engine for perl-parser
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
impl<'a> Parser<'a> {
    /// Parse if statement
    fn parse_if_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'if'

        self.expect(TokenKind::LeftParen)?;

        // Check if this is a variable declaration in the condition.
        // After the declaration, apply binary operators so that patterns like
        // `if (our $CAN_HAZ_XS && $ok)` are handled correctly (issue #2750 Pattern D).
        let condition = if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) {
            let decl = self.parse_variable_declaration()?;
            self.parse_below_assignment_with(decl)?
        } else {
            self.mark_not_stmt_start();
            self.parse_expression()?
        };

        self.expect_closing_delimiter(TokenKind::RightParen)?;

        let then_branch = self.parse_block()?;

        let mut elsif_branches = Vec::new();
        let mut else_branch = None;

        // Handle elsif chains
        while self.peek_kind() == Some(TokenKind::Elsif) {
            self.tokens.next()?; // consume 'elsif'
            self.expect(TokenKind::LeftParen)?;

            // Check if this is a variable declaration in the condition.
            // After the declaration, apply binary operators (issue #2750 Pattern D).
            let elsif_cond = if matches!(
                self.peek_kind(),
                Some(TokenKind::My)
                    | Some(TokenKind::Our)
                    | Some(TokenKind::Local)
                    | Some(TokenKind::State)
            ) {
                let decl = self.parse_variable_declaration()?;
                self.parse_below_assignment_with(decl)?
            } else {
                self.mark_not_stmt_start();
                self.parse_expression()?
            };

            self.expect_closing_delimiter(TokenKind::RightParen)?;
            let elsif_block = self.parse_block()?;
            elsif_branches.push((Box::new(elsif_cond), Box::new(elsif_block)));
        }

        // Handle else
        if self.peek_kind() == Some(TokenKind::Else) {
            self.tokens.next()?; // consume 'else'
            else_branch = Some(Box::new(self.parse_block()?));
        }

        let end = self.previous_position();
        Ok(Node::new(
            NodeKind::If {
                condition: Box::new(condition),
                then_branch: Box::new(then_branch),
                elsif_branches,
                else_branch,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse unless statement (syntactic sugar for if not)
    ///
    /// Perl allows `unless (...) { } elsif (...) { } else { }` chains,
    /// identical to if/elsif/else except the initial condition is negated.
    fn parse_unless_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'unless'

        self.expect(TokenKind::LeftParen)?;
        self.mark_not_stmt_start();
        let condition = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        // Negate the condition
        let negated_condition = Node::new(
            NodeKind::Unary { op: "!".to_string(), operand: Box::new(condition) },
            SourceLocation { start, end: self.previous_position() },
        );

        let then_branch = self.parse_block()?;

        let mut elsif_branches = Vec::new();
        let mut else_branch = None;

        // Handle elsif chains (valid Perl: unless ... elsif ... else ...)
        while self.peek_kind() == Some(TokenKind::Elsif) {
            self.tokens.next()?; // consume 'elsif'
            self.expect(TokenKind::LeftParen)?;

            let elsif_cond = if matches!(
                self.peek_kind(),
                Some(TokenKind::My)
                    | Some(TokenKind::Our)
                    | Some(TokenKind::Local)
                    | Some(TokenKind::State)
            ) {
                let decl = self.parse_variable_declaration()?;
                self.parse_below_assignment_with(decl)?
            } else {
                self.mark_not_stmt_start();
                self.parse_expression()?
            };

            self.expect_closing_delimiter(TokenKind::RightParen)?;
            let elsif_block = self.parse_block()?;
            elsif_branches.push((Box::new(elsif_cond), Box::new(elsif_block)));
        }

        // Handle else
        if self.peek_kind() == Some(TokenKind::Else) {
            self.tokens.next()?; // consume 'else'
            else_branch = Some(Box::new(self.parse_block()?));
        }

        let end = self.previous_position();

        Ok(Node::new(
            NodeKind::If {
                condition: Box::new(negated_condition),
                then_branch: Box::new(then_branch),
                elsif_branches,
                else_branch,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse while loop
    fn parse_while_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'while'

        self.expect(TokenKind::LeftParen)?;

        // Check if this is a variable declaration in the condition
        let condition = if self.peek_kind() == Some(TokenKind::RightParen) {
            // while () { } — empty condition is the infinite-loop idiom, equivalent to while (1)
            let loc = self.current_position();
            Node::new(
                NodeKind::Number { value: "1".to_string() },
                SourceLocation { start: loc, end: loc },
            )
        } else if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) {
            let decl = self.parse_variable_declaration()?;
            self.parse_below_assignment_with(decl)?
        } else {
            self.mark_not_stmt_start();
            self.parse_expression()?
        };

        self.expect_closing_delimiter(TokenKind::RightParen)?;

        let body = self.parse_block()?;

        // Handle continue block
        let continue_block = if self.peek_kind() == Some(TokenKind::Continue) {
            self.tokens.next()?; // consume 'continue'
            Some(Box::new(self.parse_block()?))
        } else {
            None
        };

        let end = self.previous_position();
        Ok(Node::new(
            NodeKind::While {
                condition: Box::new(condition),
                body: Box::new(body),
                continue_block,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse until loop (while not)
    fn parse_until_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'until'

        self.expect(TokenKind::LeftParen)?;
        self.mark_not_stmt_start();
        let condition = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        // Negate the condition
        let negated_condition = Node::new(
            NodeKind::Unary { op: "!".to_string(), operand: Box::new(condition) },
            SourceLocation { start, end: self.previous_position() },
        );

        let body = self.parse_block()?;

        // Handle continue block
        let continue_block = if self.peek_kind() == Some(TokenKind::Continue) {
            self.tokens.next()?; // consume 'continue'
            Some(Box::new(self.parse_block()?))
        } else {
            None
        };

        let end = self.previous_position();

        Ok(Node::new(
            NodeKind::While {
                condition: Box::new(negated_condition),
                body: Box::new(body),
                continue_block,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse for loop
    fn parse_for_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'for'

        // Check if it's a foreach-style for loop
        if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) || self.is_variable_start()
        {
            return self.parse_foreach_style_for();
        }

        // Parenthesized: could be C-style `for (init; cond; update)` or
        // implicit-$_ foreach `for (LIST)`. Delegate to shared helper.
        self.parse_c_style_or_implicit_foreach(start)
    }

    /// Shared parser for C-style `for/foreach (init; cond; update) BLOCK` and
    /// implicit-$_ foreach `for/foreach (LIST) BLOCK`.
    ///
    /// Called after the keyword (`for` or `foreach`) has been consumed and we
    /// know the next token is `(`. Handles both syntaxes because in Perl `for`
    /// and `foreach` are fully interchangeable.
    fn parse_c_style_or_implicit_foreach(
        &mut self,
        start: usize,
    ) -> ParseResult<Node> {
        self.expect(TokenKind::LeftParen)?;

        // Parse init (or check if it's a foreach)
        let init = if self.peek_kind() == Some(TokenKind::Semicolon) {
            None
        } else if self.peek_kind() == Some(TokenKind::My) {
            // Handle variable declaration in for loop init
            self.in_for_loop_init = true;
            let decl = self.parse_variable_declaration()?;
            self.in_for_loop_init = false;
            // Variable declarations in for loops don't have trailing semicolons
            Some(Box::new(decl))
        } else {
            // Parse expression
            self.mark_not_stmt_start();
            let expr = self.parse_expression()?;

            // If followed by ), it's a foreach loop
            if self.peek_kind() == Some(TokenKind::RightParen) {
                self.tokens.next()?; // consume )
                let body = self.parse_block()?;

                let end = self.previous_position();

                // Create implicit $_ variable
                let implicit_var = Node::new(
                    NodeKind::Variable { sigil: "$".to_string(), name: "_".to_string() },
                    SourceLocation { start, end: start },
                );

                return Ok(Node::new(
                    NodeKind::Foreach {
                        variable: Box::new(implicit_var),
                        list: Box::new(expr),
                        body: Box::new(body),
                        continue_block: None, // No continue block for implicit foreach
                    },
                    SourceLocation { start, end },
                ));
            }

            Some(Box::new(expr))
        };
        // First internal semicolon (after init) — recover inline instead of hard-failing.
        // A hard `?` here cascades into multiple spurious errors because the expression
        // parser has already consumed tokens; recovering inline keeps the For node intact.
        if self.peek_kind() == Some(TokenKind::Semicolon) {
            self.consume_token()?;
        } else {
            let pos = self.current_position();
            self.errors.push(ParseError::syntax(
                "Missing ';' after for-loop init — recovered".to_string(),
                pos,
            ));
        }

        // Parse condition — also treat `)` as empty condition to avoid cascading
        // errors when both semicolons are missing and we've consumed the init already.
        let condition = if self.peek_kind() == Some(TokenKind::Semicolon)
            || self.peek_kind() == Some(TokenKind::RightParen)
        {
            None
        } else {
            self.mark_not_stmt_start();
            Some(Box::new(self.parse_expression()?))
        };
        // Second internal semicolon (after condition) — same inline recovery pattern.
        // Skip the error if the next token is `)` — we already skipped condition as
        // empty in that case and there is nothing meaningful to report here.
        if self.peek_kind() == Some(TokenKind::Semicolon) {
            self.consume_token()?;
        } else if self.peek_kind() != Some(TokenKind::RightParen) {
            let pos = self.current_position();
            self.errors.push(ParseError::syntax(
                "Missing ';' after for-loop condition — recovered".to_string(),
                pos,
            ));
        }

        // Parse update
        let update = if self.peek_kind() == Some(TokenKind::RightParen) {
            None
        } else {
            self.mark_not_stmt_start();
            Some(Box::new(self.parse_expression()?))
        };

        self.expect_closing_delimiter(TokenKind::RightParen)?;
        let body = self.parse_block()?;

        // Handle continue block
        let continue_block = if self.peek_kind() == Some(TokenKind::Continue) {
            self.tokens.next()?; // consume 'continue'
            Some(Box::new(self.parse_block()?))
        } else {
            None
        };

        let end = self.previous_position();
        Ok(Node::new(
            NodeKind::For { init, condition, update, body: Box::new(body), continue_block },
            SourceLocation { start, end },
        ))
    }

    /// Parse foreach loop
    fn parse_foreach_statement(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'foreach'

        // In Perl, `for` and `foreach` are fully interchangeable. When the
        // next token is `(`, it could be either:
        //   - C-style:  foreach (my $i=0; $i<10; $i++) { ... }
        //   - List:     foreach (@list) { ... }
        // Delegate to the shared helper that disambiguates via semicolons.
        if self.peek_kind() == Some(TokenKind::LeftParen) {
            return self.parse_c_style_or_implicit_foreach(start);
        }

        // Set flag to prevent semicolon consumption in variable declaration
        self.in_for_loop_init = true;
        let variable = if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) {
            self.parse_variable_declaration()?
        } else {
            // foreach $var (LIST) — bare scalar without my
            self.parse_variable()?
        };
        self.in_for_loop_init = false;

        self.expect(TokenKind::LeftParen)?;
        self.mark_not_stmt_start();
        let list = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        let body = self.parse_block()?;

        // Handle continue block
        let continue_block = if self.peek_kind() == Some(TokenKind::Continue) {
            self.tokens.next()?; // consume 'continue'
            Some(Box::new(self.parse_block()?))
        } else {
            None
        };

        let end = self.previous_position();
        Ok(Node::new(
            NodeKind::Foreach {
                variable: Box::new(variable),
                list: Box::new(list),
                body: Box::new(body),
                continue_block,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse foreach-style for loop
    fn parse_foreach_style_for(&mut self) -> ParseResult<Node> {
        // Set flag to prevent semicolon consumption in variable declaration
        self.in_for_loop_init = true;
        let variable = if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) {
            self.parse_variable_declaration()?
        } else {
            // for $var (LIST) — bare scalar without my
            self.parse_variable()?
        };
        self.in_for_loop_init = false;

        self.expect(TokenKind::LeftParen)?;
        self.mark_not_stmt_start();
        let list = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        let body = self.parse_block()?;

        // Handle continue block
        let continue_block = if self.peek_kind() == Some(TokenKind::Continue) {
            self.tokens.next()?; // consume 'continue'
            Some(Box::new(self.parse_block()?))
        } else {
            None
        };

        let start = variable.location.start;
        let end = self.previous_position();

        Ok(Node::new(
            NodeKind::Foreach {
                variable: Box::new(variable),
                list: Box::new(list),
                body: Box::new(body),
                continue_block,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse format declaration
    /// Parse return statement
    fn parse_return(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'return'

        // Check if we have a value to return - only stop at clear ends or statement modifiers.
        // Word operators (or, and, xor) belong to the enclosing statement, not the return value.
        // e.g. `return or die` means `(return) or (die)`.
        let value = if Self::is_statement_terminator(self.peek_kind())
            || matches!(self.peek_kind(), Some(TokenKind::RightBrace))
            || matches!(self.peek_kind(), Some(k) if Self::is_stmt_modifier_kind(k))
            || matches!(
                self.peek_kind(),
                Some(TokenKind::WordOr | TokenKind::WordAnd | TokenKind::WordXor)
            )
        {
            None
        } else {
            // Parse the return value
            Some(Box::new(self.parse_expression()?))
        };

        let end = value.as_ref().map(|v| v.location.end).unwrap_or(self.previous_position());
        Ok(Node::new(NodeKind::Return { value }, SourceLocation { start, end }))
    }

    /// Parse return in expression context (e.g. ternary branches, short-circuit).
    ///
    /// Unlike `parse_return` (statement level), this variant is aware of
    /// expression boundaries such as `:` (ternary colon), `)`, `]`, and `,`
    /// so it does not greedily consume tokens that belong to the enclosing
    /// expression.
    fn parse_return_expr(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.tokens.next()?; // consume 'return'

        // Determine whether there is a return value.
        // Stop at all expression-level boundaries as well as statement-level ones.
        let value = if Self::is_statement_terminator(self.peek_kind())
            || matches!(
                self.peek_kind(),
                Some(TokenKind::RightBrace)
                    | Some(TokenKind::RightParen)
                    | Some(TokenKind::RightBracket)
                    | Some(TokenKind::Colon)
                    | Some(TokenKind::Comma)
            )
            || matches!(self.peek_kind(), Some(k) if Self::is_stmt_modifier_kind(k))
        {
            None
        } else {
            // Parse the return value at assignment precedence so we do not
            // accidentally consume a surrounding comma list or ternary colon.
            Some(Box::new(self.parse_assignment()?))
        };

        let end = value
            .as_ref()
            .map(|v| v.location.end)
            .unwrap_or(self.previous_position());
        Ok(Node::new(
            NodeKind::Return { value },
            SourceLocation { start, end },
        ))
    }

    /// Parse eval expression/block
    fn parse_eval(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'eval'

        // Eval can take either a block or a string expression
        if self.peek_kind() == Some(TokenKind::LeftBrace) {
            // eval { ... }
            let block = self.parse_block()?;
            let end = block.location.end;
            Ok(Node::new(NodeKind::Eval { block: Box::new(block) }, SourceLocation { start, end }))
        } else {
            // eval "string" or eval $expr
            let expr = self.parse_expression()?;
            let end = expr.location.end;
            Ok(Node::new(NodeKind::Eval { block: Box::new(expr) }, SourceLocation { start, end }))
        }
    }

    /// Parse goto statement: `goto LABEL`, `goto &sub`, `goto EXPR`
    fn parse_goto(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'goto'
        self.mark_not_stmt_start();

        // Parse the target as an assignment-level expression (not full comma
        // expression) to avoid consuming surrounding list separators.
        let target = self.parse_assignment()?;
        let end = target.location.end;
        Ok(Node::new(
            NodeKind::Goto { target: Box::new(target) },
            SourceLocation { start, end },
        ))
    }

    /// Parse `defer { ... }` block (Perl 5.36+ experimental, stable in 5.40)
    pub(crate) fn parse_defer(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'defer'
        let block = self.parse_block()?;
        let end = block.location.end;
        Ok(Node::new(
            NodeKind::Defer { block: Box::new(block) },
            SourceLocation { start, end },
        ))
    }

        /// Parse try/catch/finally block
    fn parse_try(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'try'

        // Parse the try body
        let body = self.parse_block()?;

        let mut catch_blocks = Vec::new();
        let mut finally_block = None;

        // Parse catch blocks
        while self.peek_kind() == Some(TokenKind::Catch) {
            self.consume_token()?; // consume 'catch'

            // Check for optional variable
            let var = if self.peek_kind() == Some(TokenKind::LeftParen) {
                self.consume_token()?; // consume '('
                let var_name = if self.peek_kind() == Some(TokenKind::ScalarSigil)
                    || self.tokens.peek()?.text.starts_with('$')
                {
                    let var = self.parse_variable()?;
                    match &var.kind {
                        NodeKind::Variable { sigil, name } => Some(format!("{}{}", sigil, name)),
                        _ => None,
                    }
                } else {
                    None
                };
                self.expect_closing_delimiter(TokenKind::RightParen)?;
                var_name
            } else {
                None
            };

            // Error.pm-style typed catch:
            //   catch Some::Error with { ... }
            // Keep this strict: if a class-like filter appears after `catch`,
            // require the `with` keyword before the block.
            if var.is_none() && self.peek_kind() != Some(TokenKind::LeftBrace) {
                let mut consumed_filter = false;
                while let Some(kind) = self.peek_kind() {
                    let is_component =
                        kind == TokenKind::Identifier && self.tokens.peek()?.text.as_ref() != "with";
                    if is_component {
                        self.consume_token()?;
                        consumed_filter = true;
                        continue;
                    }

                    if kind == TokenKind::DoubleColon {
                        self.consume_token()?;
                        consumed_filter = true;
                        continue;
                    }

                    break;
                }

                if consumed_filter {
                    if self.peek_kind() == Some(TokenKind::Identifier)
                        && self.tokens.peek()?.text.as_ref() == "with"
                    {
                        self.consume_token()?; // consume `with`
                    } else {
                        let error_pos = self.current_position();
                        self.errors.push(ParseError::syntax(
                            "Expected 'with' before catch block",
                            error_pos,
                        ));
                    }
                }
            }

            let block = self.parse_block()?;
            catch_blocks.push((var, block));
        }

        // Parse optional finally block
        if self.peek_kind() == Some(TokenKind::Finally) {
            self.consume_token()?; // consume 'finally'
            finally_block = Some(Box::new(self.parse_block()?));
        }

        let end = finally_block
            .as_ref()
            .map(|b| b.location.end)
            .or_else(|| catch_blocks.last().map(|(_, b)| b.location.end))
            .unwrap_or(body.location.end);

        Ok(Node::new(
            NodeKind::Try {
                body: Box::new(body),
                catch_blocks: catch_blocks.into_iter().map(|(v, b)| (v, Box::new(b))).collect(),
                finally_block,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse do expression/block
    fn parse_do(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'do'

        // Do can take either a block or a string (filename)
        if self.peek_kind() == Some(TokenKind::LeftBrace) {
            // do { ... }
            let block = self.parse_block()?;
            let end = block.location.end;
            Ok(Node::new(NodeKind::Do { block: Box::new(block) }, SourceLocation { start, end }))
        } else {
            // do "filename" or do $expr
            let expr = self.parse_expression()?;
            let end = expr.location.end;
            Ok(Node::new(NodeKind::Do { block: Box::new(expr) }, SourceLocation { start, end }))
        }
    }

    /// Parse given statement
    fn parse_given_statement(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'given'

        // Parse the expression in parentheses
        self.expect(TokenKind::LeftParen)?;
        let expr = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        // Parse the body block
        let body = self.parse_given_block()?;
        let end = body.location.end;

        Ok(Node::new(
            NodeKind::Given { expr: Box::new(expr), body: Box::new(body) },
            SourceLocation { start, end },
        ))
    }

    /// Parse given block (which contains when/default statements)
    fn parse_given_block(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        self.expect(TokenKind::LeftBrace)?;

        let mut statements = Vec::new();

        while self.peek_kind() != Some(TokenKind::RightBrace) && !self.tokens.is_eof() {
            match self.peek_kind() {
                Some(TokenKind::When) => {
                    statements.push(self.parse_when_statement()?);
                }
                Some(TokenKind::Default) => {
                    statements.push(self.parse_default_statement()?);
                }
                _ => {
                    return Err(ParseError::syntax(
                        "Expected 'when' or 'default' in given block",
                        self.current_position(),
                    ));
                }
            }
        }

        self.expect(TokenKind::RightBrace)?;
        let end = self.previous_position();

        Ok(Node::new(NodeKind::Block { statements }, SourceLocation { start, end }))
    }

    /// Parse when statement
    fn parse_when_statement(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'when'

        // Parse the condition in parentheses
        self.expect(TokenKind::LeftParen)?;
        let condition = self.parse_expression()?;
        self.expect_closing_delimiter(TokenKind::RightParen)?;

        // Parse the body block
        let body = self.parse_block()?;
        let end = body.location.end;

        Ok(Node::new(
            NodeKind::When { condition: Box::new(condition), body: Box::new(body) },
            SourceLocation { start, end },
        ))
    }

    /// Handle an orphaned `else` that appears at statement level without a
    /// preceding `if`/`unless`.  This happens when earlier error recovery
    /// consumed the `if` block, leaving the `else` stranded.
    ///
    /// Strategy: record an error, consume the `else` keyword and its block
    /// (if present), then wrap the result in an If node with a synthetic
    /// true condition so the block contents are still visible to the LSP.
    fn parse_orphaned_else(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        let else_token = self.consume_token()?; // consume 'else'

        // Record a descriptive error
        self.record_error(ParseError::syntax(
            "'else' without preceding 'if' or 'unless'",
            else_token.start,
        ));

        // Try to consume the block so we don't leave it orphaned
        let else_block = if self.peek_kind() == Some(TokenKind::LeftBrace) {
            self.parse_block()?
        } else {
            // No block follows — produce an empty placeholder
            Node::new(
                NodeKind::Block { statements: vec![] },
                SourceLocation { start, end: self.previous_position() },
            )
        };

        let end = self.previous_position();

        // Wrap in an If with a synthetic "true" condition so consumers see
        // the block contents.  The error is already recorded above.
        let synthetic_cond = Node::new(
            NodeKind::Number { value: "1".to_string() },
            SourceLocation { start, end: start },
        );

        Ok(Node::new(
            NodeKind::If {
                condition: Box::new(synthetic_cond),
                then_branch: Box::new(else_block),
                elsif_branches: vec![],
                else_branch: None,
            },
            SourceLocation { start, end },
        ))
    }

    /// Handle an orphaned `elsif` that appears at statement level without a
    /// preceding `if`/`unless`.  Same recovery approach as `parse_orphaned_else`:
    /// record the error, consume the elsif clause (and any following elsif/else
    /// chain), then wrap the whole thing in a recovered If node.
    fn parse_orphaned_elsif(&mut self) -> ParseResult<Node> {
        let start = self.current_position();
        let elsif_token = self.consume_token()?; // consume 'elsif'

        // Record a descriptive error
        self.record_error(ParseError::syntax(
            "'elsif' without preceding 'if' or 'unless'",
            elsif_token.start,
        ));

        // Parse the elsif condition
        self.expect(TokenKind::LeftParen)?;

        let condition = if matches!(
            self.peek_kind(),
            Some(TokenKind::My)
                | Some(TokenKind::Our)
                | Some(TokenKind::Local)
                | Some(TokenKind::State)
        ) {
            let decl = self.parse_variable_declaration()?;
            self.parse_below_assignment_with(decl)?
        } else {
            self.mark_not_stmt_start();
            self.parse_expression()?
        };

        self.expect_closing_delimiter(TokenKind::RightParen)?;
        let then_branch = self.parse_block()?;

        // Continue parsing any following elsif/else chain
        let mut elsif_branches = Vec::new();
        let mut else_branch = None;

        while self.peek_kind() == Some(TokenKind::Elsif) {
            self.tokens.next()?; // consume 'elsif'
            self.expect(TokenKind::LeftParen)?;

            let elsif_cond = if matches!(
                self.peek_kind(),
                Some(TokenKind::My)
                    | Some(TokenKind::Our)
                    | Some(TokenKind::Local)
                    | Some(TokenKind::State)
            ) {
                let decl = self.parse_variable_declaration()?;
                self.parse_below_assignment_with(decl)?
            } else {
                self.mark_not_stmt_start();
                self.parse_expression()?
            };

            self.expect_closing_delimiter(TokenKind::RightParen)?;
            let elsif_block = self.parse_block()?;
            elsif_branches.push((Box::new(elsif_cond), Box::new(elsif_block)));
        }

        if self.peek_kind() == Some(TokenKind::Else) {
            self.tokens.next()?; // consume 'else'
            else_branch = Some(Box::new(self.parse_block()?));
        }

        let end = self.previous_position();

        Ok(Node::new(
            NodeKind::If {
                condition: Box::new(condition),
                then_branch: Box::new(then_branch),
                elsif_branches,
                else_branch,
            },
            SourceLocation { start, end },
        ))
    }

    /// Parse default statement
    fn parse_default_statement(&mut self) -> ParseResult<Node> {
        let start = self.consume_token()?.start; // consume 'default'

        // Parse the body block
        let body = self.parse_block()?;
        let end = body.location.end;

        Ok(Node::new(NodeKind::Default { body: Box::new(body) }, SourceLocation { start, end }))
    }

}