sqruff_lib_core/parser/
parser.rsuse ahash::AHashMap;
use super::context::ParseContext;
use super::segments::base::{ErasedSegment, Tables};
use crate::dialects::base::Dialect;
use crate::errors::SQLParseError;
use crate::parser::segments::file::FileSegment;
#[derive(Clone)]
pub struct Parser<'a> {
dialect: &'a Dialect,
pub(crate) indentation_config: AHashMap<String, bool>,
}
impl<'a> From<&'a Dialect> for Parser<'a> {
fn from(value: &'a Dialect) -> Self {
Self {
dialect: value,
indentation_config: AHashMap::new(),
}
}
}
impl<'a> Parser<'a> {
pub fn new(dialect: &'a Dialect, indentation_config: AHashMap<String, bool>) -> Self {
Self {
dialect,
indentation_config,
}
}
pub fn dialect(&self) -> &Dialect {
self.dialect
}
pub fn indentation_config(&self) -> &AHashMap<String, bool> {
&self.indentation_config
}
pub fn parse(
&self,
tables: &Tables,
segments: &[ErasedSegment],
filename: Option<String>,
) -> Result<Option<ErasedSegment>, SQLParseError> {
if segments.is_empty() {
return Ok(None);
}
let mut parse_cx: ParseContext = self.into();
let root = FileSegment.root_parse(
tables,
parse_cx.dialect().name,
segments,
&mut parse_cx,
filename,
)?;
super::helpers::check_still_complete(segments, &[root.clone()], &[]);
Ok(root.into())
}
}