jsonfix 0.2.0

Repair, extract, and partially parse malformed JSON from LLMs and other unreliable sources. no_std, zero dependencies, lossless numbers.
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
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
//! A tolerant tokenizer: it never fails on input that a repair pass can fix.

use alloc::borrow::Cow;
use alloc::string::String;

use crate::chars::{
    decode_html_entity, hex_value, is_delimiter, is_double_quote, is_func_name_char, is_quote,
    is_single_quote, is_special_ws, is_unquoted_delimiter, is_url_char, is_url_scheme, is_ws,
    simple_escape,
};
use crate::error::{Error, ErrorKind};
use crate::options::{Allow, Options, Repairs};

/// One lexical unit, tolerant of the mistakes the repair passes handle.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Token<'a> {
    OpenBrace,
    CloseBrace,
    OpenBracket,
    CloseBracket,
    Colon,
    Comma,
    Plus,
    Ellipsis,
    OpenParen,
    CloseParen,
    /// A `;`, which appears after JSONP calls and in hand-written data.
    Semicolon,
    /// A string, with typographic quotes normalized and escapes decoded.
    ///
    /// Plain strings borrow the input slice (zero allocation); only strings
    /// that needed escape/entity/quote repair own a `String`.
    Str {
        text: Cow<'a, str>,
        truncated: bool,
    },
    /// A number whose text is already valid JSON.
    ///
    /// Clean numbers borrow the input slice; repaired literals own their
    /// normalized text.
    Num {
        text: Cow<'a, str>,
        truncated: bool,
    },
    Bool(bool),
    Null,
    Undefined,
    /// A bare word: an unquoted key, an unquoted value, or a function name.
    Word {
        text: &'a str,
        truncated: bool,
    },
}

/// A string-opener: the source character that opened it plus how many bytes it spans.
struct Opener {
    ch: char,
    len: usize,
    from_entity: bool,
    family: QuoteFamily,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum QuoteFamily {
    /// `"` and its typographic variants.
    Double,
    /// `'` and its typographic variants.
    Single,
}

impl Opener {
    /// An opener for the quote character `ch` spanning `len` source bytes.
    fn new(ch: char, len: usize) -> Self {
        Self {
            ch,
            len,
            from_entity: false,
            family: if is_single_quote(ch) {
                QuoteFamily::Single
            } else {
                QuoteFamily::Double
            },
        }
    }

    /// Whether `c` closes a string opened by this opener.
    fn is_end(&self, c: char) -> bool {
        match (self.family, self.ch) {
            (_, '"') => c == '"',
            (_, '\'') => c == '\'',
            (QuoteFamily::Double, _) => is_double_quote(c),
            (QuoteFamily::Single, _) => is_single_quote(c),
        }
    }
}

/// The tolerant lexer. Every method takes `&self` options so callers can run
/// repair passes selectively.
#[derive(Clone, Copy)]
pub(crate) struct Lexer<'a> {
    src: &'a str,
    pos: usize,
    opts: Options,
    /// Whether an object/array/group frame is open around the current parse
    /// position. The parser keeps this in sync with its frame stack; it
    /// gates the top-level-only `isInsideUnclosedBracket` end-quote heuristic
    /// (inside a container the following `}`/`]` closes *that* container, it
    /// is not evidence that the quote was embedded).
    in_container: bool,
}

/// A token together with the byte offset where it starts.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Spanned<'a> {
    pub(crate) token: Token<'a>,
    pub(crate) start: usize,
    /// Whether the skipped trivia before this token contained a newline, which
    /// is what separates NDJSON records.
    pub(crate) newline_before: bool,
}

impl<'a> Lexer<'a> {
    pub(crate) fn new(src: &'a str, opts: Options) -> Self {
        Self {
            src,
            pos: 0,
            opts,
            in_container: false,
        }
    }

    /// Syncs the container flag with the parser's frame stack.
    pub(crate) fn set_in_container(&mut self, in_container: bool) {
        self.in_container = in_container;
    }

    /// Byte offset of the next unread character.
    pub(crate) fn pos(&self) -> usize {
        self.pos
    }

    /// Whether the whole input has been consumed.
    pub(crate) fn at_end(&self) -> bool {
        self.pos >= self.src.len()
    }

    /// Whether nothing but whitespace remains after `pos`.
    ///
    /// Lookaheads that skip trivia (notably `end_quote_is_real`) consult the
    /// next *non-ws* byte; if that byte does not exist yet, appending input
    /// can flip their decision. Stream checkpoints must treat "only trivia
    /// left" the same as EOF.
    pub(crate) fn at_effective_end(&self) -> bool {
        self.at_end() || self.rest().chars().all(is_ws)
    }

    /// Repositions the cursor (resume support). `pos` must be a position
    /// this lexer previously produced, i.e. a char boundary.
    pub(crate) fn set_pos(&mut self, pos: usize) {
        debug_assert!(self.src.is_char_boundary(pos));
        self.pos = pos;
    }

    /// The next non-trivia character, without producing a token or moving
    /// this lexer.
    ///
    /// The parser uses this to test for `+`/`(` after a value without
    /// caching a token that was lexed in value context when the next token
    /// is really an object key (`key_position` would be wrong).
    pub(crate) fn peek_significant_char(&self) -> Option<char> {
        let mut probe = *self;
        probe.skip_trivia();
        probe.peek_char()
    }

