And other
This commit is contained in:
parent
90edb94f7a
commit
78213df3ff
@ -26,220 +26,215 @@ const State = enum {
|
|||||||
add_struct,
|
add_struct,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Parser = struct {
|
pub const Parser = @This();
|
||||||
toker: *Toker,
|
toker: *Toker,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
|
|
||||||
pub fn init(toker: *Toker, allocator: Allocator) Parser {
|
pub fn init(toker: *Toker, allocator: Allocator) Parser {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.toker = toker,
|
.toker = toker,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename something better and move it somewhere else
|
pub fn parse(self: *Parser, struct_array: *std.ArrayList(SchemaStruct)) !void {
|
||||||
|
var state: State = .expect_struct_name_OR_end;
|
||||||
|
var keep_next = false;
|
||||||
|
|
||||||
pub fn parse(self: *Parser, struct_array: *std.ArrayList(SchemaStruct)) !void {
|
var member_token: Token = undefined;
|
||||||
var state: State = .expect_struct_name_OR_end;
|
|
||||||
var keep_next = false;
|
|
||||||
|
|
||||||
var member_token: Token = undefined;
|
var name: []const u8 = undefined;
|
||||||
|
var member_list = std.ArrayList([]const u8).init(self.allocator);
|
||||||
|
defer member_list.deinit();
|
||||||
|
var type_list = std.ArrayList(DataType).init(self.allocator);
|
||||||
|
defer type_list.deinit();
|
||||||
|
var links = std.StringHashMap([]const u8).init(self.allocator);
|
||||||
|
defer links.deinit();
|
||||||
|
|
||||||
var name: []const u8 = undefined;
|
var token = self.toker.next();
|
||||||
var member_list = std.ArrayList([]const u8).init(self.allocator);
|
while ((state != .end) and (state != .invalid)) : ({
|
||||||
defer member_list.deinit();
|
token = if (!keep_next) self.toker.next() else token;
|
||||||
var type_list = std.ArrayList(DataType).init(self.allocator);
|
keep_next = false;
|
||||||
defer type_list.deinit();
|
}) switch (state) {
|
||||||
var links = std.StringHashMap([]const u8).init(self.allocator);
|
.expect_struct_name_OR_end => switch (token.tag) {
|
||||||
defer links.deinit();
|
.identifier => {
|
||||||
|
state = .expect_l_paren;
|
||||||
var token = self.toker.next();
|
name = self.toker.getTokenSlice(token);
|
||||||
while ((state != .end) and (state != .invalid)) : ({
|
member_list.append("id") catch return SchemaParserError.MemoryError;
|
||||||
token = if (!keep_next) self.toker.next() else token;
|
type_list.append(.self) catch return SchemaParserError.MemoryError;
|
||||||
keep_next = false;
|
|
||||||
}) switch (state) {
|
|
||||||
.expect_struct_name_OR_end => switch (token.tag) {
|
|
||||||
.identifier => {
|
|
||||||
state = .expect_l_paren;
|
|
||||||
name = self.toker.getTokenSlice(token);
|
|
||||||
member_list.append("id") catch return SchemaParserError.MemoryError;
|
|
||||||
type_list.append(.self) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.eof => state = .end,
|
|
||||||
else => {
|
|
||||||
std.debug.print("{s}\n", .{self.toker.getTokenSlice(token)});
|
|
||||||
return printError(
|
|
||||||
"Error parsing schema: Expected a struct name",
|
|
||||||
SchemaParserError.SynthaxError,
|
|
||||||
self.toker.buffer,
|
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
.eof => state = .end,
|
||||||
.expect_l_paren => switch (token.tag) {
|
else => {
|
||||||
.l_paren => state = .expect_member_name,
|
std.debug.print("{s}\n", .{self.toker.getTokenSlice(token)});
|
||||||
else => return printError(
|
return printError(
|
||||||
"Error parsing schema: Expected (",
|
"Error parsing schema: Expected a struct name",
|
||||||
SchemaParserError.SynthaxError,
|
SchemaParserError.SynthaxError,
|
||||||
self.toker.buffer,
|
self.toker.buffer,
|
||||||
token.loc.start,
|
token.loc.start,
|
||||||
token.loc.end,
|
token.loc.end,
|
||||||
),
|
);
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
|
||||||
.expect_member_name_OR_r_paren => switch (token.tag) {
|
.expect_l_paren => switch (token.tag) {
|
||||||
.identifier => {
|
.l_paren => state = .expect_member_name,
|
||||||
state = .expect_member_name;
|
else => return printError(
|
||||||
keep_next = true;
|
"Error parsing schema: Expected (",
|
||||||
},
|
SchemaParserError.SynthaxError,
|
||||||
.r_paren => state = .add_struct,
|
self.toker.buffer,
|
||||||
else => return printError(
|
token.loc.start,
|
||||||
"Error parsing schema: Expected member name or )",
|
token.loc.end,
|
||||||
SchemaParserError.SynthaxError,
|
),
|
||||||
self.toker.buffer,
|
},
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
|
|
||||||
.add_struct => {
|
.expect_member_name_OR_r_paren => switch (token.tag) {
|
||||||
struct_array.append(try SchemaStruct.init(
|
.identifier => {
|
||||||
name,
|
state = .expect_member_name;
|
||||||
member_list.toOwnedSlice() catch return SchemaParserError.MemoryError,
|
|
||||||
type_list.toOwnedSlice() catch return SchemaParserError.MemoryError,
|
|
||||||
try links.clone(),
|
|
||||||
)) catch return SchemaParserError.MemoryError;
|
|
||||||
|
|
||||||
links.deinit();
|
|
||||||
links = std.StringHashMap([]const u8).init(self.allocator);
|
|
||||||
|
|
||||||
member_list = std.ArrayList([]const u8).init(self.allocator);
|
|
||||||
type_list = std.ArrayList(DataType).init(self.allocator);
|
|
||||||
|
|
||||||
state = .expect_struct_name_OR_end;
|
|
||||||
keep_next = true;
|
keep_next = true;
|
||||||
},
|
},
|
||||||
|
.r_paren => state = .add_struct,
|
||||||
|
else => return printError(
|
||||||
|
"Error parsing schema: Expected member name or )",
|
||||||
|
SchemaParserError.SynthaxError,
|
||||||
|
self.toker.buffer,
|
||||||
|
token.loc.start,
|
||||||
|
token.loc.end,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
.expect_member_name => {
|
.add_struct => {
|
||||||
state = .expect_two_dot;
|
struct_array.append(try SchemaStruct.init(
|
||||||
member_list.append(self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
name,
|
||||||
member_token = token;
|
member_list.toOwnedSlice() catch return SchemaParserError.MemoryError,
|
||||||
|
type_list.toOwnedSlice() catch return SchemaParserError.MemoryError,
|
||||||
|
try links.clone(),
|
||||||
|
)) catch return SchemaParserError.MemoryError;
|
||||||
|
|
||||||
|
links.deinit();
|
||||||
|
links = std.StringHashMap([]const u8).init(self.allocator);
|
||||||
|
|
||||||
|
member_list = std.ArrayList([]const u8).init(self.allocator);
|
||||||
|
type_list = std.ArrayList(DataType).init(self.allocator);
|
||||||
|
|
||||||
|
state = .expect_struct_name_OR_end;
|
||||||
|
keep_next = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
.expect_member_name => {
|
||||||
|
state = .expect_two_dot;
|
||||||
|
member_list.append(self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
||||||
|
member_token = token;
|
||||||
|
},
|
||||||
|
|
||||||
|
.expect_two_dot => switch (token.tag) {
|
||||||
|
.two_dot => state = .expect_value_type,
|
||||||
|
else => return printError(
|
||||||
|
"Error parsing schema: Expected :",
|
||||||
|
SchemaParserError.SynthaxError,
|
||||||
|
self.toker.buffer,
|
||||||
|
token.loc.start,
|
||||||
|
token.loc.end,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
.expect_value_type => switch (token.tag) {
|
||||||
|
.type_int => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.int) catch return SchemaParserError.MemoryError;
|
||||||
},
|
},
|
||||||
|
.type_str => {
|
||||||
.expect_two_dot => switch (token.tag) {
|
state = .expect_comma;
|
||||||
.two_dot => state = .expect_value_type,
|
type_list.append(.str) catch return SchemaParserError.MemoryError;
|
||||||
else => return printError(
|
|
||||||
"Error parsing schema: Expected :",
|
|
||||||
SchemaParserError.SynthaxError,
|
|
||||||
self.toker.buffer,
|
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
|
.type_float => {
|
||||||
.expect_value_type => switch (token.tag) {
|
state = .expect_comma;
|
||||||
.type_int => {
|
type_list.append(.float) catch return SchemaParserError.MemoryError;
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.int) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_str => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.str) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_float => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.float) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_bool => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.bool) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_date => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.date) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_time => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.time) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_datetime => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.datetime) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.identifier => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.link) catch return SchemaParserError.MemoryError;
|
|
||||||
links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.lr_bracket => state = .expext_array_type,
|
|
||||||
else => return printError(
|
|
||||||
"Error parsing schema: Expected data type",
|
|
||||||
SchemaParserError.SynthaxError,
|
|
||||||
self.toker.buffer,
|
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
|
.type_bool => {
|
||||||
.expext_array_type => switch (token.tag) {
|
state = .expect_comma;
|
||||||
.type_int => {
|
type_list.append(.bool) catch return SchemaParserError.MemoryError;
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.int_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_str => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.str_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_float => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.float_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_bool => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.bool_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_date => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.date_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_time => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.time_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.type_datetime => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.datetime_array) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
.identifier => {
|
|
||||||
state = .expect_comma;
|
|
||||||
type_list.append(.link_array) catch return SchemaParserError.MemoryError;
|
|
||||||
links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
|
||||||
},
|
|
||||||
else => return printError(
|
|
||||||
"Error parsing schema: Expected data type",
|
|
||||||
SchemaParserError.SynthaxError,
|
|
||||||
self.toker.buffer,
|
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
|
.type_date => {
|
||||||
.expect_comma => switch (token.tag) {
|
state = .expect_comma;
|
||||||
.comma => state = .expect_member_name_OR_r_paren,
|
type_list.append(.date) catch return SchemaParserError.MemoryError;
|
||||||
else => return printError(
|
|
||||||
"Error parsing schema: Expected ,",
|
|
||||||
SchemaParserError.SynthaxError,
|
|
||||||
self.toker.buffer,
|
|
||||||
token.loc.start,
|
|
||||||
token.loc.end,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
|
.type_time => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.time) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_datetime => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.datetime) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.identifier => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.link) catch return SchemaParserError.MemoryError;
|
||||||
|
links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.lr_bracket => state = .expext_array_type,
|
||||||
|
else => return printError(
|
||||||
|
"Error parsing schema: Expected data type",
|
||||||
|
SchemaParserError.SynthaxError,
|
||||||
|
self.toker.buffer,
|
||||||
|
token.loc.start,
|
||||||
|
token.loc.end,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
else => unreachable,
|
.expext_array_type => switch (token.tag) {
|
||||||
};
|
.type_int => {
|
||||||
}
|
state = .expect_comma;
|
||||||
};
|
type_list.append(.int_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_str => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.str_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_float => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.float_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_bool => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.bool_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_date => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.date_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_time => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.time_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.type_datetime => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.datetime_array) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
.identifier => {
|
||||||
|
state = .expect_comma;
|
||||||
|
type_list.append(.link_array) catch return SchemaParserError.MemoryError;
|
||||||
|
links.put(self.toker.getTokenSlice(member_token), self.toker.getTokenSlice(token)) catch return SchemaParserError.MemoryError;
|
||||||
|
},
|
||||||
|
else => return printError(
|
||||||
|
"Error parsing schema: Expected data type",
|
||||||
|
SchemaParserError.SynthaxError,
|
||||||
|
self.toker.buffer,
|
||||||
|
token.loc.start,
|
||||||
|
token.loc.end,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
// TODO: Some test, weird that there isn't any yet
|
.expect_comma => switch (token.tag) {
|
||||||
|
.comma => state = .expect_member_name_OR_r_paren,
|
||||||
|
else => return printError(
|
||||||
|
"Error parsing schema: Expected ,",
|
||||||
|
SchemaParserError.SynthaxError,
|
||||||
|
self.toker.buffer,
|
||||||
|
token.loc.start,
|
||||||
|
token.loc.end,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
else => unreachable,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -55,27 +55,27 @@ pub const ThreadSyncContext = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ThreadEngine = struct {
|
pub const ThreadEngine = @This();
|
||||||
thread_arena: *std.heap.ThreadSafeAllocator,
|
|
||||||
thread_pool: *Pool,
|
|
||||||
|
|
||||||
pub fn init() ThreadEngine {
|
thread_arena: *std.heap.ThreadSafeAllocator,
|
||||||
thread_arena = std.heap.ThreadSafeAllocator{
|
thread_pool: *Pool,
|
||||||
.child_allocator = allocator,
|
|
||||||
};
|
|
||||||
|
|
||||||
thread_pool.init(std.Thread.Pool.Options{
|
pub fn init() ThreadEngine {
|
||||||
.allocator = thread_arena.allocator(),
|
thread_arena = std.heap.ThreadSafeAllocator{
|
||||||
.n_jobs = CPU_CORE,
|
.child_allocator = allocator,
|
||||||
}) catch @panic("=(");
|
};
|
||||||
|
|
||||||
return ThreadEngine{
|
thread_pool.init(std.Thread.Pool.Options{
|
||||||
.thread_pool = &thread_pool,
|
.allocator = thread_arena.allocator(),
|
||||||
.thread_arena = &thread_arena,
|
.n_jobs = CPU_CORE,
|
||||||
};
|
}) catch @panic("=(");
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(_: ThreadEngine) void {
|
return ThreadEngine{
|
||||||
thread_pool.deinit();
|
.thread_pool = &thread_pool,
|
||||||
}
|
.thread_arena = &thread_arena,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(_: ThreadEngine) void {
|
||||||
|
thread_pool.deinit();
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
pub const Loc = struct {
|
pub const Loc = @This();
|
||||||
start: usize,
|
|
||||||
end: usize,
|
start: usize,
|
||||||
};
|
end: usize,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user