phplang 0.2.2

PHP as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PhpHost heap — the first compiled standalone PHP runtime
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
//! The PHP lexer.
//!
//! Handles the two lexing modes PHP requires: *inline-HTML* mode (text outside
//! `<?php ... ?>`, emitted as a single `InlineHtml` token) and *PHP* mode (the
//! code between the tags). `<?=` opens PHP mode with an implicit `echo`.

use crate::ast::StrPart;
use crate::errlevel::E_DEPRECATED;
use std::cell::RefCell;

/// A diagnostic the *compiler* raises — a notice PHP prints once, while reading
/// the source, before the program has run a single instruction.
///
/// PHP's compiler and its runtime write to the same stream, so a compile-time
/// notice is indistinguishable from a runtime one except in ORDER: it is emitted
/// before any output the script produces, even output on an earlier line, and it
/// fires whether or not the code that carries it ever executes. Nothing in the
/// runtime can suppress one after the fact — `error_reporting(0)` on line 1 does
/// not silence a notice from line 2, because the notice was already decided when
/// the file was read. Only the *initial* error level (`-d error_reporting=…`)
/// applies to it.
#[derive(Debug, Clone, PartialEq)]
pub struct CompileDiag {
    /// The severity label PHP prints — `"Deprecated"`, `"Warning"`, `"Notice"`.
    pub severity: &'static str,
    /// The `E_*` bit the initial error level is tested against.
    pub level: i64,
    pub line: u32,
    pub msg: String,
}

thread_local! {
    /// Diagnostics collected by the lexer for the source currently being read.
    /// Reset by [`clear_diags`] when a top-level parse begins and drained by
    /// [`take_diags`] when the compilation that owns them finishes, so one
    /// compilation can never inherit the notices of another.
    ///
    /// Deliberately NOT cleared by [`lex`] itself: a string interpolation re-lexes
    /// its `{$expr}` source with a nested `lex` call, and clearing there would
    /// discard every notice raised earlier in the same file.
    static DIAGS: RefCell<Vec<CompileDiag>> = const { RefCell::new(Vec::new()) };
}

/// Drop any diagnostics left over from an earlier source. Called once per
/// top-level parse, before its first [`lex`].
pub fn clear_diags() {
    DIAGS.with(|d| d.borrow_mut().clear());
}

/// Take (and clear) the compile diagnostics raised since [`clear_diags`].
pub fn take_diags() -> Vec<CompileDiag> {
    DIAGS.with(|d| std::mem::take(&mut *d.borrow_mut()))
}

fn push_diag(severity: &'static str, level: i64, line: u32, msg: impl Into<String>) {
    DIAGS.with(|d| {
        d.borrow_mut().push(CompileDiag {
            severity,
            level,
            line,
            msg: msg.into(),
        })
    });
}

#[derive(Debug, Clone, PartialEq)]
pub enum Tok {
    InlineHtml(String),
    /// `<?=` short-echo open tag (parser emits an implicit `echo`).
    OpenEcho,
    Var(String),
    Ident(String),
    Int(i64),
    Float(f64),
    /// Single-quoted string — no interpolation.
    Str(String),
    /// Double-quoted / interpolated string.
    Interp(Vec<StrPart>),
    Punct(&'static str),
}

/// A token plus its 1-based source line.
#[derive(Debug, Clone)]
pub struct Spanned {
    pub tok: Tok,
    pub line: u32,
    /// The literal's source spelling, kept only for numbers — `0x1f`, `07`,
    /// `1_000`, `2.0` and `1e3` all lex to a value whose default rendering is not
    /// the text the author wrote, and PHP echoes that text back verbatim in a
    /// syntax error (`unexpected integer "0x1f"`). `None` for every other token,
    /// whose spelling the token itself already carries.
    pub raw: Option<Box<str>>,
}

/// The multi-character operators, longest first so `===` beats `==` beats `=`.
const OPERATORS: &[&str] = &[
    "===", "!==", "<=>", "<<=", ">>=", "**=", "...", "?->", "==", "!=", "<>", "<=", ">=", "&&",
    "||", "++", "--", "->", "=>", "::", "+=", "-=", "*=", "/=", "%=", ".=", "&=", "|=", "^=", "<<",
    ">>", "**", "(", ")", "{", "}", "[", "]", ";", ",", "=", "+", "-", "*", "/", "%", ".", "<",
    ">", "!", "?", ":", "&", "|", "^", "~", "@", "\\",
];

struct Lexer<'a> {
    src: &'a [u8],
    pos: usize,
    line: u32,
    out: Vec<Spanned>,
}

