summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-21 01:31:16 -0300
committerGabriel Schneider <[email protected]>2026-11-21 01:31:33 -0300
commit44b4d81cd1ba49a035e66699416639b46723c872 (patch)
tree9e4d0774c579249bc36fb275e2dee62c5e81f238 /build.zig
parent6d937cd125ca04a2edb92b2d014014ee6c2eb45d (diff)
downloadpardes-44b4d81cd1ba49a035e66699416639b46723c872.tar.gz
pardes-44b4d81cd1ba49a035e66699416639b46723c872.zip
library: a dependent package names its languages: `.languages = "rust"` (or a list) replaces the `.grammars` tier with the languages named and what each needs, the manifest's new `needs` field (markdown brings markdown_inline; no other grammar needs one: a fence's language is coloured when it is in the build), so a browser reader builds one module per language; a name the manifest does not have, or `.grammars` beside `.languages`, ends the configure with exit 1 and the names it has (Zig only logs an option it cannot read in a dependency and builds on with exit 0); build.zig exports `grammar_manifest` for a consumer's build script to loop over at configure time; build/core.zig treeSitter takes a set (`GrammarSet`, `tierSet`) and syntax.zig knows the set by the queries the build gave (`@hasDecl(ts_queries, ...)`), not by the tier; test/consumer loops over the manifest (`each-language`, 26/26: each language alone is in its build, linked by its symbol, and no other is), `test` runs rust and markdown of it and a configure with `klingon` that fails saying so
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig56
1 files changed, 48 insertions, 8 deletions
diff --git a/build.zig b/build.zig
index 470d8600..cae96193 100644
--- a/build.zig
+++ b/build.zig
@@ -26,8 +26,15 @@
//!
//! `.grammars`: the tree-sitter grammars compiled in: `none` (the runtime
//! alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default).
-//! `.editor`: exports `pardes-editor` besides `pardes-syntax`, and with it
-//! fetches uucode, mvzr and vaxis.
+//! `.languages`: instead of `.grammars`, the languages compiled in by name
+//! (`"rust"`, or `&.{ "rust", "go" }`), each with what it needs (markdown
+//! its inline grammar); a name the manifest does not have fails the
+//! configure. `.editor`: exports `pardes-editor` besides `pardes-syntax`,
+//! and with it fetches uucode, mvzr and vaxis.
+//!
+//! The manifest, for a package's build script to read at configure time
+//! (`@import("pardes").grammar_manifest.all`: each grammar's name, its
+//! extensions and what it needs), is `grammar_manifest`.
//!
//! Every dependency but tree-sitter's runtime is lazy in the manifest, so
//! such a package fetches what it asked for and nothing else.
@@ -72,13 +79,20 @@ pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, .
pub const TreeSitterGrammars = enum { none, minimal, full };
+/// Every grammar pardes knows (the head comment).
+pub const grammar_manifest = @import("core/grammar_manifest.zig");
+
pub fn build(b: *std.Build) void {
// What a package that depends on pardes may ask for (the head comment).
// pardes's own build takes the mode and the grammars too, and its target
// below, whose default -Dplatform says.
const optimize = b.standardOptimizeOption(.{});
- const grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full;
- if (!b.isRoot()) return library(b, b.standardTargetOptions(.{}), optimize, grammars, b.option(bool, "editor", "exports pardes-editor besides pardes-syntax, and with it fetches uucode, mvzr and vaxis") orelse false);
+ const grammars_option = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)");
+ const grammars = grammars_option orelse .full;
+ if (!b.isRoot()) return library(b, b.standardTargetOptions(.{}), optimize, librarySet(
+ grammars_option,
+ b.option([]const []const u8, "languages", "instead of grammars, the languages compiled in by name, each with what it needs"),
+ ), b.option(bool, "editor", "exports pardes-editor besides pardes-syntax, and with it fetches uucode, mvzr and vaxis") orelse false);
// Everything below is pardes's own: the editor, its tests and tools.
// This configure reads the environment (HOME, XDG_DATA_HOME,
@@ -401,13 +415,38 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co
b.getInstallStep().dependOn(&copy.step);
}
+/// The grammars a dependent package asked for: the languages it named and
+/// what each needs (the manifest's), or its tier's. A language the manifest
+/// does not have, or a tier beside languages, ends the configure: Zig
+/// reports an option it cannot read and builds on (exit code 0).
+fn librarySet(grammars: ?TreeSitterGrammars, languages: ?[]const []const u8) core.GrammarSet {
+ const names = languages orelse return core.tierSet(grammars orelse .full);
+ if (grammars) |tier| fatal("pardes: `.languages` replaces `.grammars` (here .{t}): give one of them", .{tier});
+ var set: core.GrammarSet = @splat(false);
+ for (names) |name| {
+ const i = grammar_manifest.byName(name) orelse fatal("pardes: no language \"{s}\" in its manifest (`.languages`); it has:{s}", .{ name, comptime known: {
+ var all: []const u8 = "";
+ for (grammar_manifest.all) |g| all = all ++ " " ++ g.name;
+ break :known all;
+ } });
+ set[i] = true;
+ for (grammar_manifest.all[i].needs) |need| set[grammar_manifest.byName(need).?] = true;
+ }
+ return set;
+}
+
+fn fatal(comptime format: []const u8, args: anytype) noreturn {
+ std.log.err(format, args);
+ std.process.exit(1);
+}
+
/// What a package that depends on pardes is given (the head comment):
/// `pardes-syntax`, the grammars, highlighting and themes, over tree-sitter
/// and the grammars of its set alone; and with `editor`, `pardes-editor`,
/// whose uucode, mvzr and vaxis (with what its build names) are fetched
/// then and only then. Of vaxis it takes the grapheme width, which uucode
/// alone feeds.
-fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, grammars: TreeSitterGrammars, editor: bool) void {
+fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, set: core.GrammarSet, editor: bool) void {
var threaded: std.Io.Threaded = .init(b.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
@@ -417,10 +456,11 @@ fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.O
.root_source_file = b.path("core/lib_syntax.zig"),
.link_libc = true,
});
- // A library's options: its grammars, nothing stamped, no window.
- const cfg: core.ShellConfig = .{ .tree_sitter_grammars = grammars };
+ // A library's options: nothing stamped, no window, and a tier only
+ // pardes's own tests read (syntax.zig knows the set by its queries).
+ const cfg: core.ShellConfig = .{ .tree_sitter_grammars = if (std.mem.eql(bool, &set, &core.tierSet(.full))) .full else if (std.mem.eql(bool, &set, &core.tierSet(.minimal))) .minimal else .none };
mod.addOptions("pardes_options", core.options(b, cfg, .{ .root = "core/lib_syntax.zig", .platform = .tui, .terminal = false }, false));
- const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, grammars);
+ const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, set);
const themes = core.generatedThemes(b, io, target, optimize);
mod.addImport("generated_themes", themes);
ts.wire(mod);