From 3a97e59886f51704f4dd595eb3325c701d92ba95 Mon Sep 17 00:00:00 2001 From: Fernando Araoz Date: Mon, 27 Jan 2025 22:04:54 -0500 Subject: [PATCH] feat: full path for error reporting in variable parser --- src/02_syntax/root.zig | 12 +++++++++++- src/02_syntax/statement.zig | 9 +++++---- src/main.zig | 19 ++++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/02_syntax/root.zig b/src/02_syntax/root.zig index a79e996..6f4bd3f 100644 --- a/src/02_syntax/root.zig +++ b/src/02_syntax/root.zig @@ -41,9 +41,19 @@ pub const Module = struct { // parse many statements while (current_pos < input_len) { var stmt: statement.Statement = undefined; + var current_error: errors.ErrorData = undefined; // TODO: handle other errors of vardef parsing - const next_pos = try stmt.init(tokens, current_pos, allocator); + const next_pos = stmt.init(tokens, current_pos, ¤t_error, allocator) catch |e| switch (e) { + error.Error => { + // add the error to the list of errors, + // and exit for now because i havent implemented + // error recovery yet + try err_arrl.append(current_error); + return error.Error; + }, + else => return e, + }; if (next_pos) |next_pos_actual| { current_pos = next_pos_actual; diff --git a/src/02_syntax/statement.zig b/src/02_syntax/statement.zig index c9dc01a..d10b9b5 100644 --- a/src/02_syntax/statement.zig +++ b/src/02_syntax/statement.zig @@ -20,6 +20,7 @@ pub const Statement = struct { target: *Statement, tokens: *const TokenStream, pos: usize, + err: *errors.ErrorData, allocator: std.mem.Allocator, ) ParseError!?usize { // try to parse a variable definition @@ -27,8 +28,8 @@ pub const Statement = struct { var vardef = try allocator.create(variable.VariableBinding); errdefer allocator.destroy(vardef); - // TODO: handle other errors of vardef parsing - if (try vardef.init(tokens, pos, undefined, allocator)) |vardef_end| { + const vardef_result = try vardef.init(tokens, pos, err, allocator); + if (vardef_result) |vardef_end| { // variable definition parsed // return the parsed variable definition target.* = .{ @@ -61,7 +62,7 @@ test "should parse a variable declaration statement" { defer tokens.deinit(); var statement: Statement = undefined; - _ = try statement.init(&tokens, 0, std.testing.allocator); + _ = try statement.init(&tokens, 0, undefined, std.testing.allocator); defer statement.deinit(); switch (statement.value) { @@ -80,7 +81,7 @@ test "should fail on other constructs" { defer tokens.deinit(); var statement: Statement = undefined; - const result = try statement.init(&tokens, 0, std.testing.allocator); + const result = try statement.init(&tokens, 0, undefined, std.testing.allocator); if (result == null) { // good path return; diff --git a/src/main.zig b/src/main.zig index 4adcdd4..e80c7bf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -74,6 +74,10 @@ fn repl() !void { // var error_array = std.ArrayList(errors.ErrorData).init(alloc); defer error_array.deinit(); + defer for (error_array.items) |item| { + var i = item; + i.deinit(); + }; const tokens = lexic.tokenize(line, alloc, &error_array) catch |e| switch (e) { error.OutOfMemory => { @@ -114,7 +118,20 @@ fn repl() !void { // Syntax analysis // var ast: syntax.Module = undefined; - try ast.init(&tokens, 0, alloc, &error_array); + ast.init(&tokens, 0, alloc, &error_array) catch |e| switch (e) { + error.Error => { + // Print all the errors + for (error_array.items) |ee| { + var err_item = ee; + const err_str = try err_item.get_error_str(line, "repl", alloc); + try stdout.print("\n{s}\n", .{err_str}); + try bw.flush(); + alloc.free(err_str); + } + continue; + }, + else => return e, + }; // next repl line }