probe-code 0.6.0

AI-friendly, fully local, semantic code search tool for large codebases
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
use std::collections::HashMap;
use std::collections::HashSet;
use std::iter::Peekable;
use std::str::Chars;

/// The AST representing a parsed query.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    /// A search term, which can represent multiple keywords.
    /// `keywords` => a list of keywords (possibly tokenized/split)
    /// `field` => optional field specifier (e.g. `Some("title")` for `title:foo`)
    /// `required` => a leading `+`
    /// `excluded` => a leading `-`
    /// `exact` => if originally quoted, meaning "no tokenization/splitting"
    Term {
        keywords: Vec<String>,
        field: Option<String>,
        required: bool,
        excluded: bool,
        exact: bool,
    },

    /// Logical AND of two sub-expressions.
    And(Box<Expr>, Box<Expr>),

    /// Logical OR of two sub-expressions.
    Or(Box<Expr>, Box<Expr>),
}

impl Expr {
    /// Extract required and optional terms from the AST, excluding negative terms
    #[cfg(test)]
    pub fn extract_terms(&self) -> (Vec<String>, Vec<String>) {
        let mut required = Vec::new();
        let mut optional = Vec::new();
        self.collect_terms(&mut required, &mut optional);
        (required, optional)
    }

    #[cfg(test)]
    fn collect_terms(&self, required: &mut Vec<String>, optional: &mut Vec<String>) {
        match self {
            Expr::Term {
                keywords,
                required: is_required,
                excluded,
                ..
            } => {
                if !excluded {
                    for keyword in keywords {
                        if *is_required {
                            required.push(keyword.clone());
                        } else {
                            optional.push(keyword.clone());
                        }
                    }
                }
            }
            Expr::And(left, right) => {
                left.collect_terms(required, optional);
                right.collect_terms(required, optional);
            }
            Expr::Or(left, right) => {
                left.collect_terms(required, optional);
                right.collect_terms(required, optional);
            }
        }
    }

    /// Returns `true` if this expression contains at least one `required=true` term.
    fn has_required_term(&self) -> bool {
        match self {
            Expr::Term { required, .. } => *required,
            Expr::And(left, right) | Expr::Or(left, right) => {
                left.has_required_term() || right.has_required_term()
            }
        }
    }

    /// A helper to evaluate the expression when the caller already knows if
    /// there are any required terms in the *entire* query (not just in this subtree).
    fn evaluate_with_has_required(
        &self,
        matched_terms: &HashSet<usize>,
        term_indices: &HashMap<String, usize>,
        ignore_negatives: bool,
        has_required_anywhere: bool,
    ) -> bool {
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

        match self {
            Expr::Term {
                keywords,
                required,
                excluded,
                ..
            } => {
                if keywords.is_empty() {
                    // Empty term => if excluded, trivially true, otherwise false
                    return *excluded;
                }
                // Are all keywords present?
                let all_present = keywords.iter().all(|kw| {
                    term_indices
                        .get(kw)
                        .map(|idx| matched_terms.contains(idx))
                        .unwrap_or(false)
                });

                if *excluded {
                    if ignore_negatives {
                        // Negative ignored => always true
                        true
                    } else {
                        // Excluded => none should be present
                        !keywords.iter().any(|kw| {
                            term_indices
                                .get(kw)
                                .map(|idx| matched_terms.contains(idx))
                                .unwrap_or(false)
                        })
                    }
                } else if *required && ignore_negatives {
                    // If ignoring negatives, we've already enforced required terms up front.
                    true
                } else if *required {
                    // Required => must all be present
                    all_present
                } else {
                    // Optional => if there's at least one required term anywhere in the entire query,
                    // then we do NOT fail if this optional is absent. Otherwise, we do need to match.
                    if has_required_anywhere {
                        true
                    } else {
                        // When there are no required terms, we still need to enforce that all keywords
                        // within a single Term are present (AND logic within a Term).
                        // This ensures that for a term like "JWTMiddleware" which gets tokenized to
                        // ["jwt", "middleware"], both parts must be present.

                        // Check if any keywords are present
                        let any_present = keywords.iter().any(|kw| {
                            term_indices
                                .get(kw)
                                .map(|idx| matched_terms.contains(idx))
                                .unwrap_or(false)
                        });

                        // If no keywords are present, the term doesn't match
                        if !any_present {
                            return false;
                        }

                        // If at least one keyword is present, require all keywords to be present
                        // This maintains the AND relationship between keywords in a single Term
                        all_present
                    }
                }
            }
            Expr::And(left, right) => {
                let lval = left.evaluate_with_has_required(
                    matched_terms,
                    term_indices,
                    ignore_negatives,
                    has_required_anywhere,
                );
                let rval = right.evaluate_with_has_required(
                    matched_terms,
                    term_indices,
                    ignore_negatives,
                    has_required_anywhere,
                );
                if debug_mode {
                    println!(
                        "DEBUG: AND => left={}, right={}, result={}",
                        lval,
                        rval,
                        lval && rval
                    );
                }
                lval && rval
            }
            Expr::Or(left, right) => {
                // For OR expressions, we need to be careful about how we handle has_required_anywhere
                // We want to maintain the behavior that at least one term must be present

                let lval = left.evaluate_with_has_required(
                    matched_terms,
                    term_indices,
                    ignore_negatives,
                    has_required_anywhere,
                );
                let rval = right.evaluate_with_has_required(
                    matched_terms,
                    term_indices,
                    ignore_negatives,
                    has_required_anywhere,
                );

                if debug_mode {
                    println!(
                        "DEBUG: OR => left={}, right={}, result={}",
                        lval,
                        rval,
                        lval || rval
                    );
                }
                lval || rval
            }
        }
    }

