Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: 0.14.1
version: 0.15.2
- name: Build
run: zig build -Dtarget=${{ matrix.target }}
test-pull-request:
Expand All @@ -37,7 +37,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: 0.14.1
version: 0.15.2
- name: Test
run: zig build test

85 changes: 11 additions & 74 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ pub fn build(b: *std.Build) !void {
{
const unit_tests = b.addTest(.{
.name = "unit-tests",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
try attachModules(unit_tests);

Expand Down Expand Up @@ -75,9 +77,11 @@ fn addExecutable(b: *std.Build, options: struct {
}) !*std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = "glsl_analyzer",
.root_source_file = b.path("src/main.zig"),
.target = options.target,
.optimize = options.optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = options.target,
.optimize = options.optimize,
}),
});
try attachModules(exe);
return exe;
Expand All @@ -88,8 +92,7 @@ fn attachModules(step: *std.Build.Step.Compile) !void {

step.linkLibC();

const compressed_spec = try CompressStep.create(b, "spec.json.zlib", b.path("spec/spec.json"));
step.root_module.addAnonymousImport("glsl_spec.json.zlib", .{ .root_source_file = compressed_spec.getOutput() });
step.root_module.addAnonymousImport("glsl_spec.json", .{ .root_source_file = b.path("spec/spec.json") });

const options = b.addOptions();
const build_root_path = try std.fs.path.resolve(
Expand All @@ -100,69 +103,3 @@ fn attachModules(step: *std.Build.Step.Compile) !void {
options.addOption([]const u8, "version", b.run(&.{ "git", "describe", "--tags", "--always" }));
step.root_module.addOptions("build_options", options);
}

const CompressStep = struct {
step: std.Build.Step,
generated_file: std.Build.GeneratedFile,
input: std.Build.LazyPath,

pub fn create(b: *std.Build, name: []const u8, path: std.Build.LazyPath) !*@This() {
const self = try b.allocator.create(@This());
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = name,
.owner = b,
.makeFn = &make,
}),
.generated_file = .{ .step = &self.step },
.input = path,
};
path.addStepDependencies(&self.step);
return self;
}

pub fn getOutput(self: *@This()) std.Build.LazyPath {
return .{ .generated = .{ .file = &self.generated_file } };
}

fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) anyerror!void {
const b = step.owner;
const self: *@This() = @fieldParentPtr("step", step);
const input_path = self.input.getPath(b);

var man = b.graph.cache.obtain();
defer man.deinit();

man.hash.add(@as(u32, 0x00000002));
const input_index = try man.addFile(input_path, 16 << 20);

const is_hit = try step.cacheHit(&man);

const digest = man.final();

const output_path = try b.cache_root.join(b.allocator, &.{ "o", &digest, step.name });
self.generated_file.path = output_path;

if (is_hit) return;

const input_contents = man.files.keys()[input_index].contents.?;

if (std.fs.path.dirname(output_path)) |dir| try b.cache_root.handle.makePath(dir);
var output_file = b.cache_root.handle.createFile(output_path, .{}) catch |err| {
std.log.err("could not open {s}: {s}", .{ output_path, @errorName(err) });
return err;
};
defer output_file.close();

var output_buffered = std.io.bufferedWriter(output_file.writer());
{
var compress_stream = try std.compress.zlib.compressor(output_buffered.writer(), .{});
try compress_stream.writer().writeAll(input_contents);
try compress_stream.finish();
}
try output_buffered.flush();

try step.writeManifest(&man);
}
};
6 changes: 3 additions & 3 deletions src/Document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ pub const CompleteParseTree = struct {
var arena = std.heap.ArenaAllocator.init(parent_allocator);
errdefer arena.deinit();

var diagnostics = std.ArrayList(parse.Diagnostic).init(arena.allocator());
var diagnostics = std.array_list.Managed(parse.Diagnostic).init(arena.allocator());

var ignored = std.ArrayList(parse.Token).init(parent_allocator);
var ignored = std.array_list.Managed(parse.Token).init(parent_allocator);
defer ignored.deinit();

const tree = try parse.parse(arena.allocator(), text, .{
.ignored = &ignored,
.diagnostics = &diagnostics,
});

var extensions = std.ArrayList([]const u8).init(arena.allocator());
var extensions = std.array_list.Managed([]const u8).init(arena.allocator());
errdefer extensions.deinit();

for (ignored.items) |token| {
Expand Down
35 changes: 17 additions & 18 deletions src/Spec.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ pub const Function = struct {
};
};

const compressed_bytes = @embedFile("glsl_spec.json.zlib");
const spec_bytes = @embedFile("glsl_spec.json");

pub fn load(allocator: std.mem.Allocator) !@This() {
var compressed_stream = std.io.fixedBufferStream(compressed_bytes);
var decompress_stream = std.compress.zlib.decompressor(compressed_stream.reader());

const bytes = try decompress_stream.reader().readAllAlloc(allocator, 16 << 20);

var diagnostic = std.json.Diagnostics{};
var scanner = std.json.Scanner.initCompleteInput(allocator, bytes);
var scanner = std.json.Scanner.initCompleteInput(allocator, spec_bytes);
defer scanner.deinit();
scanner.enableDiagnostics(&diagnostic);

Expand All @@ -71,7 +66,7 @@ pub fn load(allocator: std.mem.Allocator) !@This() {
"could not parse GLSL spec: {}:{}: {s}",
.{ diagnostic.getLine(), diagnostic.getColumn(), @errorName(err) },
);
std.log.err("{?s}", .{util.getJsonErrorContext(diagnostic, bytes)});
std.log.err("{s}", .{util.getJsonErrorContext(diagnostic, spec_bytes)});
return err;
};
}
Expand Down Expand Up @@ -101,30 +96,34 @@ pub const Modifiers = packed struct(u3) {
return modifiers;
}

