summaryrefslogtreecommitdiff
path: root/core/grammar_manifest.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/grammar_manifest.zig')
-rw-r--r--core/grammar_manifest.zig196
1 files changed, 196 insertions, 0 deletions
diff --git a/core/grammar_manifest.zig b/core/grammar_manifest.zig
new file mode 100644
index 00000000..711c31a4
--- /dev/null
+++ b/core/grammar_manifest.zig
@@ -0,0 +1,196 @@
+//! Dependency-free tree-sitter grammar metadata shared by build.zig and the
+//! runtime syntax registry, and the highlight-query filter the build runs.
+//! Keep target symbols and query values out of this file: the build runner
+//! imports it as host code.
+
+const std = @import("std");
+
+pub const Tier = enum { zig, minimal, full };
+
+pub const Grammar = struct {
+ name: []const u8,
+ dep: []const u8,
+ exts: []const []const u8,
+ tier: Tier,
+ src: []const u8 = "src",
+ scanner: bool = false,
+ query: []const u8 = "queries/highlights.scm",
+ /// Patterns appended to the highlights query before it is filtered.
+ highlights_extra: []const u8 = "",
+};
+
+pub const all = [_]Grammar{
+ .{ .name = "ada", .dep = "ts_ada", .exts = &.{ ".adb", ".ads", ".ada" }, .tier = .full },
+ .{ .name = "bash", .dep = "ts_bash", .exts = &.{ ".sh", ".bash", ".zsh" }, .tier = .full, .scanner = true },
+ .{ .name = "c", .dep = "ts_c", .exts = &.{ ".c", ".h" }, .tier = .minimal },
+ .{ .name = "c_sharp", .dep = "ts_c_sharp", .exts = &.{ ".cs", ".csx" }, .tier = .full, .scanner = true },
+ .{ .name = "clojure", .dep = "ts_clojure", .exts = &.{ ".clj", ".cljs", ".cljc", ".edn" }, .tier = .full },
+ .{ .name = "cpp", .dep = "ts_cpp", .exts = &.{ ".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx" }, .tier = .minimal, .scanner = true },
+ .{ .name = "css", .dep = "ts_css", .exts = &.{".css"}, .tier = .full, .scanner = true },
+ .{ .name = "elixir", .dep = "ts_elixir", .exts = &.{ ".ex", ".exs" }, .tier = .full, .scanner = true },
+ .{ .name = "erlang", .dep = "ts_erlang", .exts = &.{ ".erl", ".hrl" }, .tier = .full, .scanner = true },
+ .{ .name = "fortran", .dep = "ts_fortran", .exts = &.{ ".f", ".for", ".ftn", ".f90", ".f95", ".f03", ".f08" }, .tier = .full, .scanner = true },
+ .{ .name = "go", .dep = "ts_go", .exts = &.{".go"}, .tier = .full },
+ .{ .name = "haskell", .dep = "ts_haskell", .exts = &.{ ".hs", ".lhs" }, .tier = .full, .scanner = true },
+ .{ .name = "html", .dep = "ts_html", .exts = &.{ ".html", ".htm" }, .tier = .full, .scanner = true },
+ .{ .name = "java", .dep = "ts_java", .exts = &.{".java"}, .tier = .full },
+ .{ .name = "javascript", .dep = "ts_javascript", .exts = &.{ ".js", ".jsx", ".mjs", ".cjs" }, .tier = .full, .scanner = true },
+ .{ .name = "json", .dep = "ts_json", .exts = &.{".json"}, .tier = .full },
+ .{ .name = "kotlin", .dep = "ts_kotlin", .exts = &.{ ".kt", ".kts" }, .tier = .full, .scanner = true },
+ .{ .name = "ocaml", .dep = "ts_ocaml", .exts = &.{ ".ml", ".mli" }, .tier = .full, .src = "grammars/ocaml/src", .scanner = true },
+ .{ .name = "markdown", .dep = "ts_markdown", .exts = &.{ ".md", ".markdown" }, .tier = .full, .src = "tree-sitter-markdown/src", .scanner = true, .query = "tree-sitter-markdown/queries/highlights.scm" },
+ // Second grammar off the SAME dep: the split_parser markdown repo ships a
+ // block parser and an inline parser, and bold/italic/code-span live only in
+ // the inline one. `.exts` is empty because no filename ever selects it — the
+ // block grammar's `inline` nodes are re-parsed with it by name (forLang).
+ .{ .name = "markdown_inline", .dep = "ts_markdown", .exts = &.{}, .tier = .full, .src = "tree-sitter-markdown-inline/src", .scanner = true, .query = "tree-sitter-markdown-inline/queries/highlights.scm" },
+ .{ .name = "pascal", .dep = "ts_pascal", .exts = &.{ ".pas", ".pp", ".p" }, .tier = .full },
+ .{ .name = "php", .dep = "ts_php", .exts = &.{ ".php", ".phtml", ".php3", ".php4", ".php5" }, .tier = .full, .src = "php/src", .scanner = true },
+ .{ .name = "powershell", .dep = "ts_powershell", .exts = &.{ ".ps1", ".psm1", ".psd1" }, .tier = .full, .scanner = true },
+ .{ .name = "python", .dep = "ts_python", .exts = &.{ ".py", ".pyw" }, .tier = .full, .scanner = true },
+ .{ .name = "ruby", .dep = "ts_ruby", .exts = &.{ ".rb", ".rake" }, .tier = .full, .scanner = true },
+ .{ .name = "rust", .dep = "ts_rust", .exts = &.{".rs"}, .tier = .full, .scanner = true },
+ .{ .name = "scala", .dep = "ts_scala", .exts = &.{ ".scala", ".sc" }, .tier = .full, .scanner = true },
+ .{ .name = "typst", .dep = "ts_typst", .exts = &.{ ".typ", ".typst" }, .tier = .full, .scanner = true, .query = "queries/typst/highlights.scm", .highlights_extra =
+ \\
+ \\(call item: (ident) @keyword)
+ \\(call item: (field field: (ident) @keyword))
+ \\"#" @keyword
+ \\
+ },
+ .{ .name = "zig", .dep = "ts_zig", .exts = &.{ ".zig", ".zon" }, .tier = .zig },
+};
+
+/// The names a language goes by besides its grammar's: a fence's `py`,
+/// a `Repl sh`.
+pub const aliases = [_]struct { []const u8, []const u8 }{
+ .{ "js", "javascript" },
+ .{ "jsx", "javascript" },
+ .{ "mjs", "javascript" },
+ .{ "py", "python" },
+ .{ "sh", "bash" },
+ .{ "shell", "bash" },
+ .{ "zsh", "bash" },
+ .{ "bash", "bash" },
+ .{ "rs", "rust" },
+ .{ "c++", "cpp" },
+ .{ "cxx", "cpp" },
+ .{ "cc", "cpp" },
+ .{ "cs", "c_sharp" },
+ .{ "kt", "kotlin" },
+ .{ "rb", "ruby" },
+ .{ "ml", "ocaml" },
+ .{ "hs", "haskell" },
+};
+
+/// The grammar a language name means: its own name or an alias, in any case.
+pub fn byName(name: []const u8) ?usize {
+ var canonical = name;
+ for (aliases) |a| if (std.ascii.eqlIgnoreCase(name, a[0])) {
+ canonical = a[1];
+ break;
+ };
+ for (all, 0..) |g, i| if (std.ascii.eqlIgnoreCase(canonical, g.name)) return i;
+ return null;
+}
+
+/// The grammar a path's extension selects, in any case.
+pub fn byPath(path: []const u8) ?usize {
+ const ext = std.Io.Dir.path.extension(path);
+ if (ext.len == 0) return null;
+ for (all, 0..) |g, i| for (g.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) return i;
+ return null;
+}
+
+/// pardes's syntax classes: what a capture colours a byte as.
+pub const Syn = enum(u8) { none, keyword, string, number, comment };
+
+pub fn synFor(name: []const u8) Syn {
+ for ([_]struct { []const u8, Syn }{
+ .{ "comment", .comment },
+ .{ "string", .string },
+ .{ "character", .string },
+ .{ "number", .number },
+ .{ "numeric", .number },
+ .{ "float", .number },
+ .{ "boolean", .number },
+ .{ "keyword", .keyword },
+ .{ "include", .keyword },
+ .{ "conditional", .keyword },
+ .{ "repeat", .keyword },
+ .{ "title", .keyword },
+ .{ "uri", .string },
+ .{ "reference", .number },
+ .{ "heading", .keyword },
+ .{ "strong", .comment },
+ .{ "bold", .comment },
+ .{ "emphasis", .number },
+ .{ "italic", .number },
+ .{ "literal", .string },
+ .{ "raw", .string },
+ }) |m| {
+ if (std.mem.indexOf(u8, name, m[0]) != null) return m[1];
+ }
+ return .none;
+}
+
+/// The patterns of highlights query `source` that capture a class (synFor),
+/// written to `out` (source.len bytes): what a grammar's query compiles to.
+/// build.zig filters each one with it, ts_queries' `<name>_highlights`.
+pub fn colorQuery(source: []const u8, out: []u8) []u8 {
+ var written: usize = 0;
+ var at: usize = 0;
+ while (at < source.len) {
+ while (at < source.len) {
+ if (std.ascii.isWhitespace(source[at])) {
+ at += 1;
+ } else if (source[at] == ';') {
+ while (at < source.len and source[at] != '\n') at += 1;
+ } else break;
+ }
+ const begin = at;
+ var depth: usize = 0;
+ var colored = false;
+ var complete = false;
+ while (at < source.len) {
+ const byte = source[at];
+ if (complete and (byte == '(' or byte == '[' or byte == '"')) break;
+ if (byte == ';') {
+ while (at < source.len and source[at] != '\n') at += 1;
+ continue;
+ }
+ if (byte == '"') {
+ at += 1;
+ while (at < source.len) : (at += 1) {
+ if (source[at] == '\\') {
+ at += 1;
+ } else if (source[at] == '"') {
+ at += 1;
+ break;
+ }
+ }
+ if (depth == 0) complete = true;
+ continue;
+ }
+ if (byte == '(' or byte == '[') depth += 1;
+ if (byte == ')' or byte == ']') {
+ depth -= 1;
+ if (depth == 0) complete = true;
+ }
+ if (byte == '@') {
+ const name = at + 1;
+ at = name;
+ while (at < source.len and (std.ascii.isAlphanumeric(source[at]) or
+ source[at] == '_' or source[at] == '.' or source[at] == '-')) at += 1;
+ colored = colored or synFor(source[name..at]) != .none;
+ continue;
+ }
+ at += 1;
+ }
+ if (colored) {
+ @memcpy(out[written..][0 .. at - begin], source[begin..at]);
+ written += at - begin;
+ }
+ }
+ return out[0..written];
+}