feat: receive error list in parser root

This commit is contained in:
Fernando Araoz 2025-01-27 18:52:12 -05:00
parent db8756a1d5
commit b1ada55ff2
3 changed files with 27 additions and 16 deletions

View File

@ -41,6 +41,7 @@ pub fn tokenize(
break;
}
// FIXME: should defer deinit, otherwise we leak memory?
var current_error: errors.ErrorData = undefined;
// attempt to lex a number
@ -50,6 +51,8 @@ pub fn tokenize(
// add to list of errors
try err_arrl.append(current_error);
// FIXME: should deinit current_error now that its been allocated, otherwise we leak memory?
// ignore everything until whitespace and loop
current_pos = ignore_until_whitespace(input, actual_next_pos);
continue;

View File

@ -27,7 +27,7 @@ pub const Module = struct {
tokens: *const TokenStream,
pos: usize,
allocator: std.mem.Allocator,
error_target: *errors.ErrorData,
err_arrl: *std.ArrayList(errors.ErrorData),
) ParseError!void {
var arrl = std.ArrayList(*statement.Statement).init(allocator);
errdefer arrl.deinit();
@ -48,12 +48,15 @@ pub const Module = struct {
switch (e) {
error.Unmatched => {
// create the error value
var error_target: errors.ErrorData = undefined;
try error_target.init(
"No statement found",
current_pos,
current_pos + 1,
allocator,
);
defer error_target.deinit();
try err_arrl.append(error_target);
return error.Unmatched;
},
else => return e,
@ -90,11 +93,8 @@ test "should parse a single statement" {
const tokens = try lexic.tokenize(input, std.testing.allocator, &error_list);
defer tokens.deinit();
const error_target = try std.testing.allocator.create(errors.ErrorData);
defer std.testing.allocator.destroy(error_target);
var module: Module = undefined;
_ = try module.init(&tokens, 0, std.testing.allocator, error_target);
_ = try module.init(&tokens, 0, std.testing.allocator, &error_list);
defer module.deinit();
}
@ -106,11 +106,8 @@ test "should clean memory if a statement parsing fails after one item has been i
const tokens = try lexic.tokenize(input, std.testing.allocator, &error_list);
defer tokens.deinit();
const error_target = try std.testing.allocator.create(errors.ErrorData);
defer std.testing.allocator.destroy(error_target);
var module: Module = undefined;
_ = module.init(&tokens, 0, std.testing.allocator, error_target) catch {
_ = module.init(&tokens, 0, std.testing.allocator, &error_list) catch {
return;
};
defer module.deinit();

View File

@ -96,15 +96,26 @@ fn repl() !void {
}
}
// Print errors
for (error_array.items) |e| {
var err = e;
const err_str = try err.get_error_str(line, "repl", alloc);
try stdout.print("\n{s}\n", .{err_str});
try bw.flush();
alloc.free(err_str);
// Print errors and continue, if any
if (error_array.items.len > 0) {
for (error_array.items) |e| {
var err = e;
const err_str = try err.get_error_str(line, "repl", alloc);
try stdout.print("\n{s}\n", .{err_str});
try bw.flush();
alloc.free(err_str);
}
continue;
}
std.debug.print("should be syntax analizing the tokens...\n", .{});
//
// Syntax analysis
//
var ast: syntax.Module = undefined;
try ast.init(&tokens, 0, alloc, &error_array);
// next repl line
}