use super::{
language::{CodeLanguage, CodeMode, KeyRule, NumberForms, StringForm},
token,
};
pub(super) fn all() -> Vec<CodeLanguage> {
vec![
CodeLanguage::rust(),
CodeLanguage::toml(),
CodeLanguage::json(),
CodeLanguage::javascript(),
CodeLanguage::typescript(),
CodeLanguage::shell(),
CodeLanguage::python(),
CodeLanguage::css(),
CodeLanguage::html(),
CodeLanguage::xml(),
CodeLanguage::yaml(),
CodeLanguage::sql(),
CodeLanguage::markdown(),
]
}
fn script(name: &str) -> CodeLanguage {
CodeLanguage::new(name)
.words(
token::KEYWORD,
&[
"var",
"let",
"const",
"function",
"class",
"extends",
"new",
"delete",
"typeof",
"instanceof",
"in",
"of",
"void",
"async",
"await",
"import",
"export",
"from",
"default",
"as",
"static",
"get",
"set",
"debugger",
"with",
],
)
.words(
token::KEYWORD_CONTROL,
&[
"if", "else", "for", "while", "do", "switch", "case", "break", "continue", "return", "throw", "try", "catch",
"finally", "yield",
],
)
.words(
token::CONSTANT_LANGUAGE,
&["true", "false", "null", "undefined", "NaN", "Infinity"],
)
.words(token::VARIABLE_BUILTIN, &["this", "super", "arguments"])
.declares(token::FUNCTION, &["function"])
.declares(token::TYPE, &["class", "extends"])
.line_comments(&["//"])
.block_comment("/*", "*/", false)
.doc_block_comment("/**", "*/")
.strings(vec![
StringForm::new("\"", "\""),
StringForm::new("'", "'"),
StringForm::new("`", "`").multiline(true),
])
.numbers(NumberForms {
prefixed: true,
underscores: true,
suffixes: true,
dates: false,
})
.name_chars("_$", "_$")
.capitals_are_types(true)
.calls(true)
.attributes(&["@"])
.regexes(true)
}
impl CodeLanguage {
pub fn rust() -> Self {
Self::new("rust")
.aliases(&["rs"])
.words(
token::KEYWORD,
&[
"as", "async", "await", "const", "crate", "dyn", "enum", "extern", "fn", "impl", "in", "let", "mod", "move",
"mut", "pub", "ref", "static", "struct", "trait", "type", "union", "unsafe", "use", "where",
],
)
.words(
token::KEYWORD_CONTROL,
&[
"break", "continue", "else", "for", "if", "loop", "match", "return", "while", "yield",
],
)
.words(token::CONSTANT_LANGUAGE, &["true", "false"])
.words(token::VARIABLE_BUILTIN, &["self", "Self", "super"])
.words(
token::TYPE_BUILTIN,
&[
"bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128",
"isize", "f32", "f64",
],
)
.declares(token::FUNCTION, &["fn"])
.declares(token::TYPE, &["struct", "enum", "trait", "type", "union"])
.declares(token::NAMESPACE, &["mod"])
.line_comments(&["//"])
.doc_comments(&["///", "//!"])
.block_comment("/*", "*/", true)
.doc_block_comment("/**", "*/")
.doc_block_comment("/*!", "*/")
.strings(vec![
StringForm::new("\"", "\"")
.prefixes(&["r", "br", "cr"])
.escapes(false)
.hashes(true)
.multiline(true),
StringForm::new("\"", "\"").prefixes(&["", "b", "c"]).multiline(true),
])
.characters(true)
.numbers(NumberForms {
prefixed: true,
underscores: true,
suffixes: true,
dates: false,
})
.capitals_are_types(true)
.path("::")
.calls(true)
.macros(true)
.attributes(&["#![", "#["])
}
pub fn toml() -> Self {
Self::new("toml")
.words(token::CONSTANT_LANGUAGE, &["true", "false", "inf", "nan"])
.line_comments(&["#"])
.strings(vec![
StringForm::new("\"\"\"", "\"\"\"").multiline(true),
StringForm::new("'''", "'''").escapes(false).multiline(true),
StringForm::new("\"", "\""),
StringForm::new("'", "'").escapes(false),
])
.numbers(NumberForms {
prefixed: true,
underscores: true,
suffixes: false,
dates: true,
})
.name_chars("_-", "_")
.keys(KeyRule::Equals)
.sections(true)
}
pub fn json() -> Self {
Self::new("json")
.aliases(&["jsonc", "json5"])
.words(token::CONSTANT_LANGUAGE, &["true", "false", "null"])
.line_comments(&["//"])
.block_comment("/*", "*/", false)
.strings(vec![StringForm::new("\"", "\""), StringForm::new("'", "'")])
.keys(KeyRule::Colon)
}
pub fn javascript() -> Self {
script("javascript").aliases(&["js", "jsx", "mjs", "cjs"])
}
pub fn typescript() -> Self {
script("typescript")
.aliases(&["ts", "tsx", "mts", "cts"])
.words(
token::KEYWORD,
&[
"interface",
"type",
"enum",
"implements",
"declare",
"namespace",
"module",
"abstract",
"readonly",
"private",
"protected",
"public",
"keyof",
"infer",
"is",
"satisfies",
"asserts",
"override",
"unique",
],
)
.words(
token::TYPE_BUILTIN,
&[
"string", "number", "boolean", "any", "unknown", "never", "object", "symbol", "bigint",
],
)
.declares(
token::TYPE,
&["class", "extends", "interface", "type", "enum", "implements"],
)
.declares(token::NAMESPACE, &["namespace", "module"])
}
pub fn shell() -> Self {
Self::new("shell")
.aliases(&["sh", "bash", "zsh", "console", "shellscript", "ksh", "fish"])
.words(
token::KEYWORD_CONTROL,
&[
"if", "then", "else", "elif", "fi", "case", "esac", "for", "while", "until", "do", "done", "select", "time",
"!",
],
)
.words(
token::KEYWORD,
&[
"in", "function", "return", "local", "export", "readonly", "declare", "unset", "source", "alias", "exit",
],
)
.declares(token::FUNCTION, &["function"])
.line_comments(&["#"])
.comment_after_space(true)
.strings(vec![
StringForm::new("\"", "\"").multiline(true),
StringForm::new("'", "'").escapes(false).multiline(true),
])
.numbers(NumberForms {
prefixed: false,
underscores: false,
suffixes: false,
dates: false,
})
.name_chars("-./_:@%+,~", "-./_~")
.variables(true)
.commands(true)
.commands_after(&[
"if", "then", "else", "elif", "do", "while", "until", "time", "exec", "sudo", "!",
])
.operators("=|&<>!")
}
pub fn python() -> Self {
let plain = ["", "b", "f", "u", "fb", "bf"];
let raw = ["r", "br", "rb", "fr", "rf"];
Self::new("python")
.aliases(&["py", "python3", "pyi"])
.words(
token::KEYWORD,
&[
"and", "as", "assert", "async", "await", "class", "def", "del", "from", "global", "import", "in", "is",
"lambda", "nonlocal", "not", "or", "pass",
],
)
.words(
token::KEYWORD_CONTROL,
&[
"break", "case", "continue", "elif", "else", "except", "finally", "for", "if", "match", "raise", "return",
"try", "while", "with", "yield",
],
)
.words(token::CONSTANT_LANGUAGE, &["True", "False", "None"])
.words(token::VARIABLE_BUILTIN, &["self", "cls"])
.declares(token::FUNCTION, &["def"])
.declares(token::TYPE, &["class"])
.line_comments(&["#"])
.prefixes_any_case(true)
.strings(vec![
StringForm::new("\"\"\"", "\"\"\"")
.prefixes(&raw)
.escapes(false)
.multiline(true),
StringForm::new("'''", "'''")
.prefixes(&raw)
.escapes(false)
.multiline(true),
StringForm::new("\"\"\"", "\"\"\"").prefixes(&plain).multiline(true),
StringForm::new("'''", "'''").prefixes(&plain).multiline(true),
StringForm::new("\"", "\"").prefixes(&raw).escapes(false),
StringForm::new("'", "'").prefixes(&raw).escapes(false),
StringForm::new("\"", "\"").prefixes(&plain),
StringForm::new("'", "'").prefixes(&plain),
])
.numbers(NumberForms {
prefixed: true,
underscores: true,
suffixes: true,
dates: false,
})
.capitals_are_types(true)
.calls(true)
.attributes(&["@"])
}
pub fn css() -> Self {
Self::new("css")
.aliases(&["scss", "less"])
.block_comment("/*", "*/", false)
.strings(vec![StringForm::new("\"", "\""), StringForm::new("'", "'")])
.numbers(NumberForms {
prefixed: false,
underscores: false,
suffixes: true,
dates: false,
})
.name_chars("-_", "-_")
.keys(KeyRule::Declaration)
.selectors(true)
.at_words(true)
.calls(true)
.operators("=+*/>~:,")
}
pub fn html() -> Self {
Self::new("html")
.aliases(&["htm", "xhtml", "vue", "svelte"])
.mode(CodeMode::Markup)
.block_comment("<!--", "-->", false)
.name_chars("-:._", "_:")
}
pub fn xml() -> Self {
Self::new("xml")
.aliases(&["svg", "plist", "xsd", "xsl", "rss", "atom"])
.mode(CodeMode::Markup)
.block_comment("<!--", "-->", false)
.strings(vec![StringForm::new("<![CDATA[", "]]>").escapes(false).multiline(true)])
.name_chars("-:._", "_:")
}
pub fn yaml() -> Self {
Self::new("yaml")
.aliases(&["yml"])
.words(
token::CONSTANT_LANGUAGE,
&[
"true", "false", "True", "False", "TRUE", "FALSE", "null", "Null", "NULL", "yes", "no", "on", "off",
],
)
.line_comments(&["#"])
.comment_after_space(true)
.strings(vec![
StringForm::new("\"", "\"").multiline(true),
StringForm::new("'", "'").escapes(false).doubled(true).multiline(true),
])
.numbers(NumberForms {
prefixed: true,
underscores: true,
suffixes: false,
dates: true,
})
.name_chars("-_./", "_/")
.keys(KeyRule::Colon)
.block_scalars(true)
.operators("-:?|>&*!~,")
}
pub fn sql() -> Self {
Self::new("sql")
.aliases(&[
"postgres",
"postgresql",
"psql",
"mysql",
"sqlite",
"plsql",
"tsql",
"mssql",
])
.words_any_case(true)
.words(
token::KEYWORD,
&[
"select",
"from",
"where",
"and",
"or",
"not",
"insert",
"into",
"values",
"update",
"set",
"delete",
"create",
"table",
"drop",
"alter",
"add",
"column",
"index",
"view",
"primary",
"key",
"foreign",
"references",
"unique",
"default",
"constraint",
"join",
"inner",
"left",
"right",
"full",
"outer",
"cross",
"on",
"as",
"group",
"by",
"order",
"having",
"limit",
"offset",
"distinct",
"union",
"all",
"exists",
"in",
"between",
"like",
"ilike",
"is",
"asc",
"desc",
"with",
"recursive",
"returning",
"conflict",
"do",
"nothing",
"cascade",
"database",
"schema",
"if",
"begin",
"commit",
"rollback",
"transaction",
"grant",
"revoke",
"trigger",
"function",
"procedure",
"declare",
"replace",
"temporary",
"temp",
"using",
"natural",
"over",
"partition",
"window",
"lateral",
"fetch",
"first",
"next",
"rows",
"only",
"check",
"collate",
"explain",
"analyze",
"vacuum",
"pragma",
"autoincrement",
"serial",
],
)
.words(
token::KEYWORD_CONTROL,
&[
"case", "when", "then", "else", "end", "return", "returns", "loop", "while", "for",
],
)
.words(
token::TYPE_BUILTIN,
&[
"int",
"integer",
"bigint",
"smallint",
"tinyint",
"decimal",
"numeric",
"real",
"float",
"double",
"precision",
"char",
"varchar",
"text",
"boolean",
"bool",
"date",
"time",
"timestamp",
"timestamptz",
"interval",
"uuid",
"json",
"jsonb",
"blob",
"bytea",
"money",
],
)
.words(token::CONSTANT_LANGUAGE, &["null", "true", "false"])
.line_comments(&["--"])
.block_comment("/*", "*/", false)
.strings(vec![
StringForm::new("'", "'").escapes(false).doubled(true).multiline(true),
StringForm::new("\"", "\"")
.escapes(false)
.doubled(true)
.name(token::VARIABLE),
StringForm::new("`", "`").escapes(false).name(token::VARIABLE),
])
.numbers(NumberForms {
prefixed: true,
underscores: false,
suffixes: false,
dates: false,
})
.calls(true)
.variables(true)
}
pub fn markdown() -> Self {
Self::new("markdown")
.aliases(&["md", "mdx", "gfm"])
.mode(CodeMode::Markdown)
.block_comment("<!--", "-->", false)
}
}