php-rs-parser 0.7.0

Fast PHP parser producing a typed AST
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
use php_ast::*;

use crate::version::PhpVersion;

/// Parse the inner content of a double-quoted or backtick string into parts.
/// `source` is the full original source string.
/// `inner` is the string content without surrounding quotes — must be a verbatim
/// subslice of `source` so that sub-parser offsets are correct absolute positions.
/// `base_offset` is the byte offset of the first character of `inner` in the source.
pub fn parse_interpolated_parts<'arena, 'src>(
    arena: &'arena bumpalo::Bump,
    source: &'src str,
    inner: &'src str,
    base_offset: u32,
    version: PhpVersion,
) -> ArenaVec<'arena, StringPart<'arena, 'src>> {
    let mut parts = ArenaVec::with_capacity_in(8, arena);
    let bytes = inner.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    // Start of the current literal run within `inner`.
    let mut literal_start = 0usize;
    // Accumulated owned string; present only when the run contains escape sequences.
    let mut owned: Option<String> = None;

    while i < len {
        match bytes[i] {
            b'\\' => {
                // Materialise an owned buffer for this run if not already done.
                if owned.is_none() {
                    owned = Some(inner[literal_start..i].to_string());
                }
                let buf = owned.as_mut().unwrap();
                if i + 1 < len {
                    let next = bytes[i + 1];
                    match next {
                        b'$' => {
                            buf.push('$');
                            i += 2;
                        }
                        b'\\' => {
                            buf.push('\\');
                            i += 2;
                        }
                        b'n' => {
                            buf.push('\n');
                            i += 2;
                        }
                        b'r' => {
                            buf.push('\r');
                            i += 2;
                        }
                        b't' => {
                            buf.push('\t');
                            i += 2;
                        }
                        b'v' => {
                            buf.push('\x0B');
                            i += 2;
                        }
                        b'e' => {
                            buf.push('\x1B');
                            i += 2;
                        }
                        b'f' => {
                            buf.push('\x0C');
                            i += 2;
                        }
                        b'"' => {
                            buf.push('"');
                            i += 2;
                        }
                        b'x' | b'X' => {
                            // Hex escape: \xNN
                            i += 2;
                            let start = i;
                            while i < len && i - start < 2 && bytes[i].is_ascii_hexdigit() {
                                i += 1;
                            }
                            if i > start {
                                if let Ok(val) = u8::from_str_radix(&inner[start..i], 16) {
                                    buf.push(val as char);
                                }
                            } else {
                                buf.push('\\');
                                buf.push('x');
                            }
                        }
                        b'u' => {
                            // Unicode escape: \u{HHHH} — PHP 7.0+
                            i += 2;
                            if i < len && bytes[i] == b'{' {
                                i += 1; // skip {
                                let start = i;
                                while i < len && bytes[i].is_ascii_hexdigit() {
                                    i += 1;
                                }
                                if i < len && bytes[i] == b'}' {
                                    let hex = &inner[start..i];
                                    if let Ok(codepoint) = u32::from_str_radix(hex, 16) {
                                        if let Some(c) = char::from_u32(codepoint) {
                                            buf.push(c);
                                        }
                                    }
                                    i += 1; // skip }
                                } else {
                                    // Malformed — keep as-is
                                    buf.push_str(&inner[start - 2..i]);
                                }
                            } else {
                                buf.push('\\');
                                buf.push('u');
                            }
                        }
                        b'0'..=b'7' => {
                            // Octal escape: \NNN (up to 3 digits)
                            let start = i + 1;
                            i += 1;
                            while i < len && i - start < 3 && bytes[i] >= b'0' && bytes[i] <= b'7' {
                                i += 1;
                            }
                            if let Ok(val) = u8::from_str_radix(&inner[start..i], 8) {
                                buf.push(val as char);
                            }
                        }
                        _ => {
                            // Unknown escape: keep as-is
                            buf.push('\\');
                            buf.push(next as char);
                            i += 2;
                        }
                    }
                } else {
                    buf.push('\\');
                    i += 1;
                }
            }
            b'$' => {
                // Deprecated ${varname} syntax (PHP < 8.2): ${ ... }
                if i + 1 < len && bytes[i + 1] == b'{' {
                    if let Some(buf) = owned.take() {
                        if !buf.is_empty() {
                            parts.push(StringPart::Literal(arena.alloc_str(&buf)));
                        }
                    } else if i > literal_start {
                        parts.push(StringPart::Literal(
                            arena.alloc_str(&inner[literal_start..i]),
                        ));
                    }
                    i += 2; // skip ${
                    let var_offset = base_offset + (i - 2) as u32;
                    if i < len && bytes[i] == b'$' {
                        // ${$foo} — variable variable; use sub-parser from the inner $
                        let expr_start = i;
                        let mut depth = 1usize;
                        while i < len && depth > 0 {
                            match bytes[i] {
                                b'{' => depth += 1,
                                b'}' => depth -= 1,
                                _ => {}
                            }
                            if depth > 0 {
                                i += 1;
                            }
                        }
                        let inner_expr = parse_complex_interpolation(
                            arena,
                            source,
                            base_offset + expr_start as u32,
                            base_offset + i as u32,
                            version,
                        );
                        if i < len {
                            i += 1; // skip }
                        }
                        parts.push(StringPart::Expr(Expr {
                            kind: ExprKind::VariableVariable(arena.alloc(inner_expr)),
                            span: Span::new(var_offset, base_offset + i as u32),
                        }));
                    } else {
                        // ${varname} or ${varname[index]} — identifier as variable name
                        let name_start = i;
                        while i < len && is_var_char(bytes[i]) {
                            i += 1;
                        }
                        let var_name: &'src str =
                            &source[base_offset as usize + name_start..base_offset as usize + i];
                        let mut expr = Expr {
                            kind: ExprKind::Variable(NameStr::Src(var_name)),
                            span: Span::new(
                                base_offset + name_start as u32,
                                base_offset + i as u32,
                            ),
                        };
                        // Optional [index]
                        if i < len && bytes[i] == b'[' {
                            i += 1;
                            let idx_start = i;
                            while i < len && bytes[i] != b']' && bytes[i] != b'}' {
                                i += 1;
                            }
                            if i < len && bytes[i] == b']' {
                                let idx_str = &inner[idx_start..i];
                                i += 1;
                                let idx_offset = base_offset + idx_start as u32;
                                let idx_end = base_offset + (i - 1) as u32;
                                let index_expr =
                                    parse_simple_index(arena, source, idx_str, idx_offset, idx_end);
                                let span = Span::new(var_offset, base_offset + i as u32);
                                expr = Expr {
                                    kind: ExprKind::ArrayAccess(ArrayAccessExpr {
                                        array: arena.alloc(expr),
                                        index: Some(arena.alloc(index_expr)),
                                    }),
                                    span,
                                };
                            }
                        }
                        // Skip to closing }
                        while i < len && bytes[i] != b'}' {
                            i += 1;
                        }
                        if i < len {
                            i += 1; // skip }
                        }
                        parts.push(StringPart::Expr(expr));
                    }
                    literal_start = i;
                // Check for {$ (complex syntax handled below) - this is simple $var
                } else if i + 1 < len && is_var_start(bytes[i + 1]) {
                    // Flush literal run.
                    if let Some(buf) = owned.take() {
                        if !buf.is_empty() {
                            parts.push(StringPart::Literal(arena.alloc_str(&buf)));
                        }
                    } else if i > literal_start {
                        parts.push(StringPart::Literal(
                            arena.alloc_str(&inner[literal_start..i]),
                        ));
                    }

                    // Parse variable name
                    let var_start = i;
                    i += 1; // skip $
                    let name_start = i;
                    while i < len && is_var_char(bytes[i]) {
                        i += 1;
                    }
                    let var_name: &'src str =
                        &source[base_offset as usize + name_start..base_offset as usize + i];
                    let var_offset = base_offset + var_start as u32;

                    let mut expr = Expr {
                        kind: ExprKind::Variable(NameStr::Src(var_name)),
                        span: Span::new(var_offset, base_offset + i as u32),
                    };

                    // Check for ->identifier (simple property access)
                    if i + 2 < len && bytes[i] == b'-' && bytes[i + 1] == b'>' {
                        let prop_start = i + 2;
                        if prop_start < len && is_var_start(bytes[prop_start]) {
                            i = prop_start;
                            let pname_start = i;
                            while i < len && is_var_char(bytes[i]) {
                                i += 1;
                            }
                            let prop_name: &'src str = &source
                                [base_offset as usize + pname_start..base_offset as usize + i];
                            let prop_span =
                                Span::new(base_offset + pname_start as u32, base_offset + i as u32);
                            let span = Span::new(var_offset, base_offset + i as u32);
                            expr = Expr {
                                kind: ExprKind::PropertyAccess(PropertyAccessExpr {
                                    object: arena.alloc(expr),
                                    property: arena.alloc(Expr {
                                        kind: ExprKind::Identifier(NameStr::Src(prop_name)),
                                        span: prop_span,
                                    }),
                                }),
                                span,
                            };
                        }
                    }

                    // Check for [index] (simple array access)
                    if i < len && bytes[i] == b'[' {
                        let bracket_start = i;
                        i += 1; // skip [
                                // Find the matching ]
                        let idx_start = i;
                        while i < len && bytes[i] != b']' {
                            i += 1;
                        }
                        if i < len && bytes[i] == b']' {
                            let idx_str = &inner[idx_start..i];
                            i += 1; // skip ]

                            let idx_offset = base_offset + idx_start as u32;
                            let idx_end = base_offset + (i - 1) as u32;

                            let index_expr =
                                parse_simple_index(arena, source, idx_str, idx_offset, idx_end);

                            let span = Span::new(var_offset, base_offset + i as u32);
                            let _ = bracket_start; // used implicitly
                            expr = Expr {
                                kind: ExprKind::ArrayAccess(ArrayAccessExpr {
                                    array: arena.alloc(expr),
                                    index: Some(arena.alloc(index_expr)),
                                }),
                                span,
                            };
                        }
                    }

                    parts.push(StringPart::Expr(expr));
                    literal_start = i;
                } else {
                    // Plain `$` not starting a variable — keep in literal run.
                    if let Some(ref mut buf) = owned {
                        buf.push('$');
                    }
                    i += 1;
                }
            }
            b'{' if i + 1 < len && bytes[i + 1] == b'$' => {
                // Complex syntax: {$expr}
                if let Some(buf) = owned.take() {
                    if !buf.is_empty() {
                        parts.push(StringPart::Literal(arena.alloc_str(&buf)));
                    }
                } else if i > literal_start {
                    parts.push(StringPart::Literal(
                        arena.alloc_str(&inner[literal_start..i]),
                    ));
                }

                i += 1; // skip {
                        // Find matching }
                let expr_start = i;
                let mut depth = 1;
                while i < len && depth > 0 {
                    match bytes[i] {
                        b'{' => depth += 1,
                        b'}' => depth -= 1,
                        b'\'' | b'"' => {
                            // Skip string literals inside complex expression
                            let quote = bytes[i];
                            i += 1;
                            while i < len && bytes[i] != quote {
                                if bytes[i] == b'\\' {
                                    i += 1; // skip escaped char
                                }
                                i += 1;
                            }
                            if i < len {
                                i += 1; // skip closing quote
                            }
                            continue;
                        }
                        _ => {}
                    }
                    if depth > 0 {
                        i += 1;
                    }
                }

                let expr_end = i; // position of } or end of string
                if depth == 0 {
                    i += 1; // skip }
                }

                // Parse the expression using a sub-parser starting at the absolute offset
                let expr_offset = base_offset + expr_start as u32;
                let end_offset = base_offset + expr_end as u32;
                let expr =
                    parse_complex_interpolation(arena, source, expr_offset, end_offset, version);
                parts.push(StringPart::Expr(expr));
                literal_start = i;
            }
            _ => {
                // Regular character — push to owned buffer only if we're already materialised.
                if let Some(ref mut buf) = owned {
                    buf.push(bytes[i] as char);
                }
                i += 1;
            }
        }
    }

    // Flush remaining literal run.
    if let Some(buf) = owned {
        if !buf.is_empty() {
            parts.push(StringPart::Literal(arena.alloc_str(&buf)));
        }
    } else if i > literal_start {
        parts.push(StringPart::Literal(
            arena.alloc_str(&inner[literal_start..i]),
        ));
    }

    parts
}

