From fac70f3ca8e5b9d3ddfdc6c47ecc8e344ae89de9 Mon Sep 17 00:00:00 2001 From: pwbh Date: Sat, 7 Sep 2024 18:05:39 +0300 Subject: [PATCH 1/3] fix small bugs --- src/root.zig | 2 +- src/tests.zig | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/root.zig b/src/root.zig index 44c7df4..d0b16be 100644 --- a/src/root.zig +++ b/src/root.zig @@ -458,7 +458,7 @@ pub fn Ymlz(comptime Destination: type) type { fn withoutQuotes(self: *Self, line: []const u8) []const u8 { _ = self; - if (line[0] == '\'' or line[0] == '"' and line[line.len - 1] == '\'' or line[line.len - 1] == '"') { + if ((line[0] == '\'' or line[0] == '"') and (line[line.len - 1] == '\'' or line[line.len - 1] == '"')) { return line[1 .. line.len - 1]; } diff --git a/src/tests.zig b/src/tests.zig index b8dfa45..403a359 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -29,7 +29,11 @@ test "98YD" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 5.5. Comment Indicator")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "Spec Example 5.5. Comment Indicator")); + // dump: "" + try expect(element.dump.len == 0); } test "CC74" { @@ -57,7 +61,11 @@ test "CC74" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "Spec Example 6.20. Tag Handles")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "Spec Example 6.20. Tag Handles")); + std.debug.print("parsed:{s}\n", .{element.dump}); + try expect(std.mem.eql(u8, element.dump, "- ! \"bar\"")); } test "F6MC" { From 7146a0a45747e4af2f67e34bf0831374ac3d49a0 Mon Sep 17 00:00:00 2001 From: pwbh Date: Sat, 7 Sep 2024 18:13:23 +0300 Subject: [PATCH 2/3] fix --- src/root.zig | 24 ++++++++++++++++-------- src/tests.zig | 3 +-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/root.zig b/src/root.zig index d0b16be..7695cb7 100644 --- a/src/root.zig +++ b/src/root.zig @@ -226,7 +226,7 @@ pub fn Ymlz(comptime Destination: type) type { }, .Pointer => { if (actual_type_info.Pointer.size == .Slice and actual_type_info.Pointer.child == u8) { - @field(destination, field.name) = try self.parseStringExpression(raw_line, depth); + @field(destination, field.name) = try self.parseStringExpression(raw_line, depth, false); } else if (actual_type_info.Pointer.size == .Slice and (actual_type_info.Pointer.child == []const u8 or actual_type_info.Pointer.child == []u8)) { @field(destination, field.name) = try self.parseStringArrayExpression(actual_type_info.Pointer.child, depth + 1); } else if (actual_type_info.Pointer.size == .Slice and @typeInfo(actual_type_info.Pointer.child) != .Pointer) { @@ -333,7 +333,7 @@ pub fn Ymlz(comptime Destination: type) type { break; } - const result = try self.parseStringExpression(raw_value_line, depth); + const result = try self.parseStringExpression(raw_value_line, depth, false); try list.append(result); } @@ -362,8 +362,8 @@ pub fn Ymlz(comptime Destination: type) type { return try list.toOwnedSlice(); } - fn parseStringExpression(self: *Self, raw_line: []const u8, depth: usize) ![]const u8 { - const expression = try self.parseSimpleExpression(raw_line, depth); + fn parseStringExpression(self: *Self, raw_line: []const u8, depth: usize, is_multiline: bool) ![]const u8 { + const expression = try self.parseSimpleExpression(raw_line, depth, is_multiline); const value = self.getExpressionValue(expression); if (value.len == 0) return value; @@ -393,7 +393,7 @@ pub fn Ymlz(comptime Destination: type) type { break; } - const expression = try self.parseSimpleExpression(raw_value_line, depth); + const expression = try self.parseSimpleExpression(raw_value_line, depth, true); const value = self.getExpressionValue(expression); try list.appendSlice(value); @@ -420,7 +420,7 @@ pub fn Ymlz(comptime Destination: type) type { } fn parseBooleanExpression(self: *Self, raw_line: []const u8, depth: usize) !bool { - const expression = try self.parseSimpleExpression(raw_line, depth); + const expression = try self.parseSimpleExpression(raw_line, depth, false); const value = self.getExpressionValue(expression); const isBooleanTrue = std.mem.eql(u8, value, "True") or std.mem.eql(u8, value, "true") or std.mem.eql(u8, value, "On") or std.mem.eql(u8, value, "on"); @@ -439,7 +439,7 @@ pub fn Ymlz(comptime Destination: type) type { } fn parseNumericExpression(self: *Self, comptime T: type, raw_line: []const u8, depth: usize) !T { - const expression = try self.parseSimpleExpression(raw_line, depth); + const expression = try self.parseSimpleExpression(raw_line, depth, false); const value = self.getExpressionValue(expression); switch (@typeInfo(T)) { @@ -465,7 +465,7 @@ pub fn Ymlz(comptime Destination: type) type { return line; } - fn parseSimpleExpression(self: *Self, raw_line: []const u8, depth: usize) !Expression { + fn parseSimpleExpression(self: *Self, raw_line: []const u8, depth: usize, is_multiline: bool) !Expression { const indent_depth = self.getIndentDepth(depth); if (raw_line.len < indent_depth) { @@ -475,6 +475,14 @@ pub fn Ymlz(comptime Destination: type) type { }; } + // NOTE: Need to think about this a bit more, maybe there is a cleaner solution for this. + if (is_multiline) { + return .{ + .value = .{ .Simple = raw_line[indent_depth..] }, + .raw = raw_line, + }; + } + const line = raw_line[indent_depth..]; if (line[0] == '-') { diff --git a/src/tests.zig b/src/tests.zig index 403a359..53db4a2 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -64,8 +64,7 @@ test "CC74" { const element = result.elements[0]; try expect(std.mem.eql(u8, element.name, "Spec Example 6.20. Tag Handles")); - std.debug.print("parsed:{s}\n", .{element.dump}); - try expect(std.mem.eql(u8, element.dump, "- ! \"bar\"")); + try expect(std.mem.eql(u8, element.dump, "--- ! \"bar\"\n")); } test "F6MC" { From d4818ed78bafd3da511095f6a80b52ac79d7550c Mon Sep 17 00:00:00 2001 From: pwbh Date: Sat, 7 Sep 2024 18:26:43 +0300 Subject: [PATCH 3/3] more fixes --- src/root.zig | 6 +----- src/tests.zig | 6 +++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/root.zig b/src/root.zig index 7695cb7..8712ab3 100644 --- a/src/root.zig +++ b/src/root.zig @@ -293,12 +293,8 @@ pub fn Ymlz(comptime Destination: type) type { if (raw_line) |line| { try self.allocations.append(line); - if (line.len == 0) { - return "\n"; - } - // TODO: Need to fix this comments can start not only from index 0. - if (line[0] == '#') { + if (line.len == 0 or line[0] == '#') { // Skipping comments return self.readLine(); } diff --git a/src/tests.zig b/src/tests.zig index 53db4a2..58b2e82 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -64,6 +64,7 @@ test "CC74" { const element = result.elements[0]; try expect(std.mem.eql(u8, element.name, "Spec Example 6.20. Tag Handles")); + try expect(std.mem.eql(u8, element.tree, "+STR\n +DOC ---\n =VAL \"bar\n -DOC\n-STR")); try expect(std.mem.eql(u8, element.dump, "--- ! \"bar\"\n")); } @@ -92,5 +93,8 @@ test "F6MC" { const result = try ymlz.loadFile(yml_file_location); defer ymlz.deinit(result); - try expect(std.mem.eql(u8, result.elements[0].name, "More indented lines at the beginning of folded block scalars")); + const element = result.elements[0]; + + try expect(std.mem.eql(u8, element.name, "More indented lines at the beginning of folded block scalars")); + try expect(std.mem.eql(u8, element.json, "{\n \"a\": \" more indented\\nregular\\n\",\n \"b\": \"\\n\\n more indented\\nregular\\n\"\n}")); }