summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-23 19:19:22 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-23 19:19:22 +0200
commitd19ad26fb451c4bf10b20d90483ac424a4dd9a57 (patch)
tree21bc00ee2d1e82de4e72a9721bf4bafb23399e52 /src
parentc577aa7a2e2e1db245d20ca6ae23e4d630c51897 (diff)
Fix datum comment handling & tests.
Diffstat (limited to 'src')
-rw-r--r--src/test/parse.zig5
-rw-r--r--src/zisp/io.zig9
-rw-r--r--src/zisp/io/Decoder.zig7
-rw-r--r--src/zisp/io/Parser.zig38
4 files changed, 31 insertions, 28 deletions
diff --git a/src/test/parse.zig b/src/test/parse.zig
index 1c930a6..fb29a11 100644
--- a/src/test/parse.zig
+++ b/src/test/parse.zig
@@ -3,7 +3,6 @@ const std = @import("std");
const testing = std.testing;
const expect = testing.expect;
-const alloc = std.heap.smp_allocator;
const io = std.Io.Threaded.global_single_threaded.io();
pub const zisp = @import("../zisp.zig");
@@ -13,7 +12,7 @@ pub const Value = zisp.value.Value;
fn parse(str: []const u8) Value {
var fbs = std.Io.Reader.fixed(str);
- return zisp.io.parseNoError(alloc, &fbs);
+ return zisp.io.parseNoError(&fbs);
}
test "parse empty" {
@@ -168,7 +167,7 @@ fn parseBench(path: []const u8, iters: usize) !void {
var file_reader = file.reader(io, &buf);
const reader = &file_reader.interface;
while (true) {
- const v = zisp.io.parseNoError(alloc, reader);
+ const v = zisp.io.parseNoError(reader);
if (value.eof.eq(v)) break;
}
}
diff --git a/src/zisp/io.zig b/src/zisp/io.zig
index f0d0a8f..9d49cde 100644
--- a/src/zisp/io.zig
+++ b/src/zisp/io.zig
@@ -30,6 +30,7 @@ pub fn init() !void {
}
pub fn mainIo() std.Io {
+ init() catch @panic("OOM"); // TODO this is only here for the test suite
return main_io;
}
@@ -50,14 +51,14 @@ const ParserErrors = error{
OutOfMemory,
};
-pub fn parse(alloc: Alloc, reader: *Reader) ParserErrors!Value {
- var p = try Parser.init(alloc);
+pub fn parse(reader: *Reader) ParserErrors!Value {
+ var p = try Parser.init();
defer p.deinit();
return p.run(reader);
}
-pub fn parseNoError(alloc: Alloc, reader: *Reader) Value {
- var p = Parser.init(alloc) catch @panic("OOM");
+pub fn parseNoError(reader: *Reader) Value {
+ var p = Parser.init() catch @panic("OOM");
defer p.deinit();
return p.run(reader) catch |e| switch (e) {
error.OutOfMemory => @panic("OOM"),
diff --git a/src/zisp/io/Decoder.zig b/src/zisp/io/Decoder.zig
index ac39a02..f9d99e0 100644
--- a/src/zisp/io/Decoder.zig
+++ b/src/zisp/io/Decoder.zig
@@ -1,3 +1,4 @@
+const builtin = @import("builtin");
const std = @import("std");
const Alloc = std.mem.Allocator;
@@ -16,8 +17,10 @@ pub fn init(alloc: Alloc) !Decoder {
}
pub fn decodeInPlace(d: *Decoder, v: Value) !Value {
- std.debug.print("decode: ", .{});
- try io.printStdout(v);
+ if (!builtin.is_test) {
+ std.debug.print("decode: ", .{});
+ io.printStdout(v) catch @panic("print error");
+ }
_ = d;
return v;
}
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 {