vexy-json-core 1.5.11

Core parser for Vexy JSON's forgiving JSON syntax
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
//! ML-based pattern recognition for error recovery
//!
//! This module implements machine learning-inspired pattern recognition
//! for common JSON parsing errors.

#![allow(dead_code)]

use crate::error::recovery_v2::{ErrorContext, RecoverySuggestion, SuggestionCategory};
use crate::error::{Error, Span};
use rustc_hash::FxHashMap;

/// ML-based pattern recognizer
pub struct MLPatternRecognizer {
    /// Feature extractors
    feature_extractors: Vec<Box<dyn FeatureExtractor>>,
    /// Trained patterns with their weights
    patterns: FxHashMap<String, TrainedPattern>,
}

/// Feature extractor trait
trait FeatureExtractor {
    /// Extract features from error context
    fn extract(&self, context: &ErrorContext) -> Vec<Feature>;
}

/// Represents a feature extracted from error context
#[derive(Debug, Clone)]
struct Feature {
    /// Feature name
    name: String,
    /// Feature value (normalized 0.0-1.0)
    value: f64,
    /// Feature weight (importance)
    weight: f64,
}

/// Trained pattern for error recognition
#[derive(Debug, Clone)]
struct TrainedPattern {
    /// Pattern ID
    id: String,
    /// Feature weights
    weights: FxHashMap<String, f64>,
    /// Bias term
    bias: f64,
    /// Success rate
    success_rate: f64,
    /// Number of times used
    usage_count: usize,
    /// Associated fix template
    fix_template: FixTemplate,
}

/// Template for generating fixes
#[derive(Debug, Clone)]
enum FixTemplate {
    /// Insert character at position
    InsertChar { char: char, offset: i32 },
    /// Insert string at position
    InsertString { string: String, offset: i32 },
    /// Replace a range with new text
    ReplaceRange {
        start: i32,
        end: i32,
        replacement: String,
    },
    /// Remove a range
    RemoveRange { start: i32, end: i32 },
    /// Complex operation with multiple sub-operations
    Complex(Vec<FixOperation>),
}

/// Single fix operation
#[derive(Debug, Clone)]
enum FixOperation {
    Insert {
        position: usize,
        text: String,
    },
    Delete {
        start: usize,
        end: usize,
    },
    Replace {
        start: usize,
        end: usize,
        text: String,
    },
}

/// Record of a successful fix
#[derive(Debug, Clone)]
struct SuccessfulFix {
    /// Error signature
    error_signature: String,
}

impl MLPatternRecognizer {
    /// Create a new ML pattern recognizer
    pub fn new() -> Self {
        let mut recognizer = MLPatternRecognizer {
            feature_extractors: Vec::new(),
            patterns: FxHashMap::default(),
        };

        // Initialize feature extractors
        recognizer.add_default_extractors();

        // Load pre-trained patterns
        recognizer.load_pretrained_patterns();

        recognizer
    }

    /// Add default feature extractors
    fn add_default_extractors(&mut self) {
        self.feature_extractors
            .push(Box::new(TokenPatternExtractor));
        self.feature_extractors
            .push(Box::new(CharacterDistributionExtractor));
        self.feature_extractors
            .push(Box::new(StructuralBalanceExtractor));
        self.feature_extractors.push(Box::new(ContextualExtractor));
        self.feature_extractors.push(Box::new(ErrorTypeExtractor));
    }

    /// Load pre-trained patterns
    fn load_pretrained_patterns(&mut self) {
        // Missing closing brace pattern
        self.patterns.insert(
            "missing_closing_brace".to_string(),
            TrainedPattern {
                id: "missing_closing_brace".to_string(),
                weights: [
                    ("unmatched_open_brace".to_string(), 0.9),
                    ("at_end_of_input".to_string(), 0.8),
                    ("in_object_context".to_string(), 0.7),
                ]
                .iter()
                .cloned()
                .collect(),
                bias: -0.1,
                success_rate: 0.95,
                usage_count: 0,
                fix_template: FixTemplate::InsertChar {
                    char: '}',
                    offset: 0,
                },
            },
        );

        // Missing closing bracket pattern
        self.patterns.insert(
            "missing_closing_bracket".to_string(),
            TrainedPattern {
                id: "missing_closing_bracket".to_string(),
                weights: [
                    ("unmatched_open_bracket".to_string(), 0.9),
                    ("at_end_of_input".to_string(), 0.8),
                    ("in_array_context".to_string(), 0.7),
                ]
                .iter()
                .cloned()
                .collect(),
                bias: -0.1,
                success_rate: 0.95,
                usage_count: 0,
                fix_template: FixTemplate::InsertChar {
                    char: ']',
                    offset: 0,
                },
            },
        );

        // Missing comma pattern
        self.patterns.insert(
            "missing_comma".to_string(),
            TrainedPattern {
                id: "missing_comma".to_string(),
                weights: [
                    ("consecutive_values".to_string(), 0.9),
                    ("after_string_or_number".to_string(), 0.8),
                    ("before_string_or_brace".to_string(), 0.7),
                ]
                .iter()
                .cloned()
                .collect(),
                bias: -0.2,
                success_rate: 0.85,
                usage_count: 0,
                fix_template: FixTemplate::InsertChar {
                    char: ',',
                    offset: 0,
                },
            },
        );

        // Unmatched quote pattern
        self.patterns.insert(
            "unmatched_quote".to_string(),
            TrainedPattern {
                id: "unmatched_quote".to_string(),
                weights: [
                    ("odd_quote_count".to_string(), 0.95),
                    ("unterminated_string_error".to_string(), 0.9),
                    ("at_end_of_line".to_string(), 0.6),
                ]
                .iter()
                .cloned()
                .collect(),
                bias: -0.1,
                success_rate: 0.9,
                usage_count: 0,
                fix_template: FixTemplate::InsertChar {
                    char: '"',
                    offset: 0,
                },
            },
        );
    }

    /// Recognize patterns and suggest fixes
    pub fn recognize_and_suggest(&mut self, context: &ErrorContext) -> Vec<RecoverySuggestion> {
        // Extract features
        let features = self.extract_features(context);

        // Score each pattern
        let mut pattern_scores: Vec<(String, f64)> = self
            .patterns
            .iter()
            .map(|(id, pattern)| {
                let score = self.calculate_pattern_score(pattern, &features);
                (id.clone(), score)
            })
            .collect();

        // Sort by score
        pattern_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());

        // Generate suggestions for high-scoring patterns
        let mut suggestions = Vec::new();
        for (pattern_id, score) in pattern_scores {
            if score > 0.5 {
                if let Some(pattern) = self.patterns.get(&pattern_id) {
                    if let Some(suggestion) = self.generate_suggestion(pattern, context, score) {
                        suggestions.push(suggestion);
                    }
                }
            }
        }

