adding new tad file spec, and enums work in the parser!
This commit is contained in:
parent
a654f8d302
commit
92d17a0562
|
|
@ -0,0 +1,5 @@
|
||||||
|
generate-api
|
||||||
|
|
||||||
|
takes an input file and outputs source and output zig files
|
||||||
|
input-file - input .tad file to parse
|
||||||
|
help - shows the help dialogue
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const program = @import("build/programs.zig");
|
const program = @import("build-tooling/programs.zig");
|
||||||
|
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
@ -25,14 +25,13 @@ pub fn build(b: *std.Build) void {
|
||||||
.root_module = b.createModule(.{
|
.root_module = b.createModule(.{
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.target = target,
|
.target = target,
|
||||||
.root_source_file = b.path("tools/interface-parser.zig"),
|
.root_source_file = b.path("tools/generate-api.zig"),
|
||||||
}),
|
}),
|
||||||
.name = "parser",
|
.name = "parser",
|
||||||
});
|
});
|
||||||
|
|
||||||
const parse_interface_core = b.addRunArtifact(parser);
|
const parse_interface_core = b.addRunArtifact(parser);
|
||||||
parse_interface_core.addArgs(&.{"--input"});
|
parse_interface_core.addFileArg2(b.path("src/core/api.tad"), .{ .prefix = "--input-file=" });
|
||||||
parse_interface_core.addFileInput(b.path("core/src/core.zig"));
|
|
||||||
|
|
||||||
const generate_headers_step = b.step("generate-interface", "generates interface information from all core apis");
|
const generate_headers_step = b.step("generate-interface", "generates interface information from all core apis");
|
||||||
|
|
||||||
|
|
@ -98,6 +97,7 @@ pub fn build(b: *std.Build) void {
|
||||||
}),
|
}),
|
||||||
.linkage = .dynamic, // todo static builds
|
.linkage = .dynamic, // todo static builds
|
||||||
});
|
});
|
||||||
|
hotlib.bundle_ubsan_rt = false;
|
||||||
|
|
||||||
const inst = b.addInstallArtifact(hotlib, .{
|
const inst = b.addInstallArtifact(hotlib, .{
|
||||||
.dest_sub_path = b.fmt("{s}.pod{s}", .{
|
.dest_sub_path = b.fmt("{s}.pod{s}", .{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
//req: lets discuss- wh
|
|
||||||
// hand bombed zig version of the api
|
// hand bombed zig version of the api
|
||||||
pub const Verbosity = enum(c_int) {
|
pub const Verbosity = enum(c_int) {
|
||||||
verbose,
|
verbose,
|
||||||
|
|
@ -44,6 +43,6 @@ var core: *core_api = undefined;
|
||||||
pub export fn entrypoint(_core: *core_api) callconv(.c) i32 {
|
pub export fn entrypoint(_core: *core_api) callconv(.c) i32 {
|
||||||
core = _core;
|
core = _core;
|
||||||
const category = core.declare_logging_category(Slice.make("test"), .warn);
|
const category = core.declare_logging_category(Slice.make("test"), .warn);
|
||||||
core.log_puts(category, .info, Slice.make("lmao 2 nova"));
|
core.log_puts(category, .info, Slice.make("Game startup is called"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
# tiny api definition - tadpole
|
||||||
|
# reads linearly from top to down 4 spaces denotes an indent
|
||||||
|
#
|
||||||
|
# comments are skipped/stripped,
|
||||||
|
#
|
||||||
|
# comment := # [.*] \n
|
||||||
|
#
|
||||||
|
# root := [declaration+]
|
||||||
|
# declaration := [comment] (enum | struct | moduledef | functiondef)
|
||||||
|
# enum := "enum" label [enumdef+]
|
||||||
|
# enumdef := label ["=" integer]
|
||||||
|
#
|
||||||
|
# struct := "struct" label [field+]
|
||||||
|
# field := [docstring](label ":" typedef)
|
||||||
|
# typedef := ([pointertype] builtin) | label
|
||||||
|
#
|
||||||
|
# pointertype := c_ptr | const_c_ptr
|
||||||
|
# builtin := "usize" | "i8" | "u8" | "i32" | "u32" | "i64" | "u64" | "opaque"
|
||||||
|
#
|
||||||
|
# moduledef := "module" label [moduleoptions]
|
||||||
|
# moduleoptions := label string | integer | label
|
||||||
|
#
|
||||||
|
# functiondef := "fn" label "->" [([pointertype] builtin) | "void"] [field+]
|
||||||
|
#
|
||||||
|
|
||||||
|
module core
|
||||||
|
@import platform #
|
||||||
|
@output "src/core.zig"
|
||||||
|
|
||||||
|
|
||||||
|
# Verbosity for the logger
|
||||||
|
enum Verbosity
|
||||||
|
@backing u8
|
||||||
|
verbose = 0
|
||||||
|
info
|
||||||
|
warn
|
||||||
|
err
|
||||||
|
|
||||||
|
struct SliceMutable
|
||||||
|
ptr: c_ptr u8 # what the fuck2
|
||||||
|
len: usize #what the fuck
|
||||||
|
|
||||||
|
struct Slice
|
||||||
|
ptr: const_c_ptr u8
|
||||||
|
len: usize
|
||||||
|
|
||||||
|
fn get_engine -> c_ptr opaque
|
||||||
|
fn get_tag_registry -> c_ptr opaque
|
||||||
|
fn start -> void
|
||||||
|
fn shutdown -> void
|
||||||
|
|
||||||
|
fn declare_logging_category -> u32
|
||||||
|
name: Slice
|
||||||
|
default_verbosity: Verbosity
|
||||||
|
|
||||||
|
fn log_puts -> void
|
||||||
|
category: u32
|
||||||
|
verbosity: Verbosity
|
||||||
|
string: Slice
|
||||||
|
|
@ -89,6 +89,12 @@ var fwriter: std.Io.File.Writer = undefined;
|
||||||
pub fn setupPrint(io: std.Io) void {
|
pub fn setupPrint(io: std.Io) void {
|
||||||
fwriter = std.Io.File.stdout().writer(io, &writeBuffer);
|
fwriter = std.Io.File.stdout().writer(io, &writeBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn print(comptime fmt: []const u8, args: anytype) void {
|
||||||
|
fwriter.interface.print(fmt, args) catch return;
|
||||||
|
fwriter.interface.flush() catch {};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn println(comptime fmt: []const u8, args: anytype) void {
|
pub fn println(comptime fmt: []const u8, args: anytype) void {
|
||||||
fwriter.interface.print(fmt ++ "\n", args) catch return;
|
fwriter.interface.print(fmt ++ "\n", args) catch return;
|
||||||
fwriter.interface.flush() catch {};
|
fwriter.interface.flush() catch {};
|
||||||
|
|
@ -138,8 +144,8 @@ pub fn getEngine() *engine.Engine {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const SmallString = extern struct {
|
pub const SmallString = extern struct {
|
||||||
buffer: [64]u8,
|
|
||||||
len: u8,
|
len: u8,
|
||||||
|
buffer: [64]u8,
|
||||||
pub fn bytes(self: *const @This()) []const u8 {
|
pub fn bytes(self: *const @This()) []const u8 {
|
||||||
return self.buffer[0..self.len];
|
return self.buffer[0..self.len];
|
||||||
}
|
}
|
||||||
|
|
@ -152,8 +158,8 @@ pub const SmallString = extern struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TinyString = extern struct {
|
pub const TinyString = extern struct {
|
||||||
buffer: [32]u8,
|
|
||||||
len: u8,
|
len: u8,
|
||||||
|
buffer: [32]u8,
|
||||||
pub inline fn bytes(self: *const @This()) []const u8 {
|
pub inline fn bytes(self: *const @This()) []const u8 {
|
||||||
return self.buffer[0..self.len];
|
return self.buffer[0..self.len];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ pub fn main(init: std.process.Init) !void {
|
||||||
|
|
||||||
core.setupPrint(io);
|
core.setupPrint(io);
|
||||||
|
|
||||||
println("press h or 'help' for help \n> ", .{});
|
core.print("press h or 'help' for help \n> ", .{});
|
||||||
|
|
||||||
var cliTest: bool = false;
|
var cliTest: bool = false;
|
||||||
for (init.minimal.args.vector) |a| {
|
for (init.minimal.args.vector) |a| {
|
||||||
|
|
@ -41,7 +41,13 @@ pub fn main(init: std.process.Init) !void {
|
||||||
loadedFingerPrints = .init(init.arena.allocator());
|
loadedFingerPrints = .init(init.arena.allocator());
|
||||||
|
|
||||||
loop: while (true) {
|
loop: while (true) {
|
||||||
const l = try r.takeDelimiter('\n') orelse continue;
|
if (cliTest and core.engine.gEngine.frameCount == 10)
|
||||||
|
break :loop;
|
||||||
|
|
||||||
|
const l = try r.takeDelimiter('\n') orelse {
|
||||||
|
core.engine.gEngine.tick();
|
||||||
|
continue;
|
||||||
|
};
|
||||||
const line = std.mem.trim(u8, l, "\n ");
|
const line = std.mem.trim(u8, l, "\n ");
|
||||||
|
|
||||||
core.engine.gEngine.tick();
|
core.engine.gEngine.tick();
|
||||||
|
|
@ -67,10 +73,7 @@ pub fn main(init: std.process.Init) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cliTest and core.engine.gEngine.frameCount == 10)
|
core.print("> ", .{});
|
||||||
break :loop;
|
|
||||||
|
|
||||||
println("> ", .{});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
generate-api
|
||||||
|
|
||||||
|
takes an input file and outputs source and output zig files
|
||||||
|
input-file - input .tad file to parse
|
||||||
|
help - shows the help dialogue
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
pub fn SimpleArgParse(comptime prog_name: []const u8, comptime help_text: []const u8) type {
|
||||||
|
return struct {
|
||||||
|
var gArgsMap: std.StringHashMap([]const u8) = undefined;
|
||||||
|
pub var parsedOne: bool = false;
|
||||||
|
|
||||||
|
pub fn initArgsMap(gpa: std.mem.Allocator) void {
|
||||||
|
gArgsMap = .init(gpa);
|
||||||
|
}
|
||||||
|
|
||||||
|
const helpfile = prog_name ++ ".help.txt";
|
||||||
|
|
||||||
|
pub fn writeArgsMap(io: std.Io) !void {
|
||||||
|
var d = try std.Io.Dir.cwd().createDirPathOpen(io, ".helpfiles/", .{});
|
||||||
|
var f = try d.createFile(io, helpfile, .{});
|
||||||
|
defer f.close(io);
|
||||||
|
|
||||||
|
var b: [1024]u8 = undefined;
|
||||||
|
var fwriter = f.writer(io, &b);
|
||||||
|
const w = &fwriter.interface;
|
||||||
|
try w.writeAll(help_text);
|
||||||
|
var iter = gArgsMap.iterator();
|
||||||
|
|
||||||
|
while (iter.next()) |e| {
|
||||||
|
try w.print("{s:>16} - {s}\n", .{ e.key_ptr.*, e.value_ptr.* });
|
||||||
|
}
|
||||||
|
try w.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn printHelp(io: std.Io) !void {
|
||||||
|
var d = try std.Io.Dir.cwd().createDirPathOpen(io, ".helpfiles/", .{});
|
||||||
|
|
||||||
|
d.access(io, helpfile, .{}) catch {
|
||||||
|
writeArgsMap(io) catch {
|
||||||
|
std.debug.print("help file generation failed", .{});
|
||||||
|
return error.FailedToWriteFile;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var rbuf: [64 * 1024]u8 = undefined;
|
||||||
|
const helpcontents = try d.readFile(io, helpfile, &rbuf);
|
||||||
|
|
||||||
|
var b: [1024]u8 = undefined;
|
||||||
|
var fwriter = std.Io.File.stdout().writer(io, &b);
|
||||||
|
const w = &fwriter.interface;
|
||||||
|
|
||||||
|
try w.writeAll(helpcontents);
|
||||||
|
try w.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cliArgParse(x: [*:0]const u8, arg: []const u8, description: []const u8) ?[]const u8 {
|
||||||
|
const s = std.mem.span(x);
|
||||||
|
|
||||||
|
if (!gArgsMap.contains(arg)) {
|
||||||
|
gArgsMap.put(arg, description) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const search = mem.trim(u8, s, "-");
|
||||||
|
|
||||||
|
var right: []const u8 = "";
|
||||||
|
const left = blk: {
|
||||||
|
if (mem.find(u8, search, "=")) |sep| {
|
||||||
|
right = search[sep + 1 ..];
|
||||||
|
break :blk search[0..sep];
|
||||||
|
}
|
||||||
|
break :blk search;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (mem.eql(u8, left, arg)) {
|
||||||
|
parsedOne = true;
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Reads in a .tad file and generates an api
|
||||||
|
|
||||||
|
const helpText =
|
||||||
|
\\generate-api
|
||||||
|
\\
|
||||||
|
\\takes an input file and outputs source and output zig files
|
||||||
|
\\
|
||||||
|
;
|
||||||
|
const AP = SimpleArgParse("generate-api", helpText);
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
AP.initArgsMap(init.arena.allocator());
|
||||||
|
|
||||||
|
const argv = init.minimal.args.vector;
|
||||||
|
|
||||||
|
for (argv) |a| {
|
||||||
|
if (AP.cliArgParse(a, "help", "shows the help dialogue")) |arg| {
|
||||||
|
_ = arg;
|
||||||
|
try AP.printHelp(init.io);
|
||||||
|
}
|
||||||
|
if (AP.cliArgParse(a, "input-file", "input .tad file to parse")) |arg| {
|
||||||
|
log.info("parsing input file: {s}", .{arg});
|
||||||
|
try tad.compile(init.io, init.arena.allocator(), arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AP.writeArgsMap(init.io) catch unreachable;
|
||||||
|
if (!AP.parsedOne) {
|
||||||
|
try AP.printHelp(init.io);
|
||||||
|
}
|
||||||
|
log.info("done :)", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
const log = std.log.scoped(.generateApi);
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
const SimpleArgParse = @import("common/args.zig").SimpleArgParse;
|
||||||
|
const tad = @import("tad/tad.zig");
|
||||||
|
|
@ -1,11 +1,42 @@
|
||||||
// this thing eats a rb top level zig file and generates a C header
|
// this thing eats a top level zig file and generates a C header
|
||||||
|
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const args = init.minimal.args;
|
const args = init.minimal.args;
|
||||||
const argsSlice = try args.toSlice(init.arena.allocator());
|
const argsSlice = try args.toSlice(init.arena.allocator());
|
||||||
const io = init.io;
|
|
||||||
_ = io;
|
|
||||||
_ = argsSlice;
|
_ = argsSlice;
|
||||||
|
const io = init.io;
|
||||||
|
core.setupPrint(io);
|
||||||
|
const gpa = init.arena.allocator();
|
||||||
|
const source = try std.Io.Dir.cwd().readFileAllocOptions(io, args[1], gpa, .unlimited, .of(u8), 0);
|
||||||
|
|
||||||
|
var tree = try zig.Ast.parse(gpa, source, .{ .mode = .zig });
|
||||||
|
defer tree.deinit(gpa);
|
||||||
|
|
||||||
|
for (tree.rootDecls()) |decl| {
|
||||||
|
var buf: [12]zig.Ast.Node.Index = undefined;
|
||||||
|
const proto = tree.fullFnProto(&buf, decl) orelse continue;
|
||||||
|
const export_tok = proto.extern_export_inline_token orelse continue; // not an extern, move on
|
||||||
|
if (tree.tokenTag(export_tok) != .keyword_export) continue;
|
||||||
|
|
||||||
|
var first = tree.firstToken(decl);
|
||||||
|
while (first > 0 and tree.tokenTag(first - 1) == .doc_comment) first -= 1;
|
||||||
|
var t = first;
|
||||||
|
while (tree.tokenTag(t) == .doc_comment) : (t += 1) {
|
||||||
|
// skip over all docstrings
|
||||||
|
}
|
||||||
|
core.println("func detected: ", .{tree.tokenSlice(proto.name_token.?)});
|
||||||
|
|
||||||
|
var iter = proto.iterate(&tree);
|
||||||
|
while (iter.next()) |p| {
|
||||||
|
const name = if (p.name_token) |nametok| tree.tokenSlice(nametok) else "_";
|
||||||
|
core.printn(" {s}: {s}", .{ name, tree.getNodeSource(p.type_expr.?) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ret = if (proto.ast.return_type.unwrap()) |r| tree.getNodeSource(r) else "?";
|
||||||
|
core.println(" returns {s}\n", .{ret});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const core = @import("core");
|
||||||
|
const zig = std.zig;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,193 @@
|
||||||
|
const Module = struct {
|
||||||
|
contents: []const u8,
|
||||||
|
backing: std.mem.Allocator,
|
||||||
|
arena: std.heap.ArenaAllocator,
|
||||||
|
enums: std.ArrayList(EnumDefnition) = .empty,
|
||||||
|
|
||||||
|
const EnumDefnition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
values: []EnumValue,
|
||||||
|
docstring: ?[]const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const EnumValue = struct { name: []const u8, value: i64, backing: BuiltinType };
|
||||||
|
|
||||||
|
const BuiltinType = enum {
|
||||||
|
usize,
|
||||||
|
_opaque,
|
||||||
|
_i8,
|
||||||
|
_u8,
|
||||||
|
_i32,
|
||||||
|
_u32,
|
||||||
|
_i64,
|
||||||
|
_u64,
|
||||||
|
};
|
||||||
|
|
||||||
|
const State = enum {
|
||||||
|
root,
|
||||||
|
comment,
|
||||||
|
docstring,
|
||||||
|
module,
|
||||||
|
_enum,
|
||||||
|
_struct,
|
||||||
|
_function,
|
||||||
|
};
|
||||||
|
|
||||||
|
const ParserState = struct {
|
||||||
|
contents: []const u8,
|
||||||
|
state: State = .root,
|
||||||
|
commentStart: ?usize = null,
|
||||||
|
commentEnd: usize = 0,
|
||||||
|
indentCount: usize = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn parse(self: *@This(), allocator: std.mem.Allocator, contents: []const u8) !void {
|
||||||
|
self.* = .{
|
||||||
|
.contents = contents,
|
||||||
|
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||||
|
.backing = allocator,
|
||||||
|
};
|
||||||
|
|
||||||
|
var lineCount: u32 = 0;
|
||||||
|
var tok = mem.tokenizeScalar(u8, contents, '\n');
|
||||||
|
|
||||||
|
var state: ParserState = .{ .contents = contents };
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// block has a comment
|
||||||
|
while (tok.next()) |line| {
|
||||||
|
lineCount += 1;
|
||||||
|
// log.info("\n{d}: {s}", .{ lineCount, line });
|
||||||
|
const commentIndex = mem.findScalar(u8, line, '#'); // split the comment
|
||||||
|
|
||||||
|
state.indentCount = countIndent(line);
|
||||||
|
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 "";
|
||||||
|
|
||||||
|
if (statement.len != 0) {
|
||||||
|
state.commentEnd = tok.index - line.len;
|
||||||
|
try self.parseStatement(&state, statement, comment);
|
||||||
|
|
||||||
|
state.commentStart = null; // consume the comment
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comment.len != 0) {
|
||||||
|
if (statement.len == 0) {
|
||||||
|
if (state.commentStart == null)
|
||||||
|
state.commentStart = tok.index - line.len; // recover the line start
|
||||||
|
state.commentEnd = tok.index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commentIndex == null)
|
||||||
|
state.commentStart = null;
|
||||||
|
|
||||||
|
log.info(" (statement: {s}) (comment: ({s})", .{ statement, comment });
|
||||||
|
// remove when we're solid
|
||||||
|
if (lineCount >= 1200) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn countIndent(s: []const u8) usize {
|
||||||
|
var x = s;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (mem.startsWith(u8, x, " ")) {
|
||||||
|
x = x[4..];
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gpa(self: *@This()) mem.Allocator {
|
||||||
|
return self.arena.allocator();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn beginEnum(self: *@This(), state: *ParserState, statement: []const u8, docstring: ?[]const u8) !void {
|
||||||
|
state.state = ._enum;
|
||||||
|
const new = try self.enums.addOne(self.gpa());
|
||||||
|
new.docstring = docstring;
|
||||||
|
var split = mem.splitAny(u8, statement, " ");
|
||||||
|
var i: usize = 0;
|
||||||
|
while (split.next()) |x| {
|
||||||
|
if (x.len > 0) {
|
||||||
|
if (i == 1) {
|
||||||
|
new.name = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn endEnum(self: @This(), state: *ParserState) !void {
|
||||||
|
state.state = .root;
|
||||||
|
const last = &self.enums.items[self.enums.items.len - 1];
|
||||||
|
log.info("ENUM ADDED enum {s}: docstring: {?s}", .{ last.name, last.docstring });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parseStatement(self: *@This(), state: *ParserState, statement: []const u8, comment: []const u8) !void {
|
||||||
|
const doc = blk: {
|
||||||
|
if (state.commentStart) |cs| {
|
||||||
|
break :blk state.contents[cs..state.commentEnd];
|
||||||
|
}
|
||||||
|
if (comment.len > 0) break :blk comment;
|
||||||
|
break :blk "";
|
||||||
|
};
|
||||||
|
log.info(" docstring: len({d})] \n'{s}'", .{ doc.len, doc });
|
||||||
|
|
||||||
|
// docstring ready, start parsing the statements
|
||||||
|
switch (state.state) {
|
||||||
|
.root => {
|
||||||
|
if (mem.startsWith(u8, statement, "enum")) {
|
||||||
|
try self.beginEnum(state, statement, if (doc.len > 0) doc else null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
._enum => {
|
||||||
|
if (state.indentCount == 0)
|
||||||
|
try self.endEnum(state);
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
log.info("not implemented yet {s}", .{@tagName(state.state)});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *@This()) void {
|
||||||
|
self.arena.deinit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// normalizes a buffer in-place
|
||||||
|
fn normalizeContents(buffer: []u8) []u8 {
|
||||||
|
var cursor: usize = 0;
|
||||||
|
var wcursor: usize = 0;
|
||||||
|
while (cursor < buffer.len) : (cursor += 1) {
|
||||||
|
if (cursor + 2 > buffer.len) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (mem.eql(u8, buffer[cursor .. cursor + 2], "\r\n")) {
|
||||||
|
buffer[wcursor] = '\n';
|
||||||
|
cursor += 2;
|
||||||
|
wcursor += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer[wcursor] = buffer[cursor];
|
||||||
|
wcursor += 1;
|
||||||
|
}
|
||||||
|
return buffer[0..wcursor];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compile(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void {
|
||||||
|
const rawf = try std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .unlimited);
|
||||||
|
const normalized = normalizeContents(rawf);
|
||||||
|
|
||||||
|
var toks: Module = undefined;
|
||||||
|
try toks.parse(allocator, normalized);
|
||||||
|
defer toks.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
const log = std.log.scoped(.tad);
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
Loading…
Reference in New Issue