/// Parse the inner content of an indented heredoc body into parts.
/// `raw_body` must be a verbatim subslice of `source` (with indentation intact).
/// `body_offset` is the byte offset of `raw_body[0]` within `source`.
/// `indent` is the indentation prefix stripped from each line (e.g. `"    "`).
///
/// At the start and after every newline, `indent.len()` bytes are skipped so that
/// all source offsets remain accurate. Complex `{$expr}` interpolations are parsed
/// with a direct sub-parser on `source`, eliminating the wrapping/reoffset overhead
/// of the old `parse_interpolated_parts_heredoc` path.
pub fn parse_interpolated_parts_indented<'arena, 'src>(
    arena: &'arena bumpalo::Bump,
    source: &'src str,
    raw_body: &'src str,
    body_offset: u32,
    indent: &str,
    version: PhpVersion,
) -> ArenaVec<'arena, StringPart<'arena, 'src>> {
    let indent_len = indent.len();
    let mut parts: ArenaVec<'arena, StringPart<'arena, 'src>> =
        ArenaVec::with_capacity_in(4, arena);
    let mut literal = String::new();
    let bytes = raw_body.as_bytes();
    let len = bytes.len();

    // Skip leading indent on the first line
    let mut i = if indent_len > 0 && len >= indent_len && raw_body[..indent_len] == *indent {
        indent_len
    } else {
        0
    };

    while i < len {
        match bytes[i] {
            b'\\' => {
                // Escape sequences
                if i + 1 < len {
                    let next = bytes[i + 1];
                    match next {
                        b'$' => {
                            literal.push('$');
                            i += 2;
                        }
                        b'\\' => {
                            literal.push('\\');
                            i += 2;
                        }
                        b'n' => {
                            literal.push('\n');
                            i += 2;
                        }
                        b'r' => {
                            literal.push('\r');
                            i += 2;
                        }
                        b't' => {
                            literal.push('\t');
                            i += 2;
                        }
                        b'v' => {
                            literal.push('\x0B');
                            i += 2;
                        }
                        b'e' => {
                            literal.push('\x1B');
                            i += 2;
                        }
                        b'f' => {
                            literal.push('\x0C');
                            i += 2;
                        }
                        b'"' => {
                            literal.push('"');
                            i += 2;
                        }
                        b'x' | b'X' => {
                            i += 2;
                            let start = i;
                            while i < len && i - start < 2 && bytes[i].is_ascii_hexdigit() {
                                i += 1;
                            }
                            if i > start {
                                if let Ok(val) = u8::from_str_radix(&raw_body[start..i], 16) {
                                    literal.push(val as char);
                                }
                            } else {
                                literal.push('\\');
                                literal.push('x');
                            }
                        }
                        b'u' => {
                            // Unicode escape: \u{HHHH} — PHP 7.0+
                            i += 2;
                            if i < len && bytes[i] == b'{' {
                                i += 1; // skip {
                                let start = i;
                                while i < len && bytes[i].is_ascii_hexdigit() {
                                    i += 1;
                                }
                                if i < len && bytes[i] == b'}' {
                                    let hex = &raw_body[start..i];
                                    if let Ok(codepoint) = u32::from_str_radix(hex, 16) {
                                        if let Some(c) = char::from_u32(codepoint) {
                                            literal.push(c);
                                        }
                                    }
                                    i += 1; // skip }
                                } else {
                                    literal.push_str(&raw_body[start - 2..i]);
                                }
                            } else {
                                literal.push('\\');
                                literal.push('u');
                            }
                        }
                        b'0'..=b'7' => {
                            let start = i + 1;
                            i += 1;
                            while i < len && i - start < 3 && bytes[i] >= b'0' && bytes[i] <= b'7' {
                                i += 1;
                            }
                            if let Ok(val) = u8::from_str_radix(&raw_body[start..i], 8) {
                                literal.push(val as char);
                            }
                        }
                        _ => {
                            literal.push('\\');
                            literal.push(next as char);
                            i += 2;
                        }
                    }
                } else {
                    literal.push('\\');
                    i += 1;
                }
            }
            b'\n' => {
                // Preserve the newline in the literal, then skip the indent on the next line
                literal.push('\n');
                i += 1;
                if indent_len > 0 && i + indent_len <= len && raw_body[i..i + indent_len] == *indent
                {
                    i += indent_len;
                }
            }
            b'$' => {
                if i + 1 < len && is_var_start(bytes[i + 1]) {
                    if !literal.is_empty() {
                        parts.push(StringPart::Literal(arena.alloc_str(&literal)));
                        literal.clear();
                    }
                    let var_start = i;
                    i += 1; // skip $
                    let name_start = i;
                    while i < len && is_var_char(bytes[i]) {
                        i += 1;
                    }
                    // raw_body is &'src str so we can borrow directly
                    let var_name: &'src str = &raw_body[name_start..i];
                    let var_offset = body_offset + var_start as u32;

                    let mut expr = Expr {
                        kind: ExprKind::Variable(NameStr::Src(var_name)),
                        span: Span::new(var_offset, body_offset + i as u32),
                    };

                    if i + 2 < len && bytes[i] == b'-' && bytes[i + 1] == b'>' {
                        let prop_start = i + 2;
                        if prop_start < len && is_var_start(bytes[prop_start]) {
                            i = prop_start;
                            let pname_start = i;
                            while i < len && is_var_char(bytes[i]) {
                                i += 1;
                            }
                            let prop_name: &'src str = &raw_body[pname_start..i];
                            let prop_span =
                                Span::new(body_offset + pname_start as u32, body_offset + i as u32);
                            let span = Span::new(var_offset, body_offset + i as u32);
                            expr = Expr {
                                kind: ExprKind::PropertyAccess(PropertyAccessExpr {
                                    object: arena.alloc(expr),
                                    property: arena.alloc(Expr {
                                        kind: ExprKind::Identifier(NameStr::Src(prop_name)),
                                        span: prop_span,
                                    }),
                                }),
                                span,
                            };
                        }
                    }

                    if i < len && bytes[i] == b'[' {
                        i += 1; // skip [
                        let idx_start = i;
                        while i < len && bytes[i] != b']' {
                            i += 1;
                        }
                        if i < len && bytes[i] == b']' {
                            let idx_str = &raw_body[idx_start..i];
                            i += 1; // skip ]
                            let idx_offset = body_offset + idx_start as u32;
                            let idx_end = body_offset + (i - 1) as u32;
                            let index_expr =
                                parse_simple_index(arena, source, idx_str, idx_offset, idx_end);
                            let span = Span::new(var_offset, body_offset + i as u32);
                            expr = Expr {
                                kind: ExprKind::ArrayAccess(ArrayAccessExpr {
                                    array: arena.alloc(expr),
                                    index: Some(arena.alloc(index_expr)),
                                }),
                                span,
                            };
                        }
                    }
                    parts.push(StringPart::Expr(expr));
                } else {
                    literal.push('$');
                    i += 1;
                }
            }
            b'{' if i + 1 < len && bytes[i + 1] == b'$' => {
                if !literal.is_empty() {
                    parts.push(StringPart::Literal(arena.alloc_str(&literal)));
                    literal.clear();
                }
                i += 1; // skip {
                let expr_start = i;
                let mut depth = 1;
                while i < len && depth > 0 {
                    match bytes[i] {
                        b'{' => depth += 1,
                        b'}' => depth -= 1,
                        b'\'' | b'"' => {
                            let quote = bytes[i];
                            i += 1;
                            while i < len && bytes[i] != quote {
                                if bytes[i] == b'\\' {
                                    i += 1;
                                }
                                i += 1;
                            }
                            if i < len {
                                i += 1;
                            }
                            continue;
                        }
                        _ => {}
                    }
                    if depth > 0 {
                        i += 1;
                    }
                }
                let expr_end = i;
                if depth == 0 {
                    i += 1; // skip }
                }
                // raw_body is a verbatim source slice, so body_offset + expr_start is the
                // correct absolute position — use the fast sub-parser path directly.
                let expr_offset = body_offset + expr_start as u32;
                let end_offset = body_offset + expr_end as u32;
                let expr =
                    parse_complex_interpolation(arena, source, expr_offset, end_offset, version);
                parts.push(StringPart::Expr(expr));
            }
            _ => {
                literal.push(bytes[i] as char);
                i += 1;
            }
        }
    }

    if !literal.is_empty() {
        parts.push(StringPart::Literal(arena.alloc_str(&literal)));
    }

    parts
}
/// Check if a string inner content contains interpolation (unescaped $)
pub fn has_interpolation(inner: &str) -> bool {
    let bytes = inner.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' {
            i += 2; // skip escape
            continue;
        }
        if bytes[i] == b'$'
            && i + 1 < bytes.len()
            && (is_var_start(bytes[i + 1]) || bytes[i + 1] == b'{')
        {
            return true;
        }
        if bytes[i] == b'{' && i + 1 < bytes.len() && bytes[i + 1] == b'$' {
            return true;
        }
        i += 1;
    }
    false
}

