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
use std::borrow::Cow;
use std::ops::Deref;
use std::sync::{Arc, OnceLock};

use ahash::AHashSet;
use itertools::enumerate;
use uuid::Uuid;

use crate::core::dialects::base::Dialect;
use crate::core::errors::SQLParseError;
use crate::core::parser::context::ParseContext;
use crate::core::parser::helpers::trim_non_code_segments;
use crate::core::parser::match_algorithms::{greedy_match, prune_options};
use crate::core::parser::match_result::MatchResult;
use crate::core::parser::matchable::Matchable;
use crate::core::parser::segments::base::{ErasedSegment, Segment};
use crate::core::parser::types::ParseMode;
use crate::helpers::{capitalize, ToMatchable};
use crate::stack::ensure_sufficient_stack;

#[derive(Clone, Debug, Hash)]
#[allow(clippy::derived_hash_with_manual_eq)]
pub struct BaseGrammar {
    elements: Vec<Arc<dyn Matchable>>,
    allow_gaps: bool,
    optional: bool,
    terminators: Vec<Arc<dyn Matchable>>,
    reset_terminators: bool,
    parse_mode: ParseMode,
    cache_key: Uuid,
}

impl PartialEq for BaseGrammar {
    fn eq(&self, other: &Self) -> bool {
        // self.elements == other.elements &&
        self.allow_gaps == other.allow_gaps
            && self.optional == other.optional
            //   && self.terminators == other.terminators
            && self.reset_terminators == other.reset_terminators
            && self.parse_mode == other.parse_mode
            && self.cache_key == other.cache_key
    }
}

impl BaseGrammar {
    pub fn new(
        elements: Vec<Arc<dyn Matchable>>,
        allow_gaps: bool,
        optional: bool,
        terminators: Vec<Arc<dyn Matchable>>,
        reset_terminators: bool,
        parse_mode: ParseMode,
    ) -> Self {
        let cache_key = Uuid::new_v4();

        Self {
            elements,
            allow_gaps,
            optional,
            terminators,
            reset_terminators,
            parse_mode,
            cache_key,
        }
    }

    // Placeholder for the _resolve_ref method
    fn _resolve_ref(elem: Arc<dyn Matchable>) -> Arc<dyn Matchable> {
        // Placeholder implementation
        elem
    }
}

impl Segment for BaseGrammar {}

#[allow(unused_variables)]
impl Matchable for BaseGrammar {
    fn is_optional(&self) -> bool {
        self.optional
    }

    fn simple(
        &self,
        parse_context: &ParseContext,
        crumbs: Option<Vec<&str>>,
    ) -> Option<(AHashSet<String>, AHashSet<String>)> {
        // Placeholder implementation
        None
    }

    fn match_segments(
        &self,
        segments: &[ErasedSegment],
        parse_context: &mut ParseContext,
    ) -> Result<MatchResult, SQLParseError> {
        // Placeholder implementation
        Ok(MatchResult::new(Vec::new(), Vec::new()))
    }

    fn cache_key(&self) -> Option<Uuid> {
        Some(self.cache_key)
    }
}

#[derive(Clone)]
#[allow(clippy::derived_hash_with_manual_eq)]
pub struct Ref {
    pub(crate) reference: Cow<'static, str>,
    exclude: Option<Arc<dyn Matchable>>,
    terminators: Vec<Arc<dyn Matchable>>,
    reset_terminators: bool,
    allow_gaps: bool,
    optional: bool,
    cache_key: Uuid,
    simple_cache: OnceLock<Option<(AHashSet<String>, AHashSet<String>)>>,
}

impl std::fmt::Debug for Ref {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<Ref: {}{}>", self.reference, if self.is_optional() { " [opt]" } else { "" })
    }
}

impl Ref {
    // Constructor function
    pub fn new(reference: impl Into<Cow<'static, str>>) -> Self {
        Ref {
            reference: reference.into(),
            exclude: None,
            terminators: Vec::new(),
            reset_terminators: false,
            allow_gaps: true,
            optional: false,
            cache_key: Uuid::new_v4(),
            simple_cache: OnceLock::new(),
        }
    }

    pub fn exclude(mut self, exclude: impl ToMatchable) -> Self {
        self.exclude = exclude.to_matchable().into();
        self
    }

    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }

    // Method to get the referenced element
    fn _get_elem(&self, dialect: &Dialect) -> Arc<dyn Matchable> {
        dialect.r#ref(&self.reference)
    }

    // Static method to create a Ref instance for a keyword
    pub fn keyword(keyword: &str) -> Self {
        let name = capitalize(keyword) + "KeywordSegment";
        Ref::new(name)
    }
}

