use super::*;
enum Slot {
Word(&'static str),
AnyWord(&'static [&'static str]),
Name,
For,
}
pub(super) struct AdminShape {
slots: &'static [Slot],
not_path_assignment: bool,
}
const USER_OR_ROLE: Slot = Slot::AnyWord(&["user", "role"]);
const fn database_family(guarded: bool) -> [AdminShape; 4] {
[
AdminShape {
slots: &[Slot::Word("database")],
not_path_assignment: guarded,
},
AdminShape {
slots: &[Slot::Word("composite"), Slot::Word("database")],
not_path_assignment: guarded,
},
AdminShape {
slots: &[Slot::Word("alias"), Slot::Name, Slot::For],
not_path_assignment: false,
},
AdminShape {
slots: &[USER_OR_ROLE],
not_path_assignment: guarded,
},
]
}
pub(super) const CLAUSE_START_SHAPES: &[AdminShape] = &[
AdminShape {
slots: &[Slot::Word("start"), Slot::Word("database")],
not_path_assignment: false,
},
AdminShape {
slots: &[Slot::Word("stop"), Slot::Word("database")],
not_path_assignment: false,
},
AdminShape {
slots: &[Slot::AnyWord(&["grant", "deny", "revoke"])],
not_path_assignment: false,
},
AdminShape {
slots: &[Slot::Word("rename"), USER_OR_ROLE],
not_path_assignment: false,
},
AdminShape {
slots: &[
Slot::Word("alter"),
Slot::AnyWord(&["alias", "user", "role"]),
],
not_path_assignment: false,
},
];
pub(super) const CREATE_SHAPES: &[AdminShape] = &database_family(true);
pub(super) const DROP_SHAPES: &[AdminShape] = &database_family(false);
impl<'a> Parser<'a> {
fn slot_matches(&self, slot: &Slot, at: usize) -> bool {
let tok = self.peek_at(at);
match slot {
Slot::Word(word) => Self::token_is_ident(tok, word),
Slot::AnyWord(words) => words.iter().any(|w| Self::token_is_ident(tok, w)),
Slot::Name => tok.ty.is_ident_like(),
Slot::For => tok.ty == TokenType::FOR,
}
}
pub(super) fn at_admin_command(&self, shapes: &[AdminShape], offset: usize) -> bool {
shapes.iter().any(|shape| {
shape
.slots
.iter()
.enumerate()
.all(|(i, slot)| self.slot_matches(slot, offset + i))
&& !(shape.not_path_assignment
&& self.peek_at(offset + shape.slots.len()).ty == TokenType::Equals)
})
}
pub(super) fn reject_unsupported_admin_create(&mut self, start: usize) -> ParseResult<()> {
let or_replace = self.peek_type() == TokenType::OR && self.peek_at_is_ident(1, "replace");
if self.at_admin_command(CREATE_SHAPES, if or_replace { 2 } else { 0 }) {
return Err(self.unsupported_admin_error(start));
}
Ok(())
}
pub(super) fn drop_admin_target_words() -> Vec<String> {
DROP_SHAPES
.iter()
.flat_map(|shape| match shape.slots.first() {
Some(Slot::Word(word)) => vec![word.to_ascii_uppercase()],
Some(Slot::AnyWord(words)) => {
words.iter().map(|w| w.to_ascii_uppercase()).collect()
}
_ => Vec::new(),
})
.collect()
}
pub(in crate::parser) fn unsupported_admin_error(&mut self, start_pos: usize) -> ParseError {
while !matches!(self.peek_type(), TokenType::EOF | TokenType::Semicolon) {
self.next_token();
}
let text = self.extract_source_text(start_pos, self.pos).expect(
"invariant: unsupported_admin_error start_pos indexes the command's first token, \
so its source slice always exists",
);
let tok = &self.tokens[start_pos];
ParseError::new(text, tok.line, tok.col)
.with_source(self.source)
.with_detail(UNSUPPORTED_ADMINISTRATION_DETAIL)
}
pub(in crate::parser) fn unsupported_admin_clause(
&mut self,
start_pos: usize,
) -> ParseResult<Clause> {
Err(self.unsupported_admin_error(start_pos))
}
}