    /// The not-yet-consumed input.
    pub(crate) fn rest(&self) -> &'a str {
        &self.src[self.pos..]
    }

    /// The next character. ASCII (nearly every byte of real JSON) is read
    /// straight from the byte, skipping the slice and UTF-8 decode.
    fn peek_char(&self) -> Option<char> {
        match *self.src.as_bytes().get(self.pos)? {
            byte if byte.is_ascii() => Some(char::from(byte)),
            _ => self.rest().chars().next(),
        }
    }

    fn peek_second(&self) -> Option<char> {
        self.rest().chars().nth(1)
    }

    fn bump_char(&mut self) -> Option<char> {
        let c = self.peek_char()?;
        self.pos += c.len_utf8();
        Some(c)
    }

    /// Reads the next token, skipping trivia, comments, and fences first.
    ///
    /// `key_position` makes a bare word stop at `:` so that `{a: 1}` lexes the
    /// key and its value separately.
    pub(crate) fn next_token(&mut self, key_position: bool) -> Result<Option<Spanned<'a>>, Error> {
        let newline_before = self.skip_trivia();
        let start = self.pos;
        let Some(c) = self.peek_char() else {
            return Ok(None);
        };
        let token = if let Some(token) = punctuator(c) {
            self.pos += 1;
            token
        } else {
            self.scan_token(c, key_position)?
        };
        Ok(Some(Spanned {
            token,
            start,
            newline_before,
        }))
    }

    /// Lexes a token that is not a single-byte punctuator; `c` is the
    /// character at the cursor.
    fn scan_token(&mut self, c: char, key_position: bool) -> Result<Token<'a>, Error> {
        let start = self.pos;
        Ok(match c {
            '.' if self.rest().starts_with("...") => {
                self.pos += 3;
                self.skip_ws_only();
                if self.peek_char() == Some(',') {
                    self.pos += 1;
                }
                Token::Ellipsis
            }
            '/' => {
                if self.rest().starts_with("//") || self.rest().starts_with("/*") {
                    // Comments are only consumed by `skip_trivia` when enabled.
                    return Err(Error::new(ErrorKind::UnexpectedCharacter('/'), start));
                }
                if self.opts.repairs(Repairs::UNQUOTED) {
                    self.scan_regex()
                } else {
                    return Err(Error::new(ErrorKind::UnexpectedCharacter('/'), start));
                }
            }
            '\\' if self.opts.repairs(Repairs::UNQUOTED) => {
                // A redundant escape before a quote, as in `{\"a\": 1}`.
                self.pos += 1;
                match self.peek_char() {
                    Some(q) if is_quote(q) => {
                        self.scan_string_from(Opener::new(q, q.len_utf8()))?
                    }
                    _ => {
                        self.pos = start;
                        self.scan_word(key_position)
                    }
                }
            }
            '&' => match decode_html_entity(self.rest()) {
                Some((ch, len)) if is_quote(ch) && self.opts.repairs(Repairs::ENTITIES) => self
                    .scan_string_from(Opener {
                        from_entity: true,
                        ..Opener::new(ch, len)
                    })?,
                _ => self.scan_number_or_word(key_position)?,
            },
            c if is_quote(c) => {
                if c != '"' && !self.opts.repairs(Repairs::QUOTES) {
                    return Err(Error::new(ErrorKind::UnexpectedCharacter(c), start));
                }
                self.scan_string_from(Opener::new(c, c.len_utf8()))?
            }
            '-' | '0'..='9' | '.' => self.scan_number_or_word(key_position)?,
            _ => self.scan_word(key_position),
        })
    }
}

/// The token for a single-byte punctuator, or `None`.
fn punctuator(c: char) -> Option<Token<'static>> {
    Some(match c {
        '{' => Token::OpenBrace,
        '}' => Token::CloseBrace,
        '[' => Token::OpenBracket,
        ']' => Token::CloseBracket,
        ':' => Token::Colon,
        ',' => Token::Comma,
        ';' => Token::Semicolon,
        '+' => Token::Plus,
        '(' => Token::OpenParen,
        ')' => Token::CloseParen,
        _ => return None,
    })
}

impl<'a> Lexer<'a> {
    /// Skips whitespace, comments, and markdown fences.
    ///
    /// Returns whether a newline was skipped, which is what separates NDJSON
    /// records at the top level.
    fn skip_trivia(&mut self) -> bool {
        let mut saw_newline = false;
        loop {
            // Fast reject: classify the next byte (and, for `/`/`[`/`{`, the
            // one after it) before doing any whitespace/comment/fence work.
            // Everything else (JSON punctuators, quotes, digits, letters)
            // returns immediately — this runs once per token.
            let bytes = self.src.as_bytes();
            let next = bytes.get(self.pos + 1).copied();
            let can_start_trivia = match bytes.get(self.pos) {
                None => false,
                Some(&b) => match b {
                    b' ' | b'\n' | b'\r' | b'\t' => true,
                    b'/' => {
                        self.opts.repairs(Repairs::COMMENTS) && matches!(next, Some(b'/' | b'*'))
                    }
                    b'`' => self.opts.repairs(Repairs::FENCES),
                    // `[```` / `{```` fence openers: only when a backtick follows.
                    b'[' | b'{' => self.opts.repairs(Repairs::FENCES) && next == Some(b'`'),
                    // Special Unicode whitespace lives in multi-byte space;
                    // real non-ASCII tokens fall through to the loop, which
                    // consumes nothing and returns.
                    _ => b >= 0x80 && self.opts.repairs(Repairs::WHITESPACE),
                },
            };
            if !can_start_trivia {
                return saw_newline;
            }
            let before = self.pos;
            saw_newline |= self.skip_ws_only();
            if self.opts.repairs(Repairs::COMMENTS) {
                saw_newline |= self.skip_comment();
            }
            if self.opts.repairs(Repairs::FENCES) {
                self.skip_fence();
            }
            if self.pos == before {
                return saw_newline;
            }
        }
    }

