sqruff_lib_core/
parser.rs1pub mod context;
2pub mod grammar;
3pub mod lexer;
4pub mod lookahead;
5pub mod markers;
6pub mod match_algorithms;
7pub mod match_result;
8pub mod matchable;
9pub mod node_matcher;
10pub mod parsers;
11pub mod segments;
12pub mod types;
13
14use ahash::AHashMap;
15
16use crate::dialects::Dialect;
17use crate::errors::SQLParseError;
18use crate::parser::segments::file::FileSegment;
19use context::ParseContext;
20use segments::{ErasedSegment, Tables};
21
22#[derive(Clone)]
23pub struct Parser<'a> {
24 dialect: &'a Dialect,
25 pub(crate) indentation_config: AHashMap<String, bool>,
26}
27
28impl<'a> From<&'a Dialect> for Parser<'a> {
29 fn from(value: &'a Dialect) -> Self {
30 Self {
31 dialect: value,
32 indentation_config: AHashMap::new(),
33 }
34 }
35}
36
37impl<'a> Parser<'a> {
38 pub fn new(dialect: &'a Dialect, indentation_config: AHashMap<String, bool>) -> Self {
39 Self {
40 dialect,
41 indentation_config,
42 }
43 }
44
45 pub fn dialect(&self) -> &Dialect {
46 self.dialect
47 }
48
49 pub fn indentation_config(&self) -> &AHashMap<String, bool> {
50 &self.indentation_config
51 }
52
53 pub fn parse(
54 &self,
55 tables: &Tables,
56 segments: &[ErasedSegment],
57 ) -> Result<Option<ErasedSegment>, SQLParseError> {
58 if segments.is_empty() {
59 return Ok(None);
63 }
64
65 let mut parse_cx: ParseContext = self.into();
69
70 let root =
74 FileSegment.root_parse(tables, parse_cx.dialect().name, segments, &mut parse_cx)?;
75
76 #[cfg(debug_assertions)]
77 {
78 let join_segments_raw = |segments: &[ErasedSegment]| {
80 smol_str::SmolStr::from_iter(segments.iter().map(|s| s.raw().as_str()))
81 };
82
83 pretty_assertions::assert_eq!(&join_segments_raw(segments), root.raw());
84 }
85
86 Ok(root.into())
87 }
88}