use sqlparser::tokenizer::{Location, Token, TokenWithSpan};
pub(crate) fn last_statement_terminator(
tokens: &[TokenWithSpan],
before: Option<Location>,
) -> Option<Location> {
let mut boundary = StatementBoundary::default();
tokens
.iter()
.take_while(|token| before.is_none_or(|end| token.span.end <= end))
.filter(|token| boundary.terminator(&token.token))
.map(|token| token.span.start)
.last()
}
pub(in crate::parse) fn located_chunks(
original: &[TokenWithSpan],
) -> impl Iterator<Item = &[TokenWithSpan]> {
let mut boundary = StatementBoundary::default();
original
.split(move |token| boundary.terminator(&token.token))
.filter(|chunk| {
chunk
.iter()
.any(|token| !matches!(token.token, Token::Whitespace(_)))
})
}
pub(super) fn split_statement_tokens(tokens: Vec<Token>) -> Vec<Vec<Token>> {
let mut statements = Vec::new();
let mut current = Vec::new();
let mut boundary = StatementBoundary::default();
for token in tokens {
if boundary.terminator(&token) {
if has_non_ws(¤t) {
statements.push(std::mem::take(&mut current));
} else {
current.clear();
}
} else {
current.push(token);
}
}
if has_non_ws(¤t) {
statements.push(current);
}
statements
}
#[derive(Default)]
pub(super) struct StatementBoundary {
prefix: u8,
rule: bool,
routine: bool,
saw_begin: bool,
atomic: bool,
atomic_cases: usize,
after_as_or_dot: bool,
depth: usize,
routine_depth: usize,
}
impl StatementBoundary {
pub(super) fn terminator(&mut self, token: &Token) -> bool {
if matches!(token, Token::SemiColon) && (!self.rule || self.depth == 0) && !self.atomic {
*self = Self::default();
return true;
}
if matches!(token, Token::Whitespace(_)) {
return false;
}
let word = |expected: &str| {
matches!(token, Token::Word(word)
if word.quote_style.is_none() && word.value.eq_ignore_ascii_case(expected))
};
if self.atomic {
if !self.after_as_or_dot {
if word("CASE") {
self.atomic_cases += 1;
} else if word("END") {
if self.atomic_cases > 0 {
self.atomic_cases -= 1;
} else {
self.atomic = false;
}
}
}
self.after_as_or_dot = word("AS") || matches!(token, Token::Period);
return false;
}
self.prefix = match self.prefix {
0 if word("CREATE") => 1,
1 if word("OR") => 2,
2 if word("REPLACE") => 3,
1 | 3 if word("RULE") => {
self.rule = true;
4
}
1 | 3 if word("FUNCTION") || word("PROCEDURE") => {
self.routine = true;
4
}
_ => 4,
};
if self.routine {
match token {
Token::LParen => self.routine_depth += 1,
Token::RParen => self.routine_depth = self.routine_depth.saturating_sub(1),
_ => {}
}
if self.saw_begin && self.routine_depth == 0 && word("ATOMIC") {
self.atomic = true;
self.saw_begin = false;
return false;
}
self.saw_begin = word("BEGIN");
}
if self.rule {
match token {
Token::LParen => self.depth += 1,
Token::RParen => self.depth = self.depth.saturating_sub(1),
_ => {}
}
}
false
}
}
pub(super) fn has_non_ws(tokens: &[Token]) -> bool {
tokens
.iter()
.any(|token| !matches!(token, Token::Whitespace(_)))
}
pub(in super::super) fn skip_ws(tokens: &[Token], mut index: usize) -> usize {
while index < tokens.len() && matches!(tokens[index], Token::Whitespace(_)) {
index += 1;
}
index
}
pub(in super::super) fn next_non_ws(tokens: &[Token], start: usize) -> Option<usize> {
let index = skip_ws(tokens, start);
(index < tokens.len()).then_some(index)
}
pub(crate) struct PolicyChunk {
pub(crate) head: std::ops::Range<usize>,
pub(crate) body: Option<std::ops::Range<usize>>,
pub(crate) next: usize,
}
pub(crate) fn next_policy_chunk(
tokens: &[TokenWithSpan],
parser: &sqlparser::parser::Parser<'_>,
start: usize,
end: usize,
) -> PolicyChunk {
let mut boundary = StatementBoundary::default();
for (index, token) in tokens.iter().enumerate().take(end).skip(start) {
if boundary.terminator(&token.token) {
return PolicyChunk {
head: start..index,
body: None,
next: index + 1,
};
}
if boundary.atomic {
let begin = (start..index)
.rfind(|&at| !matches!(tokens[at].token, Token::Whitespace(_)))
.unwrap_or(index);
let close = crate::source::atomic_body_end(parser, begin).min(end);
return PolicyChunk {
head: start..begin,
body: Some(index + 1..close),
next: (close + 1).min(end),
};
}
}
PolicyChunk {
head: start..end,
body: None,
next: end,
}
}