fn is_var_start(b: u8) -> bool {
    b.is_ascii_alphabetic() || b == b'_' || b >= 0x80
}

fn is_var_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b >= 0x80
}

/// Parse an array index from simple string interpolation: `$arr[expr]`.
/// Handles integers (including negative like `-1`), `$variable` references,
/// and bare string keys (e.g. `$arr[key]`).
fn parse_simple_index<'arena, 'src>(
    arena: &'arena bumpalo::Bump,
    source: &'src str,
    idx_str: &str,
    idx_offset: u32,
    idx_end: u32,
) -> Expr<'arena, 'src> {
    let span = Span::new(idx_offset, idx_end);
    // Integer index, including negative (e.g. -1)
    if let Ok(num) = idx_str.parse::<i64>() {
        return Expr {
            kind: ExprKind::Int(num),
            span,
        };
    }
    // Negative integer written as `-N`
    if let Some(digits) = idx_str.strip_prefix('-') {
        if let Ok(num) = digits.parse::<i64>() {
            return Expr {
                kind: ExprKind::Int(-num),
                span,
            };
        }
    }
    // Variable index: $var
    if idx_str.starts_with('$') && idx_str.len() > 1 {
        let name_start = idx_offset as usize + 1;
        let name_end = idx_offset as usize + idx_str.len();
        return Expr {
            kind: ExprKind::Variable(NameStr::Src(&source[name_start..name_end])),
            span,
        };
    }
    // Bare string key (e.g. $arr[key])
    let key_start = idx_offset as usize;
    let key_end = idx_end as usize;
    Expr {
        kind: ExprKind::String(arena.alloc_str(&source[key_start..key_end])),
        span,
    }
}

