phplang 0.2.17

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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
//! 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()))
}

/// How many diagnostics are queued — a mark for [`truncate_diags`].
pub(crate) fn diag_count() -> usize {
    DIAGS.with(|d| d.borrow().len())
}

/// Drop the diagnostics queued since `mark`. For a body the compiler lowers
/// twice (a `foreach` body, once per subject kind), whose compile-time
/// warnings the reference raises once.
pub(crate) fn truncate_diags(mark: usize) {
    DIAGS.with(|d| d.borrow_mut().truncate(mark));
}

pub(crate) 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>>,
    /// Whether whitespace or a comment separated this token from the one before
    /// it. A qualified name is one token to the reference, so `A \ B` is three
    /// and `A\B` is one; only this tells them apart.
    pub space_before: bool,
    /// The doc comment pending at this token, numbered in source order: the
    /// last `/** … */` before it, unless a `{` or `}` came between (the
    /// reference's scanner resets it there). A declaration starting here takes
    /// it for `getDocComment()` unless an earlier one already did — see
    /// `Parser::take_doc`.
    pub doc: Option<std::rc::Rc<(u32, Box<str>)>>,
}

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

/// Where an interpolated body stops.
#[derive(Clone, Copy, PartialEq, Eq)]
enum InterpEnd {
    /// A double-quoted string: the closing `"`, and an unterminated body is
    /// an error.
    DoubleQuote,
    /// A heredoc body, already cut from the source: everything to the end.
    EndOfInput,
}

struct Lexer<'a> {
    src: &'a [u8],
    pos: usize,
    line: u32,
    out: Vec<Spanned>,
    /// Whitespace or a comment has been skipped since the last token — see
    /// [`Spanned::space_before`].
    gap: bool,
    /// The pending doc comment — see [`Spanned::doc`].
    doc: Option<std::rc::Rc<(u32, Box<str>)>>,
    /// How many doc comments have been read, which numbers them.
    docs: u32,
}

