summaryrefslogtreecommitdiff
path: root/src/grammar_manifest.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:31:20 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf749eb712d990a9b03f1db372d5311e77bd32edd (patch)
treeee4d9966f1eb293054b7ed865c9d4d6eb49e432b /src/grammar_manifest.zig
parent8f6621f9f70db86d56266a59b56dfb92b324b0d9 (diff)
parentb3d32656413734952bc918c2d4cfd12fede6c214 (diff)
downloadpardes-f749eb712d990a9b03f1db372d5311e77bd32edd.tar.gz
pardes-f749eb712d990a9b03f1db372d5311e77bd32edd.zip
integration: the terminal behind src/Term.zig, with the diff viewer and the plumber's first phase
A merge of term-phase0 and pardes-next. File.zig's recolorSyntax keeps the theme's grounds and names Term.Filter and Term.Span; the changelog keeps both sides' entries, the palette sentence in the place of the entry the terminal work cut down. core-test 985/985 on tui; the diff, pager and tree process cases pass, goldens unchanged. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
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];
+}