    /// Evaluate whether a set of matched term indices satisfies this logical expression.
    ///
    /// - Term: check if **all** of its keywords are present (optional/required), or
    ///   if **none** are present (excluded).
    /// - AND => both sides must match.
    /// - OR => at least one side must match.
    /// - `ignore_negatives` => if true, excluded terms are basically ignored (they don’t exclude).
    /// - Field is **ignored** in evaluation, per request.
    pub fn evaluate(
        &self,
        matched_terms: &HashSet<usize>,
        term_indices: &HashMap<String, usize>,
        ignore_negatives: bool,
    ) -> bool {
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

        // If ignoring negatives, let's ensure that all required terms are present up front.
        // (We skip enforcing them again for each subtree.)
        if ignore_negatives {
            fn collect_required(expr: &Expr) -> Vec<String> {
                match expr {
                    Expr::Term {
                        keywords,
                        required,
                        excluded,
                        ..
                    } => {
                        if *required && !*excluded {
                            keywords.clone()
                        } else {
                            vec![]
                        }
                    }
                    Expr::And(left, right) => {
                        let mut out = collect_required(left);
                        out.extend(collect_required(right));
                        out
                    }
                    Expr::Or(left, right) => {
                        let mut out = collect_required(left);
                        out.extend(collect_required(right));
                        out
                    }
                }
            }
            let required_terms = collect_required(self);
            if debug_mode && !required_terms.is_empty() {
                println!("DEBUG: Required terms (ignoring negatives): {required_terms:?}");
            }
            for term in &required_terms {
                if let Some(&idx) = term_indices.get(term) {
                    if !matched_terms.contains(&idx) {
                        if debug_mode {
                            println!("DEBUG: Missing required term '{term}' (idx={idx})");
                        }
                        return false;
                    }
                } else {
                    // If we can't find that required term at all, fail immediately
                    return false;
                }
            }
        }

        // Compute once for the entire query whether any term is required
        let has_required_anywhere = self.has_required_term();

        if debug_mode {
            println!("DEBUG: Evaluating => {self:?}");
            println!("DEBUG: matched_terms => {matched_terms:?}");
            println!("DEBUG: term_indices => {term_indices:?}");
            println!("DEBUG: Expression has_required_anywhere? {has_required_anywhere}");
        }

        // Delegate final checks to our helper, which references has_required_anywhere
        self.evaluate_with_has_required(
            matched_terms,
            term_indices,
            ignore_negatives,
            has_required_anywhere,
        )
    }
}

impl std::fmt::Display for Expr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Expr::Term {
                keywords,
                field,
                required,
                excluded,
                exact,
            } => {
                let prefix = if *required {
                    "+"
                } else if *excluded {
                    "-"
                } else {
                    ""
                };
                let field_prefix = if let Some(ref field_name) = field {
                    format!("{field_name}:")
                } else {
                    String::new()
                };
                // If there's exactly one keyword and it's exact => show it quoted
                // If multiple or not exact => "quoted" with joined keywords
                if keywords.len() == 1 && *exact {
                    write!(f, "{}{}\"{}\"", prefix, field_prefix, keywords[0])
                } else if keywords.len() == 1 {
                    write!(f, "{}{}{}", prefix, field_prefix, keywords[0])
                } else {
                    write!(f, "{}{}\"{}\"", prefix, field_prefix, keywords.join(" "))
                }
            }
            Expr::And(left, right) => write!(f, "({left} AND {right})"),
            Expr::Or(left, right) => write!(f, "({left} OR {right})"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    Plus,                 // '+'
    Minus,                // '-'
    And,                  // 'AND'
    Or,                   // 'OR'
    LParen,               // '('
    RParen,               // ')'
    Colon,                // ':'
    Ident(String),        // alphanumeric / underscore / dot
    QuotedString(String), // raw string inside quotes
}