const FormatBuffer = std.BoundedArray(u8, blk: {
const buffer_len = blk: {
var max_len: usize = std.meta.fieldNames(@This()).len;
for (std.meta.fieldNames(@This())) |name| max_len += name.len;
break :blk max_len;
});
};

const FormatBuffer = std.ArrayListUnmanaged(u8);

fn toString(self: @This(), buffer: *FormatBuffer) void {
inline for (comptime std.meta.fieldNames(@This())) |name| {
if (@field(self, name)) {
if (buffer.len != 0) buffer.appendAssumeCapacity(' ');
if (buffer.items.len != 0) buffer.appendAssumeCapacity(' ');
buffer.appendSliceAssumeCapacity(name);
}
}
}

pub fn jsonStringify(self: @This(), jw: anytype) !void {
var buffer = FormatBuffer{};
self.toString(&buffer);
try jw.write(buffer.slice());
var buffer: [buffer_len]u8 = undefined;
var format_buffer = FormatBuffer.initBuffer(&buffer);
self.toString(&format_buffer);
try jw.write(format_buffer.items);
}

pub fn format(self: @This(), _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var buffer = FormatBuffer{};
self.toString(&buffer);
try writer.writeAll(buffer.slice());
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
var buffer: [buffer_len]u8 = undefined;
var format_buffer = FormatBuffer.initBuffer(&buffer);
self.toString(&format_buffer);
try writer.writeAll(format_buffer.items);
}
};
16 changes: 8 additions & 8 deletions src/Workspace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn getOrLoadDocument(
}

fn builtinCompletions(arena: std.mem.Allocator, spec: *const Spec) ![]lsp.CompletionItem {
var completions = std.ArrayList(lsp.CompletionItem).init(arena);
var completions = std.array_list.Managed(lsp.CompletionItem).init(arena);

try completions.ensureUnusedCapacity(
spec.types.len + spec.variables.len + spec.functions.len,
Expand Down Expand Up @@ -156,10 +156,10 @@ fn builtinCompletions(arena: std.mem.Allocator, spec: *const Spec) ![]lsp.Comple
}

for (spec.variables) |variable| {
var anonymous_signature = std.ArrayList(u8).init(arena);
var anonymous_signature = std.array_list.Managed(u8).init(arena);
try writeVariableSignature(variable, anonymous_signature.writer(), .{ .names = false });

var named_signature = std.ArrayList(u8).init(arena);
var named_signature = std.array_list.Managed(u8).init(arena);
try writeVariableSignature(variable, named_signature.writer(), .{ .names = true });

try completions.append(.{
Expand All @@ -172,10 +172,10 @@ fn builtinCompletions(arena: std.mem.Allocator, spec: *const Spec) ![]lsp.Comple
}

for (spec.functions) |function| {
var anonymous_signature = std.ArrayList(u8).init(arena);
var anonymous_signature = std.array_list.Managed(u8).init(arena);
try writeFunctionSignature(function, anonymous_signature.writer(), .{ .names = false });

var named_signature = std.ArrayList(u8).init(arena);
var named_signature = std.array_list.Managed(u8).init(arena);
try writeFunctionSignature(function, named_signature.writer(), .{ .names = true });

try completions.append(.{
Expand All @@ -191,7 +191,7 @@ fn builtinCompletions(arena: std.mem.Allocator, spec: *const Spec) ![]lsp.Comple
}

fn itemDocumentation(arena: std.mem.Allocator, item: anytype) !lsp.MarkupContent {
var documentation = std.ArrayList(u8).init(arena);
var documentation = std.array_list.Managed(u8).init(arena);

for (item.description orelse &.{}) |paragraph| {
try documentation.appendSlice(paragraph);
Expand All @@ -215,7 +215,7 @@ fn writeVariableSignature(
options: struct { names: bool },
) !void {
if (!std.meta.eql(variable.modifiers, .{ .in = true })) {
try writer.print("{}", .{variable.modifiers});
try writer.print("{f}", .{variable.modifiers});
try writer.writeAll(" ");
}

Expand Down Expand Up @@ -247,7 +247,7 @@ fn writeFunctionSignature(
if (i != 0) try writer.writeAll(", ");
if (param.optional) try writer.writeAll("[");
if (param.modifiers) |modifiers| {
try writer.print("{}", .{modifiers});
try writer.print("{f}", .{modifiers});
try writer.writeAll(" ");
}
if (options.names) {
Expand Down
Loading