sqruff_lib_core/parser/grammar/
noncode.rs

1use ahash::AHashSet;
2
3use crate::dialects::syntax::SyntaxSet;
4use crate::errors::SQLParseError;
5use crate::parser::context::ParseContext;
6use crate::parser::match_result::{MatchResult, Span};
7use crate::parser::matchable::{Matchable, MatchableCacheKey, MatchableTrait};
8use crate::parser::segments::ErasedSegment;
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct NonCodeMatcher;
12
13impl MatchableTrait for NonCodeMatcher {
14    fn elements(&self) -> &[Matchable] {
15        &[]
16    }
17
18    fn is_optional(&self) -> bool {
19        // Not optional
20        false
21    }
22
23    fn simple(
24        &self,
25        _parse_context: &ParseContext,
26        _crumbs: Option<Vec<&str>>,
27    ) -> Option<(AHashSet<String>, SyntaxSet)> {
28        None
29    }
30
31    fn match_segments(
32        &self,
33        segments: &[ErasedSegment],
34        idx: u32,
35        _parse_context: &mut ParseContext,
36    ) -> Result<MatchResult, SQLParseError> {
37        let mut matched_idx = idx;
38
39        for i in idx..segments.len() as u32 {
40            if segments[i as usize].is_code() {
41                matched_idx = i;
42                break;
43            }
44        }
45
46        if matched_idx > idx {
47            return Ok(MatchResult {
48                span: Span {
49                    start: idx,
50                    end: matched_idx,
51                },
52                ..Default::default()
53            });
54        }
55
56        Ok(MatchResult::empty_at(idx))
57    }
58
59    fn cache_key(&self) -> MatchableCacheKey {
60        0
61    }
62}