    /// Skips one `//` comment (up to, not including, its newline) or one
    /// `/* */` comment at the cursor; returns whether a block comment spanned
    /// a newline. An unterminated block comment runs to the end of input.
    fn skip_comment(&mut self) -> bool {
        if self.src.as_bytes().get(self.pos) != Some(&b'/') {
            return false;
        }
        let rest = self.rest();
        if rest.starts_with("//") {
            self.pos += rest.find('\n').unwrap_or(rest.len());
        } else if let Some(body) = rest.strip_prefix("/*") {
            let (body, closer) = match body.find("*/") {
                Some(end) => (&body[..end], 2),
                None => (body, 0),
            };
            self.pos += 2 + body.len() + closer;
            return body.contains('\n');
        }
        false
    }

    /// Skips a markdown fence at the cursor: ```` ``` ```` plus an optional
    /// language specifier. Only the marker is consumed — the content stays
    /// for the parser, so inline fences like ```` ```{"a":1}``` ```` work as
    /// well as line fences (reference: `skipMarkdownCodeBlock`). The
    /// reference also treats `[```` / `{```` as openers and ```` ```] ```` /
    /// ```` ```} ```` as closers, so a bracket wrapped around a fence
    /// disappears with it (`[```\n{"a":1}\n```]` → `{"a": 1}`).
    fn skip_fence(&mut self) {
        let rest = &self.src.as_bytes()[self.pos..];
        let bracket = usize::from(matches!(rest, [b'[' | b'{', b'`', b'`', b'`', ..]));
        if !rest[bracket..].starts_with(b"```") {
            return;
        }
        self.pos += bracket + 3;
        while matches!(self.peek_char(), Some(c) if is_func_name_char(c)) {
            self.pos += 1;
        }
        if matches!(self.peek_char(), Some(']' | '}')) {
            self.pos += 1;
        }
    }

    /// Skips whitespace only; returns whether a newline was skipped.
    ///
    /// Byte-wise for ASCII (the overwhelmingly common case); non-ASCII is
    /// decoded only to test the special-Unicode-whitespace set.
    fn skip_ws_only(&mut self) -> bool {
        let mut saw_newline = false;
        let bytes = self.src.as_bytes();
        while self.pos < bytes.len() {
            match bytes[self.pos] {
                b'\n' => {
                    saw_newline = true;
                    self.pos += 1;
                }
                b' ' | b'\t' | b'\r' => self.pos += 1,
                b if b >= 0x80 => {
                    if !self.opts.repairs(Repairs::WHITESPACE) {
                        break;
                    }
                    let Some(c) = self.src[self.pos..].chars().next() else {
                        break;
                    };
                    if is_special_ws(c) {
                        self.pos += c.len_utf8();
                    } else {
                        break;
                    }
                }
                _ => break,
            }
        }
        saw_newline
    }