/// A simple error type for parsing/tokenizing.
#[derive(Debug)]
pub enum ParseError {
    #[allow(dead_code)]
    UnexpectedChar(char),
    UnexpectedEndOfInput,
    UnexpectedToken(Token),
    Generic(String),
}
impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseError::UnexpectedChar(c) => write!(f, "Unexpected character '{c}'"),
            ParseError::UnexpectedEndOfInput => write!(f, "Unexpected end of input"),
            ParseError::UnexpectedToken(t) => write!(f, "Unexpected token '{t:?}'"),
            ParseError::Generic(s) => write!(f, "{s}"),
        }
    }
}

impl std::error::Error for ParseError {}

/// Tokenize input string into a vector of tokens
fn tokenize(input: &str) -> Result<Vec<Token>, ParseError> {
    let mut chars = input.chars().peekable();
    let mut tokens = Vec::new();
    let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

    while let Some(&ch) = chars.peek() {
        match ch {
            c if c.is_whitespace() => {
                chars.next();
            }
            '+' => {
                tokens.push(Token::Plus);
                chars.next();
            }
            '-' => {
                tokens.push(Token::Minus);
                chars.next();
            }
            '(' => {
                tokens.push(Token::LParen);
                chars.next();
            }
            ')' => {
                tokens.push(Token::RParen);
                chars.next();
            }
            ':' => {
                tokens.push(Token::Colon);
                chars.next();
            }
            '"' => {
                chars.next(); // consume the opening quote
                let quoted_string = lex_quoted_string(&mut chars)?;
                tokens.push(Token::QuotedString(quoted_string));
            }
            _ => {
                // If it starts with alphanumeric, underscore, or dot => parse identifier
                if ch.is_alphanumeric() || ch == '_' || ch == '.' {
                    let ident = lex_identifier(&mut chars);
                    let ident_upper = ident.to_ascii_uppercase();
                    if ident_upper == "AND" {
                        tokens.push(Token::And);
                    } else if ident_upper == "OR" {
                        tokens.push(Token::Or);
                    } else {
                        tokens.push(Token::Ident(ident));
                    }
                } else {
                    // Skip unknown characters
                    if debug_mode {
                        println!("DEBUG: Skipping unknown character '{ch}'");
                    }
                    chars.next();
                }
            }
        }
    }

    if tokens.is_empty() {
        return Err(ParseError::Generic(
            "No valid tokens found in input".to_string(),
        ));
    }
    Ok(tokens)
}

/// Lex a quoted string, allowing `\"` to escape quotes
fn lex_quoted_string(chars: &mut Peekable<Chars>) -> Result<String, ParseError> {
    let mut buf = String::new();
    let mut escaped = false;

    while let Some(&ch) = chars.peek() {
        if escaped {
            buf.push(ch);
            escaped = false;
            chars.next();
        } else if ch == '\\' {
            escaped = true;
            chars.next();
        } else if ch == '"' {
            chars.next(); // consume the closing quote
            return Ok(buf);
        } else {
            buf.push(ch);
            chars.next();
        }
    }
    // If we get here, we ran out of characters before finding a closing quote
    Err(ParseError::UnexpectedEndOfInput)
}

fn lex_identifier(chars: &mut Peekable<Chars>) -> String {
    let mut buf = String::new();
    while let Some(&ch) = chars.peek() {
        if ch.is_alphanumeric() || ch == '_' || ch == '.' {
            buf.push(ch);
            chars.next();
        } else {
            break;
        }
    }
    buf
}

// Adjust paths to match your project structure
use probe_code::search::tokenization::{add_special_term, tokenize as custom_tokenize};

struct Parser {
    tokens: Vec<Token>,
    pos: usize,
}

impl Parser {
    fn new(tokens: Vec<Token>) -> Self {
        Parser { tokens, pos: 0 }
    }

    fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.pos)
    }

    fn next(&mut self) -> Option<Token> {
        let t = self.peek()?.clone();
        self.pos += 1;
        Some(t)
    }

    fn expect(&mut self, expected: &Token) -> Result<Token, ParseError> {
        match self.peek() {
            Some(t) if t == expected => Ok(self.next().unwrap()),
            Some(t) => Err(ParseError::UnexpectedToken(t.clone())),
            None => Err(ParseError::UnexpectedEndOfInput),
        }
    }

    fn parse_expr(&mut self) -> Result<Expr, ParseError> {
        let expr = self.parse_or_expr()?;
        // If leftover tokens remain, we ignore them for now
        Ok(expr)
    }

    fn parse_or_expr(&mut self) -> Result<Expr, ParseError> {
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
        if debug_mode {
            println!("DEBUG: parse_or_expr => pos={pos}", pos = self.pos);
        }

        let mut left = self.parse_and_expr()?;

        while let Some(Token::Or) = self.peek() {
            self.next(); // consume 'OR'
            let right = self.parse_and_expr()?;
            left = Expr::Or(Box::new(left), Box::new(right));
            if debug_mode {
                println!("DEBUG: OR => {left:?}");
            }
        }
        Ok(left)
    }

    fn parse_and_expr(&mut self) -> Result<Expr, ParseError> {
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
        if debug_mode {
            println!("DEBUG: parse_and_expr => pos={pos}", pos = self.pos);
        }

        let mut left = self.parse_factor()?;

        while let Some(token) = self.peek() {
            match token {
                // Explicit "AND"
                Token::And => {
                    self.next(); // consume 'AND'
                    let right = self.parse_factor()?;
                    left = Expr::And(Box::new(left), Box::new(right));
                    if debug_mode {
                        println!("DEBUG: AND => {left:?}");
                    }
                }
                // If we see "OR", break so parse_or_expr can handle it
                Token::Or => {
                    break;
                }
                // If next token is a plus or minus, interpret as an AND
                Token::Plus | Token::Minus => {
                    let right = self.parse_factor()?;
                    left = Expr::And(Box::new(left), Box::new(right));
                    if debug_mode {
                        println!("DEBUG: forced AND => {left:?}");
                    }
                }
                // Otherwise (Ident, QuotedString, LParen) => implicit combos
                Token::Ident(_) | Token::QuotedString(_) | Token::LParen => {
                    let right = self.parse_factor()?;
                    // Use OR for implicit combinations (space-separated terms) - Elasticsearch standard behavior
                    left = Expr::Or(Box::new(left), Box::new(right));
                    if debug_mode {
                        println!("DEBUG: implicit OR => {left:?}");
                    }
                }
                _ => break,
            }
        }
        Ok(left)
    }

    fn parse_factor(&mut self) -> Result<Expr, ParseError> {
        match self.peek() {
            Some(Token::LParen) => {
                self.next(); // consume '('
                let expr = self.parse_expr()?;
                self.expect(&Token::RParen)?;
                Ok(expr)
            }
            _ => self.parse_prefixed_term(),
        }
    }

    fn parse_prefixed_term(&mut self) -> Result<Expr, ParseError> {
        let mut required = false;
        let mut excluded = false;
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

        match self.peek() {
            Some(Token::Plus) => {
                required = true;
                self.next();
            }
            Some(Token::Minus) => {
                excluded = true;
                self.next();
            }
            _ => {}
        }

        let primary_expr = self.parse_primary()?;
        // If it's a Term => update its required/excluded
        if let Expr::Term {
            keywords,
            field,
            required: _,
            excluded: _,
            exact,
        } = primary_expr
        {
            // If exact or excluded => skip further tokenization
            let final_keywords = if exact || excluded {
                // Mark them special (no splitting)
                for kw in &keywords {
                    add_special_term(kw);
                }
                keywords
            } else {
                // Apply your custom tokenization
                let mut expanded = Vec::new();
                for kw in &keywords {
                    let splitted = custom_tokenize(kw);
                    // Only add non-empty terms
                    expanded.extend(splitted.into_iter().filter(|s| !s.is_empty()));
                }
                // If all terms were filtered out (e.g., all were stop words),
                // return an empty vector which will be handled properly
                expanded
            };

            if debug_mode {
                println!("DEBUG: parse_prefixed_term => required={required}, excluded={excluded}, final_keywords={final_keywords:?}");
            }

            Ok(Expr::Term {
                keywords: final_keywords,
                field,
                required,
                excluded,
                exact,
            })
        } else {
            // If it's a sub-expression in parentheses or something else, just return it
            Ok(primary_expr)
        }
    }

    fn parse_primary(&mut self) -> Result<Expr, ParseError> {
        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

        match self.peek() {
            // Quoted => exact
            Some(Token::QuotedString(s)) => {
                let val = s.clone();
                self.next();
                if debug_mode {
                    println!("DEBUG: QuotedString => {val}");
                }
                Ok(Expr::Term {
                    keywords: vec![val],
                    field: None,
                    required: false,
                    excluded: false,
                    exact: true,
                })
            }
            // Possibly field:term
            Some(Token::Ident(_)) => {
                let Token::Ident(first) = self.next().unwrap() else {
                    unreachable!()
                };
                if debug_mode {
                    println!("DEBUG: Ident => {first}");
                }
                if let Some(Token::Colon) = self.peek() {
                    // We have "field:"
                    self.next(); // consume colon
                                 // Next could be ident or quoted
                    match self.peek() {
                        Some(Token::Ident(ident2)) => {
                            let val2 = ident2.clone();
                            self.next();
                            Ok(Expr::Term {
                                keywords: vec![val2],
                                field: Some(first),
                                required: false,
                                excluded: false,
                                exact: false,
                            })
                        }
                        Some(Token::QuotedString(qs)) => {
                            let qval = qs.clone();
                            self.next();
                            Ok(Expr::Term {
                                keywords: vec![qval],
                                field: Some(first),
                                required: false,
                                excluded: false,
                                exact: true,
                            })
                        }
                        // If nothing or other token => empty term
                        _ => Ok(Expr::Term {
                            keywords: vec![],
                            field: Some(first),
                            required: false,
                            excluded: false,
                            exact: false,
                        }),
                    }
                } else {
                    // Just a plain ident
                    Ok(Expr::Term {
                        keywords: vec![first],
                        field: None,
                        required: false,
                        excluded: false,
                        exact: false,
                    })
                }
            }
            Some(t) => Err(ParseError::UnexpectedToken(t.clone())),
            None => Err(ParseError::UnexpectedEndOfInput),
        }
    }
}

