refactor: use new context in string lexer

This commit is contained in:
Fernando Araoz 2025-01-30 19:51:01 -05:00
parent f1a6261f32
commit 988bcbc243

View File

@ -3,6 +3,7 @@ const assert = std.debug.assert;
const token = @import("./token.zig"); const token = @import("./token.zig");
const utils = @import("./utils.zig"); const utils = @import("./utils.zig");
const errors = @import("errors"); const errors = @import("errors");
const context = @import("context");
const Token = token.Token; const Token = token.Token;
const TokenType = token.TokenType; const TokenType = token.TokenType;
@ -12,8 +13,7 @@ const LexReturn = token.LexReturn;
pub fn lex( pub fn lex(
input: []const u8, input: []const u8,
start: usize, start: usize,
err: *errors.ErrorData, ctx: *context.CompilerContext,
alloc: std.mem.Allocator,
) LexError!?LexReturn { ) LexError!?LexReturn {
const cap = input.len; const cap = input.len;
assert(start < cap); assert(start < cap);
@ -36,7 +36,7 @@ pub fn lex(
} }
// new line, initialize and return error // new line, initialize and return error
else if (next_char == '\n') { else if (next_char == '\n') {
try err.init("Incomplete String", current_pos, current_pos + 1, alloc); var err = try ctx.create_and_append_error("Incomplete String", current_pos, current_pos + 1);
try err.add_label("Found a new line here", current_pos, current_pos + 1); try err.add_label("Found a new line here", current_pos, current_pos + 1);
err.set_help("Strings must always end on the same line that they start."); err.set_help("Strings must always end on the same line that they start.");
@ -46,14 +46,14 @@ pub fn lex(
else if (next_char == '\\') { else if (next_char == '\\') {
// if next char is EOF, return error // if next char is EOF, return error
if (current_pos + 1 == cap) { if (current_pos + 1 == cap) {
try err.init("Incomplete String", current_pos, current_pos + 1, alloc); var err = try ctx.create_and_append_error("Incomplete String", current_pos, current_pos + 1);
try err.add_label("Found EOF here", current_pos, current_pos + 1); try err.add_label("Found EOF here", current_pos, current_pos + 1);
err.set_help("Strings must always end on the same line that they start."); err.set_help("Strings must always end on the same line that they start.");
return LexError.IncompleteString; return LexError.IncompleteString;
} }
// if next char is newline, return error // if next char is newline, return error
else if (input[current_pos + 1] == '\n') { else if (input[current_pos + 1] == '\n') {
try err.init("Incomplete String", current_pos, current_pos + 1, alloc); var err = try ctx.create_and_append_error("Incomplete String", current_pos, current_pos + 1);
try err.add_label("Found a new line here", current_pos, current_pos + 1); try err.add_label("Found a new line here", current_pos, current_pos + 1);
err.set_help("Strings must always end on the same line that they start."); err.set_help("Strings must always end on the same line that they start.");
return LexError.IncompleteString; return LexError.IncompleteString;
@ -68,7 +68,7 @@ pub fn lex(
} }
// this could only reach when EOF is hit, return error // this could only reach when EOF is hit, return error
try err.init("Incomplete String", current_pos, current_pos + 1, alloc); var err = try ctx.create_and_append_error("Incomplete String", current_pos, current_pos + 1);
try err.add_label("Found EOF here", current_pos, current_pos + 1); try err.add_label("Found EOF here", current_pos, current_pos + 1);
err.set_help("Strings must always end on the same line that they start."); err.set_help("Strings must always end on the same line that they start.");