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 /test/consumer | |
| 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
Diffstat (limited to 'test/consumer')
| -rw-r--r-- | test/consumer/build.zig | 47 | ||||
| -rw-r--r-- | test/consumer/one.zig | 45 |
2 files changed, 87 insertions, 5 deletions
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))); + }; +} |
