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
use std::iter::Peekable;
use crate::jsonish::{value::Fixes, Value};
use anyhow::Result;
use super::json_collection::JsonCollection;
pub struct JsonParseState {
pub collection_stack: Vec<(JsonCollection, Vec<Fixes>)>,
// Technically we may find multiple values in a single string
pub completed_values: Vec<(&'static str, Value, Vec<Fixes>)>,
}
impl JsonParseState {
pub fn new() -> Self {
JsonParseState {
collection_stack: vec![],
completed_values: vec![],
}
}
pub fn complete_collection(&mut self) {
let (collection, fixes) = match self.collection_stack.pop() {
Some(collection) => collection,
None => return,
};
let name = collection.name();
let value: Value = match collection.into() {
Some(value) => value,
None => return,
};
if let Some((last, _fixes)) = self.collection_stack.last_mut() {
match last {
JsonCollection::Object(keys, values) => {
if keys.len() == values.len() {
match value {
Value::String(s) => keys.push(s),
Value::AnyOf(_, s) => keys.push(s),
_ => keys.push(value.to_string()),
}
} else {
values.push(value);
}
}
JsonCollection::Array(values) => {
values.push(value);
}
_ => {
// TODO: this should never happen as we should only be pushing objects and arrays
panic!(
"Unexpected value: {:?} in collection stack: {:?}",
value, last
);
}
}
} else {
self.completed_values.push((name, value, fixes));
}
}
fn consume(&mut self, token: char) -> Result<usize> {
let Some((last, _)) = self.collection_stack.last_mut() else {
return Err(anyhow::anyhow!(
"No collection to consume token: {:?}",
token
));
};
match last {
JsonCollection::QuotedString(s)
| JsonCollection::TripleQuotedString(s)
| JsonCollection::BlockComment(s)
| JsonCollection::SingleQuotedString(s)
| JsonCollection::BacktickString(s)
| JsonCollection::TripleBacktickString { content: s, .. }
| JsonCollection::UnquotedString(s)
| JsonCollection::TrailingComment(s) => {
// println!("Consuming: {s} + {:?}", token);
s.push(token);
}
JsonCollection::Object(_, _) | JsonCollection::Array(_) => {
panic!("Unexpected token: {:?} in: {:?}", token, last);
}
}
Ok(0)
}
fn is_string_complete(&self) -> bool {
let Some((JsonCollection::UnquotedString(v), _)) = self.collection_stack.last() else {
return false;
};
// Check if the token is a valid json character
match v.as_str() {
"true" | "false" | "null" => true,
_ => {
// Check if the token parses as a number
if v.parse::<f64>().is_ok() {
return true;
}
false
}
}
}
fn should_close_unescaped_string(
&mut self,
mut next: Peekable<impl Iterator<Item = (usize, char)>>,
) -> Option<usize> {
let pos = if self.collection_stack.len() >= 2 {
self.collection_stack
.get(self.collection_stack.len() - 2)
.map(|(c, _)| match c {
JsonCollection::Object(keys, values) => {
if keys.len() == values.len() {
2
} else {
3
}
}
JsonCollection::Array(_) => 4,
_ => 1,
})
.unwrap()
} else {
0
};
match pos {
0 => {
// in nothing, so perhaps the first '{' or '[' is the start of a new object or array
let mut counter = 0;
for (idx, c) in next.by_ref() {
counter = idx;
match c {
// If at some point we find a valid json character, we'll close the string
'{' | '[' => return Some(idx),
x => {
let _ = self.consume(x);
}
}
}
Some(counter)
}
1 => None,
2 => {
// in object key
let mut counter = 0;
for (idx, c) in next.by_ref() {
counter = idx;
match c {
':' => return Some(idx),
x => {
let _ = self.consume(x);
}
}
}
Some(counter)
}
3 => {
// in object value
let mut counter = 0;
while let Some((idx, c)) = next.next() {
counter = idx;
match c {
',' => {
// Check if we have just numeric values in the string so far.
let Some((JsonCollection::UnquotedString(current_value), _)) =
self.collection_stack.last()
else {
return Some(idx);
};
// current value could be a numeric looking things.
let is_numeric = current_value.trim().parse::<f64>().is_ok();
let is_bool = current_value.trim().eq_ignore_ascii_case("true")
|| current_value.trim().eq_ignore_ascii_case("false");
let is_null = current_value.trim().eq_ignore_ascii_case("null");
let is_possible_value = is_numeric || is_bool || is_null;
if let Some((_, next_c)) = next.peek() {
match next_c {
'\n' => {
log::debug!("Closing due to: newline after comma");
return Some(idx);
}
' ' => {
log::debug!("Testing for comment after space + comma");
if is_possible_value {
return Some(idx);
}
// If after the space we have "//" or "/*" or the beginning of a key, we'll close the string
let mut buffer = ",".to_string();
let mut anything_but_whitespace = false;
while let Some((_, next_next_c)) = next.next() {
anything_but_whitespace = anything_but_whitespace
|| !next_next_c.is_whitespace();
buffer.push(next_next_c);
match next_next_c {
' ' => {}
'\n' => {
if anything_but_whitespace {
} else {
// Likely end of the key as the LLM generated a ", " token by mistake instead of a ","
// so drop the comma
log::debug!("Closing due to: newline after comma + space");
return Some(idx);
}
}
'/' => match next.peek() {
Some((_, '/')) => {
// This is likely a comment
return Some(idx);
}
Some((_, '*')) => {
// This is likely a comment
return Some(idx);
}
_ => {
// let _ = self.consume(c);
}
},
'"' => {
// This is likely a new key
log::debug!("Closing due to: new key after space + comma");
return Some(idx);
}
_x => {
break;
}
}
}
for c in buffer.chars() {
let _ = self.consume(c);
}
}
_ => {
let _ = self.consume(c);
}
}
} else {
// Don't include the comma
return Some(idx);
}
}
'}' => return Some(idx),
x => {
let _ = self.consume(x);
}
}
}
Some(counter)
}
4 => {
// in array
let mut counter = 0;
for (idx, c) in next {
counter = idx;
match c {
',' => return Some(idx),
']' => return Some(idx),
x => {
let _ = self.consume(x);
}
}
}
counter += 1; // Indicate that we called next() one time after the final `Some`.
Some(counter)
}
_ => unreachable!("Invalid position"),
}
}
fn should_close_string(
&mut self,
mut next: Peekable<impl Iterator<Item = (usize, char)>>,
closing_char: char,
) -> bool {
let (has_some_object, in_object_key, in_object_value, in_array) =
if self.collection_stack.len() >= 2 {
self.collection_stack
.get(self.collection_stack.len() - 2)
.map(|(c, _)| match c {
JsonCollection::Object(keys, values) => {
if keys.len() == values.len() {
(true, false, false)
} else {
(false, true, true)
}
}
JsonCollection::Array(_) => (false, false, true),
_ => (false, false, false),
})
.map(|(a, b, c)| (true, a, b, c))
.unwrap()
} else {
(false, false, false, false)
};
let closing_char_count = if closing_char == '"' {
// count the number of quotes in the string
let (last, _) = self.collection_stack.last().unwrap();
match last {
JsonCollection::QuotedString(s, ..) => {
let mut count = 0;
// Iterate with indices so we can look backwards
for (i, c) in s.char_indices() {
if c == '"' {
// Count consecutive backslashes immediately preceding this quote
let mut backslash_count = 0;
let mut j = i;
while j > 0 {
j -= 1;
if s.as_bytes()[j] == b'\\' {
backslash_count += 1;
} else {
break;
}
}
// Only count this quote if the number of backslashes is even
if backslash_count % 2 == 0 {
count += 1;
}
}
}
count
}
_ => 0,
}
} else {
0
};
if let Some((idx, next_char)) = next.peek() {
let _idx = *idx;
match next_char {
':' | '}' if in_object_key => {
// We're ready to close the key
log::debug!("Closing due to: key");
true
}
',' if in_object_value || in_array => {
if closing_char_count % 2 == 0 {
// We're ready to close the value
log::debug!("Closing due to: value",);
true
} else {
// We're not ready to close the value
false
}
}
'}' if in_object_value => {
// We're ready to close the value
log::debug!("Closing due to: value",);
true
}
']' if in_array => {
// We're ready to close the value
log::debug!("Closing due to: array");
true
}
' ' | '\t' | '\n' => {
// look ahead and see if we can find a closing bracket or comma
while let Some((_, c)) = next.next() {
match c {
' ' | '\t' | '\n' => {}
'}' if in_object_key || in_object_value => return true,
':' if in_object_key => return true,
',' if in_object_value => return true,
',' | ']' if in_array => return true,
'/' => {
// Could be a comment
match next.peek() {
Some((_, '/')) => {
// We're ready to close the comment
return true;
}
Some((_, '*')) => {
// We're ready to close the comment
return true;
}
_ => return false,
}
}
_ => return false,
}
}
// If we faile, terminate the string
true
}
x if closing_char == *x => {
// We'll close the string the next time around.
false
}
'{' | '"' | '\'' | '[' => {
if !has_some_object {
// We're in a string
true
} else {
false
}
}
_ => {
// Almost every other character should not close the string
false
}
}
} else {
true
}
}
pub fn process_token(
&mut self,
token: char,
mut next: Peekable<impl Iterator<Item = (usize, char)>>,
) -> Result<usize> {
// println!("Processing: {:?}..{:?}", token, next.peek());
match self.collection_stack.last() {
Some((last, _)) => match last {
JsonCollection::Object(_, _) => {
match token {
'}' => {
// We're ready to close the object
self.complete_collection();
Ok(0)
}
// We can safely ignore these tokens
',' | ':' => Ok(0),
// look for a new key or value
_ => self.find_any_starting_value(token, next),
}
}
JsonCollection::Array(_) => {
// We could be expecting:
// - A value
// - a comma
// - a closing bracket
match token {
']' => {
// We're ready to close the array
self.complete_collection();
Ok(0)
}
// Skip these tokens
',' => Ok(0),
_ => self.find_any_starting_value(token, next),
}
}
JsonCollection::TripleQuotedString(_) => {
// We should be expecting:
if token == '"' {
// TODO: this logic is busted. peekable.peek() does not
// advance the iterator (this is easily verified with
// a unit test), but to fix this we need to do a bit of
// refactoring, so for now we'll live with it.
let is_triple_quoted = match next.peek() {
Some((_, '"')) => matches!(next.peek(), Some((_, '"')) | None),
None => true,
_ => false,
};
if is_triple_quoted {
self.complete_collection();
Ok(3)
} else {
self.consume(token)
}
} else {
self.consume(token)
}
}
JsonCollection::QuotedString(_) => {
// We could be expecting:
// - A closing quote
// - A character
match token {
'"' => {
// It's possible that the LLM messed up the escaping
// We'll try to fix it.
if self.should_close_string(next, '"') {
self.complete_collection();
Ok(0)
} else {
self.consume(token)
}
}
'\\' => {
// Capture escaped characters
match next.peek() {
Some((_, 'n')) => {
self.consume('\n')?;
Ok(1)
}
Some((_, 't')) => {
self.consume('\t')?;
Ok(1)
}
Some((_, 'r')) => {
self.consume('\r')?;
Ok(1)
}
Some((_, 'b')) => {
self.consume('\x08')?;
Ok(1)
}
Some((_, 'f')) => {
self.consume('\x0C')?;
Ok(1)
}
Some((_, '\\')) => {
self.consume('\\')?;
Ok(1)
}
Some((_, '"')) => {
self.consume('"')?;
Ok(1)
}
Some((_, 'u')) => {
// We'll consume the 'u' and the next 4 characters
let mut buffer = String::new();
buffer.push(token);
for _ in 0..4 {
if let Some((_, c)) = next.next() {
buffer.push(c);
} else {
break;
}
}
for c in buffer.chars() {
let _ = self.consume(c);
}
Ok(5)
}
_ => self.consume(token),
}
}
_ => self.consume(token),
}
}
JsonCollection::TripleBacktickString { .. } => {
// We could be expecting:
// - A closing backtick
// - A character
if token == '`' {
// TODO: this logic is busted. peekable.peek() does not
// advance the iterator (this is easily verified with
// a unit test), but to fix this we need to do a bit of
// refactoring, so for now we'll live with it.
let is_triple_quoted = match next.peek() {
Some((_, '`')) => matches!(next.peek(), Some((_, '`')) | None),
None => true,
_ => false,
};
if is_triple_quoted {
self.complete_collection();
Ok(3)
} else {
self.consume(token)
}
} else {
self.consume(token)
}
}
JsonCollection::BacktickString(_) => {
// We could be expecting:
// - A closing backtick
// - A character
match token {
'`' => {
if self.should_close_string(next, '`') {
self.complete_collection();
Ok(0)
} else {
self.consume(token)
}
}
_ => self.consume(token),
}
}
JsonCollection::SingleQuotedString(_) => {
// We could be expecting:
// - A closing quote
// - A character
// - A space
match token {
'\'' => {
// It's possible that the LLM messed up the escaping
// We'll try to fix it.
if self.should_close_string(next, '\'') {
self.complete_collection();
Ok(0)
} else {
self.consume(token)
}
}
_ => self.consume(token),
}
}
JsonCollection::UnquotedString(_) => {
// We could be expecting:
// - A terminating json character (comma, colon, bracket, space, newline)
// - A character
let res = self.consume(token);
if let Some(count) = self.should_close_unescaped_string(next) {
self.complete_collection();
Ok(count)
} else {
res
}
}
JsonCollection::TrailingComment(_) => {
// We could be expecting:
// - A newline
// - A character
match token {
'\n' => {
// We're ready to close the comment
self.complete_collection();
Ok(0)
}
_ => self.consume(token),
}
}
JsonCollection::BlockComment(_) => {
// We could be expecting:
// - A closing comment
// - A character
match token {
'*' => {
// We could be closing the comment
match next.peek() {
Some((_, '/')) => {
// We're ready to close the comment
self.complete_collection();
Ok(1)
}
_ => Ok(0),
}
}
_ => self.consume(token),
}
}
},
None => {
// We could be expecting:
// - A value
// - Any leading whitespace
let preview = next.peekable();
self.find_any_starting_value(token, preview)
}
}
}
// Returns the number of increments to skip after processing the token
fn find_any_starting_value(
&mut self,
token: char,
mut next: Peekable<impl Iterator<Item = (usize, char)>>,
) -> Result<usize> {
match token {
'{' => {
self.collection_stack
.push((JsonCollection::Object(vec![], vec![]), Default::default()));
}
'[' => {
self.collection_stack
.push((JsonCollection::Array(vec![]), Default::default()));
}
'"' => {
// Peek if next 2 characters are also quotes
let is_triple_quoted = {
next.next_if(|&(_, c)| c == '"')
.and_then(|_| next.next_if(|&(_, c)| c == '"'))
.is_some()
};
if is_triple_quoted {
self.collection_stack.push((
JsonCollection::TripleQuotedString(String::new()),
Default::default(),
));
return Ok(2);
} else {
self.collection_stack.push((
JsonCollection::QuotedString(String::new()),
Default::default(),
))
}
}
'\'' => {
self.collection_stack.push((
JsonCollection::SingleQuotedString(String::new()),
Default::default(),
));
}
'`' => {
// Peek if next 2 characters are also quotes
let is_triple_quoted = {
next.next_if(|&(_, c)| c == '`')
.and_then(|_| next.next_if(|&(_, c)| c == '`'))
.is_some()
};
if is_triple_quoted {
self.collection_stack.push((
JsonCollection::TripleBacktickString {
lang: None,
path: None,
content: String::new(),
},
Default::default(),
));
return Ok(2);
} else {
self.collection_stack.push((
JsonCollection::BacktickString(String::new()),
Default::default(),
))
}
}
'/' => {
// Could be a comment
match next.peek() {
Some((_, '/')) => {
self.collection_stack.push((
JsonCollection::TrailingComment(String::new()),
Default::default(),
));
return Ok(1);
}
Some((_, '*')) => {
self.collection_stack.push((
JsonCollection::BlockComment(String::new()),
Default::default(),
));
return Ok(1);
}
_ => {
// if we're in an object, this could be the beginning of a string
// say a path?
if matches!(
self.collection_stack.last(),
Some((JsonCollection::Object(_, _), _))
) {
self.collection_stack.push((
JsonCollection::UnquotedString(token.into()),
Default::default(),
));
return Ok(0);
}
}
}
}
x if x.is_whitespace() => {}
x => {
self.collection_stack
.push((JsonCollection::UnquotedString(x.into()), Default::default()));
if let Some(count) = self.should_close_unescaped_string(next) {
self.complete_collection();
return Ok(count);
}
}
};
Ok(0)
}
}