diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 18:55:36 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 18:55:36 +0200 |
| commit | c46957f44e8b5ba0639b7092a1c5abd9516b1b22 (patch) | |
| tree | fdd20da760740866060a139958e2f84a0e7fc81b /src/paragraph.zig | |
| parent | cf6f08d1602841f8c337dc64365e2dcde1e4c528 (diff) | |
fix(ast): small memory leak in subs
Diffstat (limited to 'src/paragraph.zig')
| -rw-r--r-- | src/paragraph.zig | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/paragraph.zig b/src/paragraph.zig index 222f376..caee73e 100644 --- a/src/paragraph.zig +++ b/src/paragraph.zig @@ -5,7 +5,7 @@ const Lexer = @import("lexer/Lexer.zig"); const Element = @import("dom/Element.zig"); const parser = @import("parser.zig"); -pub const Error = error{ModifierNotClosed, IllegalPlacement} || Lexer.Error; +pub const Error = error{ ModifierNotClosed, IllegalPlacement } || Lexer.Error; pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { var el = try Element.init(alloc, .content, "p"); @@ -47,7 +47,8 @@ pub fn parseLine(alloc: Allocator, l: *Lexer) Error!Element { fn parseContent(alloc: Allocator, l: *Lexer) Error!Element { var content = Element.initEmpty(alloc); errdefer content.deinit(); - const v = (try l.next(alloc)).?; + var v = (try l.next(alloc)).?; + defer v.deinit(); switch (v.kind) { .literal => { const el = try Element.initLitEscaped(alloc, v.content.items); @@ -76,3 +77,36 @@ fn parseModifier(alloc: Allocator, l: *Lexer, knd: Lexed.Kind, tag: []const u8) } return Error.ModifierNotClosed; } + +fn doTest(alloc: Allocator, t: []const u8, v: []const u8) !void { + var l = try Lexer.init(t); + var p = try parse(alloc, &l); + defer p.deinit(); + const g = try p.render(alloc); + defer alloc.free(g); + std.testing.expect(std.mem.eql(u8, g, v)) catch |err| { + std.debug.print("{s}\n", .{g}); + return err; + }; +} + +fn doTestError(alloc: Allocator, t: []const u8, err: Error) !void { + var l = try Lexer.init(t); + _ = parse(alloc, &l) catch |e| return std.testing.expect(err == e); + return std.testing.expect(false); +} + +test "parse paragraphs" { + var arena = std.heap.DebugAllocator(.{}).init; + defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{}); + const alloc = arena.allocator(); + + try doTest(alloc, "hello world", "<p>hello world</p>"); + try doTest(alloc, "*hello* world", "<p><b>hello</b> world</p>"); + try doTest(alloc, "*he_ll_o* world", "<p><b>he<em>ll</em>o</b> world</p>"); + + try doTestError(alloc, "hello *world", Error.ModifierNotClosed); + try doTestError(alloc, "hello *wo_rld*", Error.ModifierNotClosed); + try doTestError(alloc, "*hell*o *wo_rld*", Error.ModifierNotClosed); + try doTestError(alloc, "hello ::: world", Error.IllegalPlacement); +} |
