Compare commits

..

1 commit

Author SHA1 Message Date
ce6ce6575d WIP: Formatter 2026-07-09 14:20:21 +00:00
14 changed files with 314 additions and 1662 deletions

View file

@ -18,7 +18,9 @@ pub fn build(b: *std.Build) void {
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addPassthruArgs();
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

12
flake.lock generated
View file

@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1791914995,
"narHash": "sha256-Zf/xfQEAasnwnmEfHqW/4hjDE/s44yn2hD4RuVIZRjA=",
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b7a79d3fea1e214f6d668d521cecec0c01e26544",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1793110635,
"narHash": "sha256-dhoW6VgFWlaodSY5jrBn+CF2FLmaUvDVgM7+C7T2+NM=",
"lastModified": 1781577229,
"narHash": "sha256-lrp67w8AulE9Ks53n27I45ADSzbOCn4H+CNW1Ck8B+8=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "31ecbaca1bdefa45dffc409f0250bd8039ea0010",
"rev": "567a49d1913ce81ac6e9582e3553dd90a955875f",
"type": "github"
},
"original": {

View file

@ -1,11 +1,9 @@
const std = @import("std");
const EnumFlags = @import("EnumFlags.zig").EnumFlags;
const Formatter = @import("Formatter.zig");
pub const FieldInfo = @import("Class/FieldInfo.zig");
pub const MethodInfo = @import("Class/MethodInfo.zig");
pub const AttributeInfo = @import("Class/AttributeInfo.zig").AttributeInfo;
pub const ConstantPool = @import("Class/ConstantPool.zig");
allocator: std.mem.Allocator,
magic: u32,
minor_version: u16,
@ -21,6 +19,11 @@ attributes: []AttributeInfo,
const Self = @This();
pub const FieldInfo = @import("Class/FieldInfo.zig");
pub const MethodInfo = @import("Class/MethodInfo.zig");
pub const AttributeInfo = @import("Class/AttributeInfo.zig").AttributeInfo;
pub const ConstantPool = @import("Class/ConstantPool.zig");
pub const ClassAccessFlags = EnumFlags(enum(u16) {
public = 0x0001,
final = 0x0010,
@ -36,8 +39,6 @@ pub const ClassAccessFlags = EnumFlags(enum(u16) {
pub const ParseError = std.Io.Reader.Error || std.mem.Allocator.Error || error {
InvalidMagicNumber,
InvalidTag,
InvalidOp,
InvalidArrayType,
};
pub const ResolveError = error {
@ -57,7 +58,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) (ParseError ||
var constant_pool: ConstantPool = try .parse(input, allocator);
errdefer constant_pool.deinit(allocator);
const access_flags: ClassAccessFlags = .from(try input.takeInt(u16, .big));
const access_flags: ClassAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
const this_class = try input.takeInt(u16, .big);
const super_class = try input.takeInt(u16, .big);
@ -90,6 +91,7 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) (ParseError ||
}
return .{
.allocator = allocator,
.magic = magic,
.minor_version = minor_version,
.major_version = major_version,
@ -121,12 +123,8 @@ fn parseFields(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: st
const fields = try allocator.alloc(FieldInfo, fields_count);
errdefer allocator.free(fields);
var parsed: usize = 0;
errdefer for (0..parsed) |i| fields[i].deinit(allocator);
for (0..fields_count) |i| {
fields[i] = try .parse(input, constant_pool, allocator);
parsed += 1;
fields[i] = try FieldInfo.parse(input, constant_pool, allocator);
}
return fields;
@ -137,12 +135,8 @@ fn parseMethods(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: s
const methods = try allocator.alloc(MethodInfo, methods_count);
errdefer allocator.free(methods);
var parsed: usize = 0;
errdefer for (0..parsed) |i| methods[i].deinit(allocator);
for (0..methods_count) |i| {
methods[i] = try .parse(input, constant_pool, allocator);
parsed += 1;
}
return methods;
@ -153,36 +147,60 @@ pub fn parseAttributes(input: *std.Io.Reader, constant_pool: ConstantPool, alloc
const attributes = try allocator.alloc(AttributeInfo, attributes_count);
errdefer allocator.free(attributes);
var parsed: usize = 0;
errdefer for (0..parsed) |i| attributes[i].deinit(allocator);
for (0..attributes_count) |i| {
attributes[i] = try .parse(input, constant_pool, allocator);
parsed += 1;
attributes[i] = try AttributeInfo.parse(input, constant_pool, allocator);
}
return attributes;
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
self.constant_pool.deinit(allocator);
pub fn deinit(self: *Self) void {
self.constant_pool.deinit(self.allocator);
self.freeInterfaces();
self.freeFields();
self.freeMethods();
self.freeAttributes();
}
allocator.free(self.interfaces);
fn freeInterfaces(self: *Self) void {
self.allocator.free(self.interfaces);
}
fn freeFields(self: *Self) void {
for (self.fields) |*field_info| {
field_info.deinit(allocator);
field_info.deinit(self.allocator);
}
self.allocator.free(self.fields);
}
allocator.free(self.fields);
fn freeMethods(self: *Self) void {
for (self.methods) |*method_info| {
method_info.deinit(allocator);
method_info.deinit(self.allocator);
}
self.allocator.free(self.methods);
}
allocator.free(self.methods);
fn freeAttributes(self: *Self) void {
for (self.attributes) |*attr| {
attr.deinit(allocator);
attr.deinit(self.allocator);
}
allocator.free(self.attributes);
self.allocator.free(self.attributes);
}
pub fn jsonStringify(self: *const Self, jws: *std.json.Stringify) !void {
try jws.beginObject();
inline for (std.meta.fields(Self)) |field| {
if (comptime std.mem.eql(u8, field.name, "allocator")) continue;
try jws.objectField(field.name);
try jws.write(@field(self, field.name));
}
try jws.endObject();
}
pub fn acceptFormatter(self: *const Self, formatter: *Formatter) Formatter.Error!void {
try formatter.printField("magic", "0x{X}", .{ self.magic });
}
pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
@ -254,3 +272,4 @@ pub fn thisClass(self: *const Self) ResolveError!ConstantPool.Info {
pub fn superClass(self: *const Self) ResolveError!?ConstantPool.Info {
return self.constant_pool.getTagOptional(self.super_class, .class);
}

View file

@ -319,9 +319,7 @@ pub const AttributeInfo = union(enum) {
defer instr_indent -= 1;
try w.splatByteAll(' ', depth * instr_indent);
try w.print("{d}:\n", .{i});
try instr.indentedFormat(w, depth, instr_indent + 1, constant_pool);
try w.print("{d}: 0x{x}\n", .{ i, instr });
}
try w.splatByteAll(' ', depth * indent);
@ -627,7 +625,7 @@ pub const AttributeInfo = union(enum) {
try w.splatByteAll(' ', depth * component_indent);
try w.print("{d}:\n", .{ i });
try component.indentedFormat(w, depth, component_indent + 1, constant_pool);
try component.indentedFormat(w, depth, component_indent, constant_pool);
}
},
.permitted_subclasses => |attr| {
@ -692,14 +690,12 @@ pub const ConstantValue = struct {
pub const Code = struct {
max_stack: u16,
max_locals: u16,
code: []Instruction,
code: []u8,
exception_table: []ExceptionHandler,
attributes: []AttributeInfo,
const Self = @This();
pub const Instruction = @import("Code/Instruction.zig").Instruction;
pub const ExceptionHandler = struct {
start_pc: u16,
end_pc: u16,
@ -740,30 +736,12 @@ pub const Code = struct {
const max_locals = try input.takeInt(u16, .big);
const code_length = try input.takeInt(u32, .big);
var code_reader_buf: [1024]u8 = undefined;
var code_reader = input.limited(.limited(code_length), &code_reader_buf);
var code = &code_reader.interface;
var byte_index: usize = 0;
var instructions: std.ArrayList(Instruction) = .empty;
defer instructions.deinit(allocator);
var parsed_instructions: usize = 0;
errdefer for (0..parsed_instructions) |i| instructions.items[i].deinit(allocator);
loop: while (true) {
_ = code.peekByte() catch |err| switch (err) {
error.EndOfStream => break :loop,
else => return err,
};
try instructions.append(allocator, try .parse(code, allocator, &byte_index));
parsed_instructions += 1;
}
const code = try input.readAlloc(allocator, code_length);
errdefer allocator.free(code);
const exception_table_length = try input.takeInt(u16, .big);
const exception_table = try allocator.alloc(ExceptionHandler, exception_table_length);
errdefer allocator.free(exception_table);
for (exception_table) |*exception_handler| {
exception_handler.* = .{
.start_pc = try input.takeInt(u16, .big),
@ -776,32 +754,22 @@ pub const Code = struct {
const attribute_count = try input.takeInt(u16, .big);
const attributes = try allocator.alloc(AttributeInfo, attribute_count);
errdefer allocator.free(attributes);
var parsed_attributes: usize = 0;
errdefer for (0..parsed_attributes) |i| attributes[i].deinit(allocator);
for (attributes) |*attribute| {
attribute.* = try .parse(input, constant_pool, allocator);
parsed_attributes += 1;
}
return .{
.max_stack = max_stack,
.max_locals = max_locals,
.code = try instructions.toOwnedSlice(allocator),
.code = code,
.exception_table = exception_table,
.attributes = attributes,
};
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
for (self.code) |*instruction| {
instruction.deinit(allocator);
}
allocator.free(self.code);
allocator.free(self.exception_table);
for (self.attributes) |*attr| {
attr.deinit(allocator);
}
@ -877,7 +845,6 @@ pub const StackMapTable = struct {
const length = frame_type - 251;
const locals = try allocator.alloc(StackMapTable.VerificationTypeInfo, length);
errdefer allocator.free(locals);
for (locals) |*local| {
local.* = try .parse(input);
}
@ -895,7 +862,6 @@ pub const StackMapTable = struct {
const number_of_locals = try input.takeInt(u16, .big);
const locals = try allocator.alloc(StackMapTable.VerificationTypeInfo, number_of_locals);
errdefer allocator.free(locals);
for (locals) |*local| {
local.* = try .parse(input);
}
@ -903,7 +869,6 @@ pub const StackMapTable = struct {
const number_of_stack_items = try input.takeInt(u16, .big);
const stack = try allocator.alloc(StackMapTable.VerificationTypeInfo, number_of_stack_items);
errdefer allocator.free(stack);
for (stack) |*stack_item| {
stack_item.* = try .parse(input);
}
@ -1119,13 +1084,8 @@ pub const StackMapTable = struct {
const number_of_entries = try input.takeInt(u16, .big);
const entries = try allocator.alloc(StackMapTable.StackMapFrame, number_of_entries);
errdefer allocator.free(entries);
var parsed: usize = 0;
errdefer for (0..parsed) |i| entries[i].deinit(allocator);
for (entries) |*entry| {
entry.* = try .parse(input, allocator);
parsed += 1;
}
return .{
@ -1150,7 +1110,6 @@ pub const Exceptions = struct {
const number_of_exceptions = try input.takeInt(u16, .big);
const exception_index_table = try allocator.alloc(u16, number_of_exceptions);
errdefer allocator.free(exception_index_table);
for (exception_index_table) |*exception_index| {
exception_index.* = try input.takeInt(u16, .big);
}
@ -1236,13 +1195,12 @@ pub const InnerClasses = struct {
const number_of_classes = try input.takeInt(u16, .big);
const classes = try allocator.alloc(InnerClass, number_of_classes);
errdefer allocator.free(classes);
for (classes) |*class| {
class.* = .{
.inner_class_info_index = try input.takeInt(u16, .big),
.outer_class_info_index = try input.takeInt(u16, .big),
.inner_name_index = try input.takeInt(u16, .big),
.inner_class_access_flags = .from(try input.takeInt(u16, .big)),
.inner_class_access_flags = .{ .mask = try input.takeInt(u16, .big) },
};
}
@ -1355,7 +1313,6 @@ pub const LineNumberTable = struct {
const line_number_table_length = try input.takeInt(u16, .big);
const line_number_table = try allocator.alloc(LineNumber, line_number_table_length);
errdefer allocator.free(line_number_table);
for (line_number_table) |*line_number| {
line_number.* = .{
.start_pc = try input.takeInt(u16, .big),
@ -1422,7 +1379,6 @@ pub const LocalVariableTable = struct {
const local_variable_table_length = try input.takeInt(u16, .big);
const local_variable_table = try allocator.alloc(LocalVariable, local_variable_table_length);
errdefer allocator.free(local_variable_table);
for (local_variable_table) |*local_variable| {
local_variable.* = .{
.start_pc = try input.takeInt(u16, .big),
@ -1492,7 +1448,6 @@ pub const LocalVariableTypeTable = struct {
const local_variable_type_table_length = try input.takeInt(u16, .big);
const local_variable_type_table = try allocator.alloc(LocalVariableType, local_variable_type_table_length);
errdefer allocator.free(local_variable_type_table);
for (local_variable_type_table) |*local_variable_type| {
local_variable_type.* = .{
.start_pc = try input.takeInt(u16, .big),
@ -1530,7 +1485,7 @@ pub const ElementValue = union(enum) {
},
pub const ConstValue = struct {
type: Type,
@"type": Type,
index: u16,
pub const Type = enum(u8) {
@ -1547,7 +1502,7 @@ pub const ElementValue = union(enum) {
pub fn value(self: *const ConstValue, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
const value_ = try constant_pool.get(self.index);
return switch (self.type) {
return switch (self.@"type") {
.byte, .char, .int, .short, .boolean => blk: {
if (!value_.is(.integer)) break :blk ResolveError.InvalidConstantType;
break :blk value_;
@ -1598,7 +1553,7 @@ pub const ElementValue = union(enum) {
return switch (tag) {
'B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z', 's' => .{
.const_value = .{
.type = @fromBackingInt(@intCast(tag)),
.@"type" = @enumFromInt(tag),
.index = try input.takeInt(u16, .big),
},
},
@ -1621,13 +1576,8 @@ pub const ElementValue = union(enum) {
const num_values = try input.takeInt(u16, .big);
const values = try allocator.alloc(ElementValue, num_values);
errdefer allocator.free(values);
var parsed: usize = 0;
errdefer for (0..parsed) |i| values[i].deinit(allocator);
for (values) |*value| {
value.* = try .parse(input, allocator);
parsed += 1;
}
break :blk .{
@ -1748,16 +1698,11 @@ pub const Annotation = struct {
const num_element_value_pairs = try input.takeInt(u16, .big);
const element_value_pairs = try allocator.alloc(ElementValuePair, num_element_value_pairs);
errdefer allocator.free(element_value_pairs);
var parsed: usize = 0;
errdefer for (0..parsed) |i| element_value_pairs[i].value.deinit(allocator);
for (element_value_pairs) |*element_value_pair| {
element_value_pair.* = .{
.element_name_index = try input.takeInt(u16, .big),
.value = try .parse(input, allocator),
};
parsed += 1;
}
return .{
@ -1780,7 +1725,7 @@ pub const Annotation = struct {
indent: u8,
constant_pool: ConstantPool,
) std.Io.Writer.Error!void {
const type_ = self.type(constant_pool) catch return error.WriteFailed;
const type_ = self.@"type"(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
_ = try w.write("type:\n");
try type_.indentedFormat(w, depth, indent + 1, constant_pool);
@ -1812,13 +1757,8 @@ pub const RuntimeVisibleAnnotations = struct {
const num_annotations = try input.takeInt(u16, .big);
const annotations = try allocator.alloc(Annotation, num_annotations);
errdefer allocator.free(annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| annotations[i].deinit(allocator);
for (annotations) |*annotation| {
annotation.* = try .parse(input, allocator);
parsed += 1;
}
return .{
@ -1843,13 +1783,8 @@ pub const RuntimeInvisibleAnnotations = struct {
const num_annotations = try input.takeInt(u16, .big);
const annotations = try allocator.alloc(Annotation, num_annotations);
errdefer allocator.free(annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| annotations[i].deinit(allocator);
for (annotations) |*annotation| {
annotation.* = try .parse(input, allocator);
parsed += 1;
}
return .{
@ -1868,33 +1803,6 @@ pub const RuntimeInvisibleAnnotations = struct {
pub const ParameterAnnotation = struct {
annotations: []Annotation,
const Self = @This();
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const num_annotations = try input.takeInt(u16, .big);
const annotations = try allocator.alloc(Annotation, num_annotations);
errdefer allocator.free(annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| annotations[i].deinit(allocator);
for (annotations) |*annotation| {
annotation.* = try .parse(input, allocator);
parsed += 1;
}
return .{
.annotations = annotations,
};
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
for (self.annotations) |*annotation| {
annotation.deinit(allocator);
}
allocator.free(self.annotations);
}
pub fn indentedFormat(
self: *const ParameterAnnotation,
w: *std.Io.Writer,
@ -1925,13 +1833,17 @@ pub const RuntimeVisibleParameterAnnotations = struct {
const num_parameters = try input.takeByte();
const parameter_annotations = try allocator.alloc(ParameterAnnotation, num_parameters);
errdefer allocator.free(parameter_annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| parameter_annotations[i].deinit(allocator);
for (parameter_annotations) |*parameter_annotation| {
parameter_annotation.* = try .parse(input, allocator);
parsed += 1;
const num_annotations = try input.takeInt(u16, .big);
const annotations = try allocator.alloc(Annotation, num_annotations);
errdefer allocator.free(annotations);
for (annotations) |*annotation| {
annotation.* = try .parse(input, allocator);
}
parameter_annotation.* = .{
.annotations = annotations,
};
}
return .{
@ -1940,8 +1852,11 @@ pub const RuntimeVisibleParameterAnnotations = struct {
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
for (self.parameter_annotations) |*parameter_annotation| {
parameter_annotation.deinit(allocator);
for (self.parameter_annotations) |parameter_annotation| {
for (parameter_annotation.annotations) |*annotation| {
annotation.deinit(allocator);
}
allocator.free(parameter_annotation.annotations);
}
allocator.free(self.parameter_annotations);
}
@ -1956,13 +1871,17 @@ pub const RuntimeInvisibleParameterAnnotations = struct {
const num_parameters = try input.takeByte();
const parameter_annotations = try allocator.alloc(ParameterAnnotation, num_parameters);
errdefer allocator.free(parameter_annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| parameter_annotations[i].deinit(allocator);
for (parameter_annotations) |*parameter_annotation| {
parameter_annotation.* = try .parse(input, allocator);
parsed += 1;
const num_annotations = try input.takeInt(u16, .big);
const annotations = try allocator.alloc(Annotation, num_annotations);
errdefer allocator.free(annotations);
for (annotations) |*annotation| {
annotation.* = try .parse(input, allocator);
}
parameter_annotation.* = .{
.annotations = annotations,
};
}
return .{
@ -1971,8 +1890,11 @@ pub const RuntimeInvisibleParameterAnnotations = struct {
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
for (self.parameter_annotations) |*parameter_annotation| {
parameter_annotation.deinit(allocator);
for (self.parameter_annotations) |parameter_annotation| {
for (parameter_annotation.annotations) |*annotation| {
annotation.deinit(allocator);
}
allocator.free(parameter_annotation.annotations);
}
allocator.free(self.parameter_annotations);
}
@ -2202,7 +2124,6 @@ pub const TypeAnnotation = struct {
const length = try input.takeByte();
const paths = try allocator.alloc(Path, length);
errdefer allocator.free(paths);
for (paths) |*path| {
path.* = .{
.type_path_kind = try input.takeByte(),
@ -2240,7 +2161,7 @@ pub const TypeAnnotation = struct {
};
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const target_type: TargetType = @fromBackingInt(@intCast(try input.takeInt(u8, .big)));
const target_type: TargetType = @enumFromInt(try input.takeInt(u8, .big));
const target_info: TargetInfo = switch (target_type) {
.type_parameter_generic_class_interface, .type_parameter_generic_method_constructor => .{
.type_parameter = .{
@ -2275,7 +2196,6 @@ pub const TypeAnnotation = struct {
const table_length = try input.takeInt(u16, .big);
const table = try allocator.alloc(TargetInfo.Localvar.Entry, table_length);
errdefer allocator.free(table);
for (table) |*entry| {
entry.* = .{
.start_pc = try input.takeInt(u16, .big),
@ -2316,16 +2236,11 @@ pub const TypeAnnotation = struct {
const num_element_value_pairs = try input.takeInt(u16, .big);
const element_value_pairs = try allocator.alloc(ElementValuePair, num_element_value_pairs);
errdefer allocator.free(element_value_pairs);
var parsed: usize = 0;
errdefer for (0..parsed) |i| element_value_pairs[i].value.deinit(allocator);
for (element_value_pairs) |*element_value_pair| {
element_value_pair.* = .{
.element_name_index = try input.takeInt(u16, .big),
.value = try .parse(input, allocator),
};
parsed += 1;
}
return .{
@ -2342,7 +2257,6 @@ pub const TypeAnnotation = struct {
.localvar => |localvar| allocator.free(localvar.table),
else => {},
}
for (self.element_value_pairs) |*element_value_pair| {
element_value_pair.value.deinit(allocator);
}
@ -2367,7 +2281,7 @@ pub const TypeAnnotation = struct {
_ = try w.write("target_path:\n");
try self.target_path.indentedFormat(w, depth + 1, indent);
const type_ = self.type(constant_pool) catch return error.WriteFailed;
const type_ = self.@"type"(constant_pool) catch return error.WriteFailed;
try w.splatByteAll(' ', depth * indent);
_ = try w.write("type:\n");
try type_.indentedFormat(w, depth, indent + 1, constant_pool);
@ -2399,13 +2313,8 @@ pub const RuntimeVisibleTypeAnnotations = struct {
const num_annotations = try input.takeInt(u16, .big);
const type_annotations = try allocator.alloc(TypeAnnotation, num_annotations);
errdefer allocator.free(type_annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| type_annotations[i].deinit(allocator);
for (type_annotations) |*type_annotation| {
type_annotation.* = try .parse(input, allocator);
parsed += 1;
}
return .{
@ -2430,13 +2339,8 @@ pub const RuntimeInvisibleTypeAnnotations = struct {
const num_annotations = try input.takeInt(u16, .big);
const type_annotations = try allocator.alloc(TypeAnnotation, num_annotations);
errdefer allocator.free(type_annotations);
var parsed: usize = 0;
errdefer for (0..parsed) |i| type_annotations[i].deinit(allocator);
for (type_annotations) |*type_annotation| {
type_annotation.* = try .parse(input, allocator);
parsed += 1;
}
return .{
@ -2513,17 +2417,12 @@ pub const BootstrapMethods = struct {
const num_bootstrap_methods = try input.takeInt(u16, .big);
const bootstrap_methods = try allocator.alloc(BootstrapMethod, num_bootstrap_methods);
errdefer allocator.free(bootstrap_methods);
var parsed: usize = 0;
errdefer for (0..parsed) |i| allocator.free(bootstrap_methods[i].bootstrap_arguments);
for (bootstrap_methods) |*bootstrap_method| {
const bootstrap_method_ref = try input.takeInt(u16, .big);
const num_bootstrap_arguments = try input.takeInt(u16, .big);
const bootstrap_arguments = try allocator.alloc(u16, num_bootstrap_arguments);
errdefer allocator.free(bootstrap_arguments);
for (bootstrap_arguments) |*bootstrap_argument| {
bootstrap_argument.* = try input.takeInt(u16, .big);
}
@ -2532,7 +2431,6 @@ pub const BootstrapMethods = struct {
.bootstrap_method_ref = bootstrap_method_ref,
.bootstrap_arguments = bootstrap_arguments,
};
parsed += 1;
}
return .{
@ -2590,11 +2488,10 @@ pub const MethodParameters = struct {
const parameter_count = try input.takeByte();
const parameters = try allocator.alloc(Parameter, parameter_count);
errdefer allocator.free(parameters);
for (parameters) |*parameter| {
parameter.* = .{
.name_index = try input.takeInt(u16, .big),
.access_flags = .from(try input.takeInt(u16, .big)),
.access_flags = .{ .mask = try input.takeInt(u16, .big) },
};
}
@ -2798,77 +2695,64 @@ pub const Module = struct {
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const module_name_index = try input.takeInt(u16, .big);
const module_flags: ModuleFlags = .from(try input.takeInt(u16, .big));
const module_flags: ModuleFlags = .{ .mask = try input.takeInt(u16, .big) };
const module_version_index = try input.takeInt(u16, .big);
const requires_count = try input.takeInt(u16, .big);
const requires = try allocator.alloc(Requires, requires_count);
errdefer allocator.free(requires);
for (requires) |*r| {
const requires_index = try input.takeInt(u16, .big);
const requires_flags: Requires.RequiresFlags = .{ .mask = try input.takeInt(u16, .big) };
const requires_version_index = try input.takeInt(u16, .big);
r.* = .{
.requires_index = try input.takeInt(u16, .big),
.requires_flags = .from(try input.takeInt(u16, .big)),
.requires_version_index = try input.takeInt(u16, .big),
.requires_index = requires_index,
.requires_flags = requires_flags,
.requires_version_index = requires_version_index,
};
}
const exports_count = try input.takeInt(u16, .big);
const exports = try allocator.alloc(Exports, exports_count);
errdefer allocator.free(exports);
var parsed_exports: usize = 0;
errdefer for (0..parsed_exports) |i| allocator.free(exports[i].exports_to_indeces);
for (exports) |*e| {
const exports_index = try input.takeInt(u16, .big);
const exports_flags: Exports.ExportsFlags = .from(try input.takeInt(u16, .big));
const exports_flags: Exports.ExportsFlags = .{ .mask = try input.takeInt(u16, .big) };
const exports_to_count = try input.takeInt(u16, .big);
const exports_to_indeces = try allocator.alloc(u16, exports_to_count);
errdefer allocator.free(exports_to_indeces);
for (exports_to_indeces) |*i| {
i.* = try input.takeInt(u16, .big);
}
e.* = .{
.exports_index = exports_index,
.exports_flags = exports_flags,
.exports_to_indeces = exports_to_indeces,
};
parsed_exports += 1;
}
const opens_count = try input.takeInt(u16, .big);
const opens = try allocator.alloc(Opens, opens_count);
errdefer allocator.free(opens);
var parsed_opens: usize = 0;
errdefer for (0..parsed_opens) |i| allocator.free(opens[i].opens_to_indeces);
for (opens) |*o| {
const opens_index = try input.takeInt(u16, .big);
const opens_flags: Opens.OpensFlags = .from(try input.takeInt(u16, .big));
const opens_flags: Opens.OpensFlags = .{ .mask = try input.takeInt(u16, .big) };
const opens_to_count = try input.takeInt(u16, .big);
const opens_to_indeces = try allocator.alloc(u16, opens_to_count);
errdefer allocator.free(opens_to_indeces);
for (opens_to_indeces) |*i| {
i.* = try input.takeInt(u16, .big);
}
o.* = .{
.opens_index = opens_index,
.opens_flags = opens_flags,
.opens_to_indeces = opens_to_indeces,
};
parsed_opens += 1;
}
const uses_count = try input.takeInt(u16, .big);
const uses_indeces = try allocator.alloc(u16, uses_count);
errdefer allocator.free(uses_indeces);
for (uses_indeces) |*i| {
i.* = try input.takeInt(u16, .big);
}
@ -2876,25 +2760,18 @@ pub const Module = struct {
const provides_count = try input.takeInt(u16, .big);
const provides = try allocator.alloc(Provides, provides_count);
errdefer allocator.free(provides);
var parsed_provides: usize = 0;
errdefer for (0..parsed_provides) |i| allocator.free(provides[i].provides_with_indeces);
for (provides) |*p| {
const provides_index = try input.takeInt(u16, .big);
const provides_with_count = try input.takeInt(u16, .big);
const provides_with_indeces = try allocator.alloc(u16, provides_with_count);
errdefer allocator.free(provides_with_indeces);
for (provides_with_indeces) |*i| {
i.* = try input.takeInt(u16, .big);
}
p.* = .{
.provides_index = provides_index,
.provides_with_indeces = provides_with_indeces,
};
parsed_provides += 1;
}
return .{
@ -2911,19 +2788,15 @@ pub const Module = struct {
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
allocator.free(self.requires);
for (self.exports) |*e| {
allocator.free(e.exports_to_indeces);
}
allocator.free(self.exports);
for (self.opens) |*o| {
allocator.free(o.opens_to_indeces);
}
allocator.free(self.opens);
allocator.free(self.uses_indeces);
for (self.provides) |*p| {
allocator.free(p.provides_with_indeces);
}
@ -3033,7 +2906,6 @@ pub const ModulePackages = struct {
const package_count = try input.takeInt(u16, .big);
const package_indeces = try allocator.alloc(u16, package_count);
errdefer allocator.free(package_indeces);
for (package_indeces) |*package_index| {
package_index.* = try input.takeInt(u16, .big);
}
@ -3088,7 +2960,6 @@ pub const NestMembers = struct {
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const number_of_classes = try input.takeInt(u16, .big);
const classes = try allocator.alloc(u16, number_of_classes);
for (classes) |*class| {
class.* = try input.takeInt(u16, .big);
}
@ -3120,13 +2991,8 @@ pub const Record = struct {
const attribute_count = try input.takeInt(u16, .big);
const attributes = try allocator.alloc(AttributeInfo, attribute_count);
errdefer allocator.free(attributes);
var parsed: usize = 0;
errdefer for (0..parsed) |i| attributes[i].deinit(allocator);
for (attributes) |*attribute| {
attribute.* = try .parse(input, constant_pool, allocator);
parsed += 1;
}
return .{
@ -3185,13 +3051,8 @@ pub const Record = struct {
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) (ParseError || ResolveError)!Self {
const components_count = try input.takeInt(u16, .big);
const components = try allocator.alloc(RecordComponentInfo, components_count);
var parsed: usize = 0;
errdefer for (0..parsed) |i| components[i].deinit(allocator);
for (components) |*component| {
component.* = try .parse(input, constant_pool, allocator);
parsed += 1;
}
return .{
@ -3215,7 +3076,6 @@ pub const PermittedSubclasses = struct {
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Self {
const number_of_classes = try input.takeInt(u16, .big);
const classes = try allocator.alloc(u16, number_of_classes);
for (classes) |*class| {
class.* = try input.takeInt(u16, .big);
}

File diff suppressed because it is too large Load diff

View file

@ -466,18 +466,8 @@ pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Sel
const constant_pool = try allocator.alloc(?Info, constant_pool_size - 1);
errdefer allocator.free(constant_pool);
var parsed: usize = 0;
errdefer {
for (0..parsed) |i| {
if (constant_pool[i]) |*info| {
info.deinit(allocator);
}
}
}
var i: usize = 0;
while (i < constant_pool_size - 1) {
defer parsed = i;
defer i += 1;
const info: Info = try .parse(input, allocator);

View file

@ -27,7 +27,7 @@ pub const FieldAccessFlags = EnumFlags(enum(u16) {
});
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self {
const access_flags: FieldAccessFlags = .from(try input.takeInt(u16, .big));
const access_flags: FieldAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
const name_index = try input.takeInt(u16, .big);
const descriptor_index = try input.takeInt(u16, .big);
const attributes = try Class.parseAttributes(input, constant_pool, allocator);
@ -87,3 +87,4 @@ pub fn name(self: *const Self, constant_pool: ConstantPool) ResolveError!Constan
pub fn descriptor(self: *const Self, constant_pool: ConstantPool) ResolveError!ConstantPool.Info {
return constant_pool.getTag(self.descriptor_index, .utf8);
}

View file

@ -30,7 +30,7 @@ pub const MethodAccessFlags = EnumFlags(enum(u16) {
});
pub fn parse(input: *std.Io.Reader, constant_pool: ConstantPool, allocator: std.mem.Allocator) !Self {
const access_flags: MethodAccessFlags = .from(try input.takeInt(u16, .big));
const access_flags: MethodAccessFlags = .{ .mask = try input.takeInt(u16, .big) };
const name_index = try input.takeInt(u16, .big);
const descriptor_index = try input.takeInt(u16, .big);
const attributes = try Class.parseAttributes(input, constant_pool, allocator);

View file

@ -10,40 +10,35 @@ pub fn EnumFlags(comptime E: type) type {
pub const empty: Self = .{ .data = 0 };
pub fn from(mask: BackingInt) Self {
return .{
.mask = mask,
};
}
pub fn contains(self: Self, flag: E) bool {
return (self.mask & @backingInt(flag)) != 0;
return (self.mask & @intFromEnum(flag)) != 0;
}
pub fn insert(self: *Self, flag: E) void {
self.mask |= @backingInt(flag);
self.mask |= @intFromEnum(flag);
}
pub fn remove(self: *Self, flag: E) void {
self.mask &= ~@backingInt(flag);
self.mask &= ~@intFromEnum(flag);
}
pub fn toggle(self: *Self, flag: E) void {
self.mask ^= @backingInt(flag);
self.mask ^= @intFromEnum(flag);
}
pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
var index: usize = 0;
inline for (@typeInfo(E).@"enum".field_names) |field| {
const key = @field(E, field);
inline for (std.meta.fields(E)) |field| {
const key = @field(E, field.name);
if (self.contains(key)) {
if (index > 0) {
_ = try w.write(" | ");
}
try w.print("{s}", .{field});
try w.print("{s}", .{ field.name });
index += 1;
}
}
}
};
}

57
src/Formatter.zig Normal file
View file

@ -0,0 +1,57 @@
const std = @import("std");
pub const Error = error {};
vtable: *const VTable,
w: *std.Io.Writer,
const Self = @This();
pub const VTable = struct {
printField: *const fn (
ptr: *Self,
comptime field: []const u8,
comptime fmt: []const u8,
args: anytype,
) Error!void,
};
pub fn printField(
self: *const Self,
comptime field: []const u8,
value: anytype,
) Error!void {
self.vtable.field(self, field);
self.vtable.value(self, value);
}
pub const IndentedFormatter = struct {
indent: u8 = 0,
depth: usize = 0,
interface: Self,
pub fn init(w: *std.Io.Writer) IndentedFormatter {
return .{
.interface = .{
.vtable = &.{
.printField = IndentedFormatter.printField,
},
.w = w,
},
};
}
fn printField(
f: *Self,
comptime field: []const u8,
comptime fmt: []const u8,
args: anytype,
) Error!void {
const self: *IndentedFormatter = @alignCast(@fieldParentPtr("interface", f));
_ = try self.interface.w.splatByteAll(' ', self.indent * self.depth);
try self.interface.w.print(field ++ ": " ++ fmt ++ "\n", args);
}
};

View file

@ -2,6 +2,8 @@ const std = @import("std");
const Class = @import("Class.zig");
const Formatter = @import("Formatter.zig");
pub fn main(init: std.process.Init) !void {
const io = init.io;
const allocator = init.gpa;
@ -19,17 +21,17 @@ pub fn main(init: std.process.Init) !void {
std.debug.assert(args.skip());
const file_path = if (args.next()) |fp| fp else {
try stderr.writeAll("No file argument provided.\n");
std.process.exit(1);
return;
};
const file = std.Io.Dir.cwd().openFile(io, file_path, .{}) catch |err| switch (err) {
error.FileNotFound => {
try stderr.writeAll("File not found.\n");
std.process.exit(1);
return;
},
error.IsDir => {
try stderr.writeAll("Path is a directory.\n");
std.process.exit(1);
return;
},
else => return err,
};
@ -42,17 +44,19 @@ pub fn main(init: std.process.Init) !void {
var class = Class.parse(reader, allocator) catch |err| switch (err) {
error.InvalidMagicNumber => {
try stderr.writeAll("Invalid magic number.\n");
std.process.exit(1);
return;
},
error.InvalidTag => {
try stderr.writeAll("Unrecognized tag type.\n");
std.process.exit(1);
return;
},
else => return err,
};
defer class.deinit(allocator);
defer class.deinit();
try stdout.print("{f}", .{class});
var indented_formatter: Formatter.IndentedFormatter = .init(stdout);
const formatter = &indented_formatter.interface;
try class.acceptFormatter(formatter);
try stdout.flush();
}

View file

@ -17,52 +17,6 @@ public class Main implements Runnable, Supplier<Integer>, Interface {
} finally {
System.err.println("Finally block");
}
var a = 8;
a <<= 3;
final var b = a >> 1;
switch (b) {
case 1:
break;
case 32:
System.out.println("Good");
break;
default:
System.out.println("Number " + a);
break;
}
final var o = getObject();
switch (o) {
case Integer i:
switch (i) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
break;
}
break;
default:
System.out.println("Something else");
break;
}
inc(5);
}
private static Object getObject() {
return 3;
}
private static void inc(int i) {
i += 1000;
}
@Override

Binary file not shown.

View file

@ -1,2 +0,0 @@
public record Record(String name, int age) {}