Link manifest-selected source graphs with module-local names, qualified structs/enums/functions and task workers through ordinary AIR verification. Keep source inputs bounded, reject source symlinks and preserve explicit grants, diagnostics and deterministic canonical lowering. Integrate spec 12/15, RFC 0021, source reference budgets, roadmap, examples and CI coverage. Validate 579 conformance tests, unit tests, 76 source constructs, hosted smoke, 36 package CLI checks and task schedule stress.
237 lines
11 KiB
Zig
237 lines
11 KiB
Zig
//! `aji.toml` package manifest (spec 12 §1, RFC 0010).
|
|
//!
|
|
//! A strict TOML subset: comments with `#`, `[section]` headers and
|
|
//! `key = "string"` lines. Unknown sections, unknown keys, duplicate keys
|
|
//! and anything outside the subset are errors, so a typo cannot silently
|
|
//! drop or misplace authority.
|
|
|
|
const std = @import("std");
|
|
const modules = @import("frontend/modules.zig");
|
|
|
|
pub const Grant = struct { parameter: []const u8, kind: []const u8, scope: ?[]const u8 };
|
|
|
|
pub const ModuleBinding = struct { name: []const u8, file: []const u8, line: u32 };
|
|
|
|
pub const Manifest = struct {
|
|
name: []const u8,
|
|
version: []const u8,
|
|
entry: ?[]const u8,
|
|
modules: []const ModuleBinding = &.{},
|
|
has_modules: bool = false,
|
|
/// `[capabilities]` in file order.
|
|
grants: []const Grant,
|
|
};
|
|
|
|
pub const Error = struct {
|
|
line: u32,
|
|
message: []const u8,
|
|
};
|
|
|
|
pub const Result = union(enum) { ok: Manifest, err: Error };
|
|
|
|
fn fail(arena: std.mem.Allocator, line: u32, comptime fmt: []const u8, args: anytype) !Result {
|
|
return .{ .err = .{ .line = line, .message = try std.fmt.allocPrint(arena, fmt, args) } };
|
|
}
|
|
|
|
/// `kind[:scope]`, the same form as `--grant` after the `=`.
|
|
pub fn parseGrantValue(parameter: []const u8, value: []const u8) ?Grant {
|
|
const colon = std.mem.indexOfScalar(u8, value, ':');
|
|
const g: Grant = .{
|
|
.parameter = parameter,
|
|
.kind = if (colon) |c| value[0..c] else value,
|
|
.scope = if (colon) |c| value[c + 1 ..] else null,
|
|
};
|
|
if (g.kind.len == 0) return null;
|
|
return g;
|
|
}
|
|
|
|
fn isKey(s: []const u8) bool {
|
|
if (s.len == 0) return false;
|
|
for (s) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_' and ch != '-') return false;
|
|
return true;
|
|
}
|
|
|
|
fn isModuleName(name: []const u8) bool {
|
|
var parts = std.mem.splitScalar(u8, name, '.');
|
|
while (parts.next()) |part| {
|
|
if (part.len == 0 or !(std.ascii.isAlphabetic(part[0]) or part[0] == '_')) return false;
|
|
for (part[1..]) |ch| if (!(std.ascii.isAlphanumeric(ch) or ch == '_')) return false;
|
|
}
|
|
return name.len != 0;
|
|
}
|
|
|
|
pub fn sourcePath(path: []const u8) bool {
|
|
if (!std.mem.endsWith(u8, path, ".aji") or std.mem.indexOfAny(u8, path, "\\:\x00") != null) return false;
|
|
var parts = std.mem.splitScalar(u8, path, '/');
|
|
while (parts.next()) |part| if (part.len == 0 or std.mem.eql(u8, part, ".") or std.mem.eql(u8, part, "..")) return false;
|
|
return true;
|
|
}
|
|
|
|
pub fn parse(arena: std.mem.Allocator, text: []const u8) !Result {
|
|
const Section = enum { none, package, capabilities, metadata, modules };
|
|
var section: Section = .none;
|
|
var seen_sections: [4]bool = @splat(false);
|
|
var name: ?[]const u8 = null;
|
|
var version: ?[]const u8 = null;
|
|
var entry: ?[]const u8 = null;
|
|
var grants: std.ArrayList(Grant) = .empty;
|
|
var bindings: std.ArrayList(ModuleBinding) = .empty;
|
|
var entry_line: u32 = 0;
|
|
var keys: std.StringHashMapUnmanaged(void) = .empty;
|
|
|
|
var lines = std.mem.splitScalar(u8, text, '\n');
|
|
var line_no: u32 = 0;
|
|
while (lines.next()) |raw| {
|
|
line_no += 1;
|
|
const line = std.mem.trim(u8, raw, " \t\r");
|
|
if (line.len == 0 or line[0] == '#') continue;
|
|
if (line[0] == '[') {
|
|
if (line[line.len - 1] != ']') return fail(arena, line_no, "malformed section header", .{});
|
|
const sname = std.mem.trim(u8, line[1 .. line.len - 1], " ");
|
|
section = std.meta.stringToEnum(Section, sname) orelse return fail(arena, line_no, "unknown section [{s}]", .{sname});
|
|
if (section == .none) return fail(arena, line_no, "unknown section [{s}]", .{sname});
|
|
const idx = @backingInt(section) - 1;
|
|
if (seen_sections[idx]) return fail(arena, line_no, "duplicate section [{s}]", .{sname});
|
|
seen_sections[idx] = true;
|
|
continue;
|
|
}
|
|
const eq = std.mem.indexOfScalar(u8, line, '=') orelse return fail(arena, line_no, "expected key = \"value\"", .{});
|
|
const key = std.mem.trim(u8, line[0..eq], " \t");
|
|
if (!(if (section == .modules) isModuleName(key) else isKey(key))) return fail(arena, line_no, "malformed key", .{});
|
|
const value = (try parseString(arena, std.mem.trim(u8, line[eq + 1 ..], " \t"))) orelse
|
|
return fail(arena, line_no, "value of {s} must be a string \"...\" (escapes \\\", \\\\, \\n), optionally followed by a # comment", .{key});
|
|
if (section == .none) return fail(arena, line_no, "key {s} outside a section", .{key});
|
|
const full = try std.fmt.allocPrint(arena, "{t}.{s}", .{ section, key });
|
|
const gop = try keys.getOrPut(arena, full);
|
|
if (gop.found_existing) return fail(arena, line_no, "duplicate key {s}", .{key});
|
|
switch (section) {
|
|
.package => {
|
|
if (std.mem.eql(u8, key, "name")) {
|
|
name = value;
|
|
} else if (std.mem.eql(u8, key, "version")) {
|
|
version = value;
|
|
} else if (std.mem.eql(u8, key, "entry")) {
|
|
entry = value;
|
|
entry_line = line_no;
|
|
} else return fail(arena, line_no, "unknown key {s} in [package]", .{key});
|
|
},
|
|
.capabilities => try grants.append(arena, parseGrantValue(key, value) orelse
|
|
return fail(arena, line_no, "grant {s} must be \"kind[:scope]\"", .{key})),
|
|
.modules => {
|
|
if (modules.reserved(key)) return fail(arena, line_no, "reserved module namespace {s}", .{key});
|
|
if (!sourcePath(value)) return fail(arena, line_no, "module {s} needs a portable relative .aji path", .{key});
|
|
if (bindings.items.len >= 1024) return fail(arena, line_no, "at most 1024 modules are allowed", .{});
|
|
try bindings.append(arena, .{ .name = key, .file = value, .line = line_no });
|
|
},
|
|
.metadata => {},
|
|
.none => unreachable,
|
|
}
|
|
}
|
|
const has_modules = seen_sections[3];
|
|
if (has_modules) {
|
|
const source_entry = entry orelse return fail(arena, line_no, "[modules] requires an entry .aji source", .{});
|
|
if (!sourcePath(source_entry)) return fail(arena, entry_line, "[modules] requires a portable relative entry .aji path", .{});
|
|
}
|
|
return .{ .ok = .{
|
|
.modules = bindings.items,
|
|
.has_modules = has_modules,
|
|
.name = name orelse return fail(arena, line_no, "[package] name is required", .{}),
|
|
.version = version orelse return fail(arena, line_no, "[package] version is required", .{}),
|
|
.entry = entry,
|
|
.grants = grants.items,
|
|
} };
|
|
}
|
|
|
|
/// `"..."` with the escapes `\"`, `\\`, `\n`, optionally followed by a comment.
|
|
fn parseString(arena: std.mem.Allocator, s: []const u8) !?[]const u8 {
|
|
if (s.len < 2 or s[0] != '"') return null;
|
|
var out: std.ArrayList(u8) = .empty;
|
|
var i: usize = 1;
|
|
while (i < s.len) : (i += 1) {
|
|
switch (s[i]) {
|
|
'"' => {
|
|
const rest = std.mem.trim(u8, s[i + 1 ..], " \t");
|
|
if (rest.len != 0 and rest[0] != '#') return null;
|
|
return out.items;
|
|
},
|
|
'\\' => {
|
|
i += 1;
|
|
if (i >= s.len) return null;
|
|
try out.append(arena, switch (s[i]) {
|
|
'"' => '"',
|
|
'\\' => '\\',
|
|
'n' => '\n',
|
|
else => return null,
|
|
});
|
|
},
|
|
else => try out.append(arena, s[i]),
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
fn expectErr(text: []const u8, line: u32) !void {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const r = try parse(arena_state.allocator(), text);
|
|
try std.testing.expect(r == .err);
|
|
try std.testing.expectEqual(line, r.err.line);
|
|
}
|
|
|
|
test "manifest with grants" {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const r = try parse(arena_state.allocator(),
|
|
\\# a package
|
|
\\[package]
|
|
\\name = "file-copy"
|
|
\\version = "0.1.0"
|
|
\\entry = "src/main.air" # relative to the manifest
|
|
\\
|
|
\\[capabilities]
|
|
\\src = "fs.read:/var/data"
|
|
\\heap = "mem.alloc:67108864"
|
|
\\out = "io.write"
|
|
\\
|
|
\\[metadata]
|
|
\\purpose = "example"
|
|
\\
|
|
);
|
|
const m = r.ok;
|
|
try std.testing.expectEqualStrings("file-copy", m.name);
|
|
try std.testing.expectEqualStrings("src/main.air", m.entry.?);
|
|
try std.testing.expectEqual(@as(usize, 3), m.grants.len);
|
|
try std.testing.expectEqualStrings("fs.read", m.grants[0].kind);
|
|
try std.testing.expectEqualStrings("/var/data", m.grants[0].scope.?);
|
|
try std.testing.expect(m.grants[2].scope == null);
|
|
}
|
|
|
|
test "strict parsing" {
|
|
try expectErr("[package]\nname = \"a\"\nversion = \"1\"\n[capabilites]\n", 4); // typo in section
|
|
try expectErr("[package]\nname = \"a\"\nnmae = \"b\"\n", 3); // unknown key
|
|
try expectErr("[package]\nname = \"a\"\nname = \"b\"\n", 3); // duplicate key
|
|
try expectErr("[package]\nname = \"a\"\nversion = 1\n", 3); // not a string
|
|
try expectErr("[package]\nname = \"a\"\nversion = \"1\"\n[capabilities]\nout = \":x\"\n", 5); // empty kind
|
|
try expectErr("[package]\nname = \"a\"\n", 3); // missing version: reported at the end of the file
|
|
}
|
|
|
|
test "explicit package module mapping and portable paths" {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const arena = arena_state.allocator();
|
|
const prefix = "[package]\nname = \"a\"\nversion = \"1\"\nentry = \"src/main.aji\"\n[modules]\n";
|
|
const good = try parse(arena, prefix ++ "app.model = \"src/model.aji\"\n");
|
|
try std.testing.expect(good == .ok);
|
|
try std.testing.expectEqualStrings("app.model", good.ok.modules[0].name);
|
|
try std.testing.expect(good.ok.has_modules);
|
|
for ([_][]const u8{
|
|
"app.model = \"../model.aji\"", "app.model = \"/model.aji\"", "app.model = \"src//model.aji\"",
|
|
"app.model = \"src/./model.aji\"", "app.model = \"src/model.air\"", "app.model = \"C:/model.aji\"",
|
|
"std.bytes = \"model.aji\"", "task.work = \"model.aji\"", "io.work = \"model.aji\"",
|
|
"app..model = \"model.aji\"", "app.0model = \"model.aji\"", "app-model = \"model.aji\"",
|
|
}) |line| try expectErr(try std.mem.concat(arena, u8, &.{ prefix, line }), 6);
|
|
try expectErr(prefix ++ "app.model = \"model.aji\"\napp.model = \"other.aji\"", 7);
|
|
try expectErr("[package]\nname = \"a\"\nversion = \"1\"\nentry = \"main.air\"\n[modules]\n", 4);
|
|
for ([_][]const u8{ "src\\model.aji", "a\x00.aji", "src/../a.aji", "src/.aji/../b.aji" }) |path| try std.testing.expect(!sourcePath(path));
|
|
}
|