jsonrepair 0.1.0

Fast, low-dependency JSON repair for Rust. Turns 'almost JSON' into valid JSON; supports streaming and writer-based output.
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
#![allow(clippy::collapsible_if)]

use crate::classify::{is_double_quote_like, is_single_quote_like, is_whitespace};
use crate::error::{RepairError, RepairErrorKind};
use crate::{Options, repair_to_string};
// use crate::parser::{parse_one_root_value, Logger as PLogger};
// use crate::emit::StringEmitter;
use memchr::{memchr, memchr2};
use std::io::Write;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum QuoteKind {
    Single,
    Double,
}

pub struct StreamRepairer {
    opts: Options,
    buf: String,
    seg_start: usize,
    scan_pos: usize,
    depth: i32,
    in_string: bool,
    quote_kind: QuoteKind,
    escape: bool,
    in_line_comment: bool,
    in_block_comment: bool,
    in_fence: bool,
    value_started: bool,
    last_sig_end: usize,
    // NDJSON aggregation (optional)
    agg_open: bool,
    agg_buf: String,
}

impl StreamRepairer {
    pub fn new(opts: Options) -> Self {
        Self {
            opts,
            buf: String::new(),
            seg_start: 0,
            scan_pos: 0,
            depth: 0,
            in_string: false,
            quote_kind: QuoteKind::Double,
            escape: false,
            in_line_comment: false,
            in_block_comment: false,
            in_fence: false,
            value_started: false,
            last_sig_end: 0,
            agg_open: false,
            agg_buf: String::new(),
        }
    }

    // Add a value to string aggregation buffer
    fn agg_add_val_str(&mut self, val: &str) {
        if !self.agg_open {
            self.agg_open = true;
            self.agg_buf.clear();
            self.agg_buf.push('[');
        }
        if self
            .agg_buf
            .as_bytes()
            .last()
            .map(|&b| b != b'[')
            .unwrap_or(false)
        {
            self.agg_buf.push(',');
            self.agg_buf.push(' ');
        }
        self.agg_buf.push_str(val);
    }

    // Finish string aggregation and take the aggregated output
    fn agg_finish_str(&mut self) -> Option<String> {
        if self.agg_open {
            self.agg_buf.push(']');
            let ret = std::mem::take(&mut self.agg_buf);
            self.agg_open = false;
            Some(ret)
        } else {
            None
        }
    }

    // Add a value to writer aggregation
    fn agg_add_val_writer<W: Write>(
        &mut self,
        writer: &mut W,
        val: &str,
    ) -> Result<(), RepairError> {
        if !self.agg_open {
            self.agg_open = true;
            writer.write_all(b"[").map_err(|e| {
                RepairError::new(RepairErrorKind::Parse(format!("io write error: {}", e)), 0)
            })?;
        } else {
            writer.write_all(b", ").map_err(|e| {
                RepairError::new(RepairErrorKind::Parse(format!("io write error: {}", e)), 0)
            })?;
        }
        writer.write_all(val.as_bytes()).map_err(|e| {
            RepairError::new(RepairErrorKind::Parse(format!("io write error: {}", e)), 0)
        })
    }

    // Finish writer aggregation
    fn agg_finish_writer<W: Write>(&mut self, writer: &mut W) -> Result<(), RepairError> {
        if self.agg_open {
            writer.write_all(b"]").map_err(|e| {
                RepairError::new(RepairErrorKind::Parse(format!("io write error: {}", e)), 0)
            })?;
            self.agg_open = false;
            self.agg_buf.clear();
        }
        Ok(())
    }

