Skip to main content

syntax_std/
lib.rs

1//! The grammars bezel links in, as one provider for `syntax::registry`.
2//!
3//! [`install`] puts every row this build carries into the registry. Nothing
4//! registers itself — an app calls `install` once, and may register more from
5//! anywhere else before or after.
6
7use syntax::{
8    lang::{Grammar, Lang},
9    registry::{self, Entry},
10};
11
12/// Register every grammar this build carries. Calling it twice replaces the
13/// same rows rather than duplicating them.
14pub fn install() {
15    for lang in LANGS {
16        registry::register(Entry {
17            name: lang.name,
18            aliases: lang.aliases,
19            files: files_for(lang.name),
20            lang: Some(lang),
21        });
22    }
23}
24
25fn files_for(name: &str) -> &'static [&'static str] {
26    FILES
27        .iter()
28        .find(|(held, _)| *held == name)
29        .map(|(_, files)| *files)
30        .unwrap_or(&[])
31}
32
33/// File names and extensions for the grammars here. Fence aliases live on each
34/// [`Lang`]; these are what a path is matched against.
35#[rustfmt::skip]
36const FILES: &[(&str, &[&str])] = &[
37    ("bash", &[".bash_profile", ".bashrc", ".profile", ".zshrc", "bash", "sh", "zsh"]),
38    ("go", &["go"]),
39    ("json", &["json", "jsonc"]),
40    ("python", &["py", "pyi"]),
41    ("rust", &["rs"]),
42    ("toml", &["toml"]),
43    ("tsx", &["cjs", "jsx", "mjs", "tsx"]),
44    ("typescript", &["cts", "mts", "ts"]),
45];
46
47#[cfg(feature = "rust")]
48static RUST: Lang = Lang::new(
49    "rust",
50    &["rust", "rs"],
51    Grammar::Native(tree_sitter_rust::LANGUAGE),
52    include_str!("../queries/rust.scm"),
53);
54#[cfg(feature = "python")]
55static PYTHON: Lang = Lang::new(
56    "python",
57    &["python", "py"],
58    Grammar::Native(tree_sitter_python::LANGUAGE),
59    include_str!("../queries/python.scm"),
60);
61#[cfg(feature = "typescript")]
62static TYPESCRIPT: Lang = Lang::new(
63    "typescript",
64    &["typescript", "ts"],
65    Grammar::Native(tree_sitter_typescript::LANGUAGE_TYPESCRIPT),
66    include_str!("../queries/typescript.scm"),
67);
68/// JavaScript rides the TSX grammar: TSX parses JS, and a separate grammar plus
69/// query would buy only the `<`-ambiguity edge cases that JSX and type
70/// assertions disagree on — which a highlighted sample does not hinge on.
71#[cfg(feature = "typescript")]
72static TSX: Lang = Lang::new(
73    "tsx",
74    &["tsx", "jsx", "javascript", "js"],
75    Grammar::Native(tree_sitter_typescript::LANGUAGE_TSX),
76    include_str!("../queries/tsx.scm"),
77);
78#[cfg(feature = "json")]
79static JSON: Lang = Lang::new(
80    "json",
81    &["json", "jsonc"],
82    Grammar::Native(tree_sitter_json::LANGUAGE),
83    include_str!("../queries/json.scm"),
84);
85#[cfg(feature = "go")]
86static GO: Lang = Lang::new(
87    "go",
88    &["go", "golang"],
89    Grammar::Native(tree_sitter_go::LANGUAGE),
90    include_str!("../queries/go.scm"),
91);
92#[cfg(feature = "bash")]
93static BASH: Lang = Lang::new(
94    "bash",
95    &["bash", "sh", "shell", "zsh", "console"],
96    Grammar::Native(tree_sitter_bash::LANGUAGE),
97    include_str!("../queries/bash.scm"),
98);
99#[cfg(feature = "toml")]
100static TOML: Lang = Lang::new(
101    "toml",
102    &["toml"],
103    Grammar::Native(tree_sitter_toml_ng::LANGUAGE),
104    include_str!("../queries/toml.scm"),
105);
106
107/// A slice rather than an array: its length is whatever the enabled features
108/// add up to, and an app that highlights one language compiles one grammar.
109pub static LANGS: &[&Lang] = &[
110    #[cfg(feature = "rust")]
111    &RUST,
112    #[cfg(feature = "python")]
113    &PYTHON,
114    #[cfg(feature = "typescript")]
115    &TYPESCRIPT,
116    #[cfg(feature = "typescript")]
117    &TSX,
118    #[cfg(feature = "json")]
119    &JSON,
120    #[cfg(feature = "go")]
121    &GO,
122    #[cfg(feature = "bash")]
123    &BASH,
124    #[cfg(feature = "toml")]
125    &TOML,
126];