use tower_lsp::lsp_types::Position;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TokenType {
Keyword,
Identifier,
String,
Number,
Operator,
Delimiter,
Comment,
Whitespace,
Unknown,
}
#[derive(Debug, Clone)]
pub struct Token {
pub token_type: TokenType,
pub text: String,
pub position: Position,
pub length: u32,
}
impl Token {
pub fn new(token_type: TokenType, text: String, position: Position) -> Self {
let length = text.len() as u32;
Self {
token_type,
text,
position,
length,
}
}
}
pub struct Keywords;
impl Keywords {
pub fn all() -> Vec<&'static str> {
vec![
"SELECT",
"INSERT",
"UPDATE",
"DELETE",
"REPLACE",
"CREATE",
"DROP",
"ALTER",
"TRUNCATE",
"RENAME",
"TABLE",
"INDEX",
"VIEW",
"DATABASE",
"SCHEMA",
"JOIN",
"INNER",
"LEFT",
"RIGHT",
"FULL",
"OUTER",
"CROSS",
"ON",
"WHERE",
"HAVING",
"AND",
"OR",
"NOT",
"IN",
"LIKE",
"BETWEEN",
"IS",
"NULL",
"GROUP",
"BY",
"ORDER",
"ASC",
"DESC",
"LIMIT",
"OFFSET",
"FETCH",
"COUNT",
"SUM",
"AVG",
"MAX",
"MIN",
"DISTINCT",
"AS",
"FROM",
"INTO",
"SET",
"VALUES",
"UNION",
"ALL",
"EXISTS",
"CASE",
"WHEN",
"THEN",
"ELSE",
"END",
"IF",
"ELSEIF",
"ELSE",
"ENDIF",
"FOR",
"WHILE",
"LOOP",
"END",
"DECLARE",
"BEGIN",
"COMMIT",
"ROLLBACK",
"TRANSACTION",
"GRANT",
"REVOKE",
"PRIVILEGES",
]
}
pub fn is_keyword(text: &str) -> bool {
Self::all().contains(&text.to_uppercase().as_str())
}
pub fn mysql() -> Vec<&'static str> {
vec![
"AUTO_INCREMENT",
"ENGINE",
"CHARSET",
"COLLATE",
"SHOW",
"DESCRIBE",
"EXPLAIN",
"USE",
"LOCK",
"UNLOCK",
"TABLES",
]
}
pub fn postgres() -> Vec<&'static str> {
vec![
"ILIKE",
"SIMILAR",
"TO",
"ARRAY",
"JSONB",
"RETURNING",
"WITH",
"RECURSIVE",
]
}
pub fn hive() -> Vec<&'static str> {
vec![
"PARTITION",
"PARTITIONED",
"CLUSTERED",
"SORTED",
"STORED",
"AS",
"TEXTFILE",
"ORCFILE",
"PARQUET",
"LATERAL",
"VIEW",
"EXPLODE",
]
}
}
pub struct Operators;
impl Operators {
pub fn all() -> Vec<&'static str> {
vec![
"+", "-", "*", "/", "%", "=", "!=", "<>", "<", ">", "<=", ">=", "<=>", "AND", "OR", "NOT", "||", "&&", "::", "->", "->>", "#>", "#>>",
]
}
pub fn is_operator(text: &str) -> bool {
Self::all().contains(&text)
}
}
pub struct Delimiters;
impl Delimiters {
pub fn all() -> Vec<&'static str> {
vec![",", ";", ".", "(", ")", "[", "]", "{", "}", ":"]
}
pub fn is_delimiter(text: &str) -> bool {
Self::all().contains(&text)
}
}