summaryrefslogtreecommitdiff
path: root/src/grammar_manifest.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/grammar_manifest.zig')
-rw-r--r--src/grammar_manifest.zig108
1 files changed, 105 insertions, 3 deletions
diff --git a/src/grammar_manifest.zig b/src/grammar_manifest.zig
index 38c54916..711c31a4 100644
--- a/src/grammar_manifest.zig
+++ b/src/grammar_manifest.zig
@@ -1,6 +1,7 @@
//! Dependency-free tree-sitter grammar metadata shared by build.zig and the
-//! runtime syntax registry. Keep target symbols and query values out of this
-//! file: the build runner imports it as host code.
+//! 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");
@@ -14,6 +15,8 @@ pub const Grammar = struct {
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{
@@ -48,7 +51,13 @@ pub const all = [_]Grammar{
.{ .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" },
+ .{ .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 },
};
@@ -92,3 +101,96 @@ pub fn byPath(path: []const u8) ?usize {
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];
+}