tad saving before rewrite of tad.zig
This commit is contained in:
parent
680732230f
commit
687eb5930e
|
|
@ -30,15 +30,17 @@ module core
|
||||||
|
|
||||||
# Verbosity for the logger
|
# Verbosity for the logger
|
||||||
enum Verbosity
|
enum Verbosity
|
||||||
@backing u8
|
@backing u8 # this is a docstring
|
||||||
verbose = 0
|
|
||||||
|
verbose = 0 # this is another docstring
|
||||||
info
|
info
|
||||||
|
|
||||||
warn
|
warn
|
||||||
err
|
err
|
||||||
|
|
||||||
struct SliceMutable
|
struct SliceMutable
|
||||||
ptr: c_ptr u8 # what the fuck2
|
ptr: c_ptr u8
|
||||||
len: usize #what the fuck
|
len: usize
|
||||||
|
|
||||||
struct Slice
|
struct Slice
|
||||||
ptr: const_c_ptr u8
|
ptr: const_c_ptr u8
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,36 @@
|
||||||
const Module = struct {
|
const Module = struct {
|
||||||
contents: []const u8,
|
contents: []const u8,
|
||||||
|
fileName: []const u8,
|
||||||
backing: std.mem.Allocator,
|
backing: std.mem.Allocator,
|
||||||
arena: std.heap.ArenaAllocator,
|
arena: std.heap.ArenaAllocator,
|
||||||
enums: std.ArrayList(EnumDefnition) = .empty,
|
enums: std.ArrayList(EnumDefnition) = .empty,
|
||||||
|
|
||||||
const EnumDefnition = struct {
|
writing: ?WritingType = null,
|
||||||
name: []const u8,
|
|
||||||
values: []EnumValue,
|
const WritingType = union {
|
||||||
docstring: ?[]const u8,
|
_enum: *EnumDefnition,
|
||||||
|
// _struct: *StructDefinition,
|
||||||
};
|
};
|
||||||
|
|
||||||
const EnumValue = struct { name: []const u8, value: i64, backing: BuiltinType };
|
const EnumDefnition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
values: std.ArrayList(EnumValue),
|
||||||
|
docstring: ?[]const u8,
|
||||||
|
backingType: BuiltinType = ._int,
|
||||||
|
allowAnyInt: bool = false,
|
||||||
|
bodyIndent: usize,
|
||||||
|
_nextvalue: i64 = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const EnumValue = struct {
|
||||||
|
name: []const u8,
|
||||||
|
value: i64,
|
||||||
|
docstring: ?[]const u8 = null,
|
||||||
|
};
|
||||||
|
|
||||||
const BuiltinType = enum {
|
const BuiltinType = enum {
|
||||||
usize,
|
_usize,
|
||||||
|
_int,
|
||||||
_opaque,
|
_opaque,
|
||||||
_i8,
|
_i8,
|
||||||
_u8,
|
_u8,
|
||||||
|
|
@ -34,37 +51,69 @@ const Module = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
const ParserState = struct {
|
const ParserState = struct {
|
||||||
|
fileName: []const u8,
|
||||||
contents: []const u8,
|
contents: []const u8,
|
||||||
state: State = .root,
|
state: State = .root,
|
||||||
commentStart: ?usize = null,
|
commentStart: ?usize = null,
|
||||||
commentEnd: usize = 0,
|
commentEnd: usize = 0,
|
||||||
|
lineCount: usize = 0,
|
||||||
|
lineLen: usize = 0,
|
||||||
|
lineStart: usize = 0,
|
||||||
indentCount: usize = 0,
|
indentCount: usize = 0,
|
||||||
|
cursor: usize = 0, // used pretty much just for tracking
|
||||||
|
|
||||||
|
var psbuf: [4096]u8 = undefined;
|
||||||
|
|
||||||
|
pub fn positionErrorString(self: @This()) []const u8 {
|
||||||
|
var w = std.Io.Writer.fixed(&psbuf);
|
||||||
|
|
||||||
|
w.print(
|
||||||
|
"parser error: pos:{d} in {s}:{d}> \n",
|
||||||
|
.{ self.cursor, self.fileName, self.lineCount },
|
||||||
|
) catch return "BAD WRITE";
|
||||||
|
|
||||||
|
w.writeAll(std.mem.trim(
|
||||||
|
u8,
|
||||||
|
self.contents[self.lineStart .. self.lineStart + self.lineLen],
|
||||||
|
"\r\n",
|
||||||
|
)) catch return "BAD WRITE";
|
||||||
|
|
||||||
|
w.writeByte('\n') catch return "BAD WRITE";
|
||||||
|
_ = w.splatByte(' ', self.cursor) catch return "BAD WRITE";
|
||||||
|
w.writeByte('^') catch return "BAD WRITE";
|
||||||
|
|
||||||
|
return w.buffered();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn parse(self: *@This(), allocator: std.mem.Allocator, contents: []const u8) !void {
|
pub fn parse(self: *@This(), allocator: std.mem.Allocator, filename: []const u8, contents: []const u8) !void {
|
||||||
self.* = .{
|
self.* = .{
|
||||||
.contents = contents,
|
.contents = contents,
|
||||||
|
.fileName = filename,
|
||||||
.arena = std.heap.ArenaAllocator.init(allocator),
|
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||||
.backing = allocator,
|
.backing = allocator,
|
||||||
};
|
};
|
||||||
|
|
||||||
var lineCount: u32 = 0;
|
|
||||||
var tok = mem.tokenizeScalar(u8, contents, '\n');
|
var tok = mem.tokenizeScalar(u8, contents, '\n');
|
||||||
|
|
||||||
var state: ParserState = .{ .contents = contents };
|
var state: ParserState = .{ .contents = contents, .fileName = filename };
|
||||||
|
|
||||||
// comments which are standalone without a space get grouped into one comment block
|
// comments which are standalone without a space get grouped into one comment block
|
||||||
// comments which are trailing on the same line are used as a docstring only if no previous
|
// comments which are trailing on the same line are used as a docstring only if no previous
|
||||||
// block has a comment
|
// block has a comment
|
||||||
while (tok.next()) |line| {
|
while (tok.next()) |line| {
|
||||||
lineCount += 1;
|
state.lineCount += 1;
|
||||||
// log.info("\n{d}: {s}", .{ lineCount, line });
|
state.lineLen = line.len;
|
||||||
|
state.lineStart = tok.index - line.len;
|
||||||
|
// log.debug("\n{d}: {s}", .{ lineCount, line });
|
||||||
const commentIndex = mem.findScalar(u8, line, '#'); // split the comment
|
const commentIndex = mem.findScalar(u8, line, '#'); // split the comment
|
||||||
|
//
|
||||||
state.indentCount = countIndent(line);
|
state.indentCount = countIndent(line);
|
||||||
const statement = mem.trim(u8, if (commentIndex) |ci| line[0..ci][state.indentCount * 4 ..] else line[state.indentCount * 4 ..], " ");
|
const statement = mem.trim(u8, if (commentIndex) |ci| line[0..ci][state.indentCount * 4 ..] else line[state.indentCount * 4 ..], " ");
|
||||||
const comment = if (commentIndex) |ci| line[ci..] else "";
|
const comment = if (commentIndex) |ci| line[ci..] else "";
|
||||||
|
|
||||||
|
state.cursor = state.indentCount * 4;
|
||||||
|
|
||||||
if (statement.len != 0) {
|
if (statement.len != 0) {
|
||||||
state.commentEnd = tok.index - line.len;
|
state.commentEnd = tok.index - line.len;
|
||||||
try self.parseStatement(&state, statement, comment);
|
try self.parseStatement(&state, statement, comment);
|
||||||
|
|
@ -83,9 +132,9 @@ const Module = struct {
|
||||||
if (commentIndex == null)
|
if (commentIndex == null)
|
||||||
state.commentStart = null;
|
state.commentStart = null;
|
||||||
|
|
||||||
log.info(" (statement: {s}) (comment: ({s})", .{ statement, comment });
|
// log.debug(" (statement: {s}) (comment: ({s})", .{ statement, comment });
|
||||||
// remove when we're solid
|
// remove when we're solid
|
||||||
if (lineCount >= 1200) break;
|
if (state.lineCount >= 1200) return error.LinesTooLong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,12 +155,17 @@ const Module = struct {
|
||||||
fn beginEnum(self: *@This(), state: *ParserState, statement: []const u8, docstring: ?[]const u8) !void {
|
fn beginEnum(self: *@This(), state: *ParserState, statement: []const u8, docstring: ?[]const u8) !void {
|
||||||
state.state = ._enum;
|
state.state = ._enum;
|
||||||
const new = try self.enums.addOne(self.gpa());
|
const new = try self.enums.addOne(self.gpa());
|
||||||
|
new.* = .{ .docstring = docstring, .bodyIndent = state.indentCount + 1, .values = .empty, .name = "@anonymous" };
|
||||||
|
self.writing = .{ ._enum = new };
|
||||||
|
new.bodyIndent = state.indentCount + 1;
|
||||||
new.docstring = docstring;
|
new.docstring = docstring;
|
||||||
|
|
||||||
var split = mem.splitAny(u8, statement, " ");
|
var split = mem.splitAny(u8, statement, " ");
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (split.next()) |x| {
|
while (split.next()) |x| {
|
||||||
if (x.len > 0) {
|
if (x.len > 0) {
|
||||||
if (i == 1) {
|
if (i == 1) {
|
||||||
|
try ensureValidLabel(x);
|
||||||
new.name = x;
|
new.name = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,10 +174,133 @@ const Module = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ensureValidLabel(label: []const u8) !void {
|
||||||
|
if (label.len == 0) return error.InvalidLabel;
|
||||||
|
for (label) |c| {
|
||||||
|
switch (c) {
|
||||||
|
'a'...'z', 'A'...'Z', '0'...'9', '_' => {},
|
||||||
|
else => {
|
||||||
|
return error.InvalidLabel;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn splitWhitespaces(statement: []const u8, vector: [][]const u8) ![][]const u8 {
|
||||||
|
var tok = mem.tokenizeScalar(u8, statement, ' ');
|
||||||
|
var j: usize = 0;
|
||||||
|
while (tok.next()) |i| {
|
||||||
|
if (i.len == 0) continue;
|
||||||
|
if (j >= vector.len) return error.OutOfMemory;
|
||||||
|
vector[j] = i;
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vector[0..j];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Directive = enum {
|
||||||
|
backing,
|
||||||
|
import,
|
||||||
|
output,
|
||||||
|
any,
|
||||||
|
|
||||||
|
pub fn get(x: []const u8) ?@This() {
|
||||||
|
const typeinfo = @typeInfo(@This()).@"enum";
|
||||||
|
inline for (typeinfo.field_names, 0..) |fname, i| {
|
||||||
|
if (mem.eql(u8, x, "@" ++ fname))
|
||||||
|
return @fromBackingInt(@intCast(typeinfo.field_values[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn eql(a: []const u8, b: []const u8) bool {
|
||||||
|
return mem.eql(u8, a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseInt(x: []const u8) ?i64 {
|
||||||
|
return std.fmt.parseInt(i64, x, 0) catch null;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getBuiltinType(x: []const u8) ?BuiltinType {
|
||||||
|
const ti = @typeInfo(BuiltinType).@"enum";
|
||||||
|
inline for (ti.field_names, 0..) |fieldname, i| {
|
||||||
|
if (eql(fieldname[1..], x))
|
||||||
|
return @fromBackingInt(@intCast(i));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseActiveEnum(self: *@This(), state: *ParserState, statement: []const u8, docstring: ?[]const u8) !void {
|
||||||
|
const activeEnum = self.writing.?._enum;
|
||||||
|
log.debug("(enum: {s}) (statement: '{s}') (docstring: {?s})", .{ activeEnum.name, statement, docstring });
|
||||||
|
|
||||||
|
var b: [32][]const u8 = undefined;
|
||||||
|
const toks = try splitWhitespaces(statement, &b);
|
||||||
|
|
||||||
|
if (statement[0] == '@') {
|
||||||
|
if (Directive.get(toks[0])) |d| {
|
||||||
|
switch (d) {
|
||||||
|
.any => {
|
||||||
|
activeEnum.allowAnyInt = true;
|
||||||
|
},
|
||||||
|
.backing => {
|
||||||
|
if (toks.len != 2) return error.MalformedDirective;
|
||||||
|
if (getBuiltinType(toks[1])) |builtintype| {
|
||||||
|
switch (builtintype) {
|
||||||
|
._opaque => return error.InvalidBackingType,
|
||||||
|
else => {
|
||||||
|
activeEnum.backingType = builtintype;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => return error.UnexpectedDirective,
|
||||||
|
}
|
||||||
|
log.debug(" (directive:{s})", .{@tagName(d)});
|
||||||
|
} else {
|
||||||
|
self.perror("unexpected directive", state);
|
||||||
|
return error.CompileError;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (toks.len) {
|
||||||
|
1 => {
|
||||||
|
log.debug(" (addstatement name:{s} value:{d})", .{ toks[0], activeEnum._nextvalue });
|
||||||
|
try activeEnum.values.append(self.arena.allocator(), .{
|
||||||
|
.name = toks[0],
|
||||||
|
.value = activeEnum._nextvalue,
|
||||||
|
.docstring = docstring,
|
||||||
|
});
|
||||||
|
activeEnum._nextvalue += 1;
|
||||||
|
},
|
||||||
|
3 => {
|
||||||
|
if (!eql(toks[1], "=")) return error.UnexpectedToken;
|
||||||
|
const results = .{ toks[0], parseInt(toks[2]) orelse return error.UnexpectedToken };
|
||||||
|
log.debug(" (addstatement name:{s} value:{d} [explicit])", .{ results[0], results[1] });
|
||||||
|
try activeEnum.values.append(self.arena.allocator(), .{
|
||||||
|
.name = results[0],
|
||||||
|
.value = results[1],
|
||||||
|
.docstring = docstring,
|
||||||
|
});
|
||||||
|
activeEnum._nextvalue = results[1] + 1;
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn endEnum(self: @This(), state: *ParserState) !void {
|
fn endEnum(self: @This(), state: *ParserState) !void {
|
||||||
state.state = .root;
|
state.state = .root;
|
||||||
const last = &self.enums.items[self.enums.items.len - 1];
|
const last = &self.enums.items[self.enums.items.len - 1];
|
||||||
log.info("ENUM ADDED enum {s}: docstring: {?s}", .{ last.name, last.docstring });
|
log.debug("(addenum {s}: docstring: {s} values:{d} backingType:{})", .{ last.name, if (last.docstring != null) "yes" else "no", last.values.items.len, last.backingType });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn perror(self: @This(), string: []const u8, state: *ParserState) void {
|
||||||
|
_ = self;
|
||||||
|
log.err("{s}", .{string});
|
||||||
|
log.err("{s}", .{state.positionErrorString()});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parseStatement(self: *@This(), state: *ParserState, statement: []const u8, comment: []const u8) !void {
|
pub fn parseStatement(self: *@This(), state: *ParserState, statement: []const u8, comment: []const u8) !void {
|
||||||
|
|
@ -134,7 +311,7 @@ const Module = struct {
|
||||||
if (comment.len > 0) break :blk comment;
|
if (comment.len > 0) break :blk comment;
|
||||||
break :blk "";
|
break :blk "";
|
||||||
};
|
};
|
||||||
log.info(" docstring: len({d})] \n'{s}'", .{ doc.len, doc });
|
// log.debug(" docstring: len({d})] \n'{s}'", .{ doc.len, doc });
|
||||||
|
|
||||||
// docstring ready, start parsing the statements
|
// docstring ready, start parsing the statements
|
||||||
switch (state.state) {
|
switch (state.state) {
|
||||||
|
|
@ -144,11 +321,19 @@ const Module = struct {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
._enum => {
|
._enum => {
|
||||||
if (state.indentCount == 0)
|
const activeEnum = self.writing.?._enum;
|
||||||
|
if (state.indentCount < self.writing.?._enum.bodyIndent) {
|
||||||
try self.endEnum(state);
|
try self.endEnum(state);
|
||||||
|
} else if (state.indentCount != activeEnum.bodyIndent) {
|
||||||
|
self.perror("unexpected indent", state);
|
||||||
|
log.err("unpexected indent {s}", .{state.positionErrorString()});
|
||||||
|
return error.ParserFailed;
|
||||||
|
} else {
|
||||||
|
try self.parseActiveEnum(state, statement, comment);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
log.info("not implemented yet {s}", .{@tagName(state.state)});
|
log.debug("not implemented yet {s}", .{@tagName(state.state)});
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -183,7 +368,7 @@ pub fn compile(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void
|
||||||
const normalized = normalizeContents(rawf);
|
const normalized = normalizeContents(rawf);
|
||||||
|
|
||||||
var toks: Module = undefined;
|
var toks: Module = undefined;
|
||||||
try toks.parse(allocator, normalized);
|
try toks.parse(allocator, path, normalized);
|
||||||
defer toks.deinit();
|
defer toks.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue