Same for ziqlParser
This commit is contained in:
parent
0f6f34e706
commit
d5f7309869
@ -65,20 +65,20 @@ const State = enum {
|
|||||||
add_array_to_map,
|
add_array_to_map,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Parser = struct {
|
pub const Parser = @This();
|
||||||
toker: *Tokenizer,
|
toker: *Tokenizer,
|
||||||
file_engine: *FileEngine,
|
file_engine: *FileEngine,
|
||||||
schema_engine: *SchemaEngine,
|
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{
|
return Parser{
|
||||||
.toker = toker,
|
.toker = toker,
|
||||||
.file_engine = file_engine,
|
.file_engine = file_engine,
|
||||||
.schema_engine = schema_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);
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
const allocator = arena.allocator();
|
const allocator = arena.allocator();
|
||||||
@ -373,11 +373,11 @@ pub const Parser = struct {
|
|||||||
|
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Take an array of UUID and populate it with what match what is between {}
|
/// 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 {}
|
/// 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 {
|
pub fn parseFilter(self: Parser, allocator: Allocator, struct_name: []const u8, is_sub: bool) ZipponError!Filter {
|
||||||
var filter = try Filter.init(allocator);
|
var filter = try Filter.init(allocator);
|
||||||
errdefer filter.deinit();
|
errdefer filter.deinit();
|
||||||
|
|
||||||
@ -490,11 +490,11 @@ pub const Parser = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse to get a Condition. Which is a struct that is use by the FileEngine to retreive data.
|
/// 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
|
/// 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 {
|
fn parseCondition(self: Parser, allocator: Allocator, token_ptr: *Token, struct_name: []const u8) ZipponError!Condition {
|
||||||
var keep_next = false;
|
var keep_next = false;
|
||||||
var state: State = .expect_member;
|
var state: State = .expect_member;
|
||||||
var token = token_ptr.*;
|
var token = token_ptr.*;
|
||||||
@ -563,14 +563,14 @@ pub const Parser = struct {
|
|||||||
try self.checkConditionValidity(condition, token);
|
try self.checkConditionValidity(condition, token);
|
||||||
|
|
||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will check if what is compared is ok, like comparing if a string is superior to another string is not for example.
|
/// Will check if what is compared is ok, like comparing if a string is superior to another string is not for example.
|
||||||
fn checkConditionValidity(
|
fn checkConditionValidity(
|
||||||
self: Parser,
|
self: Parser,
|
||||||
condition: Condition,
|
condition: Condition,
|
||||||
token: Token,
|
token: Token,
|
||||||
) ZipponError!void {
|
) ZipponError!void {
|
||||||
switch (condition.operation) {
|
switch (condition.operation) {
|
||||||
.equal => switch (condition.data_type) {
|
.equal => switch (condition.data_type) {
|
||||||
.int, .float, .str, .bool, .date, .time, .datetime => {},
|
.int, .float, .str, .bool, .date, .time, .datetime => {},
|
||||||
@ -660,16 +660,16 @@ pub const Parser = struct {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// When this function is call, next token should be [
|
/// 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
|
/// Check if an int is here -> check if ; is here -> check if member is here -> check if [ is here -> loop
|
||||||
fn parseAdditionalData(
|
fn parseAdditionalData(
|
||||||
self: Parser,
|
self: Parser,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
additional_data: *AdditionalData,
|
additional_data: *AdditionalData,
|
||||||
struct_name: []const u8,
|
struct_name: []const u8,
|
||||||
) ZipponError!void {
|
) ZipponError!void {
|
||||||
var token = self.toker.next();
|
var token = self.toker.next();
|
||||||
var keep_next = false;
|
var keep_next = false;
|
||||||
var state: State = .expect_limit;
|
var state: State = .expect_limit;
|
||||||
@ -782,19 +782,19 @@ pub const Parser = struct {
|
|||||||
|
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Take the tokenizer and return a map of the ADD action.
|
/// 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'
|
/// 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 (
|
/// Entry token need to be (
|
||||||
fn parseNewData(
|
fn parseNewData(
|
||||||
self: Parser,
|
self: Parser,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
map: *std.StringHashMap(ConditionValue),
|
map: *std.StringHashMap(ConditionValue),
|
||||||
struct_name: []const u8,
|
struct_name: []const u8,
|
||||||
order: ?*std.ArrayList([]const u8),
|
order: ?*std.ArrayList([]const u8),
|
||||||
order_full: ?bool,
|
order_full: ?bool,
|
||||||
) !void {
|
) !void {
|
||||||
var token = self.toker.next();
|
var token = self.toker.next();
|
||||||
var keep_next = false;
|
var keep_next = false;
|
||||||
var member_name: []const u8 = undefined;
|
var member_name: []const u8 = undefined;
|
||||||
@ -901,9 +901,9 @@ pub const Parser = struct {
|
|||||||
|
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseComparisonOperator(self: Parser, token: Token) ZipponError!ComparisonOperator {
|
fn parseComparisonOperator(self: Parser, token: Token) ZipponError!ComparisonOperator {
|
||||||
return switch (token.tag) {
|
return switch (token.tag) {
|
||||||
.equal => .equal, // =
|
.equal => .equal, // =
|
||||||
.angle_bracket_left => .inferior, // <
|
.angle_bracket_left => .inferior, // <
|
||||||
@ -921,10 +921,10 @@ pub const Parser = struct {
|
|||||||
token.loc.end,
|
token.loc.end,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To run just after a condition like = or > or >= to get the corresponding ConditionValue that you need to compare
|
/// 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 {
|
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 start_index = token.loc.start;
|
||||||
const expected_tag: ?Token.Tag = switch (data_type) {
|
const expected_tag: ?Token.Tag = switch (data_type) {
|
||||||
.int => .int_literal,
|
.int => .int_literal,
|
||||||
@ -1197,10 +1197,10 @@ pub const Parser = struct {
|
|||||||
},
|
},
|
||||||
.self => unreachable,
|
.self => unreachable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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, 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()) {
|
||||||
if (token.tag != tag) return printError(
|
if (token.tag != tag) return printError(
|
||||||
@ -1212,5 +1212,4 @@ pub const Parser = struct {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user