impl PartialEq for Ref {
    fn eq(&self, other: &Self) -> bool {
        self.reference == other.reference
            && self.reset_terminators == other.reset_terminators
            && self.allow_gaps == other.allow_gaps
            && self.optional == other.optional
    }
}

impl Eq for Ref {}

impl Segment for Ref {}

impl Matchable for Ref {
    fn is_optional(&self) -> bool {
        self.optional
    }

    fn simple(
        &self,
        parse_context: &ParseContext,
        crumbs: Option<Vec<&str>>,
    ) -> Option<(AHashSet<String>, AHashSet<String>)> {
        self.simple_cache
            .get_or_init(|| {
                if let Some(ref c) = crumbs {
                    if c.contains(&self.reference.deref()) {
                        let loop_string = c.join(" -> ");
                        panic!("Self referential grammar detected: {}", loop_string);
                    }
                }

                let mut new_crumbs = crumbs.unwrap_or_default();
                new_crumbs.push(&self.reference);

                self._get_elem(parse_context.dialect()).simple(parse_context, Some(new_crumbs))
            })
            .clone()
    }

    fn match_segments(
        &self,
        segments: &[ErasedSegment],
        parse_context: &mut ParseContext,
    ) -> Result<MatchResult, SQLParseError> {
        // Implement the logic for `_get_elem`
        let elem = self._get_elem(parse_context.dialect());

        // Check the exclude condition
        if let Some(exclude) = &self.exclude {
            let ctx = parse_context.deeper_match(
                &format!("{}-Exclude", self.reference),
                self.reset_terminators,
                &self.terminators,
                None,
                |this| {
                    if !exclude
                        .match_segments(segments, this)
                        .map_err(|e| dbg!(e))
                        .map_or(false, |match_result| !match_result.has_match())
                    {
                        return Some(MatchResult::from_unmatched(segments.to_vec()));
                    }

                    None
                },
            );

            if let Some(ctx) = ctx {
                return Ok(ctx);
            }
        }

        ensure_sufficient_stack(|| {
            // Match against that. NB We're not incrementing the match_depth here.
            // References shouldn't really count as a depth of match.
            parse_context.deeper_match(
                &self.reference,
                self.reset_terminators,
                &self.terminators,
                None,
                |this| elem.match_segments(segments, this),
            )
        })
    }

    fn cache_key(&self) -> Option<Uuid> {
        Some(self.cache_key)
    }
}

#[derive(Clone, Debug, Hash)]
#[allow(clippy::derived_hash_with_manual_eq)]
pub struct Anything {
    terminators: Vec<Arc<dyn Matchable>>,
}

impl PartialEq for Anything {
    #[allow(unused_variables)]
    fn eq(&self, other: &Self) -> bool {
        unimplemented!()
    }
}

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

impl Anything {
    pub fn new() -> Self {
        Self { terminators: Vec::new() }
    }

    pub fn terminators(mut self, terminators: Vec<Arc<dyn Matchable>>) -> Self {
        self.terminators = terminators;
        self
    }
}

impl Segment for Anything {}

impl Matchable for Anything {
    fn match_segments(
        &self,
        segments: &[ErasedSegment],
        parse_context: &mut ParseContext,
    ) -> Result<MatchResult, SQLParseError> {
        if self.terminators.is_empty() {
            return Ok(MatchResult::from_matched(segments.to_vec()));
        }

        greedy_match(segments.to_vec(), parse_context, self.terminators.clone(), false)
    }
}

#[derive(Clone, Debug, PartialEq, Hash)]
pub struct Nothing {}

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

impl Nothing {
    pub fn new() -> Self {
        Self {}
    }
}

impl Segment for Nothing {}

impl Matchable for Nothing {
    fn match_segments(
        &self,
        segments: &[ErasedSegment],
        _parse_context: &mut ParseContext,
    ) -> Result<MatchResult, SQLParseError> {
        Ok(MatchResult::from_unmatched(segments.to_vec()))
    }
}