/// 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(),
        gap: false,
        doc: None,
        docs: 0,
    };
    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.gap = true;
                    self.pos += 1;
                }
                b'\n' => {
                    self.gap = true;
                    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.gap = true;
                    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.gap = true;
                    self.skip_line_comment()
                }
                b'/' if self.peek(1) == Some(b'*') => {
                    self.gap = true;
                    self.skip_block_comment()?
                }
                b'$' => self.lex_variable(),
                // Before the operator table, which would otherwise take `<<`
                // and leave a stray `<` and an identifier. A `<<<` that does
                // not open a well-formed heredoc header is NOT one: the
                // reference's scanner then matches the longest operator, `<<`,
                // and the parse error names that token.
                _ if self.starts_with("<<<") && self.heredoc_header_at(self.pos) => {
                    self.lex_heredoc()?
                }
                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;
        let start = self.pos;
        self.advance(2);
        while self.pos < self.src.len() {
            if self.starts_with("*/") {
                self.advance(2);
                // `/**` followed by whitespace opens a doc comment (`/**/`
                // and `/**x` do not), as the reference's scanner reads it.
                let text = &self.src[start..self.pos];
                if text.len() >= 5 && text[2] == b'*' && text[3].is_ascii_whitespace() {
                    self.docs += 1;
                    let text: Box<str> = crate::bytes::from_bytes(text).into();
                    self.doc = Some(std::rc::Rc::new((self.docs, text)));
                }
                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> {
        let (opened, body) = (self.line, self.pos + 1);
        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;
                }
            }
        }
        // Unterminated: the reference scans the rest of the file as the
        // string'"'"'s content and the parser rejects that token, quoting it.
        let rest = String::from_utf8_lossy(&self.src[body.min(self.src.len())..]);
        // Quoted the way `zend_yytnamerr` quotes it: the quote marks at either
        // end of the token text are stripped, so a trailing escaped `\'` goes.
        let rest = rest.strip_suffix('\'').unwrap_or(&rest);
        Err(located(
            format!(
                "syntax error, unexpected string content \"{}\"",
                quoted_token_text(rest)
            ),
            opened,
        ))
    }

    fn lex_double_quote(&mut self) -> Result<(), String> {
        self.pos += 1; // opening quote
        let parts = self.scan_interp(InterpEnd::DoubleQuote)?;
        self.pos += 1; // closing quote
        self.push(Tok::Interp(parts));
        Ok(())
    }

    /// Scan interpolated body text into parts, stopping at `end`. The
    /// terminator itself is left for the caller to consume.
    ///
    /// Shared by the double-quoted string and the heredoc, which differ in
    /// exactly two ways and are otherwise the same language: where the body
    /// stops, and whether `\"` is an escape. In a heredoc it is NOT — the
    /// reference leaves the backslash in place, because a `"` there needs no
    /// escaping to begin with.
    fn scan_interp(&mut self, end: InterpEnd) -> Result<Vec<StrPart>, String> {
        let mut parts: Vec<StrPart> = Vec::new();
        let mut lit = String::new();
        // The line the current literal run began on: the string's opening, or
        // just past the interpolation that ended the previous run. The `${`
        // deprecation is reported HERE rather than on the `${` itself — the
        // reference attributes it to the start of the text token before it, so
        // `"a\n${x}"` reports the line of the `a`, and `"\n${x}"` the line
        // of the opening quote.
        let mut seg_line = self.line;
        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            match c {
                b'"' if end == InterpEnd::DoubleQuote => {
                    if !lit.is_empty() {
                        parts.push(StrPart::Lit(std::mem::take(&mut lit)));
                    }
                    return Ok(parts);
                }
                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'"' if end == InterpEnd::DoubleQuote => '"',
                        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;
                        }
                    }
                    seg_line = self.line;
                }
                // `${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,
                        seg_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));
                    seg_line = self.line;
                }
                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)),
                    }
                    seg_line = self.line;
                }
                b'\n' => {
                    self.line += 1;
                    lit.push('\n');
                    self.pos += 1;
                }
                _ => {
                    // Preserve UTF-8 multibyte sequences verbatim.
                    let ch_len = utf8_len(c);
                    let stop = (self.pos + ch_len).min(self.src.len());
                    lit.push_str(&String::from_utf8_lossy(&self.src[self.pos..stop]));
                    self.pos = stop;
                }
            }
        }
        if end == InterpEnd::EndOfInput {
            if !lit.is_empty() {
                parts.push(StrPart::Lit(lit));
            }
            return Ok(parts);
        }
        if end != InterpEnd::DoubleQuote {
            return Err(format!("unterminated string (line {})", self.line));
        }
        // An unterminated double-quoted string meets end of file inside the
        // parser'"'"'s string rule, so what it expected depends on how far it got.
        let expecting = if parts.iter().any(|p| !matches!(p, StrPart::Lit(_))) {
            ""
        } else if parts.is_empty() && lit.is_empty() {
            ", expecting variable or string content or \"${\" or \"{$\""
        } else {
            ", expecting variable or \"${\" or \"{$\""
        };
        Err(located(
            format!("syntax error, unexpected end of file{expecting}"),
            self.line,
        ))
    }

    /// Whether a heredoc header starts at `at`: `<<<`, blanks, a label (bare,
    /// or in matching single or double quotes) that does not begin with a
    /// digit, and a line break — the reference scanner's
    /// `"<<<"{TABS_AND_SPACES}({LABEL}|'{LABEL}'|"{LABEL}"){NEWLINE}`.
    fn heredoc_header_at(&self, at: usize) -> bool {
        let src = &self.src;
        let mut i = at + 3;
        while matches!(src.get(i), Some(b' ' | b'\t')) {
            i += 1;
        }
        let quote = match src.get(i) {
            Some(&q @ (b'\'' | b'"')) => {
                i += 1;
                Some(q)
            }
            _ => None,
        };
        if !matches!(src.get(i), Some(&b) if is_ident(b) && !b.is_ascii_digit()) {
            return false;
        }
        while matches!(src.get(i), Some(&b) if is_ident(b)) {
            i += 1;
        }
        if let Some(q) = quote {
            if src.get(i) != Some(&q) {
                return false;
            }
            i += 1;
        }
        matches!(src.get(i), Some(b'\n' | b'\r'))
    }

    /// `<<<LABEL` (heredoc) and `<<<'LABEL'` (nowdoc).
    ///
    /// The body is delimited by a line whose first non-blank content is the
    /// label, and the closing line's indentation is stripped from every body
    /// line (PHP 7.3's flexible form) before anything else happens — the
    /// reference dedents the raw text and only then processes escapes and
    /// interpolation, so a `$var` that begins a line is not affected by how
    /// far the block is indented.
    ///
    /// A heredoc body is the double-quoted language minus the `\"` escape, so
    /// it is scanned by [`Lexer::scan_interp`] over the dedented text rather
    /// than by a second copy of it. A nowdoc body is verbatim: no escapes at
    /// all, not even `\\`.
    fn lex_heredoc(&mut self) -> Result<(), String> {
        let opened = self.line;
        let start = self.pos;
        self.pos += 3; // `<<<`
        while matches!(self.src.get(self.pos), Some(b' ' | b'\t')) {
            self.pos += 1;
        }
        // `<<<'X'` is a nowdoc, `<<<"X"` and bare `<<<X` are heredocs.
        let quote = match self.src.get(self.pos) {
            Some(&q @ (b'\'' | b'"')) => {
                self.pos += 1;
                Some(q)
            }
            _ => None,
        };
        let label_start = self.pos;
        while matches!(self.src.get(self.pos), Some(&b) if is_ident(b)) {
            self.pos += 1;
        }
        let label = String::from_utf8_lossy(&self.src[label_start..self.pos]).into_owned();
        if label.is_empty() || label.as_bytes()[0].is_ascii_digit() {
            return Err(located(
                "syntax error, unexpected token \"<<<\"".into(),
                opened,
            ));
        }
        if let Some(q) = quote {
            if self.src.get(self.pos) != Some(&q) {
                return Err(located(
                    "syntax error, unexpected token \"<<<\"".into(),
                    opened,
                ));
            }
            self.pos += 1;
        }
        if self.src.get(self.pos) == Some(&b'\r') {
            self.pos += 1;
        }
        if self.src.get(self.pos) != Some(&b'\n') {
            return Err(located(
                "syntax error, unexpected token \"<<<\"".into(),
                opened,
            ));
        }
        self.pos += 1;
        let body_start = self.pos;

        // The closing line: the label as the first non-blank content, and not
        // the head of a longer identifier — `EOTX` does not close `EOT`.
        let (body_end, indent_len, after) = {
            let mut i = body_start;
            loop {
                let line_start = i;
                let mut j = i;
                while matches!(self.src.get(j), Some(b' ' | b'\t')) {
                    j += 1;
                }
                let is_close = self.src[j..].starts_with(label.as_bytes())
                    && !matches!(self.src.get(j + label.len()), Some(&b) if is_ident(b));
                if is_close {
                    break (line_start, j - line_start, j + label.len());
                }
                match self.src[i..].iter().position(|&b| b == b'\n') {
                    Some(n) => i += n + 1,
                    // The reference names the line the file RAN OUT on, not the
                    // line the heredoc opened on — and it words the failure by
                    // what the body scanned: a body that reached an
                    // interpolation is `unexpected end of file` alone, while
                    // one that is still plain text lists what could have
                    // followed it.
                    None => {
                        let rest = &self.src[body_start..];
                        let eof = opened
                            + self.src[start..].iter().filter(|&&b| b == b'\n').count() as u32;
                        let msg = if scanned_interpolation(rest) {
                            "syntax error, unexpected end of file".to_string()
                        } else {
                            "syntax error, unexpected end of file, expecting variable \
                             or heredoc end or \"${\" or \"{$\""
                                .to_string()
                        };
                        return Err(located(msg, eof));
                    }
                }
            }
        };

        // The newline that ends the last body line belongs to the delimiter,
        // not to the string.
        let mut raw_end = body_end;
        if raw_end > body_start && self.src[raw_end - 1] == b'\n' {
            raw_end -= 1;
            if raw_end > body_start && self.src[raw_end - 1] == b'\r' {
                raw_end -= 1;
            }
        }
        let raw = String::from_utf8_lossy(&self.src[body_start..raw_end]).into_owned();
        let body_line = opened
            + self.src[start..body_start]
                .iter()
                .filter(|&&b| b == b'\n')
                .count() as u32;
        let body = dedent_heredoc(&raw, &self.src[body_end..body_end + indent_len], body_line)?;

        // Line accounting covers the whole construct, delimiter included, so the
        // token that follows the heredoc is attributed to the closing line.
        let consumed = self.src[start..after]
            .iter()
            .filter(|&&b| b == b'\n')
            .count() as u32;
        self.pos = after;

        // The token is attributed to the line the `<<<` was written on, which is
        // where the reference reports a diagnostic raised by the body.
        if quote == Some(b'\'') {
            self.push(Tok::Str(body));
        } else {
            let mut sub = Lexer {
                src: body.as_bytes(),
                pos: 0,
                line: body_line,
                out: Vec::new(),
                gap: false,
                doc: None,
                docs: 0,
            };
            let parts = sub.scan_interp(InterpEnd::EndOfInput)?;
            self.push(Tok::Interp(parts));
        }
        self.line = opened + consumed;
        Ok(())
    }

    /// 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) {
        // A `\xHH`/`\NNN` escape may have left raw bytes that, together, are
        // a character: the literal is stored canonically (see `crate::bytes`).
        let tok = match tok {
            Tok::Str(s) => Tok::Str(crate::bytes::canonical(s)),
            Tok::Interp(parts) => Tok::Interp(
                parts
                    .into_iter()
                    .map(|p| match p {
                        StrPart::Lit(s) => StrPart::Lit(crate::bytes::canonical(s)),
                        other => other,
                    })
                    .collect(),
            ),
            other => other,
        };
        self.out.push(Spanned {
            tok,
            line: self.line,
            raw: None,
            space_before: std::mem::take(&mut self.gap),
            doc: self.doc.clone(),
        });
        if matches!(self.out.last().map(|s| &s.tok), Some(Tok::Punct("{" | "}"))) {
            self.doc = 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()),
            space_before: std::mem::take(&mut self.gap),
            doc: self.doc.clone(),
        });
    }

    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, one BYTE. `self.pos` is on the `x`.
    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 Ok(b) = u8::from_str_radix(&hex, 16) {
            lit.push_str(&crate::bytes::from_bytes(&[b]));
        }
    }

    /// `\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) {
            // Its UTF-8 bytes, stored as `crate::bytes` stores any bytes — a
            // character in the escape range is four escaped bytes.
            lit.push_str(&crate::bytes::from_bytes(
                c.encode_utf8(&mut [0u8; 4]).as_bytes(),
            ));
        }
    }

    /// `\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 Ok(v) = u32::from_str_radix(&oct, 8) {
            lit.push_str(&crate::bytes::from_bytes(&[(v & 0xff) as u8]));
        }
    }

    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).
