aboutsummaryrefslogtreecommitdiff
path: root/src/parse.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-08-03 20:31:43 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-08-03 20:31:43 +0200
commite252cb1b786c2b67222c544e8b766de8b132cb38 (patch)
tree73be893e9d4b62588bba8a057c5c66b9aa312937 /src/parse.zig
parent2b196f261fba3bfcc0411d8d9f44a3ceac9840f1 (diff)
downloadhuginn-e252cb1b786c2b67222c544e8b766de8b132cb38.tar.gz
improve error printing
Diffstat (limited to 'src/parse.zig')
-rw-r--r--src/parse.zig13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/parse.zig b/src/parse.zig
index 752eafb..f6fc0bc 100644
--- a/src/parse.zig
+++ b/src/parse.zig
@@ -295,7 +295,7 @@ fn parseProc(allocator: Allocator, lexer: *Lexer) ParseError!*Expr {
const param: ?Location = switch (param_tok.type) {
.identifier => param_tok.loc,
.right_paren => null,
- else => |ty| return expected(&.{ .identifier, .right_paren }, ty),
+ else => return expected(&.{ .identifier, .right_paren }, param_tok),
};
if (param != null) _ = try mustEat(lexer, .right_paren);
const body = try parseBlock(allocator, lexer);
@@ -395,21 +395,18 @@ fn parsePrimaryExpr(allocator: Allocator, lexer: *Lexer) !*Expr {
},
.integer_literal => .{ .loc = token.loc, .type = .integer_literal },
.identifier => .{ .loc = token.loc, .type = .identifier },
- else => |t| {
- std.debug.print("Expected '(', integer literal, or identifier. Got '{s}'\n", .{@tagName(t)});
- return error.UnexpectedToken;
- },
+ else => return expected(&.{ .left_paren, .integer_literal, .identifier }, token),
});
}
-fn expected(one_of: []const Token.Type, got: Token.Type) error{UnexpectedToken} {
+fn expected(one_of: []const Token.Type, got: Token) error{UnexpectedToken} {
for (one_of) |ty|
- std.debug.assert(ty != got);
+ std.debug.assert(ty != got.type);
std.debug.print("Expected ", .{});
for (0.., one_of) |i, ty| {
std.debug.print("{s}{s}", .{ if (i == 0) "" else " or ", @tagName(ty) });
}
- std.debug.print(". Got {s}.\n", .{@tagName(got)});
+ std.debug.print(". Got {s} at {}.\n", .{ @tagName(got.type), got.loc });
return error.UnexpectedToken;
}