diff options
| -rw-r--r-- | example.td (renamed from example.typd) | 0 | ||||
| -rw-r--r-- | src/content.zig | 46 | ||||
| -rw-r--r-- | src/link.zig | 14 | ||||
| -rw-r--r-- | src/paragraph.zig | 51 | ||||
| -rw-r--r-- | src/parser.zig | 2 |
5 files changed, 64 insertions, 49 deletions
diff --git a/example.typd b/example.td index e4fab16..e4fab16 100644 --- a/example.typd +++ b/example.td diff --git a/src/content.zig b/src/content.zig new file mode 100644 index 0000000..01d933b --- /dev/null +++ b/src/content.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const Lexed = @import("lexer/Lexed.zig"); +const Lexer = @import("lexer/Lexer.zig"); +const Element = @import("dom/Element.zig"); +const parser = @import("parser.zig"); +const link = @import("link.zig"); +const testing = @import("testing.zig"); +const doTest = testing.do; +const doTestError = testing.doError; + +pub const Error = error{ ModifierNotClosed, IllegalPlacement } || Lexer.Error; + +pub fn parse(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 parse(alloc, l)); + } + return Error.ModifierNotClosed; +} diff --git a/src/link.zig b/src/link.zig index c9fc93a..fd29cb5 100644 --- a/src/link.zig +++ b/src/link.zig @@ -4,12 +4,12 @@ const eql = std.mem.eql; const Lexed = @import("lexer/Lexed.zig"); const Lexer = @import("lexer/Lexer.zig"); const Element = @import("dom/Element.zig"); -const paragraph = @import("paragraph.zig"); +const content = @import("content.zig"); const testing = @import("testing.zig"); const doTest = testing.do; const doTestError = testing.doError; -pub const Error = error{InvalidLink} || Lexer.Error || paragraph.Error; +pub const Error = error{InvalidLink} || Lexer.Error || content.Error; pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { var el = try Element.init(alloc, .content, "a"); @@ -20,9 +20,9 @@ pub fn parse(alloc: Allocator, l: *Lexer) Error!Element { return data.first.?; }; defer alloc.free(second); - var content = if (data.first) |first| first else try Element.initLitEscaped(alloc, second); - errdefer content.deinit(); - try el.appendContent(content); + var in = if (data.first) |first| first else try Element.initLitEscaped(alloc, second); + errdefer in.deinit(); + try el.appendContent(in); try el.setAttribute("href", second); return el; } @@ -53,8 +53,8 @@ pub fn parseData(alloc: Allocator, l: *Lexer) Error!Data { break; }, else => { - const content = try paragraph.parseContent(alloc, l); - try el.appendContent(content); + const in = try content.parse(alloc, l); + try el.appendContent(in); }, } } 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" { diff --git a/src/parser.zig b/src/parser.zig index 7419c2e..775dc4a 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -21,7 +21,9 @@ pub fn parse(parent: Allocator, content: []const u8) Error![]const u8 { 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), + // other blocks .title => try title.parse(alloc, &l), .weak_delimiter, .strong_delimiter => { var v = (try l.next(alloc)).?; |
