sqruff_lib_core/parser/segments/
meta.rs

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
use std::fmt::Debug;

use ahash::AHashSet;

use super::base::ErasedSegment;
use crate::dialects::syntax::{SyntaxKind, SyntaxSet};
use crate::errors::SQLParseError;
use crate::parser::context::ParseContext;
use crate::parser::markers::PositionMarker;
use crate::parser::match_result::MatchResult;
use crate::parser::matchable::{Matchable, MatchableTrait};

pub type Indent = MetaSegment;

#[derive(Debug, Clone, PartialEq)]
pub struct MetaSegment {
    id: u32,
    position_marker: Option<PositionMarker>,
    pub(crate) kind: SyntaxKind,
}

impl MetaSegment {
    pub fn from_kind(kind: SyntaxKind) -> Self {
        Self {
            kind,
            position_marker: None,
            id: 0,
        }
    }

    pub fn indent() -> Self {
        Self::from_kind(SyntaxKind::Indent)
    }

    pub fn dedent() -> Self {
        Self::from_kind(SyntaxKind::Dedent)
    }

    pub fn implicit_indent() -> Self {
        Self::from_kind(SyntaxKind::Implicit)
    }
}

impl MatchableTrait for MetaSegment {
    fn elements(&self) -> &[Matchable] {
        &[]
    }

    fn simple(
        &self,
        _parse_context: &ParseContext,
        _crumbs: Option<Vec<&str>>,
    ) -> Option<(AHashSet<String>, SyntaxSet)> {
        None
    }

    fn match_segments(
        &self,
        _segments: &[ErasedSegment],
        _idx: u32,
        _parse_context: &mut ParseContext,
    ) -> Result<MatchResult, SQLParseError> {
        panic!(
            "{} has no match method, it should only be used in a Sequence!",
            std::any::type_name::<Self>()
        );
    }
}