Removed duplicate code

Changed how I check data type in parseCondition to prevent code
duplicate
This commit is contained in:
Adrien Bouvais 2024-10-22 00:20:25 +02:00
parent 2b9ad08abf
commit 696498d7ed
2 changed files with 70 additions and 151 deletions

View File

@ -17,4 +17,11 @@ pub const DataType = enum {
date_array, date_array,
time_array, time_array,
datetime_array, datetime_array,
pub fn is_array(self: DataType) bool {
return switch (self) {
.int_array, .float_array, .link_array, .str_array, .bool_array, .date_array, .time_array, .datetime_array => true,
else => false,
};
}
}; };

View File

@ -7,6 +7,7 @@ const Token = @import("tokenizers/ziql.zig").Token;
const UUID = @import("types/uuid.zig").UUID; const UUID = @import("types/uuid.zig").UUID;
const AND = @import("types/uuid.zig").AND; const AND = @import("types/uuid.zig").AND;
const OR = @import("types/uuid.zig").OR; const OR = @import("types/uuid.zig").OR;
const DataType = @import("types/dataType.zig").DataType;
const AdditionalData = @import("stuffs/additionalData.zig").AdditionalData; const AdditionalData = @import("stuffs/additionalData.zig").AdditionalData;
const AdditionalDataMember = @import("stuffs/additionalData.zig").AdditionalDataMember; const AdditionalDataMember = @import("stuffs/additionalData.zig").AdditionalDataMember;
const send = @import("stuffs/utils.zig").send; const send = @import("stuffs/utils.zig").send;
@ -543,155 +544,69 @@ pub const Parser = struct {
}, },
.expect_value => { .expect_value => {
switch (condition.data_type) { const start_index = token.loc.start;
.int => switch (token.tag) { const expected_tag: ?Token.Tag = switch (condition.data_type) {
.int_literal => condition.value = self.toker.getTokenSlice(token), .int => .int_literal,
else => return printError( .float => .float_literal,
"Error: Expected int", .str => .string_literal,
ZiQlParserError.SynthaxError, .link => .uuid_literal,
self.toker.buffer, .bool => null, // handle bool separately
token.loc.start, .date => .date_literal,
token.loc.end, .time => .time_literal,
), .datetime => .datetime_literal,
}, .int_array => .int_literal,
.float_array => .float_literal,
.link_array => .uuid_literal,
.str_array => .string_literal,
.bool_array => null, // handle bool array separately
.date_array => .date_literal,
.time_array => .time_literal,
.datetime_array => .datetime_literal,
};
.float => switch (token.tag) { if (expected_tag) |tag| {
.float_literal => condition.value = self.toker.getTokenSlice(token), if (condition.data_type.is_array()) {
else => return printError( token = try self.checkTokensInArray(tag);
"Error: Expected float", } else {
ZiQlParserError.SynthaxError, if (token.tag != tag) {
self.toker.buffer, return printError(
token.loc.start, "Error: Wrong type", // TODO: Print the expected type
token.loc.end, ZiQlParserError.SynthaxError,
), self.toker.buffer,
}, token.loc.start,
token.loc.end,
.str => switch (token.tag) { );
.string_literal => condition.value = self.toker.getTokenSlice(token), }
else => return printError( }
"Error: Expected string", } else {
ZiQlParserError.SynthaxError, // handle bool and bool array separately
self.toker.buffer, if (condition.data_type == .bool) {
token.loc.start, if (token.tag != .bool_literal_true and token.tag != .bool_literal_false) {
token.loc.end, return printError(
), "Error: Expected bool",
}, ZiQlParserError.SynthaxError,
self.toker.buffer,
.link => switch (token.tag) { token.loc.start,
.uuid_literal => condition.value = self.toker.getTokenSlice(token), token.loc.end,
else => return printError( );
"Error: Expected string", }
ZiQlParserError.SynthaxError, } else if (condition.data_type == .bool_array) {
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.bool => switch (token.tag) {
.bool_literal_true, .bool_literal_false => condition.value = self.toker.getTokenSlice(token),
else => return printError(
"Error: Expected bool",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.date => switch (token.tag) {
.date_literal => condition.value = self.toker.getTokenSlice(token),
else => return printError(
"Error: Expected date",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.time => switch (token.tag) {
.time_literal => condition.value = self.toker.getTokenSlice(token),
else => return printError(
"Error: Expected time",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.datetime => switch (token.tag) {
.datetime_literal => condition.value = self.toker.getTokenSlice(token),
else => return printError(
"Error: Expected datetime",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.int_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.int_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.float_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.float_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.link_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.uuid_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.str_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.string_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.bool_array => {
const start_index = token.loc.start;
token = self.toker.next(); token = self.toker.next();
while (token.tag != Token.Tag.r_bracket) : (token = self.toker.next()) { while (token.tag != .r_bracket) : (token = self.toker.next()) {
switch (token.tag) { if (token.tag != .bool_literal_true and token.tag != .bool_literal_false) {
.bool_literal_false, .bool_literal_true => continue, return printError(
else => return printError( "Error: Expected bool or ]",
"Error: Expected bool or ].",
ZiQlParserError.SynthaxError, ZiQlParserError.SynthaxError,
self.toker.buffer, self.toker.buffer,
token.loc.start, token.loc.start,
token.loc.end, token.loc.end,
), );
} }
} }
condition.value = self.toker.buffer[start_index..token.loc.end]; }
},
.date_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.date_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.time_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.time_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
.datetime_array => {
const start_index = token.loc.start;
token = try self.checkTokensInArray(.datetime_literal);
condition.value = self.toker.buffer[start_index..token.loc.end];
},
} }
condition.value = self.toker.buffer[start_index..token.loc.end];
state = .end; state = .end;
}, },
@ -1212,19 +1127,16 @@ pub const Parser = struct {
// Utils // Utils
/// Check if all token in an array is of one specific type /// Check if all token in an array is of one specific type
fn checkTokensInArray(self: Parser, comptime tag: Token.Tag) ZipponError!Token { fn checkTokensInArray(self: Parser, tag: Token.Tag) ZipponError!Token {
var token = self.toker.next(); var token = self.toker.next();
while (token.tag != .r_bracket) : (token = self.toker.next()) { while (token.tag != .r_bracket) : (token = self.toker.next()) {
switch (token.tag) { if (token.tag != tag) return printError(
tag => continue, "Error: Wrong type.",
else => return printError( ZiQlParserError.SynthaxError,
"Error: Wrong type.", self.toker.buffer,
ZiQlParserError.SynthaxError, token.loc.start,
self.toker.buffer, token.loc.end,
token.loc.start, );
token.loc.end,
),
}
} }
return token; return token;
} }