diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-23 19:19:22 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-23 19:19:22 +0200 |
| commit | d19ad26fb451c4bf10b20d90483ac424a4dd9a57 (patch) | |
| tree | 21bc00ee2d1e82de4e72a9721bf4bafb23399e52 /src/zisp/io/Parser.zig | |
| parent | c577aa7a2e2e1db245d20ca6ae23e4d630c51897 (diff) | |
Fix datum comment handling & tests.
Diffstat (limited to 'src/zisp/io/Parser.zig')
| -rw-r--r-- | src/zisp/io/Parser.zig | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index 1b3808a..53a7b8c 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -82,6 +82,8 @@ pub const Error = enum { OutOfRange, }; +// TODO: Use SegStack for context stack + pub const Context = struct { /// What to do next. next: ?Fn = undefined, @@ -92,6 +94,8 @@ pub const Context = struct { /// Start index of list elements on current parse level, within the global /// list element accumulation array. list_start: usize = undefined, + /// Are we in a comment? + comment: bool = false, }; list_pool: ?*ListPool, @@ -104,7 +108,6 @@ decoder: ?*Decoder, reader: *Reader = undefined, context: Context = .{}, -comment: bool = false, result: Value = undefined, unread_char: ?u8 = null, err_msg: []const u8 = undefined, @@ -195,12 +198,12 @@ fn getUnread(p: *Parser) ?u8 { // fn addChar(p: *Parser, c: u8) !void { - if (p.comment) return; + if (p.context.comment) return; try p.str_chars.append(p.alloc, c); } fn addUnicode(p: *Parser, uc: u21) !void { - if (p.comment) return; + if (p.context.comment) return; const n = std.unicode.utf8CodepointSequenceLength(uc) catch { return p.err(.UnicodeLengthError, "Unicode/UTF-8 escape"); }; @@ -241,12 +244,12 @@ fn getCharsAsRune(p: *Parser) Value { // fn addListElt(p: *Parser, elt: Value) !void { - if (p.comment) return; + if (p.context.comment) return; try p.list_elts.append(p.alloc, elt); } fn getList(p: *Parser) !Value { - if (p.comment) return value.nil; + if (p.context.comment) return value.nil; if (p.list_elts.items.len == p.context.list_start) return value.nil; defer p.list_elts.items.len = p.context.list_start; const vals = p.list_elts.items[p.context.list_start..]; @@ -264,7 +267,6 @@ fn makeList(p: *Parser, vals: []const Value) !Value { const Fn = enum { parseUnit, endUnit, - endSkipUnit, returnContext, parseDatum, endDatum, @@ -281,7 +283,6 @@ inline fn call(p: *Parser, f: Fn) !void { try switch (f) { .parseUnit => p.parseUnit(), .endUnit => p.endUnit(), - .endSkipUnit => p.endSkipUnit(), .returnContext => p.returnContext(), .parseDatum => p.parseDatum(), .endDatum => p.endDatum(), @@ -340,7 +341,10 @@ fn err( } fn push(p: *Parser, next: Fn) !void { - try p.ctx_stack.append(p.alloc, .{ .next = next }); + try p.ctx_stack.append(p.alloc, .{ + .next = next, + .comment = p.context.comment, + }); } fn pushContext(p: *Parser, next: Fn) !void { @@ -349,6 +353,7 @@ fn pushContext(p: *Parser, next: Fn) !void { .val = p.context.val, .char = p.context.char, .list_start = p.context.list_start, + .comment = p.context.comment, }); } @@ -365,8 +370,8 @@ fn subr(p: *Parser, start: Fn, next: Fn) !void { p.context.next = start; } -fn jump(p: *Parser, next: Fn, val: ?Value) !void { - if (val) |v| p.result = try p.decode(v); +fn jump(p: *Parser, next: Fn, val: Value) !void { + p.result = try p.decode(val); p.context.next = next; } @@ -387,7 +392,7 @@ fn retNoDecode(p: *Parser, val: Value) void { } fn decode(p: *Parser, val: Value) !Value { - if (p.comment) return val; + if (p.context.comment) return val; if (p.decoder) |dec| return dec.decodeInPlace(val); return val; } @@ -408,18 +413,13 @@ fn parseUnit(p: *Parser) !void { }, } } - return p.retval(value.eof); + return p.retNoDecode(value.eof); } fn skipUnit(p: *Parser, next: Fn) !void { - p.comment = true; try p.push(next); - return p.subr(.parseUnit, .endSkipUnit); -} - -fn endSkipUnit(p: *Parser) !void { - p.comment = false; - return p.ret(); + p.context.comment = true; + return p.jump(.parseUnit, value.none); } fn endUnit(p: *Parser) !void { |