    /// Scans a string opened by `opener`, normalizing quotes and escapes.
    ///
    /// Content that arrives verbatim from the input (bulk ASCII runs, kept
    /// quotes) never allocates: the token borrows `src[start..content_end]`.
    /// The first escape/entity/decode forces an owned buffer that the rest
    /// of the content is appended to.
    fn scan_string_from(&mut self, opener: Opener) -> Result<Token<'a>, Error> {
        self.pos += opener.len;
        let start = self.pos;
        let end_byte = match opener.family {
            QuoteFamily::Double => b'"',
            QuoteFamily::Single => b'\'',
        };
        // Fast path: clean content closed by the family's ASCII quote and
        // followed directly by a byte the end-quote rules always accept
        // (`,`/`:`/newline anywhere, a closer inside a container) — exactly
        // what the general loop below concludes, minus its bookkeeping.
        let bytes = self.src.as_bytes();
        let run_end = plain_run_end(bytes, start, end_byte);
        if !opener.from_entity && bytes.get(run_end) == Some(&end_byte) {
            let accept = match bytes.get(run_end + 1) {
                Some(b',' | b':' | b'\n') => true,
                Some(b'}' | b']') => self.in_container,
                _ => false,
            };
            if accept {
                self.pos = run_end + 1;
                return Ok(Token::Str {
                    text: Cow::Borrowed(&self.src[start..run_end]),
                    truncated: false,
                });
            }
        }
        // The general loop resumes after the run already scanned above.
        self.pos = run_end;
        // End of the verbatim prefix while `owned` is still `None`.
        let mut content_end = run_end;
        let mut owned: Option<String> = None;
        let mut truncated = false;
        let mut stop_at_delimiter = false;
        let mut stop_at_index: Option<usize> = None;
        // Whether the last pushed char came raw from the input and is a
        // delimiter (only then can the EOF two-pass rule fire — an escaped
        // `\]` is legitimate string content, not a swallowed closer).
        let mut raw_delim_tail = run_end > start && is_delimiter(char::from(bytes[run_end - 1]));
        loop {
            // Fast path: advance over a run of plain ASCII content (no escape,
            // no quote, no entity `&`, no non-ASCII). While nothing has been
            // decoded the run needs no buffer at all; afterwards it is one
            // memcpy into the owned buffer. Stop-mode runs are short by
            // definition (they end at the first delimiter), so they skip the
            // SWAR strides and also stop at unquoted-string delimiters.
            {
                let bytes = self.src.as_bytes();
                let mut i = self.pos;
                if stop_at_delimiter {
                    while i < bytes.len() {
                        let b = bytes[i];
                        if b >= 0x80
                            || b == b'&'
                            || b == b'\\'
                            || b == end_byte
                            || is_unquoted_delimiter(char::from(b))
                        {
                            break;
                        }
                        i += 1;
                    }
                } else {
                    i = plain_run_end(bytes, i, end_byte);
                }
                if let Some(stop) = stop_at_index {
                    if stop <= i {
                        i = stop;
                    }
                }
                if i > self.pos {
                    if let Some(text) = owned.as_mut() {
                        text.push_str(&self.src[self.pos..i]);
                    }
                    raw_delim_tail =
                        (bytes[i - 1] as char).is_ascii() && is_delimiter(bytes[i - 1] as char);
                    self.pos = i;
                    content_end = i;
                    continue;
                }
            }
            let Some(c) = self.peek_char() else {
                // Reference two-pass rule: when the text ends on a raw
                // delimiter (`{"a":"b}`, `["hello]`), that delimiter was
                // swallowed with the missing end quote — restart and end the
                // string at the first unquoted-string delimiter instead.
                if !stop_at_delimiter && raw_delim_tail {
                    stop_at_delimiter = true;
                    self.pos = start;
                    content_end = start;
                    owned = None;
                    continue;
                }
                truncated = true;
                break;
            };
            // Close at the comma that a missing end quote should precede
            // (`["hello,"world"]` → `"hello"` then the comma stays outside).
            if stop_at_index == Some(self.pos) {
                truncated = true;
                break;
            }
            if opener.from_entity && c == '&' {
                if let Some((decoded, len)) = decode_html_entity(self.rest()) {
                    if opener.is_end(decoded) {
                        self.pos += len;
                        break;
                    }
                    // An entity that does not close the string decodes to
                    // its character as content (`&quot;b &amp; c&quot;` →
                    // `b & c`); real-quote-opened strings keep `&…;` literal.
                    let text =
                        owned.get_or_insert_with(|| String::from(&self.src[start..content_end]));
                    text.push(decoded);
                    self.pos += len;
                    raw_delim_tail = is_delimiter(decoded);
                    continue;
                }
            }
            if c == '\\' {
                // Decode into an owned buffer; materialize the verbatim
                // prefix first so nothing decoded lands in a borrowed slice.
                let text = owned.get_or_insert_with(|| String::from(&self.src[start..content_end]));
                self.bump_char();
                // The escape target is the stop comma: close before it,
                // abandoning the backslash (`"y"\, …` → `"y"` + separator).
                if stop_at_index == Some(self.pos) {
                    truncated = true;
                    break;
                }
                if self.read_escape(text)? {
                    // Truncated `\u` escape at end of input: drop it and end
                    // the string here (reference: removing the unicode char
                    // and ending the string).
                    self.pos = self.src.len();
                    truncated = true;
                    break;
                }
                raw_delim_tail = false;
                continue;
            }
            if opener.is_end(c) {
                let quote_pos = self.pos;
                self.bump_char();
                // In stop mode any quote ends the string and is consumed as
                // the repaired closer (reference behavior).
                if stop_at_delimiter {
                    truncated = true;
                    break;
                }
                let content: Cow<'_, str> = match &owned {
                    Some(text) => Cow::Borrowed(text.as_str()),
                    None => Cow::Borrowed(&self.src[start..content_end]),
                };
                if self.end_quote_is_real(0, &content)? {
                    break;
                }
                // A quote immediately followed by `\` is a hard error in the
                // reference (`"y"\`); the fold policy keeps it as content and
                // skips the comma/delimiter restarts below.
                if self.peek_char() == Some('\\') {
                    self.pos = quote_pos;
                    self.bump_char();
                    if let Some(text) = owned.as_mut() {
                        text.push('"');
                    }
                    content_end = self.pos;
                    raw_delim_tail = false;
                    continue;
                }
                // A quote that is not a valid end quote triggers the
                // reference repair rules before falling back to keeping it
                // as content:
                //   quote preceded by a raw `,`  → close at that comma;
                //   quote preceded by any other raw delimiter → restart,
                //   stopping at the first unquoted-string delimiter.
                // Escaped characters (`\,`, `\:`) are string content, not
                // structure, so they never trigger a restart.
                let prev = self.src[..quote_pos]
                    .char_indices()
                    .rev()
                    .find(|&(_, ch)| !is_ws(ch));
                match prev {
                    Some((idx, ',')) if !escaped_at(self.src, idx) => {
                        if stop_at_index != Some(idx) {
                            stop_at_index = Some(idx);
                            self.pos = start;
                            content_end = start;
                            owned = None;
                            continue;
                        }
                    }
                    Some((idx, ch))
                        if is_delimiter(ch) && !escaped_at(self.src, idx) && !stop_at_delimiter =>
                    {
                        stop_at_delimiter = true;
                        self.pos = start;
                        content_end = start;
                        owned = None;
                        continue;
                    }
                    _ => {}
                }
                // Not an end quote: keep it as an escaped quote in the value
                // (verbatim: the consumed source quote is exactly the `"`
                // that lands in the content).
                self.pos = quote_pos;
                self.bump_char();
                if let Some(text) = owned.as_mut() {
                    text.push('"');
                }
                content_end = self.pos;
                raw_delim_tail = false;
                continue;
            }
            if stop_at_delimiter && is_unquoted_delimiter(c) {
                // A URL must not break on the `//` of its scheme
                // (`"https://…` → keep scanning url chars, as in scan_word).
                let is_url = {
                    let content: &str = match &owned {
                        Some(text) => text.as_str(),
                        None => &self.src[start..content_end],
                    };
                    c == '/' && content.ends_with(':') && is_url_scheme(content)
                };
                if is_url {
                    let url_start = self.pos;
                    while matches!(self.peek_char(), Some(u) if is_url_char(u)) {
                        self.bump_char();
                    }
                    let url = &self.src[url_start..self.pos];
                    if let Some(text) = owned.as_mut() {
                        text.push_str(url);
                    }
                    content_end = self.pos;
                    if let Some(&last) = url.as_bytes().last() {
                        raw_delim_tail = last < 0x80 && is_delimiter(last as char);
                    }
                    continue;
                }
                // The delimiter stays unconsumed for the parser:
                // `["hello]` closes the string before the `]`.
                truncated = true;
                break;
            }
            // Plain content char (typically non-ASCII): verbatim advance.
            // When `owned` is already materialized (an escape was decoded
            // earlier), the byte-range flushes above start at `self.pos`, so
            // this char must be appended directly or it would be skipped
            // (`"\u0000é"` must keep the `é`). Copy only this char —
            // `content_end` can still sit before the escape sequence.
            let char_start = self.pos;
            self.bump_char();
            if let Some(text) = owned.as_mut() {
                text.push_str(&self.src[char_start..self.pos]);
            }
            content_end = self.pos;
            raw_delim_tail = is_delimiter(c);
        }
        // The reference inserts the auto-close quote before trailing
        // whitespace when a string is truncated — trim it from the value.
        let text = match owned {
            Some(mut text) => {
                if truncated {
                    let keep = text.trim_end_matches(is_ws).len();
                    text.truncate(keep);
                }
                Cow::Owned(text)
            }
            None => {
                let raw = &self.src[start..content_end];
                if truncated {
                    Cow::Borrowed(raw.trim_end_matches(is_ws))
                } else {
                    Cow::Borrowed(raw)
                }
            }
        };
        Ok(Token::Str { text, truncated })
    }

    /// Whether a candidate closing quote is followed by a delimiter or by the
    /// end of input, which is the reference implementation's end-quote test.
    ///
    /// `content` is the string value decoded so far: a closing bracket that
    /// would land inside an unclosed bracket of the same kind (`(72")`) does
    /// not end the string (reference: `isInsideUnclosedBracket`).
    fn end_quote_is_real(&mut self, depth: u8, content: &str) -> Result<bool, Error> {
        let save = self.pos;
        self.skip_line_trivia();
        let real = match self.peek_char() {
            None => true,
            // Inside a container the closer after the quote (`}`, `]`, …)
            // belongs to that container, not to the string — `{"a": "x{"}`
            // is valid JSON. The unclosed-bracket heuristic only disambiguates
            // *top-level* strings whose embedded quote is followed by a stray
            // bracket (reference: `"the set {a, b"} more"`).
            Some(c) if is_delimiter(c) => {
                self.in_container || !is_inside_unclosed_bracket(content, c)
            }
            Some(c) if is_quote(c) && depth == 0 => {
                // `"The TV is 72""`: this quote is embedded when the next one
                // is a real end quote.
                self.bump_char();
                !self.end_quote_is_real(depth + 1, content)?
            }
            Some(_) => false,
        };
        self.pos = save;
        Ok(real)
    }

    /// Skips same-line whitespace and comments for the end-quote lookahead.
    ///
    /// Newlines are never consumed — a `\n` right after the quote is itself
    /// a delimiter (reference: `parseWhitespaceAndSkipComments(false)`), so
    /// `"John"\n lastName` closes the string instead of absorbing the next
    /// line.
    fn skip_line_trivia(&mut self) {
        loop {
            let before = self.pos;
            while let Some(c) = self.peek_char() {
                let skip = match c {
                    ' ' | '\r' | '\t' => true,
                    '\n' => false,
                    c => is_special_ws(c) && self.opts.repairs(Repairs::WHITESPACE),
                };
                if !skip {
                    break;
                }
                self.pos += c.len_utf8();
            }
            if self.opts.repairs(Repairs::COMMENTS) {
                self.skip_comment();
            }
            if self.pos == before {
                break;
            }
        }
    }
}

/// End of the run of plain string content starting at `i`: the first `&`,
/// `\`, `end_byte` (the family's ASCII quote), or non-ASCII byte (typographic
/// quotes), else the end of input. Long runs advance in 8-byte SWAR strides;
/// the scalar tail decides the exact stop byte.
fn plain_run_end(bytes: &[u8], mut i: usize, end_byte: u8) -> usize {
    use crate::swar::{broadcast, load_word, non_ascii_lanes, zero_lanes};
    let (amp, backslash, end) = (broadcast(b'&'), broadcast(b'\\'), broadcast(end_byte));
    while i + 8 <= bytes.len() {
        let word = load_word(bytes, i);
        // One branch for all four stop classes (see `zero_lanes`).
        let stop = zero_lanes(word ^ amp)
            | zero_lanes(word ^ backslash)
            | zero_lanes(word ^ end)
            | non_ascii_lanes(word);
        if stop != 0 {
            break;
        }
        i += 8;
    }
    while i < bytes.len() {
        let b = bytes[i];
        if b >= 0x80 || b == b'&' || b == b'\\' || b == end_byte {
            break;
        }
        i += 1;
    }
    i
}

/// Whether `content` contains more opening than closing copies of the
/// bracket that `close` would close — i.e. the closer would be inside an
/// unclosed bracket (reference: `isInsideUnclosedBracket`).
fn is_inside_unclosed_bracket(content: &str, close: char) -> bool {
    match close {
        ')' => content.matches('(').count() > content.matches(')').count(),
        ']' => content.matches('[').count() > content.matches(']').count(),
        '}' => content.matches('{').count() > content.matches('}').count(),
        _ => false,
    }
}

