diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 22:28:56 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 22:28:56 +0200 |
| commit | db2f49115713c47b48587022511b3654a7042493 (patch) | |
| tree | 159535d22691f2d1287e568e44fe3a90dc8443c7 /src/parser.zig | |
| parent | b9d58fbbd94dc8f97f9f4c6c333039386bbbeaa1 (diff) | |
feat(lib): support input from file and from reader
Diffstat (limited to 'src/parser.zig')
| -rw-r--r-- | src/parser.zig | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/parser.zig b/src/parser.zig index 775dc4a..85a757d 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -11,20 +11,30 @@ pub const Error = error{ FeatureNotSupported, } || Lexer.Error || paragraph.Error || title.Error || link.Error; +pub fn parseReader(parent: Allocator, r: *std.io.Reader) ![]const u8 { + var l = try Lexer.initReader(parent, r); + defer parent.free(l.iter.bytes); + return gen(parent, &l); +} + pub fn parse(parent: Allocator, content: []const u8) Error![]const u8 { + var l = try Lexer.init(content); + return gen(parent, &l); +} + +fn gen(parent: Allocator, l: *Lexer) Error![]const u8 { var arena = std.heap.ArenaAllocator.init(parent); defer arena.deinit(); const alloc = arena.allocator(); var elements = try std.ArrayList(Element).initCapacity(alloc, 2); - var l = try Lexer.init(content); base: while (l.nextKind()) |it| { try elements.append(alloc, switch (it) { // block paragraph - .literal, .bold, .italic, .code, .link => try paragraph.parse(alloc, &l), + .literal, .bold, .italic, .code, .link => try paragraph.parse(alloc, l), // other blocks - .title => try title.parse(alloc, &l), + .title => try title.parse(alloc, l), .weak_delimiter, .strong_delimiter => { var v = (try l.next(alloc)).?; v.deinit(); |
