aprender-verify-ml 0.31.2

Synthetic Data Factory for Domain-Specific Code Intelligence
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
//! C grammar definition
//!
//! Grammar rules for C code generation, targeting decy transpilation (C-to-Rust).
//! Uses tree-sitter-c for proper AST validation when the `tree-sitter` feature is enabled.

use crate::Language;

use super::Grammar;

/// C grammar for code generation
///
/// When the `tree-sitter` feature is enabled, uses tree-sitter-c for
/// proper syntax validation. Otherwise, falls back to basic heuristics.
pub struct CGrammar {
    #[cfg(feature = "tree-sitter")]
    parser: std::sync::Mutex<tree_sitter::Parser>,
}

impl std::fmt::Debug for CGrammar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CGrammar").field("language", &"c").finish()
    }
}

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

impl CGrammar {
    /// Create a new C grammar
    ///
    /// # Panics
    ///
    /// Panics if the tree-sitter C grammar fails to load (should never happen
    /// with a correctly compiled tree-sitter-c dependency).
    #[must_use]
    #[allow(clippy::expect_used)]
    pub fn new() -> Self {
        #[cfg(feature = "tree-sitter")]
        {
            let mut parser = tree_sitter::Parser::new();
            parser
                .set_language(&tree_sitter_c::LANGUAGE.into())
                .expect("Failed to load C grammar");
            Self {
                parser: std::sync::Mutex::new(parser),
            }
        }
        #[cfg(not(feature = "tree-sitter"))]
        {
            Self {}
        }
    }

    /// Parse C code and return the AST tree
    ///
    /// Returns `None` if parsing fails or tree-sitter feature is disabled.
    #[cfg(feature = "tree-sitter")]
    pub fn parse(&self, code: &str) -> Option<tree_sitter::Tree> {
        let mut parser = self.parser.lock().ok()?;
        parser.parse(code, None)
    }

    /// Get the root node of parsed code
    #[cfg(feature = "tree-sitter")]
    pub fn root_node(&self, code: &str) -> Option<String> {
        self.parse(code)
            .map(|tree| tree.root_node().kind().to_string())
    }

    /// Check if the parsed code has any syntax errors
    #[cfg(feature = "tree-sitter")]
    pub fn has_errors(&self, code: &str) -> bool {
        self.parse(code)
            .map_or(true, |tree| tree.root_node().has_error())
    }

    /// Get the AST depth of parsed code
    #[cfg(feature = "tree-sitter")]
    pub fn ast_depth(&self, code: &str) -> usize {
        fn max_depth(node: tree_sitter::Node<'_>) -> usize {
            let child_depths = node
                .children(&mut node.walk())
                .map(max_depth)
                .max()
                .unwrap_or(0);
            1 + child_depths
        }

        self.parse(code)
            .map_or(0, |tree| max_depth(tree.root_node()))
    }

    /// Count the number of nodes in the AST
    #[cfg(feature = "tree-sitter")]
    pub fn node_count(&self, code: &str) -> usize {
        fn count_nodes(node: tree_sitter::Node<'_>) -> usize {
            1 + node
                .children(&mut node.walk())
                .map(count_nodes)
                .sum::<usize>()
        }

        self.parse(code)
            .map_or(0, |tree| count_nodes(tree.root_node()))
    }