/// Parse a complex interpolation expression using a sub-parser that starts directly
/// in the original source at the given offset, avoiding string allocation and span reoffset.
fn parse_complex_interpolation<'arena, 'src>(
    arena: &'arena bumpalo::Bump,
    source: &'src str,
    offset: u32,
    end: u32,
    version: PhpVersion,
) -> Expr<'arena, 'src> {
    let mut sub = crate::parser::Parser::new_at(arena, source, offset as usize, version);
    let expr = crate::expr::parse_expr(&mut sub);
    if matches!(expr.kind, ExprKind::Error) {
        Expr {
            kind: ExprKind::Error,
            span: Span::new(offset, end),
        }
    } else {
        expr
    }
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[test]
    fn indented_heredoc_simple_var() {
        let arena = bumpalo::Bump::new();
        let result = crate::parse(&arena, "<?php\n$x = <<<END\n    Hello $name!\n    END;\n");
        assert!(result.errors.is_empty(), "{:?}", result.errors);
    }

    #[test]
    fn indented_heredoc_complex_interpolation() {
        let arena = bumpalo::Bump::new();
        let result = crate::parse(
            &arena,
            "<?php\n$x = <<<END\n    Hello {$obj->name}!\n    END;\n",
        );
        assert!(result.errors.is_empty(), "{:?}", result.errors);
    }

    #[test]
    fn indented_heredoc_multiline_interpolation() {
        let arena = bumpalo::Bump::new();
        let result = crate::parse(
            &arena,
            "<?php\n$x = <<<END\n    Line 1 {$a}\n    Line 2 {$b}\n    END;\n",
        );
        assert!(result.errors.is_empty(), "{:?}", result.errors);
    }

    #[test]
    fn indented_nowdoc() {
        let arena = bumpalo::Bump::new();
        let result = crate::parse(&arena, "<?php\n$x = <<<'END'\n    Hello world!\n    END;\n");
        assert!(result.errors.is_empty(), "{:?}", result.errors);
    }
}