/// Parse the query string into an AST
pub fn parse_query(input: &str, exact: bool) -> Result<Expr, ParseError> {
    let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";

    if debug_mode {
        println!("DEBUG: parse_query('{input}', exact={exact})");
    }

    // If exact search is enabled, treat the entire query as a single term
    if exact {
        if debug_mode {
            println!("DEBUG: Exact search enabled, treating query as a single term");
        }
        return Ok(Expr::Term {
            keywords: vec![input.to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: true,
        });
    }

    // Tokenize
    let tokens_result = tokenize(input);
    if debug_mode {
        println!("DEBUG: Tokens => {tokens_result:?}");
    }

    // If tokenization fails => fallback
    let tokens = match tokens_result {
        Ok(ts) => ts,
        Err(_) => {
            let cleaned_input = input
                .chars()
                .filter(|&c| c.is_alphanumeric() || c.is_whitespace() || c == '_' || c == '.')
                .collect::<String>();
            if cleaned_input.trim().is_empty() {
                return Err(ParseError::Generic("No valid tokens found".to_string()));
            }
            let keywords = cleaned_input
                .split_whitespace()
                .map(|s| s.to_lowercase())
                .collect::<Vec<String>>();
            return Ok(Expr::Term {
                keywords,
                field: None,
                required: false,
                excluded: false,
                exact: false,
            });
        }
    };

    // Parse into AST
    let mut parser = Parser::new(tokens);
    let parsed = parser.parse_expr();

    if parsed.is_err() {
        // If parse fails => fallback to any Ident tokens
        let idents = parser
            .tokens
            .iter()
            .filter_map(|t| match t {
                Token::Ident(s) => Some(s.clone()),
                _ => None,
            })
            .collect::<Vec<_>>();
        if idents.is_empty() {
            return Err(ParseError::Generic(
                "No valid identifiers found".to_string(),
            ));
        }
        return Ok(Expr::Term {
            keywords: idents,
            field: None,
            required: false,
            excluded: false,
            exact: false,
        });
    }

    // Otherwise success
    Ok(parsed.unwrap())
}

/// Backward compatibility wrapper for parse_query
#[allow(dead_code)]
pub fn parse_query_compat(input: &str) -> Result<Expr, ParseError> {
    parse_query(input, false)
}

// Make parse_query_test available for tests in other modules
#[allow(dead_code)]
pub fn parse_query_test(input: &str) -> Result<Expr, ParseError> {
    parse_query(input, false)
}

#[cfg(test)]
mod tests {
    include!("elastic_query_tests.rs");
    include!("elastic_query_evaluate_tests.rs");
    include!("elastic_query_tokenization_tests.rs");
}