    /// Check if code consists only of preprocessor directives
    ///
    /// tree-sitter-c may report errors for preprocessor-only code, but these
    /// are valid C fragments for our generation purposes.
    fn is_preprocessor_only(code: &str) -> bool {
        let mut has_preprocessor = false;
        for line in code.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            // Check if it's a preprocessor directive or a continuation
            if trimmed.starts_with('#') || trimmed.ends_with('\\') {
                has_preprocessor = true;
            } else {
                // Non-preprocessor, non-empty line found
                return false;
            }
        }
        has_preprocessor
    }

    /// Check for balanced braces (fallback validation)
    #[cfg(not(feature = "tree-sitter"))]
    fn is_balanced_braces(code: &str) -> bool {
        let mut brace_count = 0i32;
        let mut paren_count = 0i32;
        let mut bracket_count = 0i32;
        let mut in_string = false;
        let mut in_char = false;
        let mut in_line_comment = false;
        let mut in_block_comment = false;
        let mut prev_char = '\0';

        for c in code.chars() {
            // Handle comments
            if !in_string && !in_char {
                if prev_char == '/' && c == '/' {
                    in_line_comment = true;
                } else if prev_char == '/' && c == '*' {
                    in_block_comment = true;
                } else if in_block_comment && prev_char == '*' && c == '/' {
                    in_block_comment = false;
                    prev_char = c;
                    continue;
                } else if in_line_comment && c == '\n' {
                    in_line_comment = false;
                }
            }

            if in_line_comment || in_block_comment {
                prev_char = c;
                continue;
            }

            // Handle strings and chars
            match c {
                '"' if !in_char && prev_char != '\\' => in_string = !in_string,
                '\'' if !in_string && prev_char != '\\' => in_char = !in_char,
                _ => {}
            }

            // Only count brackets outside of strings/chars
            if !in_string && !in_char {
                match c {
                    '{' => brace_count += 1,
                    '}' => brace_count -= 1,
                    '(' => paren_count += 1,
                    ')' => paren_count -= 1,
                    '[' => bracket_count += 1,
                    ']' => bracket_count -= 1,
                    _ => {}
                }

                if brace_count < 0 || paren_count < 0 || bracket_count < 0 {
                    return false;
                }
            }
            prev_char = c;
        }

        brace_count == 0 && paren_count == 0 && bracket_count == 0
    }

    /// Check for basic C syntax patterns (fallback validation)
    #[cfg(not(feature = "tree-sitter"))]
    fn has_valid_structure(code: &str) -> bool {
        // Check for unmatched preprocessor directives
        let has_include = code.contains("#include");
        let has_define = code.contains("#define");
        let has_ifdef = code.contains("#ifdef") || code.contains("#ifndef");
        let has_endif = code.contains("#endif");

        // If we have ifdef/ifndef, we need endif
        if has_ifdef && !has_endif {
            return false;
        }

        // Basic structure check - must have some content
        let trimmed = code.trim();
        !trimmed.is_empty()
            && (has_include || has_define || trimmed.contains(';') || trimmed.contains('{'))
    }
}

impl Grammar for CGrammar {
    fn language(&self) -> Language {
        Language::C
    }

    fn validate(&self, code: &str) -> bool {
        if code.is_empty() {
            return false;
        }

        // Preprocessor-only code is always valid (tree-sitter may report errors)
        if Self::is_preprocessor_only(code) {
            return true;
        }

        #[cfg(feature = "tree-sitter")]
        {
            !self.has_errors(code)
        }

        #[cfg(not(feature = "tree-sitter"))]
        {
            // Basic fallback validation without tree-sitter
            Self::is_balanced_braces(code) && Self::has_valid_structure(code)
        }
    }

