summaryrefslogtreecommitdiff
path: root/src/zisp/io/Parser.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/io/Parser.zig')
-rw-r--r--src/zisp/io/Parser.zig35
1 files changed, 22 insertions, 13 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig
index ce61817..f92a308 100644
--- a/src/zisp/io/Parser.zig
+++ b/src/zisp/io/Parser.zig
@@ -152,7 +152,12 @@ fn read(p: *Parser) !?u8 {
}
fn readNoEof(p: *Parser, comptime emsg: []const u8) !u8 {
- return if (try p.read()) |c| c else p.err(.UnexpectedEof, emsg);
+ return try p.read() orelse p.err(.UnexpectedEof, emsg);
+}
+
+// Fake optional, for use in: while (readToEof()) |c| { }
+fn readNoEof2(p: *Parser, comptime emsg: []const u8) !?u8 {
+ return try p.read() orelse p.err(.UnexpectedEof, emsg);
}
fn unread(p: *Parser, c: u8) void {
@@ -187,12 +192,13 @@ fn addUnicode(p: *Parser, uc: u21) !void {
fn getCharsAsString(p: *Parser) !Value {
defer p.chars.clearRetainingCapacity();
const s = p.chars.items;
- if (value.sstr.isValidSstr(s))
+ if (value.sstr.isValidSstr(s)) {
return value.sstr.pack(s);
- else if (value.istr.isValidIstr(s))
+ } else if (value.istr.isValidIstr(s)) {
return value.istr.intern(s);
- else
- return @panic("not implemented"); // TODO
+ } else {
+ @panic("not implemented"); // TODO
+ }
}
fn getCharsAsRune(p: *Parser) Value {
@@ -445,7 +451,7 @@ fn parseCladDatum(p: *Parser, c: u8, next: Fn) !void {
fn getString(p: *Parser, comptime close: u8) !Value {
const msg = "string(" ++ .{close} ++ ")";
- while (try p.readNoEof(msg)) |c| sw: switch (c) {
+ while (try p.readNoEof2(msg)) |c| sw: switch (c) {
close => break,
'\\' => switch (try p.readNoEof("string backslash escape")) {
'\\', '|', '"' => |c2| try p.addChar(c2),
@@ -475,33 +481,36 @@ fn getString(p: *Parser, comptime close: u8) !Value {
fn getAtString(p: *Parser) !Value {
const sentinel = try p.readNoEof("at-string");
- while (try p.readNoEof("at-string")) |c| {
+ while (try p.readNoEof2("at-string")) |c| {
if (c == sentinel) break;
try p.addChar(c);
}
- const s = p.getCharsAsString();
+ const s = try p.getCharsAsString();
return p.cons(ATSTR, s);
}
fn skipStringLfEscape(p: *Parser) !u8 {
const msg = "string linefeed escape";
- while (try p.readNoEof(msg)) |c| switch (c) {
- '\t', ' ' => {},
+ while (try p.readNoEof2(msg)) |c| switch (c) {
+ '\t', ' ' => continue,
'\n' => return p.skipStringIndent(),
else => return p.err(.InvalidCharacter, msg),
};
+ unreachable;
}
fn skipStringIndent(p: *Parser) !u8 {
- while (try p.readNoEof("string newline escape")) |c| switch (c) {
- '\t', ' ' => {},
+ const msg = "string newline escape";
+ while (try p.readNoEof2(msg)) |c| switch (c) {
+ '\t', ' ' => continue,
else => return c,
};
+ unreachable;
}
fn parseStringRawHexEsc(p: *Parser) !void {
const msg = "string raw hex escape";
- while (try p.readNoEof(msg)) |c1| {
+ while (try p.readNoEof2(msg)) |c1| {
if (c1 == ';') break;
const c2 = try p.readNoEof(msg);
const hi = try p.parseHexDigit(c1, msg);