/// Whether the byte at `idx` is an escape target (preceded by an odd run of
/// backslashes), i.e. `\,` is an escaped comma but `",` is not.
fn escaped_at(src: &str, idx: usize) -> bool {
    src.as_bytes()[..idx]
        .iter()
        .rev()
        .take_while(|b| **b == b'\\')
        .count()
        % 2
        == 1
}

impl Lexer<'_> {
    /// Whether every repair pass is disabled.
    fn strict(&self) -> bool {
        self.opts.repairs == Repairs::NONE
    }

    /// Decodes one escape sequence, repairing the JavaScript-only ones.
    ///
    /// Returns `true` when a truncated `\u` escape at end of input was
    /// dropped and the surrounding string must end there.
    fn read_escape(&mut self, text: &mut String) -> Result<bool, Error> {
        let Some(c) = self.bump_char() else {
            // Trailing backslash at end of input: drop it (reference: an
            // unknown escape char is removed) and let the truncation repair
            // close the string.
            return Ok(false);
        };
        if let Some(decoded) = simple_escape(c) {
            text.push(decoded);
            return Ok(false);
        }
        match c {
            'u' => return self.read_unicode_escape(text),
            'x' => {
                let hi = self.peek_char().and_then(hex_value);
                let lo = self.peek_second().and_then(hex_value);
                match (hi, lo) {
                    (Some(hi), Some(lo)) => {
                        self.bump_char();
                        self.bump_char();
                        push_code(text, hi * 16 + lo);
                    }
                    _ => {
                        if self.strict() {
                            return Err(Error::new(ErrorKind::InvalidEscape, self.pos));
                        }
                        text.push('x');
                    }
                }
            }
            other => text.push(other),
        }
        Ok(false)
    }

    /// Decodes `\uXXXX`, joining a surrogate pair when one follows.
    ///
    /// Returns `true` when fewer than four hex digits remain and the input
    /// ends right after them — the truncated escape is dropped and the
    /// string must end (reference behavior).
    fn read_unicode_escape(&mut self, text: &mut String) -> Result<bool, Error> {
        match self.read_hex4() {
            Some(code) => {
                if (0xD800..=0xDBFF).contains(&code) && self.rest().starts_with("\\u") {
                    let save = self.pos;
                    self.pos += 2;
                    match self.read_hex4() {
                        Some(low) if (0xDC00..=0xDFFF).contains(&low) => {
                            let combined = 0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00);
                            push_code(text, combined);
                            return Ok(false);
                        }
                        _ => self.pos = save,
                    }
                }
                push_code(text, code);
                Ok(false)
            }
            None => {
                if self.strict() {
                    return Err(Error::new(ErrorKind::InvalidUnicodeEscape, self.pos));
                }
                let rest = self.rest();
                let hex_run = rest.chars().take_while(|c| hex_value(*c).is_some()).count();
                if hex_run == rest.len() && hex_run < 4 {
                    // Fewer than four hex digits and then EOF: drop the
                    // escape and end the string here.
                    return Ok(true);
                }
                text.push('u');
                Ok(false)
            }
        }
    }

    /// Reads exactly four hexadecimal digits, restoring the position on failure.
    fn read_hex4(&mut self) -> Option<u32> {
        let save = self.pos;
        let mut code = 0u32;
        for _ in 0..4 {
            match self.peek_char().and_then(hex_value) {
                Some(digit) => {
                    code = code * 16 + digit;
                    self.bump_char();
                }
                None => {
                    self.pos = save;
                    return None;
                }
            }
        }
        Some(code)
    }
}

impl<'a> Lexer<'a> {
    fn at_end_of_number(&self) -> bool {
        match self.peek_char() {
            None => true,
            Some(c) => is_delimiter(c) || is_ws(c),
        }
    }

