use std::collections::HashSet;
use std::ops::Range;
use ferrokit_core::text::{Color, TextStyle};
#[derive(Clone, Default)]
pub struct Syntax {
keywords: Vec<(HashSet<String>, TextStyle)>,
ignore_case: bool,
strings: Vec<char>,
string_style: TextStyle,
escape: Option<char>,
doubled_quotes: bool,
identifiers: Vec<(char, char)>,
line_comments: Vec<String>,
block_comment: Option<(String, String)>,
comment_style: TextStyle,
numbers: Option<TextStyle>,
}
impl Syntax {
pub fn new() -> Self {
Self::default()
}
pub fn keywords(mut self, words: &[&str], style: TextStyle) -> Self {
let words = words.iter().map(|w| if self.ignore_case { w.to_lowercase() } else { w.to_string() }).collect();
self.keywords.push((words, style));
self
}
pub fn ignore_case(mut self) -> Self {
self.ignore_case = true;
self
}
pub fn strings(mut self, quotes: &str, style: TextStyle) -> Self {
self.strings = quotes.chars().collect();
self.string_style = style;
self
}
pub fn escape(mut self, escape: char) -> Self {
self.escape = Some(escape);
self
}
pub fn doubled_quotes(mut self) -> Self {
self.doubled_quotes = true;
self
}
pub fn quoted_identifiers(mut self, pairs: &[(char, char)]) -> Self {
self.identifiers = pairs.to_vec();
self
}
pub fn line_comment(mut self, prefix: &str, style: TextStyle) -> Self {
self.line_comments.push(prefix.into());
self.comment_style = style;
self
}
pub fn block_comment(mut self, open: &str, close: &str, style: TextStyle) -> Self {
self.block_comment = Some((open.into(), close.into()));
self.comment_style = style;
self
}
pub fn numbers(mut self, style: TextStyle) -> Self {
self.numbers = Some(style);
self
}
pub fn sql() -> Self {
Syntax::new()
.ignore_case()
.keywords(SQL_KEYWORDS, TextStyle::new().color(Color::Pink).bold())
.keywords(SQL_TYPES, TextStyle::new().color(Color::Purple))
.keywords(SQL_FUNCTIONS, TextStyle::new().color(Color::Indigo))
.strings("'", TextStyle::new().color(Color::Red))
.doubled_quotes()
.quoted_identifiers(&[('"', '"'), ('`', '`'), ('[', ']')])
.line_comment("--", TextStyle::new().color(Color::Gray).italic())
.block_comment("/*", "*/", TextStyle::new().color(Color::Gray).italic())
.numbers(TextStyle::new().color(Color::Blue))
}
pub fn rust() -> Self {
Syntax::new()
.keywords(RUST_KEYWORDS, TextStyle::new().color(Color::Pink).bold())
.keywords(RUST_TYPES, TextStyle::new().color(Color::Purple))
.strings("\"", TextStyle::new().color(Color::Red))
.escape('\\')
.line_comment("//", TextStyle::new().color(Color::Gray).italic())
.block_comment("/*", "*/", TextStyle::new().color(Color::Gray).italic())
.numbers(TextStyle::new().color(Color::Blue))
}
pub fn highlight(&self, text: &str) -> Vec<(Range<usize>, TextStyle)> {
let mut runs = Vec::new();
let mut i = 0;
let word = |c: char| c.is_alphanumeric() || c == '_';
while let Some(c) = text[i..].chars().next() {
let rest = &text[i..];
if let Some((open, close)) = self.block_comment.as_ref().filter(|(open, _)| rest.starts_with(open.as_str())) {
let end = rest[open.len()..].find(close.as_str()).map_or(text.len(), |e| i + open.len() + e + close.len());
runs.push((i..end, self.comment_style));
i = end;
continue;
}
if self.line_comments.iter().any(|p| rest.starts_with(p.as_str())) {
let end = rest.find('\n').map_or(text.len(), |e| i + e);
runs.push((i..end, self.comment_style));
i = end;
continue;
}
if self.strings.contains(&c) {
let end = self.close(text, i, c, c);
runs.push((i..end, self.string_style));
i = end;
continue;
}
if let Some(&(open, close)) = self.identifiers.iter().find(|(open, _)| *open == c) {
i = self.close(text, i, open, close);
continue;
}
if word(c) {
let end = rest.find(|ch: char| !word(ch) && ch != '.').map_or(text.len(), |e| i + e);
let mut end = end;
if c.is_ascii_digit() {
while end < text.len() && matches!(text.as_bytes()[end], b'+' | b'-') && matches!(text.as_bytes()[end - 1], b'e' | b'E') {
end = text[end + 1..].find(|ch: char| !ch.is_ascii_alphanumeric() && ch != '.').map_or(text.len(), |e| end + 1 + e);
}
if let Some(style) = self.numbers {
runs.push((i..end, style));
}
} else {
end = rest.find(|ch: char| !word(ch)).map_or(text.len(), |e| i + e);
let w = &text[i..end];
let lower;
let key = if self.ignore_case {
lower = w.to_lowercase();
lower.as_str()
} else {
w
};
let member = text[..i].ends_with('.');
if let Some((_, style)) = self.keywords.iter().find(|(set, _)| set.contains(key)).filter(|_| !member) {
runs.push((i..end, *style));
}
}
i = end;
continue;
}
i += c.len_utf8();
}
runs
}
fn close(&self, text: &str, start: usize, open: char, close: char) -> usize {
let mut chars = text[start + open.len_utf8()..].char_indices().peekable();
let base = start + open.len_utf8();
while let Some((at, ch)) = chars.next() {
if open == close && self.escape == Some(ch) && self.strings.contains(&open) {
chars.next();
} else if ch == close {
if self.doubled_quotes && open == close && chars.peek().is_some_and(|(_, n)| *n == close) {
chars.next();
continue;
}
return base + at + ch.len_utf8();
}
}
text.len()
}
}
const SQL_KEYWORDS: &[&str] = &[
"abort", "action", "add", "after", "all", "alter", "always", "analyze", "and", "as", "asc", "attach", "autoincrement", "before", "begin",
"between", "by", "cascade", "case", "cast", "check", "collate", "column", "commit", "conflict", "constraint", "create", "cross",
"current_date", "current_time", "current_timestamp", "database", "default", "deferred", "delete", "desc", "detach", "distinct", "do",
"drop", "each", "else", "end", "escape", "except", "exclusive", "exists", "explain", "fail", "filter", "first", "following", "for",
"foreign", "from", "full", "generated", "glob", "group", "having", "if", "ignore", "immediate", "in", "index", "indexed", "initially",
"inner", "insert", "instead", "intersect", "into", "is", "isnull", "join", "key", "last", "left", "like", "limit", "match",
"materialized", "natural", "no", "not", "nothing", "notnull", "null", "nulls", "of", "offset", "on", "or", "order", "outer", "over",
"partition", "plan", "pragma", "preceding", "primary", "query", "raise", "range", "recursive", "references", "regexp", "reindex",
"release", "rename", "replace", "restrict", "returning", "right", "rollback", "row", "rows", "savepoint", "select", "set", "strict",
"table", "temp", "temporary", "then", "ties", "to", "transaction", "trigger", "unbounded", "union", "unique", "update", "using",
"vacuum", "values", "view", "virtual", "when", "where", "window", "with", "without", "true", "false",
];
const SQL_TYPES: &[&str] = &["integer", "int", "real", "text", "blob", "numeric", "boolean", "date", "datetime", "varchar", "char", "float", "double", "decimal", "bigint"];
const SQL_FUNCTIONS: &[&str] = &[
"abs", "avg", "char", "coalesce", "count", "date", "datetime", "glob", "group_concat", "hex", "ifnull", "iif", "instr", "json",
"json_extract", "julianday", "length", "lower", "ltrim", "max", "min", "nullif", "printf", "quote", "random", "round", "rtrim",
"sqlite_version", "strftime", "substr", "substring", "sum", "time", "total", "trim", "typeof", "unicode", "upper", "zeroblob",
];
const RUST_KEYWORDS: &[&str] = &[
"as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", "false", "fn", "for", "if", "impl", "in",
"let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct", "super", "trait", "true",
"type", "unsafe", "use", "where", "while",
];
const RUST_TYPES: &[&str] = &[
"bool", "char", "f32", "f64", "i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32", "u64", "u128", "usize", "str", "String",
"Vec", "Option", "Result", "Box", "Rc", "Arc", "Some", "None", "Ok", "Err",
];
#[cfg(test)]
mod tests {
use super::*;
fn spans(syntax: &Syntax, text: &str) -> Vec<(String, TextStyle)> {
syntax.highlight(text).into_iter().map(|(r, s)| (text[r].to_string(), s)).collect()
}
#[test]
fn sql_tokens() {
let sql = Syntax::sql();
let kw = TextStyle::new().color(Color::Pink).bold();
let got = spans(&sql, "SELECT name, count(*) FROM \"select\" WHERE x = 'it''s' AND y > 1.5e-3 -- done\n/* a\nb */ limit 10");
let texts: Vec<&str> = got.iter().map(|(t, _)| t.as_str()).collect();
assert_eq!(texts, ["SELECT", "count", "FROM", "WHERE", "'it''s'", "AND", "1.5e-3", "-- done", "/* a\nb */", "limit", "10"]);
assert_eq!(got[0].1, kw);
assert_eq!(got[1].1, TextStyle::new().color(Color::Indigo));
assert_eq!(got[4].1, TextStyle::new().color(Color::Red));
assert_eq!(got[6].1, TextStyle::new().color(Color::Blue));
}
#[test]
fn unclosed_runs_reach_the_end() {
let sql = Syntax::sql();
assert_eq!(sql.highlight("SELECT 'abc").last().unwrap().0, 7..11);
assert_eq!(sql.highlight("/* open").last().unwrap().0, 0..7);
}
#[test]
fn dotted_names_and_unicode() {
let sql = Syntax::sql();
let got = spans(&sql, "select o.total, 'café' from orders o");
let texts: Vec<&str> = got.iter().map(|(t, _)| t.as_str()).collect();
assert_eq!(texts, ["select", "'café'", "from"]);
}
#[test]
fn rust_escapes() {
let rust = Syntax::rust();
let got = spans(&rust, r#"let s: String = "a \" b"; // c"#);
let texts: Vec<&str> = got.iter().map(|(t, _)| t.as_str()).collect();
assert_eq!(texts, ["let", "String", r#""a \" b""#, "// c"]);
}
}