/// Render a lexer failure the way the CLI prints one: the message, the script it
/// came from and the line it names.
///
/// A parse failure gets this from `Parser::syntax_error`; a LEXER failure never
/// reached that code, so without this it printed bare and lost the ` in <file>
/// on line N` half every other diagnostic carries.
fn located(msg: String, line: u32) -> String {
    crate::host::with_host(|h| format!("{msg} in {} on line {line}", h.script_name()))
}

/// Tokenize a PHP source string.
pub fn lex(src: &str) -> Result<Vec<Spanned>, String> {
    let mut lx = Lexer {
        src: src.as_bytes(),
        pos: 0,
        line: 1,
        out: Vec::new(),
    };
    lx.run()?;
    Ok(lx.out)
}

impl<'a> Lexer<'a> {
    fn run(&mut self) -> Result<(), String> {
        loop {
            // Inline-HTML mode: consume verbatim up to the next `<?`.
            let html = self.read_inline_html();
            if !html.is_empty() {
                self.push(Tok::InlineHtml(html));
            }
            if self.pos >= self.src.len() {
                return Ok(());
            }
            // At an opening tag.
            if self.starts_with("<?php") {
                self.advance(5);
            } else if self.starts_with("<?=") {
                self.advance(3);
                self.push(Tok::OpenEcho);
            } else if self.starts_with("<?") {
                self.advance(2);
            }
            // PHP mode until the matching `?>` (or EOF).
            self.lex_php()?;
        }
    }

    /// Consume text until the next `<?` (exclusive) or EOF.
    fn read_inline_html(&mut self) -> String {
        let start = self.pos;
        while self.pos < self.src.len() {
            if self.starts_with("<?") {
                break;
            }
            if self.src[self.pos] == b'\n' {
                self.line += 1;
            }
            self.pos += 1;
        }
        String::from_utf8_lossy(&self.src[start..self.pos]).into_owned()
    }

    fn lex_php(&mut self) -> Result<(), String> {
        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            match c {
                b' ' | b'\t' | b'\r' => self.pos += 1,
                b'\n' => {
                    self.line += 1;
                    self.pos += 1;
                }
                _ if self.starts_with("?>") => {
                    self.advance(2);
                    // PHP swallows one newline immediately after a close tag.
                    if self.pos < self.src.len() && self.src[self.pos] == b'\n' {
                        self.line += 1;
                        self.pos += 1;
                    }
                    // A `?>` acts as an implicit statement terminator.
                    self.push(Tok::Punct(";"));
                    return Ok(());
                }
                b'/' if self.peek(1) == Some(b'/') => self.skip_line_comment(),
                // `#[` opens an ATTRIBUTE, not a comment. The two spellings share
                // a leading `#`, and PHP 8 resolved the ambiguity in favour of the
                // attribute: `#[Attr] class C {}` declares a class, it does not
                // comment the whole line out. Lexing it as a comment silently
                // deletes every attributed declaration to the end of the line —
                // for `php -r`, usually the entire program.
                b'#' if self.peek(1) == Some(b'[') => {
                    self.push(Tok::Punct("#["));
                    self.advance(2);
                }
                b'#' => self.skip_line_comment(),
                b'/' if self.peek(1) == Some(b'*') => self.skip_block_comment()?,
                b'$' => self.lex_variable(),
                b'\'' => self.lex_single_quote()?,
                b'"' => self.lex_double_quote()?,
                b'0'..=b'9' => self.lex_number(),
                b'.' if matches!(self.peek(1), Some(b'0'..=b'9')) => self.lex_number(),
                _ if c == b'_' || c.is_ascii_alphabetic() => self.lex_ident(),
                _ => self.lex_operator()?,
            }
        }
        Ok(())
    }

    fn skip_line_comment(&mut self) {
        while self.pos < self.src.len() && self.src[self.pos] != b'\n' {
            // A `?>` ends a `//`/`#` comment (and PHP mode) even mid-line.
            if self.starts_with("?>") {
                return;
            }
            self.pos += 1;
        }
    }

    fn skip_block_comment(&mut self) -> Result<(), String> {
        // The reference names the line the comment OPENED on, not the line the
        // scan gave up on, so it is captured before the walk moves `self.line`.
        let opened = self.line;
        self.advance(2);
        while self.pos < self.src.len() {
            if self.starts_with("*/") {
                self.advance(2);
                return Ok(());
            }
            if self.src[self.pos] == b'\n' {
                self.line += 1;
            }
            self.pos += 1;
        }
        Err(located(
            format!("Unterminated comment starting line {opened}"),
            opened,
        ))
    }

    fn lex_variable(&mut self) {
        self.pos += 1; // `$`
        let start = self.pos;
        while self.pos < self.src.len() && is_ident(self.src[self.pos]) {
            self.pos += 1;
        }
        let name = String::from_utf8_lossy(&self.src[start..self.pos]).into_owned();
        // `$$x` and `${expr}`: a `$` with no name after it is the
        // variable-variable sigil, not a variable called "". The parser reads
        // the operand that follows and looks the name up at run time.
        if name.is_empty() {
            self.push(Tok::Punct("$"));
            return;
        }
        self.push(Tok::Var(name));
    }

    fn lex_ident(&mut self) {
        let start = self.pos;
        while self.pos < self.src.len() && is_ident(self.src[self.pos]) {
            self.pos += 1;
        }
        let name = String::from_utf8_lossy(&self.src[start..self.pos]).into_owned();
        self.push(Tok::Ident(name));
    }

    fn lex_number(&mut self) {
        let tok_start = self.pos;
        // Radix-prefixed integer literals: `0x`/`0X` hex, `0b`/`0B` binary,
        // `0o`/`0O` octal (PHP 8.1). Underscores are permitted as separators.
        if self.src[self.pos] == b'0' {
            if let Some(radix) = match self.peek(1) {
                Some(b'x') | Some(b'X') => Some(16u32),
                Some(b'b') | Some(b'B') => Some(2),
                Some(b'o') | Some(b'O') => Some(8),
                _ => None,
            } {
                self.advance(2);
                let ds = self.pos;
                while self.pos < self.src.len() {
                    let c = self.src[self.pos];
                    let ok = c == b'_'
                        || match radix {
                            16 => c.is_ascii_hexdigit(),
                            8 => (b'0'..=b'7').contains(&c),
                            _ => c == b'0' || c == b'1',
                        };
                    if ok {
                        self.pos += 1;
                    } else {
                        break;
                    }
                }
                let digits: String = String::from_utf8_lossy(&self.src[ds..self.pos])
                    .chars()
                    .filter(|c| *c != '_')
                    .collect();
                let raw = String::from_utf8_lossy(&self.src[tok_start..self.pos]).into_owned();
                self.push_num(parse_radix(&digits, radix), &raw);
                return;
            }
        }

        let start = self.pos;
        let mut is_float = false;
        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            if c.is_ascii_digit() || c == b'_' {
                self.pos += 1;
            } else if c == b'.' && !is_float && matches!(self.peek(1), Some(b'0'..=b'9')) {
                is_float = true;
                self.pos += 1;
            } else if (c == b'e' || c == b'E')
                && matches!(self.peek(1), Some(b'0'..=b'9' | b'+' | b'-'))
            {
                is_float = true;
                self.pos += 1;
                if matches!(self.src.get(self.pos), Some(b'+' | b'-')) {
                    self.pos += 1;
                }
            } else {
                break;
            }
        }
        let src_text = String::from_utf8_lossy(&self.src[start..self.pos]).into_owned();
        let raw: String = src_text.chars().filter(|c| *c != '_').collect();
        if is_float {
            self.push_num(Tok::Float(raw.parse().unwrap_or(0.0)), &src_text);
            return;
        }
        // A leading-zero integer with all-octal digits is an octal literal
        // (`0755`), the classic PHP form; a `0` alone stays decimal zero.
        if raw.len() > 1 && raw.starts_with('0') && raw.bytes().all(|b| (b'0'..=b'7').contains(&b))
        {
            self.push_num(parse_radix(&raw[1..], 8), &src_text);
            return;
        }
        match raw.parse::<i64>() {
            Ok(n) => self.push_num(Tok::Int(n), &src_text),
            // Integer literal that overflows i64 becomes a float, as PHP does.
            Err(_) => self.push_num(Tok::Float(raw.parse().unwrap_or(0.0)), &src_text),
        }
    }

    fn lex_single_quote(&mut self) -> Result<(), String> {
        self.pos += 1; // opening quote
        let mut s = String::new();
        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            match c {
                b'\'' => {
                    self.pos += 1;
                    self.push(Tok::Str(s));
                    return Ok(());
                }
                b'\\' if matches!(self.peek(1), Some(b'\'' | b'\\')) => {
                    // In single quotes only \' and \\ are escapes.
                    self.pos += 1;
                    s.push(self.src[self.pos] as char);
                    self.pos += 1;
                }
                b'\n' => {
                    self.line += 1;
                    s.push('\n');
                    self.pos += 1;
                }
                _ => {
                    // Preserve multibyte UTF-8 sequences verbatim (a raw
                    // `c as char` would decode each byte as Latin-1 and mojibake
                    // any non-ASCII text).
                    let ch_len = utf8_len(c);
                    let end = (self.pos + ch_len).min(self.src.len());
                    s.push_str(&String::from_utf8_lossy(&self.src[self.pos..end]));
                    self.pos = end;
                }
            }
        }
        Err(format!("unterminated string (line {})", self.line))
    }

    fn lex_double_quote(&mut self) -> Result<(), String> {
        self.pos += 1; // opening quote
        let mut parts: Vec<StrPart> = Vec::new();
        let mut lit = String::new();
        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            match c {
                b'"' => {
                    self.pos += 1;
                    if !lit.is_empty() {
                        parts.push(StrPart::Lit(std::mem::take(&mut lit)));
                    }
                    self.push(Tok::Interp(parts));
                    return Ok(());
                }
                b'\\' => {
                    self.pos += 1;
                    let e = self.src.get(self.pos).copied().unwrap_or(b'\\');
                    // The variable-length escapes consume their own digits and
                    // push their own bytes, so they `continue` past the single
                    // trailing `self.pos += 1` below.
                    match e {
                        b'x' if self.hex_digits_at(self.pos + 1) > 0 => {
                            self.read_hex_escape(&mut lit);
                            continue;
                        }
                        b'u' if self.peek(1) == Some(b'{') => {
                            self.read_unicode_escape(&mut lit);
                            continue;
                        }
                        b'0'..=b'7' => {
                            self.read_octal_escape(&mut lit);
                            continue;
                        }
                        _ => {}
                    }
                    let ch = match e {
                        b'n' => '\n',
                        b't' => '\t',
                        b'r' => '\r',
                        b'v' => '\x0b',
                        b'f' => '\x0c',
                        b'"' => '"',
                        b'\\' => '\\',
                        b'$' => '$',
                        b'e' => '\x1b',
                        other => {
                            // Unknown escape: PHP keeps the backslash verbatim.
                            lit.push('\\');
                            other as char
                        }
                    };
                    lit.push(ch);
                    self.pos += 1;
                }
                // `{$expr}` — the complex form, any expression. Recorded as source
                // for the parser; a `{` not followed by `$` is ordinary text.
                b'{' if self.peek(1) == Some(b'$') => {
                    if !lit.is_empty() {
                        parts.push(StrPart::Lit(std::mem::take(&mut lit)));
                    }
                    match self.read_braced_interp() {
                        Some(src) => parts.push(StrPart::Raw(src)),
                        // Unbalanced: PHP would not have accepted it either, but
                        // treat the brace as literal rather than lose the rest.
                        None => {
                            lit.push('{');
                            self.pos += 1;
                        }
                    }
                }
                // `${name}` — the pre-8.2 form, deprecated but still substituted.
                // The notice is raised HERE, at lex time, because that is when the
                // reference engine raises it: it is a property of the source text,
                // not of the interpolation ever running.
                b'$' if self.peek(1) == Some(b'{') => {
                    if !lit.is_empty() {
                        parts.push(StrPart::Lit(std::mem::take(&mut lit)));
                    }
                    push_diag(
                        "Deprecated",
                        E_DEPRECATED,
                        self.line,
                        "Using ${var} in strings is deprecated, use {$var} instead",
                    );
                    self.pos += 2; // `${`
                    let start = self.pos;
                    while self.pos < self.src.len() && is_ident(self.src[self.pos]) {
                        self.pos += 1;
                    }
                    let name = String::from_utf8_lossy(&self.src[start..self.pos]).into_owned();
                    if self.src.get(self.pos) == Some(&b'}') {
                        self.pos += 1;
                    }
                    parts.push(StrPart::Var(name));
                }
                b'$' if matches!(self.peek(1), Some(b) if b == b'_' || b.is_ascii_alphabetic()) => {
                    if !lit.is_empty() {
                        parts.push(StrPart::Lit(std::mem::take(&mut lit)));
                    }
                    self.pos += 1; // `$`
                    let start = self.pos;
                    while self.pos < self.src.len() && is_ident(self.src[self.pos]) {
                        self.pos += 1;
                    }
                    let name = String::from_utf8_lossy(&self.src[start..self.pos]).into_owned();
                    // Simple syntax reaches exactly one level deep: `$a->p` and
                    // `$a[k]`, and no further (`"$a->p->q"` interpolates `$a->p`
                    // and leaves `->q` as text).
                    match self.read_simple_interp_suffix(&name) {
                        Some(src) => parts.push(StrPart::Raw(src)),
                        None => parts.push(StrPart::Var(name)),
                    }
                }
                b'\n' => {
                    self.line += 1;
                    lit.push('\n');
                    self.pos += 1;
                }
                _ => {
                    // Preserve UTF-8 multibyte sequences verbatim.
                    let ch_len = utf8_len(c);
                    let end = (self.pos + ch_len).min(self.src.len());
                    lit.push_str(&String::from_utf8_lossy(&self.src[self.pos..end]));
                    self.pos = end;
                }
            }
        }
        Err(format!("unterminated string (line {})", self.line))
    }

    /// Consume a `{$…}` interpolation and return the expression source between
    /// the braces, or `None` if the braces are unbalanced.
    ///
    /// Braces nest (`"{$a[$b['{']]}"`), and a brace inside a quoted string in the
    /// expression is not a delimiter, so both are tracked while scanning.
    fn read_braced_interp(&mut self) -> Option<String> {
        let start = self.pos + 1; // just past `{`
        let mut i = start;
        let mut depth = 1usize;
        let mut quote: Option<u8> = None;
        while i < self.src.len() {
            let c = self.src[i];
            match quote {
                Some(q) => {
                    if c == b'\\' {
                        i += 1;
                    } else if c == q {
                        quote = None;
                    }
                }
                None => match c {
                    b'\'' | b'"' => quote = Some(c),
                    b'{' => depth += 1,
                    b'}' => {
                        depth -= 1;
                        if depth == 0 {
                            let src = String::from_utf8_lossy(&self.src[start..i]).into_owned();
                            self.line +=
                                self.src[start..i].iter().filter(|&&b| b == b'\n').count() as u32;
                            self.pos = i + 1;
                            return Some(src);
                        }
                    }
                    b'\n' => {}
                    _ => {}
                },
            }
            i += 1;
        }
        None
    }

    /// After a bare `$name` inside a string, consume PHP's *simple* interpolation
    /// suffix if one follows, returning the expression source for it.
    ///
    /// `$a->prop` reads one property. `$a[k]` reads one element, and in this
    /// syntax an unquoted key is a *string* — `"$a[k]"` means `$a['k']`, unlike
    /// the same text outside a string, where `k` would be a constant. A `$var` or
    /// a signed integer key is taken as written.
    fn read_simple_interp_suffix(&mut self, name: &str) -> Option<String> {
        if self.src.get(self.pos) == Some(&b'-') && self.peek(1) == Some(b'>') {
            let after = self.pos + 2;
            if !matches!(self.src.get(after), Some(&b) if b == b'_' || b.is_ascii_alphabetic()) {
                return None;
            }
            let mut i = after;
            while i < self.src.len() && is_ident(self.src[i]) {
                i += 1;
            }
            let prop = String::from_utf8_lossy(&self.src[after..i]).into_owned();
            self.pos = i;
            return Some(format!("${name}->{prop}"));
        }
        if self.src.get(self.pos) != Some(&b'[') {
            return None;
        }
        let key_start = self.pos + 1;
        let mut i = key_start;
        while i < self.src.len() && self.src[i] != b']' {
            i += 1;
        }
        if i >= self.src.len() {
            return None;
        }
        let key = String::from_utf8_lossy(&self.src[key_start..i]).into_owned();
        if key.is_empty() {
            return None;
        }
        let key_expr = if key.starts_with('$')
            || key.parse::<i64>().is_ok()
            || (key.starts_with('\'') && key.ends_with('\''))
            || (key.starts_with('"') && key.ends_with('"'))
        {
            key
        } else {
            format!("'{}'", key.replace('\\', "\\\\").replace('\'', "\\'"))
        };
        self.pos = i + 1;
        Some(format!("${name}[{key_expr}]"))
    }

    fn lex_operator(&mut self) -> Result<(), String> {
        for op in OPERATORS {
            if self.starts_with(op) {
                self.advance(op.len());
                self.push(Tok::Punct(op));
                return Ok(());
            }
        }
        let c = self.src[self.pos] as char;
        Err(format!("unexpected character '{c}' (line {})", self.line))
    }

    // ── helpers ──────────────────────────────────────────────────────────────

    fn push(&mut self, tok: Tok) {
        self.out.push(Spanned {
            tok,
            line: self.line,
            raw: None,
        });
    }

    /// [`push`](Self::push) for a numeric literal, recording the source spelling
    /// alongside the parsed value — see [`Spanned::raw`].
    fn push_num(&mut self, tok: Tok, raw: &str) {
        self.out.push(Spanned {
            tok,
            line: self.line,
            raw: Some(raw.into()),
        });
    }

    fn starts_with(&self, s: &str) -> bool {
        self.src[self.pos..].starts_with(s.as_bytes())
    }

    fn peek(&self, n: usize) -> Option<u8> {
        self.src.get(self.pos + n).copied()
    }

    /// How many hex digits (capped at 2) start at `at` — used to tell a real
    /// `\xHH` escape from a literal `\x` that PHP leaves alone.
    fn hex_digits_at(&self, at: usize) -> usize {
        (0..2)
            .take_while(|i| self.src.get(at + i).is_some_and(|b| b.is_ascii_hexdigit()))
            .count()
    }

    /// `\xHH` — one or two hex digits. `self.pos` is on the `x`.
    ///
    /// The scaffold stores strings as Rust `String`, so a byte at or above 0x80
    /// is appended as that Unicode scalar (its UTF-8 encoding) rather than the
    /// single raw byte PHP would store.
    fn read_hex_escape(&mut self, lit: &mut String) {
        self.pos += 1; // past `x`
        let n = self.hex_digits_at(self.pos);
        let hex: String = self.src[self.pos..self.pos + n]
            .iter()
            .map(|b| *b as char)
            .collect();
        self.pos += n;
        if let Some(c) = u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) {
            lit.push(c);
        }
    }

    /// `\u{HHH…}` — a Unicode code point. `self.pos` is on the `u`.
    fn read_unicode_escape(&mut self, lit: &mut String) {
        let close = self.src[self.pos..]
            .iter()
            .position(|b| *b == b'}')
            .map(|i| self.pos + i);
        let Some(close) = close else {
            // No closing brace: not an escape, keep the backslash verbatim.
            lit.push_str("\\u");
            self.pos += 1;
            return;
        };
        let hex: String = self.src[self.pos + 2..close]
            .iter()
            .map(|b| *b as char)
            .collect();
        self.pos = close + 1;
        if let Some(c) = u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) {
            lit.push(c);
        }
    }

    /// `\NNN` — one to three octal digits. `self.pos` is on the first digit.
    fn read_octal_escape(&mut self, lit: &mut String) {
        let n = (0..3)
            .take_while(|i| {
                self.src
                    .get(self.pos + i)
                    .is_some_and(|b| (b'0'..=b'7').contains(b))
            })
            .count();
        let oct: String = self.src[self.pos..self.pos + n]
            .iter()
            .map(|b| *b as char)
            .collect();
        self.pos += n;
        // PHP truncates an out-of-range octal escape to a byte.
        if let Some(c) = u32::from_str_radix(&oct, 8)
            .ok()
            .and_then(|v| char::from_u32(v & 0xff))
        {
            lit.push(c);
        }
    }

    fn advance(&mut self, n: usize) {
        for _ in 0..n {
            if self.pos < self.src.len() && self.src[self.pos] == b'\n' {
                self.line += 1;
            }
            self.pos += 1;
        }
    }
}

/// Parse `digits` in the given radix into an integer token, falling back to a
/// float when the value overflows `i64` (as PHP does for large literals).
fn parse_radix(digits: &str, radix: u32) -> Tok {
    match i64::from_str_radix(digits, radix) {
        Ok(n) => Tok::Int(n),
        Err(_) => Tok::Float(
            u128::from_str_radix(digits, radix)
                .map(|u| u as f64)
                .unwrap_or(0.0),
        ),
    }
}

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

/// Byte length of a UTF-8 code point given its leading byte.
fn utf8_len(b: u8) -> usize {
    match b {
        0x00..=0x7f => 1,
        0xc0..=0xdf => 2,
        0xe0..=0xef => 3,
        _ => 4,
    }
}