    fn scan_number_or_word(&mut self, key_position: bool) -> Result<Token<'a>, Error> {
        match self.scan_number()? {
            Some(token) => Ok(token),
            None => Ok(self.scan_word(key_position)),
        }
    }

    /// Scans a number, normalizing its text into valid JSON.
    ///
    /// Returns `None` when the text turns out not to be a number, so the caller
    /// can retry it as a bare word.
    ///
    /// A number that is already valid JSON (the common case) is recognized by
    /// a cheap grammar pre-scan and **borrows** the input slice — no `String`
    /// is built. Anything needing repair falls into the slow path, which
    /// still borrows when the raw slice happens to equal the normalized text.
    fn scan_number(&mut self) -> Result<Option<Token<'a>>, Error> {
        if let Some(end) = self.clean_number_end() {
            let text = Cow::Borrowed(&self.src[self.pos..end]);
            self.pos = end;
            return Ok(Some(Token::Num {
                text,
                truncated: false,
            }));
        }
        let start = self.pos;
        let mut num = String::new();
        let mut invalid = false;
        let mut truncated = false;
        if self.peek_char() == Some('-') {
            num.push('-');
            self.bump_char();
            if !matches!(self.peek_char(), Some(c) if c.is_ascii_digit()) && self.at_end_of_number()
            {
                num.push('0');
                truncated = self.at_end();
            }
        }
        if self.peek_char() == Some('0')
            && matches!(self.peek_second(), Some(c) if c.is_ascii_digit())
        {
            invalid = true;
        }
        self.take_digits(&mut num);
        if self.peek_char() == Some('.') {
            if num.is_empty() || num == "-" {
                num.push('0');
            }
            num.push('.');
            self.bump_char();
            if !matches!(self.peek_char(), Some(c) if c.is_ascii_digit()) {
                num.push('0');
                truncated = self.at_end();
            }
            self.take_digits(&mut num);
        }
        if self.pos == start {
            return Ok(None);
        }
        if matches!(self.peek_char(), Some('e' | 'E')) {
            if num == "-" {
                invalid = true;
            }
            if let Some(c) = self.bump_char() {
                num.push(c);
            }
            if matches!(self.peek_char(), Some('-' | '+')) {
                if let Some(c) = self.bump_char() {
                    num.push(c);
                }
            }
            if !matches!(self.peek_char(), Some(c) if c.is_ascii_digit()) {
                num.push('0');
                truncated = self.at_end();
            }
            self.take_digits(&mut num);
        }
        if !self.at_end_of_number() {
            // Not a number after all: let the caller scan it as a bare word.
            self.pos = start;
            return Ok(None);
        }
        let raw = &self.src[start..self.pos];
        if invalid {
            if self.strict() {
                return Err(Error::new(ErrorKind::UnexpectedCharacter('0'), start));
            }
            // Leading zeros cannot be respelled as a JSON number, so the
            // digits are kept verbatim as a string (borrowed, not copied).
            return Ok(Some(Token::Str {
                text: Cow::Borrowed(raw),
                truncated: false,
            }));
        }
        if raw == num {
            // The slow path recognized a number that needs no repair (e.g.
            // followed by special whitespace the fast path declined): borrow
            // the slice instead of keeping the temporary buffer.
            return Ok(Some(Token::Num {
                text: Cow::Borrowed(raw),
                truncated,
            }));
        }
        if !self.opts.repairs(Repairs::NUMBERS) {
            // The literal needed a repair that is switched off.
            return Err(Error::new(ErrorKind::UnexpectedCharacter('.'), start));
        }
        Ok(Some(Token::Num {
            text: Cow::Owned(num),
            truncated,
        }))
    }

    /// End position of a number at `self.pos` that is already valid JSON,
    /// or `None` when it needs the repair-oriented slow path.
    fn clean_number_end(&self) -> Option<usize> {
        let bytes = self.src.as_bytes();
        let mut i = self.pos;
        if i < bytes.len() && bytes[i] == b'-' {
            i += 1;
        }
        if i >= bytes.len() {
            return None;
        }
        if bytes[i] == b'0' {
            i += 1;
            // A leading zero followed by more digits is invalid JSON (the
            // slow path demotes it to a string).
            if i < bytes.len() && bytes[i].is_ascii_digit() {
                return None;
            }
        } else if bytes[i].is_ascii_digit() {
            while i < bytes.len() && bytes[i].is_ascii_digit() {
                i += 1;
            }
        } else {
            return None;
        }
        if i < bytes.len() && bytes[i] == b'.' {
            i += 1;
            let digits = i;
            while i < bytes.len() && bytes[i].is_ascii_digit() {
                i += 1;
            }
            if i == digits {
                return None;
            }
        }
        if i < bytes.len() && (bytes[i] == b'e' || bytes[i] == b'E') {
            i += 1;
            if i < bytes.len() && (bytes[i] == b'+' || bytes[i] == b'-') {
                i += 1;
            }
            let digits = i;
            while i < bytes.len() && bytes[i].is_ascii_digit() {
                i += 1;
            }
            if i == digits {
                return None;
            }
        }
        if i == self.pos {
            return None;
        }
        // Boundary check mirroring `at_end_of_number`; non-ASCII after the
        // number (e.g. special whitespace) defers to the slow path.
        match bytes.get(i) {
            None => Some(i),
            Some(&b) if b < 0x80 => {
                let c = b as char;
                if is_delimiter(c) || is_ws(c) {
                    Some(i)
                } else {
                    None
                }
            }
            Some(_) => None,
        }
    }

    fn take_digits(&mut self, out: &mut String) {
        let bytes = self.src.as_bytes();
        let begin = self.pos;
        while self.pos < bytes.len() && bytes[self.pos].is_ascii_digit() {
            self.pos += 1;
        }
        if self.pos > begin {
            out.push_str(&self.src[begin..self.pos]);
        }
    }
}