        suggestions
    }

    /// Extract features from context
    fn extract_features(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        for extractor in &self.feature_extractors {
            features.extend(extractor.extract(context));
        }

        features
    }

    /// Calculate pattern score using logistic regression
    fn calculate_pattern_score(&self, pattern: &TrainedPattern, features: &[Feature]) -> f64 {
        let mut score = pattern.bias;

        for feature in features {
            if let Some(weight) = pattern.weights.get(&feature.name) {
                score += weight * feature.value;
            }
        }

        // Apply sigmoid function
        1.0 / (1.0 + (-score).exp())
    }

    /// Generate suggestion from pattern
    fn generate_suggestion(
        &self,
        pattern: &TrainedPattern,
        context: &ErrorContext,
        confidence: f64,
    ) -> Option<RecoverySuggestion> {
        let fixed_input = self.apply_fix_template(&pattern.fix_template, context)?;

        // Determine appropriate category based on pattern ID
        let category = match pattern.id.as_str() {
            "missing_closing_brace" | "missing_closing_bracket" => SuggestionCategory::MissingBracket,
            "unmatched_quote" => SuggestionCategory::UnmatchedQuote,
            "missing_comma" => SuggestionCategory::MissingComma,
            _ => SuggestionCategory::Other,
        };

        Some(RecoverySuggestion {
            description: format!("ML: {}", pattern.id.replace('_', " ")),
            confidence: confidence * pattern.success_rate,
            fixed_input,
            category,
            fix_location: Span {
                start: context.position,
                end: context.position,
            },
        })
    }

    /// Apply fix template to generate fixed input
    fn apply_fix_template(&self, template: &FixTemplate, context: &ErrorContext) -> Option<String> {
        match template {
            FixTemplate::InsertChar { char, offset } => {
                let position = (context.position as i32 + offset).max(0) as usize;
                let mut fixed = context.input.clone();
                if position <= fixed.len() {
                    fixed.insert(position, *char);
                    Some(fixed)
                } else {
                    fixed.push(*char);
                    Some(fixed)
                }
            }
            FixTemplate::InsertString { string, offset } => {
                let position = (context.position as i32 + offset).max(0) as usize;
                let mut fixed = context.input.clone();
                if position <= fixed.len() {
                    fixed.insert_str(position, string);
                    Some(fixed)
                } else {
                    fixed.push_str(string);
                    Some(fixed)
                }
            }
            FixTemplate::ReplaceRange {
                start,
                end,
                replacement,
            } => {
                let start_pos = (context.position as i32 + start).max(0) as usize;
                let end_pos = (context.position as i32 + end).max(0) as usize;
                let mut fixed = context.input.clone();
                fixed.replace_range(start_pos..end_pos, replacement);
                Some(fixed)
            }
            FixTemplate::RemoveRange { start, end } => {
                let start_pos = (context.position as i32 + start).max(0) as usize;
                let end_pos = (context.position as i32 + end).max(0) as usize;
                let mut fixed = context.input.clone();
                fixed.replace_range(start_pos..end_pos, "");
                Some(fixed)
            }
            FixTemplate::Complex(operations) => {
                let mut fixed = context.input.clone();
                for op in operations {
                    match op {
                        FixOperation::Insert { position, text } => {
                            if *position <= fixed.len() {
                                fixed.insert_str(*position, text);
                            }
                        }
                        FixOperation::Delete { start, end } => {
                            if *start <= fixed.len() && *end <= fixed.len() && start <= end {
                                fixed.replace_range(*start..*end, "");
                            }
                        }
                        FixOperation::Replace { start, end, text } => {
                            if *start <= fixed.len() && *end <= fixed.len() && start <= end {
                                fixed.replace_range(*start..*end, text);
                            }
                        }
                    }
                }
                Some(fixed)
            }
        }
    }

    /// Update patterns based on user feedback
    pub fn update_from_feedback(&mut self, pattern_id: &str, success: bool) {
        if let Some(pattern) = self.patterns.get_mut(pattern_id) {
            pattern.usage_count += 1;

            // Update success rate with exponential moving average
            let alpha = 0.1; // Learning rate
            pattern.success_rate =
                (1.0 - alpha) * pattern.success_rate + alpha * (if success { 1.0 } else { 0.0 });

            // Adjust weights if needed
            if !success && pattern.success_rate < 0.5 {
                // Reduce all weights slightly
                for weight in pattern.weights.values_mut() {
                    *weight *= 0.95;
                }
            }
        }
    }
}

impl Default for MLPatternRecognizer {
    fn default() -> Self {
        Self::new()
    }
}

// Feature Extractor Implementations

/// Extract token-based patterns
struct TokenPatternExtractor;

impl FeatureExtractor for TokenPatternExtractor {
    fn extract(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        // Check for consecutive values without comma
        if context.tokens_before.len() >= 2 {
            let _last_two: Vec<_> = context.tokens_before.iter().rev().take(2).collect();
            // Simple heuristic for consecutive values
            features.push(Feature {
                name: "consecutive_values".to_string(),
                value: 0.5, // Would need actual token analysis
                weight: 1.0,
            });
        }

        features
    }
}

/// Extract character distribution features
struct CharacterDistributionExtractor;

impl FeatureExtractor for CharacterDistributionExtractor {
    fn extract(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        // Count brackets and quotes
        let mut open_braces = 0;
        let mut close_braces = 0;
        let mut open_brackets = 0;
        let mut close_brackets = 0;
        let mut quotes = 0;

        for ch in context.input.chars() {
            match ch {
                '{' => open_braces += 1,
                '}' => close_braces += 1,
                '[' => open_brackets += 1,
                ']' => close_brackets += 1,
                '"' => quotes += 1,
                _ => {}
            }
        }

        features.push(Feature {
            name: "unmatched_open_brace".to_string(),
            value: if open_braces > close_braces { 1.0 } else { 0.0 },
            weight: 1.0,
        });

        features.push(Feature {
            name: "unmatched_open_bracket".to_string(),
            value: if open_brackets > close_brackets {
                1.0
            } else {
                0.0
            },
            weight: 1.0,
        });

        features.push(Feature {
            name: "odd_quote_count".to_string(),
            value: if quotes % 2 == 1 { 1.0 } else { 0.0 },
            weight: 1.0,
        });

        features
    }
}

