diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 21:30:32 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 21:30:32 +0200 |
| commit | ec6f949c48323a0bea244b723af547f60e0db33f (patch) | |
| tree | f20497988d2e2c675153c60892cfe8e5715b7fb3 /src/paragraph.zig | |
| parent | fe3e8fe67b7f655699be321796af70d4bdff230f (diff) | |
style(ast): split paragraph and content
Diffstat (limited to 'src/paragraph.zig')
| -rw-r--r-- | src/paragraph.zig | 51 |
1 files changed, 9 insertions, 42 deletions
diff --git a/src/paragraph.zig b/src/paragraph.zig index 2b96e19..0382e5d 100644 --- a/src/paragraph.zig +++ b/src/paragraph.zig @@ -5,11 +5,12 @@ const Lexer = @import("lexer/Lexer.zig"); const Element = @import("dom/Element.zig"); const parser = @import("parser.zig"); const link = @import("link.zig"); +const content = @import("content.zig"); const testing = @import("testing.zig"); const doTest = testing.do; const doTestError = testing.doError; -pub const Error = error{ ModifierNotClosed, IllegalPlacement, InvalidLink } || Lexer.Error; +pub const Error = content.Error || link.Error || Lexer.Error; pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { var el = try Element.init(alloc, .content, "p"); @@ -34,58 +35,24 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { } pub fn parseLine(alloc: Allocator, l: *Lexer) Error!Element { - var content = Element.initEmpty(alloc); - errdefer content.deinit(); + var line = Element.initEmpty(alloc); + errdefer line.deinit(); while (l.nextKind()) |kind| { switch (kind) { - .weak_delimiter, .strong_delimiter => return content, + .weak_delimiter, .strong_delimiter => return line, .link => { var el = try link.parse(alloc, l); errdefer el.deinit(); - try content.appendContent(el); + try line.appendContent(el); }, else => { - var el = try parseContent(alloc, l); + var el = try content.parse(alloc, l); errdefer el.deinit(); - try content.appendContent(el); + try line.appendContent(el); }, } } - return content; -} - -pub fn parseContent(alloc: Allocator, l: *Lexer) Error!Element { - var content = Element.initEmpty(alloc); - errdefer content.deinit(); - var v = (try l.next(alloc)).?; - defer v.deinit(); - switch (v.kind) { - .literal => { - const el = try Element.initLitEscaped(alloc, v.content.items); - try content.appendContent(el); - }, - .bold => try content.appendContent(try parseModifier(alloc, l, .bold, "b")), - .italic => try content.appendContent(try parseModifier(alloc, l, .italic, "em")), - .code => try content.appendContent(try parseModifier(alloc, l, .code, "code")), - else => return Error.IllegalPlacement, - } - return content; -} - -fn parseModifier(alloc: Allocator, l: *Lexer, knd: Lexed.Kind, tag: []const u8) Error!Element { - var el = try Element.init(alloc, .content, tag); - errdefer el.deinit(); - while (l.nextKind()) |it| { - if (it == knd) { - // consuming the finisher - var v = (try l.next(alloc)).?; - v.deinit(); - return el; - } - if (it.isDelimiter()) return Error.ModifierNotClosed; - try el.appendContent(try parseContent(alloc, l)); - } - return Error.ModifierNotClosed; + return line; } test "parse paragraphs" { |
