Same for ziqlParser

This commit is contained in:
Adrien Bouvais 2025-01-11 17:57:19 +01:00
parent 0f6f34e706
commit d5f7309869

View File

@ -65,20 +65,20 @@ const State = enum {
add_array_to_map,
};
pub const Parser = struct {
toker: *Tokenizer,
file_engine: *FileEngine,
schema_engine: *SchemaEngine,
pub const Parser = @This();
toker: *Tokenizer,
file_engine: *FileEngine,
schema_engine: *SchemaEngine,
pub fn init(toker: *Tokenizer, file_engine: *FileEngine, schema_engine: *SchemaEngine) Parser {
pub fn init(toker: *Tokenizer, file_engine: *FileEngine, schema_engine: *SchemaEngine) Parser {
return Parser{
.toker = toker,
.file_engine = file_engine,
.schema_engine = schema_engine,
};
}
}
pub fn parse(self: Parser) ZipponError!void {
pub fn parse(self: Parser) ZipponError!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
@ -373,11 +373,11 @@ pub const Parser = struct {
else => unreachable,
};
}
}
/// Take an array of UUID and populate it with what match what is between {}
/// Main is to know if between {} or (), main is true if between {}, otherwise between () inside {}
pub fn parseFilter(self: Parser, allocator: Allocator, struct_name: []const u8, is_sub: bool) ZipponError!Filter {
/// Take an array of UUID and populate it with what match what is between {}
/// Main is to know if between {} or (), main is true if between {}, otherwise between () inside {}
pub fn parseFilter(self: Parser, allocator: Allocator, struct_name: []const u8, is_sub: bool) ZipponError!Filter {
var filter = try Filter.init(allocator);
errdefer filter.deinit();
@ -490,11 +490,11 @@ pub const Parser = struct {
};
return filter;
}
}
/// Parse to get a Condition. Which is a struct that is use by the FileEngine to retreive data.
/// In the query, it is this part name = 'Bob' or age <= 10
fn parseCondition(self: Parser, allocator: Allocator, token_ptr: *Token, struct_name: []const u8) ZipponError!Condition {
/// Parse to get a Condition. Which is a struct that is use by the FileEngine to retreive data.
/// In the query, it is this part name = 'Bob' or age <= 10
fn parseCondition(self: Parser, allocator: Allocator, token_ptr: *Token, struct_name: []const u8) ZipponError!Condition {
var keep_next = false;
var state: State = .expect_member;
var token = token_ptr.*;
@ -563,14 +563,14 @@ pub const Parser = struct {
try self.checkConditionValidity(condition, token);
return condition;
}
}
/// Will check if what is compared is ok, like comparing if a string is superior to another string is not for example.
fn checkConditionValidity(
/// Will check if what is compared is ok, like comparing if a string is superior to another string is not for example.
fn checkConditionValidity(
self: Parser,
condition: Condition,
token: Token,
) ZipponError!void {
) ZipponError!void {
switch (condition.operation) {
.equal => switch (condition.data_type) {
.int, .float, .str, .bool, .date, .time, .datetime => {},
@ -660,16 +660,16 @@ pub const Parser = struct {
),
},
}
}
}
/// When this function is call, next token should be [
/// Check if an int is here -> check if ; is here -> check if member is here -> check if [ is here -> loop
fn parseAdditionalData(
/// When this function is call, next token should be [
/// Check if an int is here -> check if ; is here -> check if member is here -> check if [ is here -> loop
fn parseAdditionalData(
self: Parser,
allocator: Allocator,
additional_data: *AdditionalData,
struct_name: []const u8,
) ZipponError!void {
) ZipponError!void {
var token = self.toker.next();
var keep_next = false;
var state: State = .expect_limit;
@ -782,19 +782,19 @@ pub const Parser = struct {
else => unreachable,
};
}
}
/// Take the tokenizer and return a map of the ADD action.
/// Keys are the member name and value are the string of the value in the query. E.g. 'Adrien' or '10'
/// Entry token need to be (
fn parseNewData(
/// Take the tokenizer and return a map of the ADD action.
/// Keys are the member name and value are the string of the value in the query. E.g. 'Adrien' or '10'
/// Entry token need to be (
fn parseNewData(
self: Parser,
allocator: Allocator,
map: *std.StringHashMap(ConditionValue),
struct_name: []const u8,
order: ?*std.ArrayList([]const u8),
order_full: ?bool,
) !void {
) !void {
var token = self.toker.next();
var keep_next = false;
var member_name: []const u8 = undefined;
@ -901,9 +901,9 @@ pub const Parser = struct {
else => unreachable,
};
}
}
fn parseComparisonOperator(self: Parser, token: Token) ZipponError!ComparisonOperator {
fn parseComparisonOperator(self: Parser, token: Token) ZipponError!ComparisonOperator {
return switch (token.tag) {
.equal => .equal, // =
.angle_bracket_left => .inferior, // <
@ -921,10 +921,10 @@ pub const Parser = struct {
token.loc.end,
),
};
}
}
/// To run just after a condition like = or > or >= to get the corresponding ConditionValue that you need to compare
fn parseConditionValue(self: Parser, allocator: Allocator, struct_name: []const u8, member_name: []const u8, data_type: dtype.DataType, token: *Token) ZipponError!ConditionValue {
/// To run just after a condition like = or > or >= to get the corresponding ConditionValue that you need to compare
fn parseConditionValue(self: Parser, allocator: Allocator, struct_name: []const u8, member_name: []const u8, data_type: dtype.DataType, token: *Token) ZipponError!ConditionValue {
const start_index = token.loc.start;
const expected_tag: ?Token.Tag = switch (data_type) {
.int => .int_literal,
@ -1197,10 +1197,10 @@ pub const Parser = struct {
},
.self => unreachable,
}
}
}
/// Check if all token in an array is of one specific type
fn checkTokensInArray(self: Parser, tag: Token.Tag) ZipponError!Token {
/// Check if all token in an array is of one specific type
fn checkTokensInArray(self: Parser, tag: Token.Tag) ZipponError!Token {
var token = self.toker.next();
while (token.tag != .r_bracket) : (token = self.toker.next()) {
if (token.tag != tag) return printError(
@ -1212,5 +1212,4 @@ pub const Parser = struct {
);
}
return token;
}
};
}