Added links to schema struct

Added a hash map that contain the name of the struct corresponding to
the link.

So the member "best_friend: User" is an entry key: "best_friend" value:
"User"
This commit is contained in:
Adrien Bouvais 2024-10-20 18:01:17 +02:00
parent 9d2948d686
commit 08869e5e3d

View File

@ -38,6 +38,7 @@ pub const Parser = struct {
name: []const u8, name: []const u8,
members: std.ArrayList([]const u8), members: std.ArrayList([]const u8),
types: std.ArrayList(DataType), types: std.ArrayList(DataType),
links: std.StringHashMap([]const u8),
pub fn init(allocator: Allocator, name: []const u8) SchemaStruct { pub fn init(allocator: Allocator, name: []const u8) SchemaStruct {
return SchemaStruct{ return SchemaStruct{
@ -45,12 +46,14 @@ pub const Parser = struct {
.name = name, .name = name,
.members = std.ArrayList([]const u8).init(allocator), .members = std.ArrayList([]const u8).init(allocator),
.types = std.ArrayList(DataType).init(allocator), .types = std.ArrayList(DataType).init(allocator),
.links = std.StringHashMap([]const u8).init(allocator),
}; };
} }
pub fn deinit(self: *SchemaStruct) void { pub fn deinit(self: *SchemaStruct) void {
self.types.deinit(); self.types.deinit();
self.members.deinit(); self.members.deinit();
self.links.deinit();
} }
}; };
@ -69,6 +72,8 @@ pub const Parser = struct {
} }
} }
var member_token: Token = undefined;
var token = self.toker.next(); var token = self.toker.next();
while ((state != .end) and (state != .invalid)) : ({ while ((state != .end) and (state != .invalid)) : ({
token = if (!keep_next) self.toker.next() else token; token = if (!keep_next) self.toker.next() else token;
@ -121,6 +126,7 @@ pub const Parser = struct {
.expect_member_name => { .expect_member_name => {
state = .expect_two_dot; state = .expect_two_dot;
struct_array.items[index].members.append(self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError; struct_array.items[index].members.append(self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
member_token = token;
}, },
.expect_two_dot => switch (token.tag) { .expect_two_dot => switch (token.tag) {
@ -137,33 +143,37 @@ pub const Parser = struct {
.expect_value_type => switch (token.tag) { .expect_value_type => switch (token.tag) {
.type_int => { .type_int => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.int) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.int) catch return SchemaParserError.MemoryError;
}, },
.type_str => { .type_str => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.str) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.str) catch return SchemaParserError.MemoryError;
}, },
.type_float => { .type_float => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.float) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.float) catch return SchemaParserError.MemoryError;
}, },
.type_bool => { .type_bool => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.bool) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.bool) catch return SchemaParserError.MemoryError;
}, },
.type_date => { .type_date => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.date) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.date) catch return SchemaParserError.MemoryError;
}, },
.type_time => { .type_time => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.time) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.time) catch return SchemaParserError.MemoryError;
}, },
.type_datetime => { .type_datetime => {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.datetime) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(.datetime) catch return SchemaParserError.MemoryError;
},
.identifier => {
state = .expect_comma;
struct_array.items[index].types.append(.link) catch return SchemaParserError.MemoryError;
struct_array.items[index].links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
}, },
.identifier => return SchemaParserError.FeatureMissing,
.lr_bracket => state = .expext_array_type, .lr_bracket => state = .expext_array_type,
else => return printError( else => return printError(
"Error parsing schema: Expected data type", "Error parsing schema: Expected data type",
@ -203,13 +213,11 @@ pub const Parser = struct {
state = .expect_comma; state = .expect_comma;
struct_array.items[index].types.append(DataType.datetime_array) catch return SchemaParserError.MemoryError; struct_array.items[index].types.append(DataType.datetime_array) catch return SchemaParserError.MemoryError;
}, },
.identifier => return printError( .identifier => {
"Error parsing schema: Relationship not yet implemented", state = .expect_comma;
SchemaParserError.FeatureMissing, struct_array.items[index].types.append(.link) catch return SchemaParserError.MemoryError;
self.toker.buffer, struct_array.items[index].links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
token.loc.start, },
token.loc.end,
),
else => return printError( else => return printError(
"Error parsing schema: Expected data type", "Error parsing schema: Expected data type",
SchemaParserError.SynthaxError, SchemaParserError.SynthaxError,