zig fmt **.zig

This commit is contained in:
Robin Voetter
2021-06-13 15:15:42 +02:00
parent 4b4ef38c93
commit 419e541a16
12 changed files with 313 additions and 342 deletions

View File

@@ -3,9 +3,7 @@ const vk = @import("vulkan");
const c = @import("c.zig"); const c = @import("c.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const required_device_extensions = [_][]const u8{ const required_device_extensions = [_][]const u8{vk.extension_info.khr_swapchain.name};
vk.extension_info.khr_swapchain.name
};
const BaseDispatch = struct { const BaseDispatch = struct {
vkCreateInstance: vk.PfnCreateInstance, vkCreateInstance: vk.PfnCreateInstance,
@@ -207,7 +205,7 @@ fn initializeCandidate(vki: InstanceDispatch, candidate: DeviceCandidate) !vk.De
.queue_family_index = candidate.queues.present_family, .queue_family_index = candidate.queues.present_family,
.queue_count = 1, .queue_count = 1,
.p_queue_priorities = &priority, .p_queue_priorities = &priority,
} },
}; };
const queue_count: u32 = if (candidate.queues.graphics_family == candidate.queues.present_family) const queue_count: u32 = if (candidate.queues.graphics_family == candidate.queues.present_family)
@@ -281,19 +279,14 @@ fn checkSuitable(
return DeviceCandidate{ return DeviceCandidate{
.pdev = pdev, .pdev = pdev,
.props = props, .props = props,
.queues = allocation .queues = allocation,
}; };
} }
return null; return null;
} }
fn allocateQueues( fn allocateQueues(vki: InstanceDispatch, pdev: vk.PhysicalDevice, allocator: *Allocator, surface: vk.SurfaceKHR) !?QueueAllocation {
vki: InstanceDispatch,
pdev: vk.PhysicalDevice,
allocator: *Allocator,
surface: vk.SurfaceKHR
) !?QueueAllocation {
var family_count: u32 = undefined; var family_count: u32 = undefined;
vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null); vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null);
@@ -307,7 +300,7 @@ fn allocateQueues(
for (families) |properties, i| { for (families) |properties, i| {
const family = @intCast(u32, i); const family = @intCast(u32, i);
if (graphics_family == null and properties.queue_flags.contains(.{.graphics_bit = true})) { if (graphics_family == null and properties.queue_flags.graphics_bit) {
graphics_family = family; graphics_family = family;
} }
@@ -319,7 +312,7 @@ fn allocateQueues(
if (graphics_family != null and present_family != null) { if (graphics_family != null and present_family != null) {
return QueueAllocation{ return QueueAllocation{
.graphics_family = graphics_family.?, .graphics_family = graphics_family.?,
.present_family = present_family.? .present_family = present_family.?,
}; };
} }

View File

@@ -52,7 +52,7 @@ pub fn main() !void {
@intCast(c_int, extent.height), @intCast(c_int, extent.height),
app_name, app_name,
null, null,
null null,
) orelse return error.WindowInitFailed; ) orelse return error.WindowInitFailed;
defer c.glfwDestroyWindow(window); defer c.glfwDestroyWindow(window);
@@ -114,7 +114,7 @@ pub fn main() !void {
swapchain.extent, swapchain.extent,
render_pass, render_pass,
pipeline, pipeline,
framebuffers framebuffers,
); );
defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs); defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs);
@@ -146,13 +146,12 @@ pub fn main() !void {
swapchain.extent, swapchain.extent,
render_pass, render_pass,
pipeline, pipeline,
framebuffers framebuffers,
); );
} }
c.glfwSwapBuffers(window); c.glfwSwapBuffers(window);
c.glfwPollEvents(); c.glfwPollEvents();
} }
try swapchain.waitForAllFences(); try swapchain.waitForAllFences();
@@ -297,12 +296,7 @@ fn destroyCommandBuffers(gc: *const GraphicsContext, pool: vk.CommandPool, alloc
allocator.free(cmdbufs); allocator.free(cmdbufs);
} }
fn createFramebuffers( fn createFramebuffers(gc: *const GraphicsContext, allocator: *Allocator, render_pass: vk.RenderPass, swapchain: Swapchain) ![]vk.Framebuffer {
gc: *const GraphicsContext,
allocator: *Allocator,
render_pass: vk.RenderPass,
swapchain: Swapchain
) ![]vk.Framebuffer {
const framebuffers = try allocator.alloc(vk.Framebuffer, swapchain.swap_images.len); const framebuffers = try allocator.alloc(vk.Framebuffer, swapchain.swap_images.len);
errdefer allocator.free(framebuffers); errdefer allocator.free(framebuffers);
@@ -506,7 +500,8 @@ fn createPipeline(
_ = try gc.vkd.createGraphicsPipelines( _ = try gc.vkd.createGraphicsPipelines(
gc.dev, gc.dev,
.null_handle, .null_handle,
1, @ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci), 1,
@ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci),
null, null,
@ptrCast([*]vk.Pipeline, &pipeline), @ptrCast([*]vk.Pipeline, &pipeline),
); );

View File

@@ -143,15 +143,12 @@ pub const CTokenizer = struct {
']' => kind = .rbracket, ']' => kind = .rbracket,
'(' => kind = .lparen, '(' => kind = .lparen,
')' => kind = .rparen, ')' => kind = .rparen,
else => return error.UnexpectedCharacter else => return error.UnexpectedCharacter,
} }
const start = self.offset; const start = self.offset;
_ = self.consumeNoEof(); _ = self.consumeNoEof();
return Token{ return Token{ .kind = kind, .text = self.source[start..self.offset] };
.kind = kind,
.text = self.source[start .. self.offset]
};
} }
}; };
@@ -365,7 +362,7 @@ fn parseDeclaration(allocator: *Allocator, xctok: *XmlCTokenizer) ParseError!Dec
.array = .{ .array = .{
.size = array_size, .size = array_size,
.child = child, .child = child,
} },
}; };
// update the inner_type pointer so it points to the proper // update the inner_type pointer so it points to the proper
@@ -403,8 +400,8 @@ fn parseFnPtrSuffix(allocator: *Allocator, xctok: *XmlCTokenizer, return_type: T
.return_type = return_type_heap, .return_type = return_type_heap,
.success_codes = &[_][]const u8{}, .success_codes = &[_][]const u8{},
.error_codes = &[_][]const u8{}, .error_codes = &[_][]const u8{},
} },
} },
}; };
const first_param = try parseDeclaration(allocator, xctok); const first_param = try parseDeclaration(allocator, xctok);
@@ -500,10 +497,10 @@ fn parseArrayDeclarator(xctok: *XmlCTokenizer) !?ArraySize {
.int = std.fmt.parseInt(usize, size_tok.text, 10) catch |err| switch (err) { .int = std.fmt.parseInt(usize, size_tok.text, 10) catch |err| switch (err) {
error.Overflow => return error.Overflow, error.Overflow => return error.Overflow,
error.InvalidCharacter => unreachable, error.InvalidCharacter => unreachable,
} },
}, },
.enum_name => .{ .alias = size_tok.text }, .enum_name => .{ .alias = size_tok.text },
else => return error.InvalidSyntax else => return error.InvalidSyntax,
}; };
_ = try xctok.expect(.rbracket); _ = try xctok.expect(.rbracket);
@@ -551,13 +548,9 @@ fn testTokenizer(tokenizer: anytype, expected_tokens: []const Token) !void {
} }
test "CTokenizer" { test "CTokenizer" {
var ctok = CTokenizer { var ctok = CTokenizer{ .source = "typedef ([const)]** VKAPI_PTR 123,;aaaa" };
.source = \\typedef ([const)]** VKAPI_PTR 123,;aaaa
};
try testTokenizer( try testTokenizer(&ctok, &[_]Token{
&ctok,
&[_]Token{
.{ .kind = .kw_typedef, .text = "typedef" }, .{ .kind = .kw_typedef, .text = "typedef" },
.{ .kind = .lparen, .text = "(" }, .{ .kind = .lparen, .text = "(" },
.{ .kind = .lbracket, .text = "[" }, .{ .kind = .lbracket, .text = "[" },
@@ -571,13 +564,11 @@ test "CTokenizer" {
.{ .kind = .comma, .text = "," }, .{ .kind = .comma, .text = "," },
.{ .kind = .semicolon, .text = ";" }, .{ .kind = .semicolon, .text = ";" },
.{ .kind = .id, .text = "aaaa" }, .{ .kind = .id, .text = "aaaa" },
} });
);
} }
test "XmlCTokenizer" { test "XmlCTokenizer" {
const document = try xml.parse( const document = try xml.parse(testing.allocator,
testing.allocator,
\\<root>// comment <name>commented name</name> <type>commented type</type> trailing \\<root>// comment <name>commented name</name> <type>commented type</type> trailing
\\ typedef void (VKAPI_PTR *<name>PFN_vkVoidFunction</name>)(void); \\ typedef void (VKAPI_PTR *<name>PFN_vkVoidFunction</name>)(void);
\\</root> \\</root>
@@ -586,9 +577,7 @@ test "XmlCTokenizer" {
var xctok = XmlCTokenizer.init(document.root); var xctok = XmlCTokenizer.init(document.root);
try testTokenizer( try testTokenizer(&xctok, &[_]Token{
&xctok,
&[_]Token{
.{ .kind = .kw_typedef, .text = "typedef" }, .{ .kind = .kw_typedef, .text = "typedef" },
.{ .kind = .id, .text = "void" }, .{ .kind = .id, .text = "void" },
.{ .kind = .lparen, .text = "(" }, .{ .kind = .lparen, .text = "(" },
@@ -600,13 +589,11 @@ test "XmlCTokenizer" {
.{ .kind = .id, .text = "void" }, .{ .kind = .id, .text = "void" },
.{ .kind = .rparen, .text = ")" }, .{ .kind = .rparen, .text = ")" },
.{ .kind = .semicolon, .text = ";" }, .{ .kind = .semicolon, .text = ";" },
} });
);
} }
test "parseTypedef" { test "parseTypedef" {
const document = try xml.parse( const document = try xml.parse(testing.allocator,
testing.allocator,
\\<root> // comment <name>commented name</name> trailing \\<root> // comment <name>commented name</name> trailing
\\ typedef const struct <type>Python</type>* pythons[4]; \\ typedef const struct <type>Python</type>* pythons[4];
\\ // more comments \\ // more comments

View File

@@ -118,10 +118,13 @@ fn parseBitmaskType(ty: *xml.Element) !registry.Declaration {
return registry.Declaration{ return registry.Declaration{
.name = ty.getCharData("name") orelse return error.InvalidRegistry, .name = ty.getCharData("name") orelse return error.InvalidRegistry,
.decl_type = .{.bitmask = .{ .decl_type = .{
.bits_enum = ty.getAttribute("requires") orelse ty.getAttribute("bitvalues"), // Who knows why these are different fields .bitmask = .{
// Who knows why these are different fields
.bits_enum = ty.getAttribute("requires") orelse ty.getAttribute("bitvalues"),
.bitwidth = bitwidth, .bitwidth = bitwidth,
}}, },
},
}; };
} }
} }
@@ -132,7 +135,9 @@ fn parseHandleType(ty: *xml.Element) !registry.Declaration {
const alias = ty.getAttribute("alias") orelse return error.InvalidRegistry; const alias = ty.getAttribute("alias") orelse return error.InvalidRegistry;
return registry.Declaration{ return registry.Declaration{
.name = name, .name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}}, .decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
}; };
} else { } else {
const name = ty.getCharData("name") orelse return error.InvalidRegistry; const name = ty.getCharData("name") orelse return error.InvalidRegistry;
@@ -148,7 +153,7 @@ fn parseHandleType(ty: *xml.Element) !registry.Declaration {
.handle = .{ .handle = .{
.parent = ty.getAttribute("parent"), .parent = ty.getAttribute("parent"),
.is_dispatchable = dispatchable, .is_dispatchable = dispatchable,
} },
}, },
}; };
} }
@@ -175,7 +180,9 @@ fn parseContainer(allocator: *Allocator, ty: *xml.Element, is_union: bool) !regi
if (ty.getAttribute("alias")) |alias| { if (ty.getAttribute("alias")) |alias| {
return registry.Declaration{ return registry.Declaration{
.name = name, .name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}}, .decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
}; };
} }
@@ -211,8 +218,8 @@ fn parseContainer(allocator: *Allocator, ty: *xml.Element, is_union: bool) !regi
.stype = maybe_stype, .stype = maybe_stype,
.fields = members, .fields = members,
.is_union = is_union, .is_union = is_union,
} },
} },
}; };
} }
@@ -295,7 +302,9 @@ fn parseEnumAlias(allocator: *Allocator, elem: *xml.Element) !?registry.Declarat
const name = elem.getAttribute("name") orelse return error.InvalidRegistry; const name = elem.getAttribute("name") orelse return error.InvalidRegistry;
return registry.Declaration{ return registry.Declaration{
.name = name, .name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}}, .decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
}; };
} }
@@ -419,7 +428,9 @@ fn parseCommand(allocator: *Allocator, elem: *xml.Element) !registry.Declaration
const name = elem.getAttribute("name") orelse return error.InvalidRegistry; const name = elem.getAttribute("name") orelse return error.InvalidRegistry;
return registry.Declaration{ return registry.Declaration{
.name = name, .name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_command}} .decl_type = .{
.alias = .{ .name = alias, .target = .other_command },
},
}; };
} }
@@ -471,8 +482,8 @@ fn parseCommand(allocator: *Allocator, elem: *xml.Element) !registry.Declaration
.return_type = return_type, .return_type = return_type,
.success_codes = success_codes, .success_codes = success_codes,
.error_codes = error_codes, .error_codes = error_codes,
} },
} },
}; };
} }
@@ -546,9 +557,7 @@ fn parseDefines(types: *xml.Element, out: []registry.ApiConstant) !usize {
var xctok = cparse.XmlCTokenizer.init(ty); var xctok = cparse.XmlCTokenizer.init(ty);
out[i] = .{ out[i] = .{
.name = name, .name = name,
.value = .{ .value = .{ .version = cparse.parseVersion(&xctok) catch continue },
.version = cparse.parseVersion(&xctok) catch continue
},
}; };
} }
i += 1; i += 1;
@@ -609,7 +618,7 @@ fn parseFeature(allocator: *Allocator, feature: *xml.Element) !registry.Feature
return registry.Feature{ return registry.Feature{
.name = name, .name = name,
.level = feature_level, .level = feature_level,
.requires = allocator.shrink(requires, i) .requires = allocator.shrink(requires, i),
}; };
} }
@@ -642,7 +651,10 @@ fn parseEnumExtension(elem: *xml.Element, parent_extnumber: ?u31) !?registry.Req
return registry.Require.EnumExtension{ return registry.Require.EnumExtension{
.extends = extends, .extends = extends,
.extnumber = actual_extnumber, .extnumber = actual_extnumber,
.field = .{.name = name, .value = .{.int = value}}, .field = .{
.name = name,
.value = .{ .int = value },
},
}; };
} }
@@ -771,7 +783,6 @@ fn parseExtension(allocator: *Allocator, extension: *xml.Element) !registry.Exte
const promotedto = extension.getAttribute("promotedto") orelse break :blk .none; const promotedto = extension.getAttribute("promotedto") orelse break :blk .none;
if (mem.startsWith(u8, promotedto, "VK_VERSION_")) { if (mem.startsWith(u8, promotedto, "VK_VERSION_")) {
const feature_level = try splitFeatureLevel(promotedto["VK_VERSION_".len..], "_"); const feature_level = try splitFeatureLevel(promotedto["VK_VERSION_".len..], "_");
break :blk .{ .feature = feature_level }; break :blk .{ .feature = feature_level };
} }
@@ -816,7 +827,7 @@ fn parseExtension(allocator: *Allocator, extension: *xml.Element) !registry.Exte
.promoted_to = promoted_to, .promoted_to = promoted_to,
.platform = platform, .platform = platform,
.required_feature_level = requires_core, .required_feature_level = requires_core,
.requires = allocator.shrink(requires, i) .requires = allocator.shrink(requires, i),
}; };
} }

