diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-18 20:21:10 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-18 20:21:10 +0200 |
| commit | 8c5214df39ab82c3d42ccd492d8699f8a5aadb0a (patch) | |
| tree | 3ca338fffd31620470a898fd900c7a5f12188c5a /src/parser.zig | |
| parent | 9b47b7ee8b80bf427116b4d2cf42e6f3c9d8be62 (diff) | |
feat(ast): test errors
Diffstat (limited to 'src/parser.zig')
| -rw-r--r-- | src/parser.zig | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/parser.zig b/src/parser.zig index 5f8c676..af571d4 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -7,9 +7,8 @@ const paragraph = @import("paragraph.zig"); const title = @import("title.zig"); pub const Error = error{ - InvalidSequence, FeatureNotSupported, -} || Lexer.Error || paragraph.Error; +} || Lexer.Error || paragraph.Error || title.Error; pub fn parse(parent: Allocator, content: []const u8) Error![]const u8 { var arena = std.heap.ArenaAllocator.init(parent); @@ -19,20 +18,21 @@ pub fn parse(parent: Allocator, content: []const u8) Error![]const u8 { var elements = try std.ArrayList(Element).initCapacity(alloc, 2); var l = try Lexer.init(content); - while (l.nextKind()) |it| { + base: while (l.nextKind()) |it| { try elements.append(alloc, switch (it) { .literal, .bold, .italic, .code => try paragraph.parse(alloc, &l), .title => try title.parse(alloc, &l), .weak_delimiter, .strong_delimiter => { var v = (try l.next(alloc)).?; v.deinit(); - continue; + continue :base; }, else => return Error.FeatureNotSupported, }); } var res = try std.ArrayList(u8).initCapacity(parent, elements.items.len); + errdefer res.deinit(parent); for (elements.items) |it| { var v = it; try res.appendSlice(parent, try v.render(alloc)); @@ -49,6 +49,11 @@ fn doTest(alloc: Allocator, t: []const u8, v: []const u8) !void { }; } +fn doTestError(alloc: Allocator, t: []const u8, err: Error) !void { + _ = parse(alloc, t) 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", .{}); @@ -65,13 +70,17 @@ test "parse paragraphs" { \\foo bar \\in new paragraph , "<p>hello world</p><p>foo bar in new paragraph</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); } test "parse title" { var arena = std.heap.DebugAllocator(.{}).init; defer if (arena.deinit() == .leak) std.debug.print("leaking!\n", .{}); const alloc = arena.allocator(); - + try doTest(alloc, "# hey", "<h1>hey</h1>"); try doTest(alloc, "## hey", "<h2>hey</h2>"); try doTest(alloc, "### hey", "<h3>hey</h3>"); @@ -82,4 +91,6 @@ test "parse title" { \\## subtitle \\hehe , "<h1>title</h1><p>hello world ;3</p><h2>subtitle</h2><p>hehe</p>"); + + try doTestError(alloc, "# aa :::", Error.InvalidTitleContent); } |