/// Extract structural balance features
struct StructuralBalanceExtractor;

impl FeatureExtractor for StructuralBalanceExtractor {
    fn extract(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        // Check if at end of input
        features.push(Feature {
            name: "at_end_of_input".to_string(),
            value: if context.position >= context.input.len() - 1 {
                1.0
            } else {
                0.0
            },
            weight: 1.0,
        });

        features
    }
}

/// Extract contextual features
struct ContextualExtractor;

impl FeatureExtractor for ContextualExtractor {
    fn extract(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        // Check parsing context
        features.push(Feature {
            name: "in_object_context".to_string(),
            value: if context.parsing_context.contains("object") {
                1.0
            } else {
                0.0
            },
            weight: 1.0,
        });

        features.push(Feature {
            name: "in_array_context".to_string(),
            value: if context.parsing_context.contains("array") {
                1.0
            } else {
                0.0
            },
            weight: 1.0,
        });

        features
    }
}

/// Extract error type features
struct ErrorTypeExtractor;

impl FeatureExtractor for ErrorTypeExtractor {
    fn extract(&self, context: &ErrorContext) -> Vec<Feature> {
        let mut features = Vec::new();

        // Check error type
        match &context.error {
            Error::UnterminatedString(_) => {
                features.push(Feature {
                    name: "unterminated_string_error".to_string(),
                    value: 1.0,
                    weight: 1.0,
                });
            }
            Error::UnexpectedEof(_) | Error::UnexpectedChar(_, _) => {
                features.push(Feature {
                    name: "unexpected_token_error".to_string(),
                    value: 1.0,
                    weight: 1.0,
                });
            }
            Error::Expected { expected, found: _, position } => {
                // Check if expecting comma
                if expected.contains("comma") || expected.contains(",") {
                    features.push(Feature {
                        name: "expecting_comma".to_string(),
                        value: 1.0,
                        weight: 1.0,
                    });
                }
                
                // Check if we're between values
                if position > &0 && position < &context.input.len() {
                    let before = &context.input[..*position];
                    let after = context.input[*position..].trim_start();
                    
                    // Check if we have a value before and after
                    if (before.ends_with('"') || before.chars().last().is_some_and(|c| c.is_numeric())) &&
                       (after.starts_with('"') || after.starts_with('[') || after.starts_with('{')) {
                        features.push(Feature {
                            name: "consecutive_values".to_string(),
                            value: 1.0,
                            weight: 1.0,
                        });
                        features.push(Feature {
                            name: "after_string_or_number".to_string(),
                            value: 1.0,
                            weight: 1.0,
                        });
                        features.push(Feature {
                            name: "before_string_or_brace".to_string(),
                            value: 1.0,
                            weight: 1.0,
                        });
                    }
                }
            }
            _ => {}
        }

        features
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ml_pattern_recognition() {
        let mut recognizer = MLPatternRecognizer::new();

        // Test missing closing brace
        let context = ErrorContext {
            error: Error::UnexpectedEof(10),
            input: r#"{"name": "test""#.to_string(),
            position: 15,
            tokens_before: vec![],
            partial_ast: None,
            parsing_context: "in_object".to_string(),
        };

        let suggestions = recognizer.recognize_and_suggest(&context);
        assert!(!suggestions.is_empty());

        // Should suggest adding closing brace
        let first = &suggestions[0];
        assert!(first.fixed_input.ends_with('}'));
    }

    #[test]
    fn test_feature_extraction() {
        let recognizer = MLPatternRecognizer::new();

        let context = ErrorContext {
            error: Error::UnterminatedString(5),
            input: r#"{"key": "value"#.to_string(),
            position: 14,
            tokens_before: vec![],
            partial_ast: None,
            parsing_context: "in_string".to_string(),
        };

        let features = recognizer.extract_features(&context);

        // Should extract odd quote count feature
        let quote_feature = features
            .iter()
            .find(|f| f.name == "odd_quote_count")
            .expect("Should have odd quote count feature");

        assert_eq!(quote_feature.value, 1.0);
    }
}