diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 17:55:42 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-19 17:55:42 +0200 |
| commit | cf6f08d1602841f8c337dc64365e2dcde1e4c528 (patch) | |
| tree | 4e94b76fa7c035b2de9e983bab248063aef5a6b4 /src/paragraph.zig | |
| parent | 25fa85cbb622e1f9dc9818b0d18fe0de2ef7e5a3 (diff) | |
fix(ast): error if title contains modifier
Diffstat (limited to 'src/paragraph.zig')
| -rw-r--r-- | src/paragraph.zig | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/paragraph.zig b/src/paragraph.zig index 8cdaeb0..222f376 100644 --- a/src/paragraph.zig +++ b/src/paragraph.zig @@ -5,10 +5,11 @@ const Lexer = @import("lexer/Lexer.zig"); const Element = @import("dom/Element.zig"); const parser = @import("parser.zig"); -pub const Error = error{ModifierNotClosed} || 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"); + errdefer el.deinit(); while (l.nextKind()) |kind| { switch (kind) { // because nextKind returns only an hint for the next rune @@ -22,14 +23,30 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { else => return el, } }, - else => try el.appendContent(try parseContent(alloc, l)), + else => try el.appendContent(try parseLine(alloc, l)), } } return el; } -pub fn parseContent(alloc: Allocator, l: *Lexer) Error!Element { +pub fn parseLine(alloc: Allocator, l: *Lexer) Error!Element { var content = Element.initEmpty(alloc); + errdefer content.deinit(); + while (l.nextKind()) |kind| { + switch (kind) { + .weak_delimiter, .strong_delimiter => return content, + else => { + const el = try parseContent(alloc, l); + try content.appendContent(el); + }, + } + } + return content; +} + +fn parseContent(alloc: Allocator, l: *Lexer) Error!Element { + var content = Element.initEmpty(alloc); + errdefer content.deinit(); const v = (try l.next(alloc)).?; switch (v.kind) { .literal => { @@ -39,13 +56,14 @@ pub fn parseContent(alloc: Allocator, l: *Lexer) Error!Element { .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 => unreachable, + 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 |
