From a1f08ee687c268aaebe0df2c540ea5eb9fa1cdcb Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Mon, 1 Feb 2021 02:24:57 +0100 Subject: [PATCH] Allow top level comments in xml parser --- generator/xml.zig | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/generator/xml.zig b/generator/xml.zig index d05aa45..31c694c 100644 --- a/generator/xml.zig +++ b/generator/xml.zig @@ -245,7 +245,7 @@ const ParseContext = struct { fn currentLine(self: ParseContext) []const u8 { var begin: usize = 0; - if (mem.indexOfScalarPos(u8, self.source[0 .. self.offset], '\n')) |prev_nl| { + if (mem.lastIndexOfScalar(u8, self.source[0 .. self.offset], '\n')) |prev_nl| { begin = prev_nl + 1; } @@ -328,10 +328,15 @@ fn parseDocument(ctx: *ParseContext, backing_allocator: *Allocator) !Document { errdefer doc.deinit(); + try trySkipComments(ctx, &doc.arena.allocator); + doc.xml_decl = try tryParseProlog(ctx, &doc.arena.allocator); _ = ctx.eatWs(); + try trySkipComments(ctx, &doc.arena.allocator); + doc.root = (try tryParseElement(ctx, &doc.arena.allocator)) orelse return error.InvalidDocument; _ = ctx.eatWs(); + try trySkipComments(ctx, &doc.arena.allocator); if (ctx.peek() != null) return error.InvalidDocument; @@ -581,6 +586,12 @@ test "tryParseProlog" { } } +fn trySkipComments(ctx: *ParseContext, alloc: *Allocator) !void { + while (try tryParseComment(ctx, alloc)) |_| { + _ = ctx.eatWs(); + } +} + fn tryParseComment(ctx: *ParseContext, alloc: *Allocator) !?[]const u8 { if (!ctx.eatStr(""); + testing.expectEqualSlices(u8, "python", doc.root.tag); +} \ No newline at end of file