sqruff_lib_core/
dialects.rs

1pub mod common;
2pub mod init;
3pub mod syntax;
4
5use std::borrow::Cow;
6use std::collections::hash_map::Entry;
7use std::fmt::Debug;
8
9use ahash::{AHashMap, AHashSet};
10
11use crate::dialects::init::DialectKind;
12use crate::dialects::syntax::SyntaxKind;
13use crate::helpers::ToMatchable;
14use crate::parser::lexer::{Lexer, Matcher};
15use crate::parser::matchable::Matchable;
16use crate::parser::parsers::StringParser;
17use crate::parser::types::DialectElementType;
18
19#[derive(Debug, Clone, Default)]
20pub struct Dialect {
21    pub name: DialectKind,
22    lexer_matchers: Option<Vec<Matcher>>,
23    library: AHashMap<Cow<'static, str>, DialectElementType>,
24    sets: AHashMap<&'static str, AHashSet<&'static str>>,
25    pub bracket_collections: AHashMap<&'static str, AHashSet<BracketPair>>,
26    lexer: Option<Lexer>,
27}
28
29impl PartialEq for Dialect {
30    fn eq(&self, other: &Self) -> bool {
31        self.name == other.name
32    }
33}
34
35impl Dialect {
36    pub fn new() -> Self {
37        Dialect {
38            name: DialectKind::Ansi,
39            ..Default::default()
40        }
41    }
42
43    pub fn name(&self) -> DialectKind {
44        self.name
45    }
46
47    pub fn add(&mut self, iter: impl IntoIterator<Item = (Cow<'static, str>, DialectElementType)>) {
48        self.library.extend(iter);
49    }
50
51    pub fn grammar(&self, name: &str) -> Matchable {
52        match self
53            .library
54            .get(name)
55            .unwrap_or_else(|| panic!("not found {name}"))
56        {
57            DialectElementType::Matchable(matchable) => matchable.clone(),
58            DialectElementType::SegmentGenerator(_) => {
59                unreachable!("Attempted to fetch non grammar [{name}] with `Dialect::grammar`.")
60            }
61        }
62    }
63
64    #[track_caller]
65    pub fn replace_grammar(&mut self, name: &'static str, match_grammar: Matchable) {
66        match self
67            .library
68            .get_mut(name)
69            .unwrap_or_else(|| panic!("Failed to get mutable reference for {name}"))
70        {
71            DialectElementType::Matchable(matchable) => {
72                matchable.as_node_matcher().unwrap().replace(match_grammar);
73            }
74            DialectElementType::SegmentGenerator(_) => {
75                unreachable!("Attempted to fetch non grammar [{name}] with `Dialect::grammar`.")
76            }
77        }
78    }
79
80    pub fn lexer_matchers(&self) -> &[Matcher] {
81        match &self.lexer_matchers {
82            Some(lexer_matchers) => lexer_matchers,
83            None => panic!("Lexing struct has not been set for dialect {self:?}"),
84        }
85    }
86
87    pub fn insert_lexer_matchers(&mut self, lexer_patch: Vec<Matcher>, before: &str) {
88        let mut buff = Vec::new();
89        let mut found = false;
90
91        if self.lexer_matchers.is_none() {
92            panic!("Lexer struct must be defined before it can be patched!");
93        }
94
95        for elem in self.lexer_matchers.take().unwrap() {
96            if elem.name() == before {
97                found = true;
98                for patch in lexer_patch.clone() {
99                    buff.push(patch);
100                }
101                buff.push(elem);
102            } else {
103                buff.push(elem);
104            }
105        }
106
107        if !found {
108            panic!("Lexer struct insert before '{before}' failed because tag never found.");
109        }
110
111        self.lexer_matchers = Some(buff);
112    }
113
114    pub fn patch_lexer_matchers(&mut self, lexer_patch: Vec<Matcher>) {
115        let mut buff = Vec::with_capacity(self.lexer_matchers.as_ref().map_or(0, Vec::len));
116        if self.lexer_matchers.is_none() {
117            panic!("Lexer struct must be defined before it can be patched!");
118        }
119
120        let patch_dict: AHashMap<&'static str, Matcher> = lexer_patch
121            .into_iter()
122            .map(|elem| (elem.name(), elem))
123            .collect();
124
125        for elem in self.lexer_matchers.take().unwrap() {
126            if let Some(patch) = patch_dict.get(elem.name()) {
127                buff.push(patch.clone());
128            } else {
129                buff.push(elem);
130            }
131        }
132
133        self.lexer_matchers = Some(buff);
134    }
135
136    pub fn set_lexer_matchers(&mut self, lexer_matchers: Vec<Matcher>) {
137        self.lexer_matchers = lexer_matchers.into();
138    }
139
140    pub fn sets(&self, label: &str) -> AHashSet<&'static str> {
141        match label {
142            "bracket_pairs" | "angle_bracket_pairs" => {
143                panic!("Use `bracket_sets` to retrieve {label} set.");
144            }
145            _ => (),
146        }
147
148        self.sets.get(label).cloned().unwrap_or_default()
149    }
150
151    pub fn sets_mut(&mut self, label: &'static str) -> &mut AHashSet<&'static str> {
152        assert!(
153            label != "bracket_pairs" && label != "angle_bracket_pairs",
154            "Use `bracket_sets` to retrieve {label} set."
155        );
156
157        match self.sets.entry(label) {
158            Entry::Occupied(entry) => entry.into_mut(),
159            Entry::Vacant(entry) => entry.insert(<_>::default()),
160        }
161    }
162
163    pub fn update_keywords_set_from_multiline_string(
164        &mut self,
165        set_label: &'static str,
166        values: &'static str,
167    ) {
168        let keywords = values.lines().map(str::trim);
169        self.sets_mut(set_label).extend(keywords);
170    }
171
172    pub fn add_keyword_to_set(&mut self, set_label: &'static str, value: &'static str) {
173        self.sets_mut(set_label).insert(value);
174    }
175
176    pub fn bracket_sets(&self, label: &str) -> AHashSet<BracketPair> {
177        assert!(
178            label == "bracket_pairs" || label == "angle_bracket_pairs",
179            "Invalid bracket set. Consider using another identifier instead."
180        );
181
182        self.bracket_collections
183            .get(label)
184            .cloned()
185            .unwrap_or_default()
186    }
187
188    pub fn bracket_sets_mut(&mut self, label: &'static str) -> &mut AHashSet<BracketPair> {
189        assert!(
190            label == "bracket_pairs" || label == "angle_bracket_pairs",
191            "Invalid bracket set. Consider using another identifier instead."
192        );
193
194        self.bracket_collections.entry(label).or_default()
195    }
196
197    pub fn update_bracket_sets(&mut self, label: &'static str, pairs: Vec<BracketPair>) {
198        let set = self.bracket_sets_mut(label);
199        for pair in pairs {
200            set.insert(pair);
201        }
202    }
203
204    #[track_caller]
205    pub fn r#ref(&self, name: &str) -> Matchable {
206        match self.library.get(name) {
207            Some(DialectElementType::Matchable(matchable)) => matchable.clone(),
208            Some(DialectElementType::SegmentGenerator(_)) => {
209                panic!("Unexpected SegmentGenerator while fetching '{name}'");
210            }
211            None => {
212                panic!("Grammar refers to '{name}' which was not found in the dialect.",);
213            }
214        }
215    }
216
217    pub fn expand(&mut self) {
218        // Temporarily take ownership of 'library' from 'self' to avoid borrow checker
219        // errors during mutation.
220        let mut library = std::mem::take(&mut self.library);
221        for element in library.values_mut() {
222            if let DialectElementType::SegmentGenerator(generator) = element {
223                *element = DialectElementType::Matchable(generator.expand(self));
224            }
225        }
226        self.library = library;
227
228        for keyword_set in ["unreserved_keywords", "reserved_keywords"] {
229            if let Some(keywords) = self.sets.get(keyword_set) {
230                for &kw in keywords {
231                    if !self.library.contains_key(kw) {
232                        let parser = StringParser::new(kw, SyntaxKind::Keyword);
233
234                        self.library.insert(
235                            kw.into(),
236                            DialectElementType::Matchable(parser.to_matchable()),
237                        );
238                    }
239                }
240            }
241        }
242
243        self.lexer = Lexer::new(self.lexer_matchers()).into();
244    }
245
246    pub fn lexer(&self) -> &Lexer {
247        self.lexer.as_ref().unwrap()
248    }
249}
250
251pub type BracketPair = (&'static str, &'static str, &'static str, bool);