impl<'a> Lexer<'a> {
    /// Scans a bare word: an unquoted key, an unquoted value, or a function name.
    ///
    /// Spaces and tabs are word *content* (matching the reference: `hello
    /// world` is one string, and numbers still stop at whitespace); only line
    /// breaks, quotes, and structural delimiters end a word.
    fn scan_word(&mut self, key_position: bool) -> Token<'a> {
        let start = self.pos;
        // Byte-wise inner loop: JSON/prose words are ASCII, so avoid
        // per-byte UTF-8 decoding. Typographic quotes (all non-ASCII) are
        // the only non-ASCII word terminators.
        let bytes = self.src.as_bytes();
        while self.pos < bytes.len() {
            let b = bytes[self.pos];
            if b < 0x80 {
                let stop = matches!(
                    b,
                    b'\n'
                        | b'\r'
                        | b'"'
                        | b'\''
                        | b'('
                        | b')'
                        | b','
                        | b'/'
                        | b'+'
                        | b'['
                        | b']'
                        | b'{'
                        | b'}'
                ) || (key_position && b == b':');
                if stop {
                    break;
                }
                self.pos += 1;
            } else {
                let Some(c) = self.src[self.pos..].chars().next() else {
                    break;
                };
                if is_quote(c) {
                    break;
                }
                self.pos += c.len_utf8();
            }
        }
        // Trailing whitespace is trivia, not word content: `true ` stays `true`.
        {
            let span = &self.src[start..self.pos];
            let trimmed = span.trim_end_matches(is_ws);
            self.pos = start + trimmed.len();
        }
        if self.pos == start {
            // Never return an empty token: that would stall the parser.
            self.bump_char();
        }
        // A URL must survive the `//` comment scanner; the scheme may follow
        // other words and spaces (`see https://…`), so test the last segment.
        let span = &self.src[start..self.pos];
        if span.ends_with(':') && self.rest().starts_with("//") {
            let scheme = span.rsplit(is_ws).next().unwrap_or(span);
            if is_url_scheme(scheme) {
                while matches!(self.peek_char(), Some(c) if is_url_char(c)) {
                    self.bump_char();
                }
            }
        }
        let text = &self.src[start..self.pos];
        // Trailing whitespace was rewound above, so `at_end()` alone misses
        // the EOF case (`foo  ` at end): if only whitespace remains, the word
        // was lexed at logical EOF and can still grow when more input arrives
        // (`foo  ` + `bar` is one word, not a finished value + a new token).
        let truncated = self.at_end() || self.rest().chars().all(is_ws);
        if self.peek_char() == Some('"') {
            // Missing start quote: in `abc"` the trailing `"` is the end
            // quote of a string that never opened — skip it (reference:
            // parseUnquotedString's trailing-quote repair). Skipped after
            // `text` is captured so the quote is not word content.
            self.bump_char();
        }
        let keywords = self.opts.repairs(Repairs::KEYWORDS);
        match text {
            "true" => Token::Bool(true),
            "false" => Token::Bool(false),
            "null" => Token::Null,
            "True" if keywords => Token::Bool(true),
            "False" if keywords => Token::Bool(false),
            "None" if keywords => Token::Null,
            "undefined" if keywords => Token::Undefined,
            // Truncated-keyword promotion is a *value*-position repair: a
            // cut-off key like `{"t` must stay a word so `Allow::KEY` (not
            // `Allow::BOOL`) decides whether it is kept — otherwise `t:`
            // would be rewritten to the key `"true"`.
            _ if truncated && !key_position && self.opts.repairs(Repairs::TRUNCATION) => {
                if self.opts.allows(Allow::BOOL) {
                    if "true".starts_with(text) {
                        return Token::Bool(true);
                    }
                    if "false".starts_with(text) {
                        return Token::Bool(false);
                    }
                }
                if self.opts.allows(Allow::NULL) && "null".starts_with(text) {
                    return Token::Null;
                }
                Token::Word { text, truncated }
            }
            _ => Token::Word { text, truncated },
        }
    }

    /// Scans a JavaScript regular expression literal as a string value.
    fn scan_regex(&mut self) -> Token<'a> {
        let start = self.pos;
        self.bump_char();
        while let Some(c) = self.peek_char() {
            if c == '\\' {
                self.bump_char();
                self.bump_char();
                continue;
            }
            self.bump_char();
            if c == '/' {
                break;
            }
        }
        Token::Str {
            text: Cow::Borrowed(&self.src[start..self.pos]),
            truncated: self.at_end(),
        }
    }
}

fn push_code(text: &mut String, code: u32) {
    text.push(char::from_u32(code).unwrap_or('\u{FFFD}'));
}