pub fn longest_trimmed_match(
    mut segments: &[ErasedSegment],
    matchers: Vec<Arc<dyn Matchable>>,
    parse_context: &mut ParseContext,
    trim_noncode: bool,
) -> Result<(MatchResult, Option<Arc<dyn Matchable>>), SQLParseError> {
    // Have we been passed an empty list?
    if segments.is_empty() {
        return Ok((MatchResult::from_empty(), None));
    }
    // If presented with no options, return no match
    else if matchers.is_empty() {
        return Ok((MatchResult::from_unmatched(segments.to_vec()), None));
    }

    let available_options = prune_options(&matchers, segments, parse_context);
    if available_options.is_empty() {
        return Ok((MatchResult::from_unmatched(segments.to_vec()), None));
    }

    let mut pre_nc = &[][..];
    let mut post_nc = &[][..];

    if trim_noncode {
        (pre_nc, segments, post_nc) = trim_non_code_segments(segments);
    }

    let loc_key = (
        segments[0].get_raw().unwrap(),
        segments[0].get_position_marker().unwrap().working_loc(),
        segments[0].get_type(),
        segments.len(),
    );

    let mut best_match_length = 0;
    let mut best_match = None;

    for (_idx, matcher) in enumerate(available_options) {
        let matcher_key = matcher.cache_key();

        let match_result = if let Some(matcher_key) = matcher_key {
            match parse_context.check_parse_cache(loc_key.clone(), matcher_key) {
                Some(match_result) => match_result,
                None => {
                    let match_result = matcher.match_segments(segments, parse_context)?;
                    parse_context.put_parse_cache(
                        loc_key.clone(),
                        matcher_key,
                        match_result.clone(),
                    );
                    match_result
                }
            }
        } else {
            let match_result = matcher.match_segments(segments, parse_context)?;
            if let Some(matcher_key) = matcher_key {
                parse_context.put_parse_cache(loc_key.clone(), matcher_key, match_result.clone());
            }
            match_result
        };

        // No match. Skip this one.
        if !match_result.has_match() {
            continue;
        }

        if match_result.is_complete() {
            // Just return it! (WITH THE RIGHT OTHER STUFF)
            return if trim_noncode {
                let mut matched_segments = pre_nc.to_vec();
                matched_segments.extend(match_result.matched_segments);
                matched_segments.extend(post_nc.to_vec());

                Ok((MatchResult::from_matched(matched_segments), matcher.into()))
            } else {
                Ok((match_result, matcher.into()))
            };
        } else if match_result.has_match()
            && match_result.trimmed_matched_length() > best_match_length
        {
            best_match_length = match_result.trimmed_matched_length();
            best_match = (match_result, matcher).into();
        }
    }

    // If we get here, then there wasn't a complete match. If we
    // has a best_match, return that.
    if best_match_length > 0 {
        let (match_result, matchable) = best_match.unwrap();
        return if trim_noncode {
            let mut matched_segments = pre_nc.to_vec();
            matched_segments.extend(match_result.matched_segments);

            let mut unmatched_segments = match_result.unmatched_segments;
            unmatched_segments.extend(post_nc.iter().cloned());

            Ok((MatchResult { matched_segments, unmatched_segments }, matchable.into()))
        } else {
            Ok((match_result, matchable.into()))
        };
    }

    // If no match at all, return nothing
    Ok((MatchResult::from_unmatched(segments.to_vec()), None))
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;
    use crate::core::parser::grammar::anyof::one_of;
    use crate::core::parser::grammar::sequence::Sequence;
    use crate::core::parser::parsers::StringParser;
    use crate::core::parser::segments::keyword::KeywordSegment;
    use crate::core::parser::segments::test_functions::{
        fresh_ansi_dialect, generate_test_segments_func, make_result_tuple, test_segments,
    };
    use crate::helpers::ToErasedSegment;

    #[test]
    fn test__parser__grammar__ref_eq() {
        // Assuming Ref implements Clone and PartialEq
        let r1 = Ref::new("foo".to_string());
        let r2 = Ref::new("foo".to_string());

        // Rust does not directly compare object identities like Python's `is`,
        // but we can ensure they are not the same object by comparing memory addresses
        assert_ne!(&r1 as *const _, &r2 as *const _);
        assert_eq!(r1, r2);

        // For lists, we use Vec in Rust
        let mut check_list = vec![r2.clone()];

        // In Rust, we use `contains` to check for presence in a Vec
        assert!(check_list.contains(&r1));

        // Finding the index of an item in a Vec
        let index = check_list.iter().position(|x| *x == r1).expect("Item not found");
        assert_eq!(index, 0);

        // Removing an item from a Vec
        check_list.retain(|x| *x != r1);
        assert!(!check_list.contains(&r1));
    }

    #[test]
    fn test__parser__grammar__ref_repr() {
        // Assuming that Ref has a constructor that accepts a &str and an optional bool
        let r1 = Ref::new("foo".to_string());
        assert_eq!(format!("{:?}", r1), "<Ref: foo>");

        let r2 = Ref::new("bar".to_string()).optional();
        assert_eq!(format!("{:?}", r2), "<Ref: bar [opt]>");
    }

    #[test]
    fn test__parser__grammar_ref_exclude() {
        // Assuming 'Ref' and 'NakedIdentifierSegment' are defined elsewhere
        let ni = Ref::new("NakedIdentifierSegment".to_string()).exclude(Ref::keyword("ABS"));

        // Assuming 'generate_test_segments' and 'fresh_ansi_dialect' are implemented
        // elsewhere
        let ts = generate_test_segments_func(vec!["ABS", "ABSOLUTE"]);
        let dialect = fresh_ansi_dialect();
        let mut ctx = ParseContext::new(&dialect, <_>::default());

        // Assert ABS does not match, due to the exclude
        assert!(ni.match_segments(&[ts[0].clone()], &mut ctx).unwrap().matched_segments.is_empty());

        // Assert ABSOLUTE does match
        assert!(
            !ni.match_segments(&[ts[1].clone()], &mut ctx).unwrap().matched_segments.is_empty()
        );
    }

    #[test]
    fn test_parser_grammar_nothing() {
        let dialect = fresh_ansi_dialect();
        let mut ctx = ParseContext::new(&dialect, <_>::default());

        assert!(
            Nothing::new()
                .match_segments(&test_segments(), &mut ctx)
                .unwrap()
                .matched_segments
                .is_empty()
        );
    }

    #[test]
    fn test__parser__grammar__base__longest_trimmed_match__basic() {
        let test_segments = test_segments();
        let cases = [
            // Matching the first element of the list
            (0..test_segments.len(), "bar", false, (0..1).into()),
            // Matching with a bit of whitespace before
            (1..test_segments.len(), "foo", true, (1..3).into()),
            // Matching with a bit of whitespace before (not trim_noncode)
            (1..test_segments.len(), "foo", false, None),
            // Matching with whitespace after
            (0..2, "bar", true, (0..2).into()),
        ];

        let dialect = fresh_ansi_dialect();
        let mut ctx = ParseContext::new(&dialect, <_>::default());
        for (segments_slice, matcher_keyword, trim_noncode, result_slice) in cases {
            let matchers = vec![
                StringParser::new(
                    matcher_keyword,
                    |segment| {
                        KeywordSegment::new(
                            segment.get_raw().unwrap(),
                            segment.get_position_marker().unwrap().into(),
                        )
                        .to_erased_segment()
                    },
                    None,
                    false,
                    None,
                )
                .to_matchable(),
            ];

            let (m, _) = longest_trimmed_match(
                &test_segments[segments_slice],
                matchers,
                &mut ctx,
                trim_noncode,
            )
            .unwrap();

            let expected_result =
                make_result_tuple(result_slice, &[matcher_keyword], &test_segments);

            assert_eq!(expected_result, m.matched_segments);
        }
    }

    #[test]
    fn test__parser__grammar__base__longest_trimmed_match__adv() {
        let bs = Arc::new(StringParser::new(
            "bar",
            |segment| {
                KeywordSegment::new(
                    segment.get_raw().unwrap(),
                    segment.get_position_marker().unwrap().into(),
                )
                .to_erased_segment()
            },
            None,
            false,
            None,
        )) as Arc<dyn Matchable>;

        let fs = Arc::new(StringParser::new(
            "foo",
            |segment| {
                KeywordSegment::new(
                    segment.get_raw().unwrap(),
                    segment.get_position_marker().unwrap().into(),
                )
                .to_erased_segment()
            },
            None,
            false,
            None,
        )) as Arc<dyn Matchable>;

        let matchers: Vec<Arc<dyn Matchable>> = vec![
            bs.clone(),
            fs.clone(),
            Arc::new(Sequence::new(vec![bs.clone(), fs.clone()])),
            Arc::new(one_of(vec![bs.clone(), fs.clone()])),
            Arc::new(Sequence::new(vec![bs, fs])),
        ];

        let dialect = fresh_ansi_dialect();
        let mut ctx = ParseContext::new(&dialect, <_>::default());
        // Matching the first element of the list
        let (match_result, matcher) =
            longest_trimmed_match(&test_segments(), matchers.clone(), &mut ctx, true).unwrap();

        // Check we got a match
        assert!(match_result.has_match());
        // Check we got the right one.
        assert!(matcher.unwrap().dyn_eq(&*matchers[2]));
        // And it matched the first three segments
        assert_eq!(match_result.len(), 3);
    }
}