/// Whether an unterminated heredoc body reached an interpolation before the
/// file ran out — a `$name` or a `{$`, either of which puts the reference's
/// lexer in a state whose end-of-file message lists nothing.
fn scanned_interpolation(body: &[u8]) -> bool {
    body.windows(2).any(|w| match w {
        [b'$', c] => is_ident(*c) && !c.is_ascii_digit(),
        [b'{', b'$'] => true,
        _ => false,
    })
}

/// Strip the closing delimiter's indentation from every line of a heredoc body
/// (PHP 7.3's flexible heredoc).
///
/// A line that is entirely empty carries no indentation and is legal at any
/// level; any other line must begin with the delimiter's exact indentation, and
/// the reference refuses the whole block when one does not — it is a PARSE
/// error, naming the first offending line, not a line silently left as written.
fn dedent_heredoc(raw: &str, indent: &[u8], first_line: u32) -> Result<String, String> {
    if indent.is_empty() {
        return Ok(raw.to_string());
    }
    let indent = String::from_utf8_lossy(indent).into_owned();
    let mut out = String::with_capacity(raw.len());
    for (n, line) in raw.split('\n').enumerate() {
        if n > 0 {
            out.push('\n');
        }
        if line.strip_suffix('\r').unwrap_or(line).is_empty() {
            out.push_str(line);
            continue;
        }
        match line.strip_prefix(indent.as_str()) {
            Some(rest) => out.push_str(rest),
            None => {
                return Err(located(
                    format!(
                        "Invalid body indentation level (expecting an indentation \
                         level of at least {})",
                        indent.chars().count()
                    ),
                    first_line + n as u32,
                ))
            }
        }
    }
    Ok(out)
}

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,
    }
}

/// A token's text as a syntax error quotes it (`zend_yytnamerr`): cut at the
/// first line break, and past 33 bytes shortened to its first 30 and `...`.
pub(crate) fn quoted_token_text(s: &str) -> String {
    let line = s.split('\n').next().unwrap_or_default();
    if line.len() <= 33 {
        return line.to_string();
    }
    let mut cut = 30;
    while !line.is_char_boundary(cut) {
        cut -= 1;
    }
    format!("{}...", &line[..cut])
}