    fn max_enumeration_depth(&self) -> usize {
        6 // C programs can have deep nesting (structs, unions, nested functions)
    }
}

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

    // Basic grammar tests
    #[test]
    fn test_c_grammar_language() {
        let grammar = CGrammar::new();
        assert_eq!(grammar.language(), Language::C);
    }

    #[test]
    fn test_c_grammar_debug() {
        let grammar = CGrammar::new();
        let debug = format!("{:?}", grammar);
        assert!(debug.contains("CGrammar"));
        assert!(debug.contains("c"));
    }

    #[test]
    fn test_c_grammar_default() {
        let grammar = CGrammar::default();
        assert_eq!(grammar.language(), Language::C);
    }

    #[test]
    fn test_c_grammar_max_depth() {
        let grammar = CGrammar::new();
        assert_eq!(grammar.max_enumeration_depth(), 6);
    }

    #[test]
    fn test_c_grammar_validate_empty() {
        let grammar = CGrammar::new();
        assert!(!grammar.validate(""));
    }

    // Variable declarations
    #[test]
    fn test_c_grammar_validate_int_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x;"));
        assert!(grammar.validate("int x = 42;"));
        assert!(grammar.validate("int x, y, z;"));
    }

    #[test]
    fn test_c_grammar_validate_float_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("float f;"));
        assert!(grammar.validate("double d = 3.14;"));
        assert!(grammar.validate("long double ld;"));
    }

    #[test]
    fn test_c_grammar_validate_char_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("char c;"));
        assert!(grammar.validate("char c = 'a';"));
        assert!(grammar.validate("unsigned char uc;"));
    }

    #[test]
    fn test_c_grammar_validate_pointer_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int *p;"));
        assert!(grammar.validate("int **pp;"));
        assert!(grammar.validate("char *str = \"hello\";"));
        assert!(grammar.validate("void *ptr;"));
    }

    #[test]
    fn test_c_grammar_validate_array_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int arr[10];"));
        assert!(grammar.validate("int arr[] = {1, 2, 3};"));
        assert!(grammar.validate("char str[] = \"hello\";"));
        assert!(grammar.validate("int matrix[3][3];"));
    }

    #[test]
    fn test_c_grammar_validate_const_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("const int x = 10;"));
        assert!(grammar.validate("const char *str = \"hello\";"));
        assert!(grammar.validate("static const int SIZE = 100;"));
    }

    // Functions
    #[test]
    fn test_c_grammar_validate_function_declaration() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int main(void);"));
        assert!(grammar.validate("void foo(int x, int y);"));
        assert!(grammar.validate("char *strdup(const char *s);"));
    }

    #[test]
    fn test_c_grammar_validate_function_definition() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int main(void) { return 0; }"));
        assert!(grammar.validate("void foo() {}"));
        assert!(grammar.validate("int add(int a, int b) { return a + b; }"));
    }

    #[test]
    fn test_c_grammar_validate_function_with_body() {
        let grammar = CGrammar::new();
        let code = r#"
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_variadic_function() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int printf(const char *fmt, ...);"));
    }

    // Control flow
    #[test]
    fn test_c_grammar_validate_if_statement() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; if (x > 0) { x = 1; }"));
        assert!(grammar.validate("int x; if (x) x++;"));
    }

    #[test]
    fn test_c_grammar_validate_if_else() {
        let grammar = CGrammar::new();
        let code = "int x; if (x > 0) { x = 1; } else { x = 0; }";
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_if_else_if() {
        let grammar = CGrammar::new();
        let code = r#"
int x;
if (x > 0) {
    x = 1;
} else if (x < 0) {
    x = -1;
} else {
    x = 0;
}
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_for_loop() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int i; for (i = 0; i < 10; i++) {}"));
        assert!(grammar.validate("for (int i = 0; i < 10; i++) {}"));
    }

    #[test]
    fn test_c_grammar_validate_while_loop() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = 10; while (x > 0) { x--; }"));
        assert!(grammar.validate("while (1) { break; }"));
    }

    #[test]
    fn test_c_grammar_validate_do_while() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = 0; do { x++; } while (x < 10);"));
    }

    #[test]
    fn test_c_grammar_validate_switch() {
        let grammar = CGrammar::new();
        let code = r#"
int x;
switch (x) {
    case 0: break;
    case 1: break;
    default: break;
}
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_goto() {
        let grammar = CGrammar::new();
        let code = r#"
int main() {
    goto end;
    end:
    return 0;
}
"#;
        assert!(grammar.validate(code));
    }

    // Structs and unions
    #[test]
    fn test_c_grammar_validate_struct() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("struct Point { int x; int y; };"));
    }

    #[test]
    fn test_c_grammar_validate_struct_typedef() {
        let grammar = CGrammar::new();
        let code = r#"
typedef struct {
    int x;
    int y;
} Point;
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_nested_struct() {
        let grammar = CGrammar::new();
        let code = r#"
struct Outer {
    struct Inner {
        int value;
    } inner;
    int other;
};
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_union() {
        let grammar = CGrammar::new();
        let code = r#"
union Data {
    int i;
    float f;
    char str[20];
};
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_enum() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("enum Color { RED, GREEN, BLUE };"));
        assert!(grammar.validate("enum { A = 1, B = 2, C = 4 };"));
    }

    // Preprocessor
    #[test]
    fn test_c_grammar_validate_include() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("#include <stdio.h>"));
        assert!(grammar.validate("#include \"myheader.h\""));
    }

    #[test]
    fn test_c_grammar_validate_define() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("#define MAX 100"));
        assert!(grammar.validate("#define SQUARE(x) ((x) * (x))"));
    }

    #[test]
    fn test_c_grammar_validate_ifdef() {
        let grammar = CGrammar::new();
        let code = r#"
#ifdef DEBUG
int debug = 1;
#endif
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_ifndef_guard() {
        let grammar = CGrammar::new();
        let code = r#"
#ifndef HEADER_H
#define HEADER_H
int x;
#endif
"#;
        assert!(grammar.validate(code));
    }

    // Operators
    #[test]
    fn test_c_grammar_validate_arithmetic() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = 1 + 2;"));
        assert!(grammar.validate("int x = 10 - 5;"));
        assert!(grammar.validate("int x = 3 * 4;"));
        assert!(grammar.validate("int x = 10 / 2;"));
        assert!(grammar.validate("int x = 10 % 3;"));
    }

    #[test]
    fn test_c_grammar_validate_bitwise() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = a & b;"));
        assert!(grammar.validate("int x = a | b;"));
        assert!(grammar.validate("int x = a ^ b;"));
        assert!(grammar.validate("int x = ~a;"));
        assert!(grammar.validate("int x = a << 2;"));
        assert!(grammar.validate("int x = a >> 2;"));
    }

    #[test]
    fn test_c_grammar_validate_logical() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = a && b;"));
        assert!(grammar.validate("int x = a || b;"));
        assert!(grammar.validate("int x = !a;"));
    }

    #[test]
    fn test_c_grammar_validate_comparison() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = a == b;"));
        assert!(grammar.validate("int x = a != b;"));
        assert!(grammar.validate("int x = a < b;"));
        assert!(grammar.validate("int x = a > b;"));
        assert!(grammar.validate("int x = a <= b;"));
        assert!(grammar.validate("int x = a >= b;"));
    }

    #[test]
    fn test_c_grammar_validate_assignment() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; x = 1;"));
        assert!(grammar.validate("int x; x += 1;"));
        assert!(grammar.validate("int x; x -= 1;"));
        assert!(grammar.validate("int x; x *= 2;"));
        assert!(grammar.validate("int x; x /= 2;"));
        assert!(grammar.validate("int x; x %= 2;"));
    }

    #[test]
    fn test_c_grammar_validate_increment_decrement() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; x++;"));
        assert!(grammar.validate("int x; x--;"));
        assert!(grammar.validate("int x; ++x;"));
        assert!(grammar.validate("int x; --x;"));
    }

    #[test]
    fn test_c_grammar_validate_ternary() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = a > b ? a : b;"));
    }

    #[test]
    fn test_c_grammar_validate_sizeof() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = sizeof(int);"));
        assert!(grammar.validate("int x = sizeof(x);"));
    }

    #[test]
    fn test_c_grammar_validate_cast() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x = (int)3.14;"));
        assert!(grammar.validate("void *p; int *ip = (int *)p;"));
    }

    // Pointers and memory
    #[test]
    fn test_c_grammar_validate_address_of() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; int *p = &x;"));
    }

    #[test]
    fn test_c_grammar_validate_dereference() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int *p; int x = *p;"));
    }

    #[test]
    fn test_c_grammar_validate_struct_access() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("struct Point p; int x = p.x;"));
        assert!(grammar.validate("struct Point *p; int x = p->x;"));
    }

    #[test]
    fn test_c_grammar_validate_array_access() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int arr[10]; int x = arr[0];"));
        assert!(grammar.validate("int arr[10]; arr[5] = 42;"));
    }

    // Comments
    #[test]
    fn test_c_grammar_validate_line_comment() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; // this is a comment"));
    }

    #[test]
    fn test_c_grammar_validate_block_comment() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("int x; /* block comment */"));
        assert!(grammar.validate("/* multi\nline\ncomment */ int x;"));
    }

    // String and char literals
    #[test]
    fn test_c_grammar_validate_string_literal() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("char *s = \"hello world\";"));
        assert!(grammar.validate("char *s = \"hello\\nworld\";"));
    }

    #[test]
    fn test_c_grammar_validate_char_literal() {
        let grammar = CGrammar::new();
        assert!(grammar.validate("char c = 'a';"));
        assert!(grammar.validate("char c = '\\n';"));
        assert!(grammar.validate("char c = '\\0';"));
    }

    // Complex programs
    #[test]
    fn test_c_grammar_validate_hello_world() {
        let grammar = CGrammar::new();
        let code = r#"
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
"#;
        assert!(grammar.validate(code));
    }

    #[test]
    fn test_c_grammar_validate_linked_list() {
        let grammar = CGrammar::new();
        let code = r#"
struct Node {
    int data;
    struct Node *next;
};

struct Node *create_node(int data) {
    struct Node *node = malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}
"#;
        assert!(grammar.validate(code));
    }

    // Invalid code
    #[test]
    fn test_c_grammar_validate_unbalanced_braces() {
        let grammar = CGrammar::new();
        assert!(!grammar.validate("int main() {"));
        assert!(!grammar.validate("int main() { return 0; "));
    }

    #[test]
    fn test_c_grammar_validate_unbalanced_parens() {
        let grammar = CGrammar::new();
        assert!(!grammar.validate("int x = (1 + 2;"));
        assert!(!grammar.validate("if (x > 0 { }"));
    }

    // Fallback validation tests (only run when tree-sitter is disabled)
    #[cfg(not(feature = "tree-sitter"))]
    #[test]
    fn test_balanced_braces() {
        assert!(CGrammar::is_balanced_braces("{}"));
        assert!(CGrammar::is_balanced_braces("{ { } }"));
        assert!(CGrammar::is_balanced_braces("int main() { return 0; }"));
        assert!(!CGrammar::is_balanced_braces("{"));
        assert!(!CGrammar::is_balanced_braces("}"));
        assert!(!CGrammar::is_balanced_braces("{ { }"));
    }

    #[cfg(not(feature = "tree-sitter"))]
    #[test]
    fn test_balanced_braces_with_strings() {
        assert!(CGrammar::is_balanced_braces("char *s = \"{\";"));
        assert!(CGrammar::is_balanced_braces("char c = '{';"));
    }

    #[cfg(not(feature = "tree-sitter"))]
    #[test]
    fn test_balanced_braces_with_comments() {
        assert!(CGrammar::is_balanced_braces("int x; // { not counted"));
        assert!(CGrammar::is_balanced_braces("int x; /* { */ int y;"));
    }

    #[cfg(not(feature = "tree-sitter"))]
    #[test]
    fn test_has_valid_structure() {
        assert!(CGrammar::has_valid_structure("int x;"));
        assert!(CGrammar::has_valid_structure("#include <stdio.h>"));
        assert!(CGrammar::has_valid_structure("#define MAX 100"));
        assert!(CGrammar::has_valid_structure("int main() {}"));
        assert!(!CGrammar::has_valid_structure(""));
        assert!(!CGrammar::has_valid_structure("   "));
    }

    #[test]
    fn test_is_preprocessor_only() {
        // Valid preprocessor-only code
        assert!(CGrammar::is_preprocessor_only("#include <stdio.h>"));
        assert!(CGrammar::is_preprocessor_only("#define MAX 100"));
        assert!(CGrammar::is_preprocessor_only(
            "#include <stdio.h>\n#include <stdlib.h>"
        ));
        assert!(CGrammar::is_preprocessor_only("#ifdef DEBUG\n#endif"));
        assert!(CGrammar::is_preprocessor_only(
            "#ifndef HEADER_H\n#define HEADER_H\n#endif"
        ));

        // Mixed content (not preprocessor-only)
        assert!(!CGrammar::is_preprocessor_only("int x;"));
        assert!(!CGrammar::is_preprocessor_only(
            "#include <stdio.h>\nint main() {}"
        ));
        assert!(!CGrammar::is_preprocessor_only("int x;\n#define Y 1"));

        // Empty/whitespace
        assert!(!CGrammar::is_preprocessor_only(""));
        assert!(!CGrammar::is_preprocessor_only("   "));
    }

    #[cfg(feature = "tree-sitter")]
    mod tree_sitter_tests {
        use super::*;

        #[test]
        fn test_parse_simple() {
            let grammar = CGrammar::new();
            let tree = grammar.parse("int x;");
            assert!(tree.is_some());
        }

        #[test]
        fn test_root_node() {
            let grammar = CGrammar::new();
            let root = grammar.root_node("int x;");
            assert_eq!(root, Some("translation_unit".to_string()));
        }

        #[test]
        fn test_has_errors_valid() {
            let grammar = CGrammar::new();
            assert!(!grammar.has_errors("int x;"));
            assert!(!grammar.has_errors("int main() { return 0; }"));
        }

        #[test]
        fn test_has_errors_invalid() {
            let grammar = CGrammar::new();
            assert!(grammar.has_errors("int x")); // missing semicolon
            assert!(grammar.has_errors("int main() {")); // unclosed brace
        }

        #[test]
        fn test_ast_depth() {
            let grammar = CGrammar::new();
            let simple_depth = grammar.ast_depth("int x;");
            let complex_depth = grammar.ast_depth("int main() { if (x) { return y + z; } }");
            assert!(simple_depth > 0);
            assert!(complex_depth > simple_depth);
        }

        #[test]
        fn test_node_count() {
            let grammar = CGrammar::new();
            let simple_count = grammar.node_count("int x;");
            let complex_count = grammar.node_count("int x; int y; int z;");
            assert!(simple_count > 0);
            assert!(complex_count > simple_count);
        }
    }
}