diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 01:31:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 01:31:33 -0300 |
| commit | 44b4d81cd1ba49a035e66699416639b46723c872 (patch) | |
| tree | 9e4d0774c579249bc36fb275e2dee62c5e81f238 | |
| parent | 6d937cd125ca04a2edb92b2d014014ee6c2eb45d (diff) | |
| download | pardes-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
| -rw-r--r-- | build.zig | 56 | ||||
| -rw-r--r-- | build/core.zig | 24 | ||||
| -rw-r--r-- | core/grammar_manifest.zig | 8 | ||||
| -rw-r--r-- | core/syntax.zig | 7 | ||||
| -rw-r--r-- | test/consumer/build.zig | 47 | ||||
| -rw-r--r-- | test/consumer/one.zig | 45 |
6 files changed, 162 insertions, 25 deletions
@@ -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(©.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); diff --git a/build/core.zig b/build/core.zig index b668211d..63b4a5e9 100644 --- a/build/core.zig +++ b/build/core.zig @@ -144,6 +144,16 @@ pub fn generatedThemes(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarg }); } +/// The grammars a build compiles in, a flag each in the manifest's order. +pub const GrammarSet = [grammar_manifest.all.len]bool; + +/// A tier's (`-Dgrammars`, a dependent package's `.grammars`). +pub fn tierSet(tier: TreeSitterGrammars) GrammarSet { + var set: GrammarSet = undefined; + for (grammar_manifest.all, &set) |g, *in| in.* = tier == .full or (tier == .minimal and g.tier == .minimal); + return set; +} + pub const TreeSitter = struct { mod: *std.Build.Module, queries: *std.Build.Step.Options, @@ -158,15 +168,15 @@ pub const TreeSitter = struct { }; /// tree-sitter's bindings, and for each grammar of the set its queries (the -/// `ts_queries` options syntax.zig reads) and its parser, a static library. -/// A grammar not fetched yet is asked for and left out: the configure runs -/// again once it is there. -pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c_optimize: std.lang.Optimize, tree_sitter_grammars: TreeSitterGrammars) TreeSitter { +/// `ts_queries` options syntax.zig reads, and by which it knows the set) +/// and its parser, a static library. A grammar not fetched yet is asked for +/// and left out: the configure runs again once it is there. +pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c_optimize: std.lang.Optimize, set: GrammarSet) TreeSitter { const tree_sitter_mod = b.dependency("tree_sitter", .{ .target = target, .optimize = c_optimize }).module("tree_sitter"); var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; const ts_queries = b.addOptions(); - inline for (grammar_manifest.all) |g| { - if (tree_sitter_grammars == .full or (tree_sitter_grammars == .minimal and g.tier == .minimal)) grammar: { + inline for (grammar_manifest.all, set) |g, in| { + if (in) grammar: { const dep = b.dependencyLazy(g.dep, .{}) catch break :grammar; const upstream = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); // the patterns that colour, filtered here once and not in @@ -433,7 +443,7 @@ pub fn assemble(b: *std.Build, c: struct { // nothing else. pardes takes these whatever it builds: the missing ones // are asked for here in one round, the grammars with them, where each // `b.dependency` below would end the configure to fetch its own alone. - const ts = treeSitter(b, io, target, c_optimize, c.shell_cfg.tree_sitter_grammars); + const ts = treeSitter(b, io, target, c_optimize, tierSet(c.shell_cfg.tree_sitter_grammars)); inline for (.{ "cloud9", "vaxis", "ghostty", "uucode", "zstbi", "mvzr", "zstd", "xitdb" }) |name| { // A dependency by path (none of these today) has no hash and is always there. const dep = @field(zon.dependencies, name); diff --git a/core/grammar_manifest.zig b/core/grammar_manifest.zig index b4ff30b9..b0ce146c 100644 --- a/core/grammar_manifest.zig +++ b/core/grammar_manifest.zig @@ -17,6 +17,12 @@ pub const Grammar = struct { query: []const u8 = "queries/highlights.scm", /// Patterns appended to the highlights query before it is filtered. highlights_extra: []const u8 = "", + /// The grammars it cannot work without, compiled in with it when a + /// package names it alone (build.zig's `languages`). Only markdown has + /// one: its inline grammar. A fence's or a raw block's language + /// (markdown's, typst's) is coloured when it is in the build and left + /// plain when it is not. + needs: []const []const u8 = &.{}, }; pub const all = [_]Grammar{ @@ -38,7 +44,7 @@ pub const all = [_]Grammar{ .{ .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" }, + .{ .name = "markdown", .dep = "ts_markdown", .exts = &.{ ".md", ".markdown" }, .tier = .full, .src = "tree-sitter-markdown/src", .scanner = true, .query = "tree-sitter-markdown/queries/highlights.scm", .needs = &.{"markdown_inline"} }, // 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 diff --git a/core/syntax.zig b/core/syntax.zig index 00ee497f..1bb8fb52 100644 --- a/core/syntax.zig +++ b/core/syntax.zig @@ -37,11 +37,10 @@ const Spec = struct { capture_styles: [256]u8 = undefined, }; +/// A grammar is in the build when the build gave its queries (build/core.zig +/// `treeSitter`): a tier's set, or the languages a package named. fn grammarSelected(comptime g: grammar_manifest.Grammar) bool { - return switch (g.tier) { - .minimal => minimal_grammars, - .full => full_grammars, - }; + return @hasDecl(ts_queries, g.name ++ "_highlights"); } fn specCount() comptime_int { diff --git a/test/consumer/build.zig b/test/consumer/build.zig index c643bfb3..fbf19f61 100644 --- a/test/consumer/build.zig +++ b/test/consumer/build.zig @@ -1,21 +1,29 @@ //! A package that depends on pardes, as a program would (build.zig's head //! comment has its lines): pardes's `consumer-test`, which `unit-test` runs, //! runs `zig build test` here. `pardes-syntax` with the minimal grammars, -//! as a program that draws source text itself takes it; and -//! `pardes-editor` with no grammar of pardes's and one of its own, C's, -//! compiled here from the package pardes pins and handed in at run time, -//! as sketchbook's `examples/live` takes it. +//! as a program that draws source text itself takes it; with one language +//! named, for each language of pardes's manifest, read here at configure +//! time; and `pardes-editor` with no grammar of pardes's and one of its own, +//! C's, compiled here from the package pardes pins and handed in at run +//! time, as sketchbook's `examples/live` takes it. const std = @import("std"); +const pardes = @import("pardes"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + // A name pardes's manifest does not have ends the configure: the step + // `bad-language` below runs this build with one. + if (b.option([]const u8, "language", "configure pardes with this one language named, and stop")) |name| { + _ = b.dependency("pardes", .{ .target = target, .optimize = optimize, .languages = name }); + return; + } const syntax = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal }); const editor = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .none, .editor = true }); // The grammar of its own: C's, from the package pardes's manifest pins // (so `--pkg-dir` at pardes's zig-pkg has it), as that package builds it. const grammar = b.dependency("ts_c", .{ .target = target, .optimize = .ReleaseFast, .@"build-shared" = false }).artifact("tree-sitter-c"); - const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's syntax, and edit a text with its editor and a grammar of this package's"); + const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's syntax, build it with one language named, and edit a text with its editor and a grammar of this package's"); // A file belongs to one module: the syntax alone, and the editor (which // has it as `.syntax`), each a test of its own. for ([_]struct { []const u8, []const u8, *std.Build.Dependency }{ .{ "lib.zig", "pardes-syntax", syntax }, .{ "editor.zig", "pardes-editor", editor } }) |t| { @@ -28,6 +36,35 @@ pub fn build(b: *std.Build) void { if (t[2] == editor) mod.linkLibrary(grammar); step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); } + + // Each language a file names, from pardes's manifest: `pardes-syntax` + // with it alone (and what it needs). `test` runs two: rust, alone, and + // markdown, which brings its inline grammar. + const each = b.step("each-language", "build and test pardes-syntax with each language of its manifest named alone"); + for (pardes.grammar_manifest.all) |g| if (g.exts.len > 0) { + const name = b.addOptions(); + name.addOption([]const u8, "name", g.name); + const one = b.addRunArtifact(b.addTest(.{ .name = b.fmt("one-{s}", .{g.name}), .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("one.zig"), + .imports = &.{ + .{ .name = "pardes-syntax", .module = b.dependency("pardes", .{ .target = target, .optimize = optimize, .languages = g.name }).module("pardes-syntax") }, + .{ .name = "language", .module = name.createModule() }, + }, + }) })); + each.dependOn(&one.step); + if (std.mem.eql(u8, g.name, "rust") or std.mem.eql(u8, g.name, "markdown")) step.dependOn(&one.step); + }; + // ...and a name it does not have: the configure fails, saying so. + const bad = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "-Dlanguage=klingon", "--pkg-dir", "../../zig-pkg" }); + bad.setName("a language pardes does not have fails the configure"); + bad.setCwd(b.path(".")); + bad.expectExitCode(1); + bad.addCheck(.{ .expect_stderr_match = "no language \"klingon\"" }); + bad.has_side_effects = true; + step.dependOn(&bad.step); + // What a key costs, a page count and a parse: `zig build bench -- <KiB> <keys>`. const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ .target = target, diff --git a/test/consumer/one.zig b/test/consumer/one.zig new file mode 100644 index 00000000..4a113842 --- /dev/null +++ b/test/consumer/one.zig @@ -0,0 +1,45 @@ +//! `pardes-syntax` built with one language named (`.languages`, build.zig +//! here: the build's loop over pardes's manifest makes one of these a +//! language): that language and what it needs (the manifest's `needs`) are +//! compiled in, and no other. +const std = @import("std"); +const ps = @import("pardes-syntax"); +const language = @import("language").name; + +test "one language named: it and what it needs are in the build, no other grammar is linked" { + const gpa = std.testing.allocator; + ps.start(gpa); + defer ps.stop(); + const g = ps.grammars; + const named = comptime named: { + @setEvalBranchQuota(10_000); + break :named g.all[g.byName(language).?]; + }; + // This test program's own bytes (a Debug build keeps its symbols): a + // name is spelled whole only in its symbol table. + const self = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "/proc/self/exe", gpa, .unlimited); + defer gpa.free(self); + var name: [64]u8 = undefined; + inline for (g.all) |other| { + const in = comptime std.mem.eql(u8, other.name, named.name) or for (named.needs) |n| { + if (std.mem.eql(u8, n, other.name)) break true; + } else false; + // the parser itself, by its symbol in this program's symbol table: + // linked, or not in it + const symbol = try std.fmt.bufPrint(&name, "\x00tree_sitter_{s}\x00", .{other.name}); + try std.testing.expectEqual(in, std.mem.indexOf(u8, self, symbol) != null); + if (other.exts.len > 0) try std.testing.expectEqual(in, ps.supportsPath(ps.pathFor(other.name).?)); + } + + // what two of them colour: rust alone, and markdown with the inline + // grammar it brings (its bold is markdown_inline's) + for ([_]struct { []const u8, []const u8, []const u8, ps.Syn }{ + .{ "rust", "fn main() { let x = 1; }\n", "fn", .keyword }, + .{ "markdown", "# Title\n\nsome **bold** text\n", "bold", .comment }, + }) |case| if (std.mem.eql(u8, case[0], named.name)) { + const styles = try ps.highlightFileRange(gpa, ps.pathFor(language).?, case[1], 0, case[1].len); + defer gpa.free(styles); + const at = std.mem.indexOf(u8, case[1], case[2]).?; + for (styles[at..][0..case[2].len]) |s| try std.testing.expectEqual(case[3], @as(ps.Syn, @fromBackingInt(s))); + }; +} |