View File

@@ -70,15 +70,12 @@ pub const Container = struct {
}; };
pub const Enum = struct { pub const Enum = struct {
pub const Value = union(enum) { pub const Value = union(enum) { bitpos: u6, // 1 << bitpos
bitpos: u6, // 1 << bitpos
bit_vector: i32, // Combined flags & some vendor IDs bit_vector: i32, // Combined flags & some vendor IDs
int: i32, int: i32, alias: struct {
alias: struct {
name: []const u8, name: []const u8,
is_compat_alias: bool, is_compat_alias: bool,
} } };
};
pub const Field = struct { pub const Field = struct {
name: []const u8, name: []const u8,
@@ -118,7 +115,7 @@ pub const Pointer = struct {
one, one,
many, // The length is given by some complex expression, possibly involving another field many, // The length is given by some complex expression, possibly involving another field
other_field: []const u8, // The length is given by some other field or parameter other_field: []const u8, // The length is given by some other field or parameter
zero_terminated zero_terminated,
}; };
is_const: bool, is_const: bool,

View File

@@ -539,7 +539,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierFmt("{s}Flags{s}{s}", .{ try self.writeIdentifierFmt("{s}Flags{s}{s}", .{
trimVkNamespace(bitflag_name.base_name), trimVkNamespace(bitflag_name.base_name),
@as([]const u8, if (bitflag_name.revision) |revision| revision else ""), @as([]const u8, if (bitflag_name.revision) |revision| revision else ""),
@as([]const u8, if (bitflag_name.tag) |tag| tag else "") @as([]const u8, if (bitflag_name.tag) |tag| tag else ""),
}); });
return; return;
} else if (mem.startsWith(u8, name, "vk")) { } else if (mem.startsWith(u8, name, "vk")) {
@@ -579,7 +579,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierFmt("{s}Flags{s}{s}", .{ try self.writeIdentifierFmt("{s}Flags{s}{s}", .{
trimVkNamespace(bitflag_name.base_name), trimVkNamespace(bitflag_name.base_name),
@as([]const u8, if (bitflag_name.revision) |revision| revision else ""), @as([]const u8, if (bitflag_name.revision) |revision| revision else ""),
@as([]const u8, if (bitflag_name.tag) |tag| tag else "") @as([]const u8, if (bitflag_name.tag) |tag| tag else ""),
}); });
try self.writer.writeAll(".IntType"); try self.writer.writeAll(".IntType");
break :blk; break :blk;
@@ -828,15 +828,13 @@ fn Renderer(comptime WriterType: type) type {
\\ = packed struct {{ \\ = packed struct {{
\\_reserved_bits: {s} = 0, \\_reserved_bits: {s} = 0,
\\pub usingnamespace FlagsMixin( \\pub usingnamespace FlagsMixin(
, .{ flags_type } , .{flags_type});
);
try self.renderName(name); try self.renderName(name);
try self.writer.print( try self.writer.print(
\\, {s}); \\, {s});
\\}}; \\}};
\\ \\
, .{ flags_type } , .{flags_type});
);
} }
} }
@@ -943,8 +941,7 @@ fn Renderer(comptime WriterType: type) type {
\\pub fn {s}(comptime Self: type) type {{ \\pub fn {s}(comptime Self: type) type {{
\\ return struct {{ \\ return struct {{
\\ \\
, .{name} , .{name});
);
try self.renderWrapperLoader(dispatch_type); try self.renderWrapperLoader(dispatch_type);
@@ -986,8 +983,7 @@ fn Renderer(comptime WriterType: type) type {
\\ return self; \\ return self;
\\}} \\}}
\\ \\
, .{params, loader_first_param} , .{ params, loader_first_param });
);
} }
fn derefName(name: []const u8) []const u8 { fn derefName(name: []const u8) []const u8 {
@@ -1016,7 +1012,8 @@ fn Renderer(comptime WriterType: type) type {
.bitflags, // Special stuff handled in renderWrapperCall .bitflags, // Special stuff handled in renderWrapperCall
.buffer_len, .buffer_len,
.mut_buffer_len, .mut_buffer_len,
.other => { .other,
=> {
try self.writeIdentifierWithCase(.snake, param.name); try self.writeIdentifierWithCase(.snake, param.name);
try self.writer.writeAll(": "); try self.writer.writeAll(": ");
try self.renderTypeInfo(param.param_type); try self.renderTypeInfo(param.param_type);
@@ -1064,10 +1061,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierWithCase(.snake, param.name); try self.writeIdentifierWithCase(.snake, param.name);
try self.writer.writeAll(".toInt()"); try self.writer.writeAll(".toInt()");
}, },
.in_out_pointer, .in_out_pointer, .buffer_len, .mut_buffer_len, .other => {
.buffer_len,
.mut_buffer_len,
.other => {
try self.writeIdentifierWithCase(.snake, param.name); try self.writeIdentifierWithCase(.snake, param.name);
}, },
} }
@@ -1131,16 +1125,13 @@ fn Renderer(comptime WriterType: type) type {
try self.writer.writeAll(": "); try self.writer.writeAll(": ");
try self.renderTypeInfo(ret.return_value_type); try self.renderTypeInfo(ret.return_value_type);
try self.writer.writeAll(", "); try self.writer.writeAll(", ");
} }
try self.writer.writeAll("};\n"); try self.writer.writeAll("};\n");
} }
fn renderWrapper(self: *Self, name: []const u8, command: reg.Command) !void { fn renderWrapper(self: *Self, name: []const u8, command: reg.Command) !void {
const returns_vk_result = command.return_type.* == .name const returns_vk_result = command.return_type.* == .name and mem.eql(u8, command.return_type.name, "VkResult");
and mem.eql(u8, command.return_type.name, "VkResult"); const returns_void = command.return_type.* == .name and mem.eql(u8, command.return_type.name, "void");
const returns_void = command.return_type.* == .name
and mem.eql(u8, command.return_type.name, "void");
const returns = try self.extractReturns(command); const returns = try self.extractReturns(command);

View File

@@ -7,13 +7,13 @@ const ArrayList = std.ArrayList;
pub const Attribute = struct { pub const Attribute = struct {
name: []const u8, name: []const u8,
value: []const u8 value: []const u8,
}; };
pub const Content = union(enum) { pub const Content = union(enum) {
CharData: []const u8, CharData: []const u8,
Comment: []const u8, Comment: []const u8,
Element: *Element Element: *Element,
}; };
pub const Element = struct { pub const Element = struct {
@@ -50,7 +50,7 @@ pub const Element = struct {
return switch (child.children.items[0]) { return switch (child.children.items[0]) {
.CharData => |char_data| char_data, .CharData => |char_data| char_data,
else => null else => null,
}; };
} }
@@ -74,7 +74,7 @@ pub const Element = struct {
pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator { pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator {
return .{ return .{
.inner = self.elements(), .inner = self.elements(),
.tag = tag .tag = tag,
}; };
} }
@@ -129,7 +129,7 @@ pub const Element = struct {
pub const XmlDecl = struct { pub const XmlDecl = struct {
version: []const u8, version: []const u8,
encoding: ?[]const u8, encoding: ?[]const u8,
standalone: ?bool standalone: ?bool,
}; };
pub const Document = struct { pub const Document = struct {
@@ -154,7 +154,7 @@ const ParseContext = struct {
.source = source, .source = source,
.offset = 0, .offset = 0,
.line = 0, .line = 0,
.column = 0 .column = 0,
}; };
} }
@@ -232,7 +232,7 @@ const ParseContext = struct {
ws = true; ws = true;
_ = self.consumeNoEof(); _ = self.consumeNoEof();
}, },
else => break else => break,
} }
} }
@@ -311,7 +311,7 @@ pub const ParseError = error {
InvalidStandaloneValue, InvalidStandaloneValue,
NonMatchingClosingTag, NonMatchingClosingTag,
InvalidDocument, InvalidDocument,
OutOfMemory OutOfMemory,
}; };
pub fn parse(backing_allocator: *Allocator, source: []const u8) !Document { pub fn parse(backing_allocator: *Allocator, source: []const u8) !Document {
@@ -323,7 +323,7 @@ fn parseDocument(ctx: *ParseContext, backing_allocator: *Allocator) !Document {
var doc = Document{ var doc = Document{
.arena = ArenaAllocator.init(backing_allocator), .arena = ArenaAllocator.init(backing_allocator),
.xml_decl = null, .xml_decl = null,
.root = undefined .root = undefined,
}; };
errdefer doc.deinit(); errdefer doc.deinit();
@@ -376,7 +376,7 @@ fn parseNameNoDupe(ctx: *ParseContext) ![]const u8 {
switch (ch) { switch (ch) {
' ', '\t', '\n', '\r' => break, ' ', '\t', '\n', '\r' => break,
'&', '"', '\'', '<', '>', '?', '=', '/' => break, '&', '"', '\'', '<', '>', '?', '=', '/' => break,
else => _ = ctx.consumeNoEof() else => _ = ctx.consumeNoEof(),
} }
} }
@@ -392,7 +392,7 @@ fn tryParseCharData(ctx: *ParseContext, alloc: *Allocator) !?[]const u8 {
while (ctx.peek()) |ch| { while (ctx.peek()) |ch| {
switch (ch) { switch (ch) {
'<' => break, '<' => break,
else => _ = ctx.consumeNoEof() else => _ = ctx.consumeNoEof(),
} }
} }
@@ -605,17 +605,14 @@ fn tryParseComment(ctx: *ParseContext, alloc: *Allocator) !?[]const u8 {
} }
fn unescapeEntity(text: []const u8) !u8 { fn unescapeEntity(text: []const u8) !u8 {
const EntitySubstition = struct { const EntitySubstition = struct { text: []const u8, replacement: u8 };
text: []const u8,
replacement: u8
};
const entities = [_]EntitySubstition{ const entities = [_]EntitySubstition{
.{ .text = "&lt;", .replacement = '<' }, .{ .text = "&lt;", .replacement = '<' },
.{ .text = "&gt;", .replacement = '>' }, .{ .text = "&gt;", .replacement = '>' },
.{ .text = "&amp;", .replacement = '&' }, .{ .text = "&amp;", .replacement = '&' },
.{ .text = "&apos;", .replacement = '\'' }, .{ .text = "&apos;", .replacement = '\'' },
.{.text = "&quot;", .replacement = '"'} .{ .text = "&quot;", .replacement = '"' },
}; };
for (entities) |entity| { for (entities) |entity| {