From 63d618e06f4d1d224aff4bf36cbb3f756c03c07d Mon Sep 17 00:00:00 2001 From: Fernando Araoz Date: Fri, 31 Jan 2025 19:08:25 -0500 Subject: [PATCH] feat: implementar msg de error dinamicos --- src/01_lexic/token.zig | 21 +++++++++++++++++++++ src/02_syntax/variable.zig | 15 ++++++++++++++- src/context/root.zig | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/01_lexic/token.zig b/src/01_lexic/token.zig index 73d0cca..35e217a 100644 --- a/src/01_lexic/token.zig +++ b/src/01_lexic/token.zig @@ -18,6 +18,27 @@ pub const TokenType = enum { Newline, // Each keyword will have its own token K_Var, + + pub fn to_string(self: *TokenType) []const u8 { + return switch (self.*) { + TokenType.Int => "Int", + TokenType.Float => "Float", + TokenType.Identifier => "Identifier", + TokenType.Datatype => "Datatype", + TokenType.Operator => "Operator", + TokenType.Comment => "Comment", + TokenType.String => "String", + TokenType.LeftParen => "LeftParen", + TokenType.RightParen => "RightParen", + TokenType.LeftBracket => "LeftBracket", + TokenType.RightBracket => "RightBracket", + TokenType.LeftBrace => "LeftBrace", + TokenType.RightBrace => "RightBrace", + TokenType.Comma => "Comma", + TokenType.Newline => "Newline", + TokenType.K_Var => "K_Var", + }; + } }; pub const Token = struct { diff --git a/src/02_syntax/variable.zig b/src/02_syntax/variable.zig index 22d66a4..e8f6390 100644 --- a/src/02_syntax/variable.zig +++ b/src/02_syntax/variable.zig @@ -47,7 +47,20 @@ pub const VariableBinding = struct { // try to parse an identifier const identifier = if (utils.expect_token_type(lexic.TokenType.Identifier, &tokens.items[pos + 1])) |i| i else { - // TODO: populate error information + const faulty_token = &tokens.items[pos + 1]; + var err = try ctx.create_and_append_error( + "Invalid variable declaration", + faulty_token.start_pos, + faulty_token.start_pos + faulty_token.value.len, + ); + const token_name = faulty_token.token_type.to_string(); + const error_name = try std.fmt.allocPrint(ctx.allocator, "Expected an identifier here, found a {s}", .{token_name}); + try err.add_label(ctx.create_error_label_alloc( + error_name, + faulty_token.start_pos, + faulty_token.start_pos + faulty_token.value.len, + )); + return ParseError.Error; }; diff --git a/src/context/root.zig b/src/context/root.zig index 8afbe24..3757ad5 100644 --- a/src/context/root.zig +++ b/src/context/root.zig @@ -64,6 +64,26 @@ pub const CompilerContext = struct { }; } + /// Creates a new ErrorLabel with a dynamic message. + /// Resource cleanup is automatic. + /// + /// `message` **must** be allocated with the allocator + /// of this context, because on cleanup this will be + /// passed to `ctx.allocator.destroy(message)`. + pub fn create_error_label_alloc( + self: *CompilerContext, + message: []u8, + start: usize, + end: usize, + ) ErrorLabel { + _ = self; + return .{ + .message = .{ .dynamic = message }, + .start = start, + .end = end, + }; + } + pub fn deinit(self: *CompilerContext) void { for (self.errors.items) |*error_item| { error_item.deinit(self.allocator);