    /// Push a UTF-8 chunk into the streaming repairer and return any completed JSON text.
    ///
    /// Returns `Some(String)` when this call produces a complete root-level JSON value;
    /// otherwise returns `None` (no output yet).
    pub fn push(&mut self, chunk: &str) -> Result<Option<String>, RepairError> {
        self.buf.push_str(chunk);
        let mut out = String::new();
        let mut i = self.scan_pos;
        while i < self.buf.len() {
            // Root-level helper: drop JSONP prefix like ident '(' (allow spaces)
            if self.depth == 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && !self.in_fence
            {
                if let Some(slice) = self.buf.get(self.seg_start..self.buf.len()) {
                    // Use lex helper to compute JSONP prefix length relative to current segment start
                    let rel = crate::parser::lex::jsonp_prefix_len(slice);
                    if let Some(off) = rel {
                        let abs = self.seg_start + off;
                        self.scan_pos = abs;
                        self.drop_prefix(abs);
                        i = self.scan_pos;
                        continue;
                    }
                }
            }
            // additional fast path: inside a container (depth>0), not in string/comment
            // jump to next interesting ASCII char to reduce per-char overhead
            if self.depth > 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && let Some(bytes) = self.buf.as_bytes().get(i..)
            {
                let mut best: Option<usize> = None;
                // search for '}' or ']'
                if let Some(p) = memchr2(b'}', b']', bytes) {
                    best = Some(p);
                }
                // search for '"' or '\''
                if let Some(p) = memchr2(b'"', b'\'', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                // search for '/'
                if let Some(p) = memchr(b'/', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                // also consider nested container openers '{' and '['
                if let Some(p) = memchr2(b'{', b'[', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                if let Some(p) = best
                    && p > 0
                {
                    i += p;
                    continue;
                }
            }
            // fast-path: at root and before a value, drop up to next newline quickly
            if self.depth == 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && !self.value_started
                && let Some(bytes) = self.buf.as_bytes().get(i..)
                && let Some(pos) = memchr2(b'\n', b'\r', bytes)
            {
                // only drop to newline if the prefix up to newline is spaces/tabs
                let prefix = &bytes[..pos];
                if prefix.iter().all(|&b| b == b' ' || b == b'\t') {
                    let end = i + pos + 1;
                    self.scan_pos = end;
                    self.drop_prefix(end);
                    i = self.scan_pos;
                    continue;
                }
            }
            if self.in_line_comment {
                if let Some(bytes) = self.buf.as_bytes().get(i..)
                    && let Some(pos) = memchr2(b'\n', b'\r', bytes)
                {
                    i += pos + 1; // skip newline
                    self.in_line_comment = false;
                    continue;
                }
                break; // need more data
            }
            if self.in_block_comment {
                if let Some(bytes) = self.buf.as_bytes().get(i..) {
                    let mut offset = 0;
                    while let Some(pos) = memchr(b'*', &bytes[offset..]) {
                        let idx = i + offset + pos;
                        if idx + 1 < self.buf.len() && self.buf.as_bytes()[idx + 1] == b'/' {
                            // found */
                            i = idx + 2;
                            self.in_block_comment = false;
                            // at root and no value started: drop comment segment
                            if self.depth == 0 && !self.value_started {
                                self.scan_pos = i;
                                self.drop_prefix(i);
                                i = self.scan_pos;
                            }
                            break;
                        }
                        offset += pos + 1;
                    }
                    if self.in_block_comment {
                        break;
                    }
                    continue;
                } else {
                    break;
                }
            }

            let (ch, len) = next_char(&self.buf, i);
            if len == 0 {
                break;
            }

            if self.in_string {
                if self.escape {
                    self.escape = false;
                    i += len;
                    continue;
                }
                if ch == '\\' {
                    self.escape = true;
                    i += len;
                    continue;
                }
                let end = match self.quote_kind {
                    QuoteKind::Double => is_double_quote_like(ch),
                    QuoteKind::Single => is_single_quote_like(ch),
                };
                if end {
                    self.in_string = false;
                    self.last_sig_end = i + len;
                }
                i += len;
                continue;
            }

            // not in string/comment
            if ch == '/' {
                // lookahead
                let (ch2, l2) = next_char(&self.buf, i + len);
                if l2 > 0 && ch2 == '/' {
                    self.in_line_comment = true;
                    i += len + l2;
                    continue;
                }
                if l2 > 0 && ch2 == '*' {
                    self.in_block_comment = true;
                    i += len + l2;
                    continue;
                }
            } else if ch == '#' {
                // treat # as line comment when allowed
                self.in_line_comment = true;
                i += len;
                continue;
            } else if ch == '`' && self.depth == 0 {
                // fenced code block markers ``` at root-level: drop the markers themselves
                let (c2, l2) = next_char(&self.buf, i + len);
                let (c3, l3) = next_char(&self.buf, i + len + l2);
                if l2 == 0 || l3 == 0 {
                    break;
                }
                if c2 == '`' && c3 == '`' {
                    let mut j = i + len + l2 + l3; // after ```
                    if !self.in_fence {
                        // opening fence: optional language + ws + optional newline
                        let rel = crate::parser::lex::fence_open_lang_newline_len(&self.buf[j..]);
                        j += rel;
                        self.scan_pos = j;
                        self.drop_prefix(self.scan_pos);
                        i = self.scan_pos;
                        self.in_fence = true;
                        continue;
                    } else {
                        // closing fence after a value: emit previous JSON up to last_sig_end, then drop the fence
                        let end = if self.last_sig_end > self.seg_start {
                            self.last_sig_end
                        } else {
                            i
                        };
                        self.scan_pos = i;
                        let emitted = self.emit_segment(end)?;
                        out.push_str(&emitted);
                        i = self.scan_pos;
                        // drop the fence markers themselves
                        let abs_end = i + len + l2 + l3;
                        self.drop_prefix(abs_end);
                        i = self.scan_pos;
                        self.value_started = false;
                        self.in_fence = false;
                        continue;
                    }
                }
            }

            if is_whitespace(ch) {
                // at root and no value: we can drop accumulated whitespace to keep buffer small
                if self.depth == 0 && !self.value_started {
                    self.scan_pos = i + len;
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                } else {
                    i += len;
                }
                continue;
            }

            // at root inside an opened fence, before value: drop residual language/whitespace/newline across chunks
            if self.depth == 0 && self.in_fence && !self.value_started {
                let rel = crate::parser::lex::fence_open_lang_newline_len(&self.buf[i..]);
                if rel > 0 {
                    self.scan_pos = i + rel;
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                    continue;
                }
            }

            // handle JSONP function wrapper at root before value: drop `name(` prefix using lex helper
            if self.depth == 0 && !self.value_started {
                if let Some(rel) = crate::parser::lex::jsonp_prefix_len(&self.buf[i..]) {
                    self.scan_pos = i + rel; // drop prefix up to '('
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                    continue;
                }
            }

            // ignore JSONP trailing artifacts at root when no value collected
            if self.depth == 0 && !self.value_started && (ch == ')' || ch == ';') {
                self.scan_pos = i + len;
                self.drop_prefix(self.scan_pos);
                i = self.scan_pos;
                continue;
            }
            if !self.value_started {
                self.value_started = true;
            }

            if is_double_quote_like(ch) {
                self.in_string = true;
                self.quote_kind = QuoteKind::Double;
                i += len;
                continue;
            }
            if is_single_quote_like(ch) {
                self.in_string = true;
                self.quote_kind = QuoteKind::Single;
                i += len;
                continue;
            }

            match ch {
                '{' | '[' => {
                    self.depth += 1;
                    self.last_sig_end = i + len;
                    i += len;
                }
                '}' | ']' => {
                    self.depth -= 1;
                    self.last_sig_end = i + len;
                    i += len;
                    if self.depth == 0 {
                        let emitted = self.emit_segment(i)?;
                        if self.opts.stream_ndjson_aggregate {
                            self.agg_add_val_str(&emitted);
                        } else {
                            out.push_str(&emitted);
                        }
                        // reset scanner relative to new buffer after drain
                        i = self.scan_pos;
                        self.value_started = false;
                    }
                }
                '\n' => {
                    if self.depth == 0 && self.value_started {
                        let end = if self.last_sig_end > self.seg_start {
                            self.last_sig_end
                        } else {
                            i
                        };
                        // If the root-level segment is only an identifier (likely JSONP prefix), do not emit yet
                        let seg = &self.buf[self.seg_start..end].trim();
                        let is_ident = seg
                            .chars()
                            .next()
                            .map(|c| c.is_ascii_alphabetic() || c == '_' || c == '$')
                            .unwrap_or(false)
                            && seg
                                .chars()
                                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$');
                        if is_ident {
                            // drop the newline and keep buffer to await '('
                            self.scan_pos = i + len;
                            self.drop_prefix(self.seg_start); // preserve name at start; we only drop processed prefix before seg_start
                            i = self.scan_pos;
                            self.value_started = false;
                            continue;
                        }
                        self.scan_pos = i;
                        let emitted = self.emit_segment(end)?;
                        if self.opts.stream_ndjson_aggregate {
                            self.agg_add_val_str(&emitted);
                        } else {
                            out.push_str(&emitted);
                        }
                        i = self.scan_pos;
                        self.value_started = false;
                    } else if self.depth == 0 && !self.value_started {
                        // pure blank/comment line: drop it
                        self.scan_pos = i + len;
                        self.drop_prefix(self.scan_pos);
                        i = self.scan_pos;
                    }
                    i += len;
                }
                _ => {
                    self.last_sig_end = i + len;
                    i += len;
                }
            }
        }
        self.scan_pos = i;
        if out.is_empty() {
            Ok(None)
        } else {
            Ok(Some(out))
        }
    }

    /// Stream input chunk and write any completed repaired JSON values directly into `writer`.
    ///
    /// When `Options.stream_ndjson_aggregate` is true, this will not emit values immediately;
    /// instead, it writes an opening `[` on first value and defers closing `]` to `flush_to_writer`.
    pub fn push_to_writer<W: Write>(
        &mut self,
        chunk: &str,
        writer: &mut W,
    ) -> Result<(), RepairError> {
        self.buf.push_str(chunk);
        let mut i = self.scan_pos;
        while i < self.buf.len() {
            // Grammar fast-path at root for writer: drop JSONP prefix via lex helper
            if self.depth == 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && !self.in_fence
            {
                if let Some(slice) = self.buf.get(self.seg_start..self.buf.len()) {
                    if let Some(rel) = crate::parser::lex::jsonp_prefix_len(slice) {
                        let abs = self.seg_start + rel;
                        self.scan_pos = abs;
                        self.drop_prefix(abs);
                        i = self.scan_pos;
                        continue;
                    }
                }
            }
            if self.depth > 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && let Some(bytes) = self.buf.as_bytes().get(i..)
            {
                let mut best: Option<usize> = None;
                if let Some(p) = memchr2(b'}', b']', bytes) {
                    best = Some(p);
                }
                if let Some(p) = memchr2(b'"', b'\'', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                if let Some(p) = memchr(b'/', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                if let Some(p) = memchr2(b'{', b'[', bytes) {
                    best = Some(best.map_or(p, |b| b.min(p)));
                }
                if let Some(p) = best
                    && p > 0
                {
                    i += p;
                    continue;
                }
            }
            if self.depth == 0
                && !self.in_string
                && !self.in_line_comment
                && !self.in_block_comment
                && !self.value_started
                && let Some(bytes) = self.buf.as_bytes().get(i..)
                && let Some(pos) = memchr2(b'\n', b'\r', bytes)
            {
                let prefix = &bytes[..pos];
                if prefix.iter().all(|&b| b == b' ' || b == b'\t') {
                    let end = i + pos + 1;
                    self.scan_pos = end;
                    self.drop_prefix(end);
                    i = self.scan_pos;
                    continue;
                }
            }
            if self.in_line_comment {
                if let Some(bytes) = self.buf.as_bytes().get(i..)
                    && let Some(pos) = memchr2(b'\n', b'\r', bytes)
                {
                    i += pos + 1;
                    self.in_line_comment = false;
                    continue;
                }
                break;
            }
            if self.in_block_comment {
                if let Some(bytes) = self.buf.as_bytes().get(i..) {
                    let mut offset = 0;
                    while let Some(pos) = memchr(b'*', &bytes[offset..]) {
                        let idx = i + offset + pos;
                        if idx + 1 < self.buf.len() && self.buf.as_bytes()[idx + 1] == b'/' {
                            i = idx + 2;
                            self.in_block_comment = false;
                            if self.depth == 0 && !self.value_started {
                                self.scan_pos = i;
                                self.drop_prefix(i);
                                i = self.scan_pos;
                            }
                            break;
                        }
                        offset += pos + 1;
                    }
                    if self.in_block_comment {
                        break;
                    }
                    continue;
                } else {
                    break;
                }
            }

            let (ch, len) = next_char(&self.buf, i);
            if len == 0 {
                break;
            }

            if self.in_string {
                if self.escape {
                    self.escape = false;
                    i += len;
                    continue;
                }
                if ch == '\\' {
                    self.escape = true;
                    i += len;
                    continue;
                }
                let end = match self.quote_kind {
                    QuoteKind::Double => is_double_quote_like(ch),
                    QuoteKind::Single => is_single_quote_like(ch),
                };
                if end {
                    self.in_string = false;
                    self.last_sig_end = i + len;
                }
                i += len;
                continue;
            }

            if ch == '/' {
                let (ch2, l2) = next_char(&self.buf, i + len);
                if l2 > 0 && ch2 == '/' {
                    self.in_line_comment = true;
                    i += len + l2;
                    continue;
                }
                if l2 > 0 && ch2 == '*' {
                    self.in_block_comment = true;
                    i += len + l2;
                    continue;
                }
            } else if ch == '#' {
                self.in_line_comment = true;
                i += len;
                continue;
            } else if ch == '`' && self.depth == 0 {
                let (c2, l2) = next_char(&self.buf, i + len);
                let (c3, l3) = next_char(&self.buf, i + len + l2);
                if l2 == 0 || l3 == 0 {
                    break;
                }
                if c2 == '`' && c3 == '`' {
                    let mut j = i + len + l2 + l3; // after ```
                    if !self.in_fence {
                        let rel = crate::parser::lex::fence_open_lang_newline_len(&self.buf[j..]);
                        j += rel;
                        self.scan_pos = j;
                        self.drop_prefix(self.scan_pos);
                        i = self.scan_pos;
                        self.in_fence = true;
                        continue;
                    } else {
                        let end = if self.last_sig_end > self.seg_start {
                            self.last_sig_end
                        } else {
                            i
                        };
                        self.scan_pos = i;
                        let emitted = self.emit_segment(end)?;
                        if self.opts.stream_ndjson_aggregate {
                            if !self.agg_open {
                                self.agg_open = true;
                                writer.write_all(b"[").map_err(|e| {
                                    RepairError::new(
                                        RepairErrorKind::Parse(format!("io write error: {}", e)),
                                        i,
                                    )
                                })?;
                            }
                            if self.agg_open && writer.flush().is_err() {}
                            // add comma if necessary
                            // We can't peek previous char, so rely on caller to not mix aggregate fencing with multi-values often
                            // We will use agg_buf only for state in non-writer path; here emit comma via a small heuristic
                            // If we've already written at least one element, we write a comma.
                        }
                        if self.opts.stream_ndjson_aggregate {
                            self.agg_add_val_writer(writer, &emitted)?;
                        } else {
                            writer.write_all(emitted.as_bytes()).map_err(|e| {
                                RepairError::new(
                                    RepairErrorKind::Parse(format!("io write error: {}", e)),
                                    i,
                                )
                            })?;
                        }
                        i = self.scan_pos;
                        let abs_end = i + len + l2 + l3;
                        self.drop_prefix(abs_end);
                        i = self.scan_pos;
                        self.value_started = false;
                        self.in_fence = false;
                        continue;
                    }
                }
            }

            if is_whitespace(ch) {
                if self.depth == 0 && !self.value_started {
                    self.scan_pos = i + len;
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                } else {
                    i += len;
                }
                continue;
            }

            if self.depth == 0 && self.in_fence && !self.value_started {
                let rel = crate::parser::lex::fence_open_lang_newline_len(&self.buf[i..]);
                if rel > 0 {
                    self.scan_pos = i + rel;
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                    continue;
                }
            }

            if self.depth == 0
                && !self.value_started
                && (ch.is_ascii_alphabetic() || ch == '_' || ch == '$')
            {
                let mut j = i + len;
                loop {
                    let (cx, lx) = next_char(&self.buf, j);
                    if lx > 0 && (cx.is_ascii_alphanumeric() || cx == '_' || cx == '$') {
                        j += lx;
                    } else {
                        break;
                    }
                }
                loop {
                    let (cx, lx) = next_char(&self.buf, j);
                    if lx > 0 && is_whitespace(cx) {
                        j += lx;
                    } else {
                        break;
                    }
                }
                let (c2, l2) = next_char(&self.buf, j);
                if l2 > 0 && c2 == '(' {
                    self.scan_pos = j + l2;
                    self.drop_prefix(self.scan_pos);
                    i = self.scan_pos;
                    continue;
                }
            }

            if self.depth == 0 && !self.value_started && (ch == ')' || ch == ';') {
                self.scan_pos = i + len;
                self.drop_prefix(self.scan_pos);
                i = self.scan_pos;
                continue;
            }
            if !self.value_started {
                self.value_started = true;
            }

            if is_double_quote_like(ch) {
                self.in_string = true;
                self.quote_kind = QuoteKind::Double;
                i += len;
                continue;
            }
            if is_single_quote_like(ch) {
                self.in_string = true;
                self.quote_kind = QuoteKind::Single;
                i += len;
                continue;
            }

            match ch {
                '{' | '[' => {
                    self.depth += 1;
                    self.last_sig_end = i + len;
                    i += len;
                }
                '}' | ']' => {
                    self.depth -= 1;
                    self.last_sig_end = i + len;
                    i += len;
                    if self.depth == 0 {
                        let emitted = self.emit_segment(i)?;
                        if self.opts.stream_ndjson_aggregate {
                            self.agg_add_val_writer(writer, &emitted)?;
                        } else {
                            writer.write_all(emitted.as_bytes()).map_err(|e| {
                                RepairError::new(
                                    RepairErrorKind::Parse(format!("io write error: {}", e)),
                                    i,
                                )
                            })?;
                        }
                        i = self.scan_pos;
                        self.value_started = false;
                    }
                }
                '\n' => {
                    if self.depth == 0 && self.value_started {
                        let end = if self.last_sig_end > self.seg_start {
                            self.last_sig_end
                        } else {
                            i
                        };
                        self.scan_pos = i;
                        let emitted = self.emit_segment(end)?;
                        if self.opts.stream_ndjson_aggregate {
                            self.agg_add_val_writer(writer, &emitted)?;
                        } else {
                            writer.write_all(emitted.as_bytes()).map_err(|e| {
                                RepairError::new(
                                    RepairErrorKind::Parse(format!("io write error: {}", e)),
                                    i,
                                )
                            })?;
                        }
                        i = self.scan_pos;
                        self.value_started = false;
                    } else if self.depth == 0 && !self.value_started {
                        self.scan_pos = i + len;
                        self.drop_prefix(self.scan_pos);
                        i = self.scan_pos;
                    }
                    i += len;
                }
                _ => {
                    self.last_sig_end = i + len;
                    i += len;
                }
            }
        }
        self.scan_pos = i;
        Ok(())
    }

    /// Flush and write any remaining data into `writer`. If NDJSON aggregation is enabled,
    /// this closes the array.
    pub fn flush_to_writer<W: Write>(&mut self, writer: &mut W) -> Result<(), RepairError> {
        if self.seg_start < self.buf.len() {
            if self.depth == 0
                && !self.value_started
                && !self.in_string
                && !self.in_block_comment
                && !self.in_line_comment
                && self.last_sig_end <= self.seg_start
            {
                self.buf.clear();
                self.seg_start = 0;
                self.scan_pos = 0;
                if self.opts.stream_ndjson_aggregate {
                    self.agg_finish_writer(writer)?;
                }
                return Ok(());
            }
            let s = self.buf[self.seg_start..].to_string();
            let fixed = repair_to_string(&s, &self.opts)?;
            if self.opts.stream_ndjson_aggregate {
                self.agg_add_val_writer(writer, &fixed)?;
            } else {
                writer.write_all(fixed.as_bytes()).map_err(|e| {
                    RepairError::new(RepairErrorKind::Parse(format!("io write error: {}", e)), 0)
                })?;
            }
        }
        self.buf.clear();
        self.seg_start = 0;
        self.scan_pos = 0;
        self.depth = 0;
        self.in_string = false;
        self.escape = false;
        self.in_line_comment = false;
        self.in_block_comment = false;
        self.value_started = false;
        self.last_sig_end = 0;
        if self.opts.stream_ndjson_aggregate {
            self.agg_finish_writer(writer)?;
        }
        Ok(())
    }

    /// Flush any remaining buffered content.
    ///
    /// Returns `Some(String)` when there is final output to emit; otherwise `None`.
    pub fn flush(&mut self) -> Result<Option<String>, RepairError> {
        let mut out = String::new();
        if self.seg_start < self.buf.len() {
            // If nothing meaningful collected at root, skip repairing
            if self.depth == 0
                && !self.value_started
                && !self.in_string
                && !self.in_block_comment
                && !self.in_line_comment
                && self.last_sig_end <= self.seg_start
            {
                self.buf.clear();
                self.seg_start = 0;
                self.scan_pos = 0;
                // if aggregating and already opened, close and return aggregated array
                if self.opts.stream_ndjson_aggregate {
                    return Ok(self.agg_finish_str());
                } else {
                    return Ok(if out.is_empty() { None } else { Some(out) });
                }
            }
            let s = self.buf[self.seg_start..].to_string();
            let fixed = repair_to_string(&s, &self.opts)?;
            if self.opts.stream_ndjson_aggregate {
                self.agg_add_val_str(&fixed);
            } else {
                out.push_str(&fixed);
            }
        }
        // reset
        self.buf.clear();
        self.seg_start = 0;
        self.scan_pos = 0;
        self.depth = 0;
        self.in_string = false;
        self.escape = false;
        self.in_line_comment = false;
        self.in_block_comment = false;
        self.value_started = false;
        self.last_sig_end = 0;
        if self.opts.stream_ndjson_aggregate {
            Ok(self.agg_finish_str())
        } else {
            Ok(if out.is_empty() { None } else { Some(out) })
        }
    }

    fn emit_segment(&mut self, end: usize) -> Result<String, RepairError> {
        if end <= self.seg_start {
            self.seg_start = end;
            return Ok(String::new());
        }
        let segment = self.buf[self.seg_start..end].to_string();
        let fixed = repair_to_string(&segment, &self.opts)?;
        // drop processed part from buffer to keep memory bounded
        self.buf.drain(..end);
        // adjust indices
        if self.scan_pos >= end {
            self.scan_pos -= end;
        } else {
            self.scan_pos = 0;
        }
        if self.last_sig_end >= end {
            self.last_sig_end -= end;
        } else {
            self.last_sig_end = 0;
        }
        self.seg_start = 0;
        Ok(fixed)
    }

    fn drop_prefix(&mut self, end: usize) {
        if end <= self.seg_start {
            self.seg_start = end;
            return;
        }
        self.buf.drain(..end);
        if self.scan_pos >= end {
            self.scan_pos -= end;
        } else {
            self.scan_pos = 0;
        }
        if self.last_sig_end >= end {
            self.last_sig_end -= end;
        } else {
            self.last_sig_end = 0;
        }
        self.seg_start = 0;
    }
}

#[inline]
fn next_char(s: &str, i: usize) -> (char, usize) {
    if i >= s.len() {
        return ('\0', 0);
    }
    match s[i..].chars().next() {
        Some(c) => (c, c.len_utf8()),
        None => ('\0', 0),
    }
}