aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/hello-body.zig4
-rw-r--r--src/api/hello-json.zig4
-rw-r--r--src/api/hello-param.zig6
-rw-r--r--src/api/root.zig2
-rw-r--r--src/db/sqlite.zig82
-rw-r--r--src/db/sqlite_new.zig32
-rw-r--r--src/html/Element.zig5
-rw-r--r--src/http/Response.zig51
-rw-r--r--src/http/serve.zig21
-rw-r--r--src/main.zig8
-rw-r--r--src/web/component/navigation/index.css66
-rw-r--r--src/web/component/navigation/index.html11
-rw-r--r--src/web/component/navigation/index.js11
-rw-r--r--src/web/component/navigation/root.zig8
-rw-r--r--src/web/component/root.zig1
-rw-r--r--src/web/index.css1
-rw-r--r--src/web/index.html2
-rw-r--r--src/web/root.zig1
-rw-r--r--src/z/File.zig1
-rw-r--r--src/z/parser/js/grammar/identifier.zig60
-rw-r--r--src/z/parser/js/grammar/keyword.zig115
-rw-r--r--src/z/parser/js/grammar/literal.zig192
-rw-r--r--src/z/parser/js/grammar/punctuator.zig122
-rw-r--r--src/z/parser/js/grammar/root.zig66
-rw-r--r--src/z/parser/js/grammar/whitespace.zig102
-rw-r--r--src/z/parser/js/root.zig7
-rw-r--r--src/z/parser/lexer.zig127
-rw-r--r--src/z/parser/root.zig17
-rw-r--r--src/z/rest.js20
-rw-r--r--src/z/root.zig6
-rw-r--r--src/z/z.js21
31 files changed, 1086 insertions, 86 deletions
diff --git a/src/api/hello-body.zig b/src/api/hello-body.zig
index 2e5a09e..43afb87 100644
--- a/src/api/hello-body.zig
+++ b/src/api/hello-body.zig
@@ -12,9 +12,9 @@ const Body = struct {
};
fn post(request: Ctx.Request) Response {
- const body = request.json(Body) catch return .bad_request(.void);
+ const body = request.json(Body) catch return .badRequest(.void);
- return .ok_or_error(.json(request.arena, .{
+ return .okOrError(.json(request.arena, .{
.value = body.value,
}));
}
diff --git a/src/api/hello-json.zig b/src/api/hello-json.zig
index 7c96c29..12f1e72 100644
--- a/src/api/hello-json.zig
+++ b/src/api/hello-json.zig
@@ -2,13 +2,13 @@ const http = @import("http");
const Context = http.handler.Context;
const Response = http.Response;
-const Ctx = Context(.{ "hello-json" });
+const Ctx = Context(.{"hello-json"});
pub const ctx: Ctx = .{
.get = get,
};
fn get(request: Ctx.Request) Response {
- return .ok_or_error(.json(request.arena, .{
+ return .okOrError(.json(request.arena, .{
.value = 42,
.message = "Hello, Json!",
}));
diff --git a/src/api/hello-param.zig b/src/api/hello-param.zig
index ba798af..b67212b 100644
--- a/src/api/hello-param.zig
+++ b/src/api/hello-param.zig
@@ -8,7 +8,9 @@ pub const ctx: Ctx = .{
};
fn get(request: Ctx.Request) Response {
- const value = request.params.value.as(usize) catch return .bad_request(.string("parameter needs to be an int"));
+ const value = request.params.value.as(usize) catch return .badRequest(.string("parameter needs to be an int"));
- return .ok_or_error(.json(request.arena, .{ .value = value, }));
+ return .okOrError(.json(request.arena, .{
+ .value = value,
+ }));
}
diff --git a/src/api/root.zig b/src/api/root.zig
index 7570a76..2a54b91 100644
--- a/src/api/root.zig
+++ b/src/api/root.zig
@@ -16,7 +16,7 @@ pub const interfaces: []const Interface = ifs: {
var ifs: []const Interface = &.{};
for (modules) |module| {
- const interface: []const Interface = &.{ module.ctx.interface() };
+ const interface: []const Interface = &.{module.ctx.interface()};
ifs = ifs ++ interface;
}
diff --git a/src/db/sqlite.zig b/src/db/sqlite.zig
index 55cb60a..9a9d5bf 100644
--- a/src/db/sqlite.zig
+++ b/src/db/sqlite.zig
@@ -8,7 +8,7 @@ const Writer = std.Io.Writer;
pub fn Database(comptime T: type) type {
const tables = switch (@typeInfo(T)) {
.@"struct" => |s| s.fields,
- else => @compileError("Database schema must be of type `struct`"),
+ else => @compileError("Database schema must be of type `struct`"),
};
// NOTE: `std.math.log2_int_ceil(@TypeOf(tables.len), tables.len)` panics
@@ -16,14 +16,14 @@ pub fn Database(comptime T: type) type {
// a (useless) schema that does not define any tables.
const TagIntTables = @Int(.unsigned, @bitSizeOf(@TypeOf(tables.len)) - @clz(tables.len));
- var table_names : [tables.len][]const u8 = undefined;
- var table_types : [tables.len]type = undefined;
+ var table_names: [tables.len][]const u8 = undefined;
+ var table_types: [tables.len]type = undefined;
var table_values: [tables.len]TagIntTables = undefined;
inline for (&table_names, &table_types, &table_values, tables, 0..) |*table_name, *table_type, *table_value, table, table_index| {
const columns = switch (@typeInfo(table.type)) {
.@"struct" => |s| s.fields,
- else => @compileError("Table `" ++ table.name ++ "` must be of type `struct`"),
+ else => @compileError("Table `" ++ table.name ++ "` must be of type `struct`"),
};
// NOTE: A table must have at least one column; see
@@ -32,8 +32,8 @@ pub fn Database(comptime T: type) type {
const TagIntColumns = @Int(.unsigned, std.math.log2_int_ceil(@TypeOf(columns.len), columns.len));
- var column_names : [columns.len][]const u8 = undefined;
- var column_types : [columns.len]type = undefined;
+ var column_names: [columns.len][]const u8 = undefined;
+ var column_types: [columns.len]type = undefined;
var column_values: [columns.len]TagIntColumns = undefined;
inline for (&column_names, &column_types, &column_values, columns, 0..) |*column_name, *column_type, *column_value, column, column_index| {
@@ -49,15 +49,15 @@ pub fn Database(comptime T: type) type {
}
const TagColumns = @Enum(TagIntColumns, .exhaustive, &column_names, &column_values);
- const Columns = @Union(.auto, TagColumns, &column_names, &column_types, &@splat(.{}));
+ const Columns = @Union(.auto, TagColumns, &column_names, &column_types, &@splat(.{}));
- table_name.* = table.name;
- table_type.* = Columns;
+ table_name.* = table.name;
+ table_type.* = Columns;
table_value.* = table_index;
}
const TagTables = @Enum(TagIntTables, .exhaustive, &table_names, &table_values);
- const Tables = @Union(.auto, TagTables, &table_names, &table_types, &@splat(.{}));
+ const Tables = @Union(.auto, TagTables, &table_names, &table_types, &@splat(.{}));
return struct {
handle: *sqlite.sqlite3,
@@ -87,14 +87,14 @@ pub fn Database(comptime T: type) type {
};
const Schema = Tables;
- const Self = @This();
+ const Self = @This();
// Source: https://sqlite.org/c3ref/open.html
pub fn init(kind: Kind) Error!Self {
const name = switch (kind) {
.temp_in_memory => ":memory:",
- .temp_on_disk => "",
- .path => |path| path,
+ .temp_on_disk => "",
+ .path => |path| path,
};
var handle: ?*sqlite.sqlite3 = null;
@@ -491,7 +491,7 @@ fn Select(comptime Schema: type, comptime from: std.meta.Tag(Schema), comptime c
try writer.writeAll(separator);
try writer.writeAll(@tagName(column));
-}
+ }
switch (self.filter) {
.none => {},
@@ -829,7 +829,7 @@ test {
};
};
- var db: Database(Schema) = try .init(.temp_in_memory);
+ var db: Database(Schema) = try .init(.temp_in_memory);
defer assert(db.deinit() == .ok);
const query = try db.select(.contacts, &.{ .first_name , .last_name, .age })
@@ -847,30 +847,30 @@ test {
}
// test {
- // NOTE: we don't use `union(enum)` here as if a column has a default value,
- // an integer tag type is needed explicitely, which, from a usability
- // standpoint, is not great; we can calulate this value in `Schema.from`.
- //
- // CREATE TABLE contacts (
- // contact_id INTEGER PRIMARY KEY,
- // first_name TEXT NOT NULL DEFAULT "Hans",
- // last_name TEXT NOT NULL DEFAULT "Mustermann",
- // nickname TEXT,
- // email TEXT NOT NULL UNIQUE,
- // phone TEXT NOT NULL UNIQUE
- // );
- // const S = struct {
- // contacts: struct {
- // contact_id: PrimaryKey(Integer, .{}),
- // first_name: VarChar(255, .{}) = "Hans",
- // last_name: VarChar(255, .{}) = "Mustermann",
- // nickname: ?VarChar(255, .{}),
- // email: VarChar(255, .{.unique}),
- // phone: VarChar(255, .{.unique}),
- // },
- // };
- //
- // // NOTE: init connects to DB and creates tables if not already present
- // var db: Database(Schema(S)) = try .init(.temp_in_memory);
- // db.deinit();
+// NOTE: we don't use `union(enum)` here as if a column has a default value,
+// an integer tag type is needed explicitely, which, from a usability
+// standpoint, is not great; we can calulate this value in `Schema.from`.
+//
+// CREATE TABLE contacts (
+// contact_id INTEGER PRIMARY KEY,
+// first_name TEXT NOT NULL DEFAULT "Hans",
+// last_name TEXT NOT NULL DEFAULT "Mustermann",
+// nickname TEXT,
+// email TEXT NOT NULL UNIQUE,
+// phone TEXT NOT NULL UNIQUE
+// );
+// const S = struct {
+// contacts: struct {
+// contact_id: PrimaryKey(Integer, .{}),
+// first_name: VarChar(255, .{}) = "Hans",
+// last_name: VarChar(255, .{}) = "Mustermann",
+// nickname: ?VarChar(255, .{}),
+// email: VarChar(255, .{.unique}),
+// phone: VarChar(255, .{.unique}),
+// },
+// };
+//
+// // NOTE: init connects to DB and creates tables if not already present
+// var db: Database(Schema(S)) = try .init(.temp_in_memory);
+// db.deinit();
// }
diff --git a/src/db/sqlite_new.zig b/src/db/sqlite_new.zig
index 46706b8..36eaf61 100644
--- a/src/db/sqlite_new.zig
+++ b/src/db/sqlite_new.zig
@@ -25,7 +25,7 @@ pub fn Database(comptime T: type) type {
// TODO: make functions out of `table` and `columns`: Table(Schema), []const Column(Schema, table) for readability?
pub fn select(self: *Self, comptime table: std.meta.Tag(Schema), comptime columns: []const std.meta.Tag(@FieldType(Schema, @tagName(table)))) Select(Schema, table, columns) {
_ = self;
- std.debug.print("{any}\n", .{ columns });
+ std.debug.print("{any}\n", .{columns});
return .{
// .handle = self.handle,
};
@@ -152,17 +152,17 @@ fn Unique(comptime T: type) type {
const Type = T.Type;
// TODO: `contains(expected, str)` function
const string = if (std.mem.count(u8, T.string, "UNIQUE") > 0)
- @compileError(std.fmt.comptimePrint("Type `{s}` is already unique", .{ @typeName(T) }))
+ @compileError(std.fmt.comptimePrint("Type `{s}` is already unique", .{@typeName(T)}))
else
- std.fmt.comptimePrint("UNIQUE {s}", .{ T.string });
+ std.fmt.comptimePrint("UNIQUE {s}", .{T.string});
};
}
fn Default(comptime T: type, comptime value: T.Type) type {
return struct {
const Type = T.type;
- const string = if(std.mem.count(u8, T.string, "DEFAULT") > 0)
- @compileError(std.fmt.comptimePrint("Type `{s}` already has a default value", .{ @typeName(T) }))
+ const string = if (std.mem.count(u8, T.string, "DEFAULT") > 0)
+ @compileError(std.fmt.comptimePrint("Type `{s}` already has a default value", .{@typeName(T)}))
else
std.fmt.comptimePrint("{s} DEFAULT {}", .{ T.string, value });
};
@@ -202,12 +202,12 @@ fn Test(comptime Schema: type, comptime table: std.meta.Tag(Schema), comptime co
fn Eql(comptime column: std.meta.Tag(ColumnUnion), value: @FieldType(ColumnUnion, @tagName(column))) type {
return struct {
- const fmt: []const u8 = std.fmt.comptimePrint("{s} = ", .{ @tagName(column) });
- const args: @Tuple(&.{ @TypeOf(value) }) = .{ value };
+ const fmt: []const u8 = std.fmt.comptimePrint("{s} = ", .{@tagName(column)});
+ const args: @Tuple(&.{@TypeOf(value)}) = .{value};
};
}
// fn And() type {
- // todo: concat fmt slices and args tuples
+ // todo: concat fmt slices and args tuples
// }
string: Node,
@@ -236,10 +236,10 @@ test "schema" {
const Schema = struct {
contacts: Contacts,
// contacts: struct {
- // contact_id: Integer = .constrain(.primary_key),
- // age: Integer = .unconstrained,
- // contact_id: Integer(usize) = .constrain(.{ .primary_key = true }),
- // first_name: Default(VarChar(255), "Hans"),
+ // contact_id: Integer = .constrain(.primary_key),
+ // age: Integer = .unconstrained,
+ // contact_id: Integer(usize) = .constrain(.{ .primary_key = true }),
+ // first_name: Default(VarChar(255), "Hans"),
// },
};
@@ -254,10 +254,10 @@ test "schema" {
const t: Test(@TypeOf(db).Schema, .contacts, &.{ .id, .first_name }) = .eql(.id, 42);
// const t: Test(@TypeOf(db).Schema, .contacts, &.{ .id, .first_name }) = .op_and(.eql(.id, 42), .eql(.first_name, "name"));
- std.debug.print("test output: {s}{}\n", switch (t.string) { .eql => |eq| .{ eq.fmt, eq.args } });
+ std.debug.print("test output: {s}{}\n", switch (t.string) {
+ .eql => |eq| .{ eq.fmt, eq.args },
+ });
const Int = Default(Unique(Integer(usize)), 42);
- std.debug.print("default unique integer with default 42: {s}\n", .{ Int.string });
-
-
+ std.debug.print("default unique integer with default 42: {s}\n", .{Int.string});
}
diff --git a/src/html/Element.zig b/src/html/Element.zig
index 8ecf31e..a9e5897 100644
--- a/src/html/Element.zig
+++ b/src/html/Element.zig
@@ -39,7 +39,7 @@ fn attributesFrom(attributes: anytype) []const Attribute {
const name = field.name;
const value = @field(attributes, name);
- const next_attr: []const Attribute = &.{ .{ .name = name, .value = value } };
+ const next_attr: []const Attribute = &.{.{ .name = name, .value = value }};
attrs = attrs ++ next_attr;
}
@@ -79,7 +79,6 @@ pub fn toHtml(comptime self: Self) []const u8 {
break :string string;
};
-
switch (self.content) {
.void => {},
.string => |s| {
@@ -91,7 +90,7 @@ pub fn toHtml(comptime self: Self) []const u8 {
}
string = string ++ "</" ++ self.tag ++ ">";
- }
+ },
}
string = string ++ if (self.tag.len == 0) "" else "</" ++ self.tag ++ ">";
diff --git a/src/http/Response.zig b/src/http/Response.zig
index d54792b..69bec65 100644
--- a/src/http/Response.zig
+++ b/src/http/Response.zig
@@ -27,34 +27,74 @@ pub const Message = union(enum) {
status: std.http.Status,
message: Message,
+headers: []const std.http.Header = &.{},
pub fn with(status: std.http.Status, message: Message) @This() {
return .{ .status = status, .message = message };
}
+pub fn withHeaders(
+ status: std.http.Status,
+ message: Message,
+ headers: []const std.http.Header,
+) @This() {
+ return .{
+ .status = status,
+ .message = message,
+ .headers = headers,
+ };
+}
+
pub fn ok(message: Message) @This() {
return with(.ok, message);
}
-pub fn ok_or_error(message: anyerror!Message) @This() {
- return ok(message catch return with(.internal_server_error, .void));
+pub fn okOr(message: anyerror!Message, err: @This()) @This() {
+ return ok(message catch return err);
}
-pub fn bad_request(message: Message) @This() {
+pub fn okOrError(message: anyerror!Message) @This() {
+ return okOr(message, .with(.internal_server_error, .void));
+}
+
+pub fn badRequest(message: Message) @This() {
return with(.bad_request, message);
}
+pub const SendOptions = struct {
+ headers: []const std.http.Header,
+};
+
pub fn send(
self: *const @This(),
io: std.Io,
+ arena: std.mem.Allocator,
request: *std.http.Server.Request,
+ options: SendOptions,
) !void {
+ const headers = hdrs: {
+ if (options.headers.len == 0) {
+ break :hdrs self.headers;
+ }
+
+ const h = try arena.alloc(std.http.Header, options.headers.len + self.headers.len);
+ @memcpy(h[0..options.headers.len], options.headers);
+ @memcpy(h[options.headers.len..], self.headers);
+ break :hdrs h;
+ };
+
switch (self.message) {
.void => {
- try request.respond("", .{ .status = self.status });
+ try request.respond("", .{
+ .status = self.status,
+ .extra_headers = headers,
+ });
},
.static => |content| {
- try request.respond(content, .{ .status = self.status });
+ try request.respond(content, .{
+ .status = self.status,
+ .extra_headers = headers,
+ });
},
.streaming => |file| {
var write_buffer: [1024]u8 = undefined;
@@ -63,6 +103,7 @@ pub fn send(
var body_writer = try request.respondStreaming(&write_buffer, .{
.respond_options = .{
.status = self.status,
+ .extra_headers = headers,
.transfer_encoding = .chunked,
},
});
diff --git a/src/http/serve.zig b/src/http/serve.zig
index 587f4a3..036eecd 100644
--- a/src/http/serve.zig
+++ b/src/http/serve.zig
@@ -1,20 +1,30 @@
const std = @import("std");
+const Response = @import("Response.zig");
const RouteSet = @import("RouteSet.zig");
+pub const Options = struct {
+ address: std.Io.net.IpAddress,
+ headers: []const std.http.Header = &.{},
+};
+
pub fn serve(
gpa: std.mem.Allocator,
comptime routes: RouteSet,
- address: std.Io.net.IpAddress,
+ options: Options,
) !void {
var threaded: std.Io.Threaded = .init(gpa, .{});
var group: std.Io.Group = .init;
const io = threaded.io();
- var server = try address.listen(io, .{ .reuse_address = true });
+ var server = try options.address.listen(io, .{ .reuse_address = true });
+
+ const send_options: Response.SendOptions = .{
+ .headers = options.headers,
+ };
while (true) {
const stream = try server.accept(io);
- group.async(io, handle, .{ io, gpa, stream, &routes });
+ group.async(io, handle, .{ io, gpa, stream, &routes, send_options });
}
}
@@ -23,6 +33,7 @@ pub fn handle(
gpa: std.mem.Allocator,
stream: std.Io.net.Stream,
routes: *const RouteSet,
+ send_options: Response.SendOptions,
) void {
var read_buffer: [1024 * 8]u8 = undefined;
var write_buffer: [1024 * 8]u8 = undefined;
@@ -44,8 +55,8 @@ pub fn handle(
std.debug.print("{s} {s}\n", .{ @tagName(request.head.method), request.head.target });
if (routes.getInterface(request.head.target)) |interface| {
- const message = interface.handle(gpa, arena.allocator(), io, &request);
- message.send(io, &request) catch return;
+ const response = interface.handle(gpa, arena.allocator(), io, &request);
+ response.send(io, arena.allocator(), &request, send_options) catch return;
} else {
request.respond("", .{ .status = .not_found }) catch return;
}
diff --git a/src/main.zig b/src/main.zig
index bf1b09f..176d3b2 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -12,9 +12,7 @@ const web = @import("web/root.zig");
const routes: http.RouteSet = .init(api.interfaces ++ web.interfaces);
pub fn main(init: std.process.Init) !void {
- try http.serve(
- init.gpa,
- routes,
- try .parseLiteral("0.0.0.0:8080"),
- );
+ try http.serve(init.gpa, routes, .{
+ .address = try .parseLiteral("0.0.0.0:8080"),
+ });
}
diff --git a/src/web/component/navigation/index.css b/src/web/component/navigation/index.css
new file mode 100644
index 0000000..836e3b5
--- /dev/null
+++ b/src/web/component/navigation/index.css
@@ -0,0 +1,66 @@
+#toggle {
+ --radius-open: 200px;
+
+ position: absolute;
+ right: 10px;
+ bottom: 10px;
+ width: 45px;
+ height: 45px;
+ border-radius: 100%;
+ background: red;
+ z-index: 500;
+ cursor: pointer;
+ text-align: center;
+ align-content: center;
+ transition:
+ border-radius 0.1s ease,
+ right 0.1s ease,
+ bottom 0.1s ease,
+ width 0.1s ease,
+ height 0.1s ease;
+
+ .bi {
+ baseline-shift: -2px;
+ }
+
+ .items {
+ position: relative;
+ top: 0px;
+ left: 0px;
+ right: 0px;
+ bottom: 0px;
+ }
+
+ .item {
+ display: none;
+ width: 45px;
+ height: 45px;
+ border-radius: 100%;
+ background: blue;
+ position: absolute;
+ right: calc(cos(18deg * var(--index)) * var(--radius-open));
+ bottom: calc(sin(18deg * var(--index)) * var(--radius-open));
+ font-size: calc(var(--index) * 1px);
+ }
+
+ .item {
+ --index: sibling-index();
+ }
+}
+
+#toggle.open {
+ width: var(--radius-open);
+ height: var(--radius-open);
+ bottom: 0px;
+ right: 0px;
+ border-radius: 0px;
+ border-top-left-radius: 250px;
+
+ .bi {
+ display: none;
+ }
+
+ .item {
+ display: block;
+ }
+}
diff --git a/src/web/component/navigation/index.html b/src/web/component/navigation/index.html
new file mode 100644
index 0000000..b3b5ce0
--- /dev/null
+++ b/src/web/component/navigation/index.html
@@ -0,0 +1,11 @@
+<div id="toggle">
+ <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-three-dots-vertical" viewBox="0 0 16 16">
+ <path d="M9.5 13a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0"/>
+ </svg>
+ <div class="items">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+</div>
diff --git a/src/web/component/navigation/index.js b/src/web/component/navigation/index.js
new file mode 100644
index 0000000..2c996a0
--- /dev/null
+++ b/src/web/component/navigation/index.js
@@ -0,0 +1,11 @@
+
+const toggle = Z.by_id('toggle');
+
+toggle.onclick = (event) => {
+ toggle.classList.toggle('open');
+ event.stopPropagation();
+};
+
+document.addEventListener('click', () => {
+ toggle.classList.remove('open');
+});
diff --git a/src/web/component/navigation/root.zig b/src/web/component/navigation/root.zig
new file mode 100644
index 0000000..919cddb
--- /dev/null
+++ b/src/web/component/navigation/root.zig
@@ -0,0 +1,8 @@
+const z = @import("z");
+
+pub const component: z.Component = .{
+ .name = .navigation,
+ .body = @embedFile("index.html"),
+ .style = @embedFile("index.css"),
+ .script = @embedFile("index.js"),
+};
diff --git a/src/web/component/root.zig b/src/web/component/root.zig
index 226f3f7..ba2f565 100644
--- a/src/web/component/root.zig
+++ b/src/web/component/root.zig
@@ -3,4 +3,5 @@ const z = @import("z");
pub const components: []const z.Component = &.{
@import("image/root.zig").component,
@import("timeline/root.zig").component,
+ @import("navigation/root.zig").component,
};
diff --git a/src/web/index.css b/src/web/index.css
index 6b1927f..bcdecae 100644
--- a/src/web/index.css
+++ b/src/web/index.css
@@ -1,6 +1,7 @@
html {
height: 100vh;
width: 100vw;
+ overflow: hidden;
}
body {
diff --git a/src/web/index.html b/src/web/index.html
index d2bc6fe..bcbb165 100644
--- a/src/web/index.html
+++ b/src/web/index.html
@@ -1,2 +1,4 @@
<z-component type="timeline" id="timeline">
</z-component>
+<z-component type="navigation" id="navigation">
+</z-component>
diff --git a/src/web/root.zig b/src/web/root.zig
index c9c1b08..d027dc8 100644
--- a/src/web/root.zig
+++ b/src/web/root.zig
@@ -24,4 +24,3 @@ const document: html.Element = .native(.html, .{}, .children(&.{
pub const interfaces: []const http.handler.Interface = &.{
Static(.index, document.toDocument()).interface(),
};
-
diff --git a/src/z/File.zig b/src/z/File.zig
index cd7094f..cd19dba 100644
--- a/src/z/File.zig
+++ b/src/z/File.zig
@@ -15,4 +15,3 @@ pub fn load(comptime self: @This(), comptime content: []const u8) Element {
return .transparent(.content(comptime self.minifier.minify(content)));
}
}
-
diff --git a/src/z/parser/js/grammar/identifier.zig b/src/z/parser/js/grammar/identifier.zig
new file mode 100644
index 0000000..ccc5268
--- /dev/null
+++ b/src/z/parser/js/grammar/identifier.zig
@@ -0,0 +1,60 @@
+const std = @import("std");
+
+const root = @import("root.zig");
+const Lexer = root.Lexer;
+const Token = root.Token;
+
+pub const Identifier = enum {
+ public,
+ private,
+
+ pub inline fn tokenize(comptime lexer: *Lexer) Lexer.Error!void {
+ comptime {
+ defer lexer.revert();
+
+ lexer.context.regex_possible = false;
+
+ const token = tok: {
+ if (lexer.peekChar() == '#') {
+ const t = lexer.start(.{ .identifier = .private });
+ lexer.skip() catch unreachable;
+ break :tok t;
+ } else {
+ break :tok lexer.start(.{ .identifier = .public });
+ }
+ };
+
+ switch (try lexer.consume()) {
+ 'a'...'z', 'A'...'Z', '_' => {},
+ else => return Lexer.Error.UnexpectedToken,
+ }
+
+ while (lexer.peekChar()) |ch| {
+ switch (ch) {
+ 'a'...'z', 'A'...'Z', '0'...'9', '_' => {},
+ else => break,
+ }
+
+ try lexer.skip();
+ }
+
+ lexer.commit(token);
+ }
+ }
+};
+
+test "simple" {
+ {
+ comptime var lexer: Lexer = .init("some_literal9");
+ try Identifier.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(13, lexer.tokens[0].slice.len);
+ }
+
+ {
+ comptime var lexer: Lexer = .init("#some_literal9");
+ try Identifier.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(14, lexer.tokens[0].slice.len);
+ }
+}
diff --git a/src/z/parser/js/grammar/keyword.zig b/src/z/parser/js/grammar/keyword.zig
new file mode 100644
index 0000000..8e99b4a
--- /dev/null
+++ b/src/z/parser/js/grammar/keyword.zig
@@ -0,0 +1,115 @@
+const std = @import("std");
+
+const root = @import("root.zig");
+const Lexer = root.Lexer;
+const Token = root.Token;
+
+pub const Keyword = enum {
+ @"break",
+ case,
+ @"catch",
+ class,
+ @"const",
+ @"continue",
+ debugger,
+ default,
+ delete,
+ do,
+ @"else",
+ @"export",
+ extends,
+ finally,
+ @"for",
+ function,
+ @"if",
+ import,
+ in,
+ instanceof,
+ new,
+ @"return",
+ super,
+ @"switch",
+ this,
+ throw,
+ @"try",
+ typeof,
+ @"var",
+ void,
+ @"while",
+ with,
+ yield,
+ let,
+ static,
+ await,
+ async,
+ of,
+ from,
+ get,
+ set,
+ meta,
+ @"enum",
+ null,
+ true,
+ false,
+
+ const Self = @This();
+
+ const keywordmap: std.StaticStringMap(Self) = .initComptime(kws: {
+ var keywords: []const struct { []const u8, Self } = &.{};
+
+ for (@typeInfo(Self).@"enum".fields) |field| {
+ const name: []const u8 = field.name;
+ const value: Self = @field(Self, field.name);
+
+ keywords = keywords ++ .{ .{ name, value } };
+ }
+
+ break :kws keywords;
+ });
+
+ pub inline fn tokenize(lexer: *Lexer) Lexer.Error!void {
+ comptime {
+ @setEvalBranchQuota(10000);
+ errdefer lexer.revert();
+
+ lexer.context.regex_possible = true;
+
+ const index = std.mem.findNone(u8, lexer.buffer, std.ascii.lowercase) orelse lexer.buffer.len;
+
+ if (index == 0) return Lexer.Error.UnexpectedToken;
+
+ if (keywordmap.get(lexer.buffer[0..index])) |tag| {
+ const token = lexer.start(.{ .keyword = tag });
+ try lexer.skipTo(index);
+ lexer.commit(token);
+ } else {
+ return Lexer.Error.UnexpectedToken;
+ }
+ }
+ }
+};
+
+test "simple" {
+ inline for (comptime Keyword.keywordmap.keys()) |key| {
+ comptime var lexer: Lexer = .init(key);
+ try Keyword.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(key.len, lexer.tokens[0].slice.len);
+ }
+}
+
+test "ending" {
+ inline for (comptime Keyword.keywordmap.keys()) |key| {
+ comptime var lexer: Lexer = .init(key ++ ".");
+ try Keyword.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(key.len, lexer.tokens[0].slice.len);
+ }
+}
+
+test "subword" {
+ inline for (comptime Keyword.keywordmap.keys()) |key| {
+ comptime var lexer: Lexer = .init(key ++ "a");
+ try std.testing.expectError(Lexer.Error.UnexpectedToken, Keyword.tokenize(&lexer));
+ }
+}
diff --git a/src/z/parser/js/grammar/literal.zig b/src/z/parser/js/grammar/literal.zig
new file mode 100644
index 0000000..4316dee
--- /dev/null
+++ b/src/z/parser/js/grammar/literal.zig
@@ -0,0 +1,192 @@
+const std = @import("std");
+
+const root = @import("root.zig");
+const Lexer = root.Lexer;
+const Token = root.Token;
+
+pub const Literal = union(enum) {
+ // NOTE: currently only decimal literals are tokenized
+ numeric: void,
+ bigint: void,
+ string: void,
+ template: enum {
+ whole,
+ start,
+ middle,
+ end,
+ },
+ regex: void,
+
+ const digits = "_0123456789abcdef";
+
+ pub inline fn tokenize(comptime lexer: *Lexer) Lexer.Error!void {
+ comptime {
+ errdefer lexer.revert();
+
+ var token = lexer.start(.{ .literal = undefined });
+
+ const regex_possible = lexer.context.regex_possible;
+ lexer.context.regex_possible = false;
+
+ switch (try lexer.consume()) {
+ '0'...'9', '.' => |c| {
+ const base = if (c != '0') 10 else switch (lexer.peekChar() orelse 0) {
+ 'x' => 16,
+ 'o' => 8,
+ 'b' => 2,
+ else => 10,
+ };
+
+ if (base != 10) lexer.skip() catch unreachable;
+
+ {
+ const index = std.mem.findNone(
+ u8,
+ lexer.buffer,
+ digits[0 .. base + 1],
+ ) orelse lexer.buffer.len;
+ try lexer.skipTo(index);
+
+
+ if (index == 0 and base != 10) return Lexer.Error.UnexpectedToken;
+ }
+
+ if (base == 10 and lexer.peekChar() == '.') {
+ try lexer.skip();
+ const index = std.mem.findNone(
+ u8,
+ lexer.buffer,
+ digits[0 .. base + 1],
+ ) orelse lexer.buffer.len;
+ try lexer.skipTo(index);
+ }
+
+ if (lexer.peekChar() == 'n') {
+ lexer.skip() catch unreachable;
+ token.kind.literal = .bigint;
+ } else {
+ token.kind.literal = .numeric;
+ }
+ },
+ '\'', '"' => |quote| {
+ token.kind.literal = .string;
+ while (true) {
+ switch (try lexer.consume()) {
+ quote => break,
+ '\\' => try lexer.skip(),
+ else => {},
+ }
+ }
+ },
+ '`' => {
+ token.kind.literal = .{ .template = undefined };
+
+ while (true) {
+ switch (try lexer.consume()) {
+ '`' => {
+ token.kind.literal.template = .whole;
+ break;
+ },
+ '\\' => try lexer.skip(),
+ '$' => if (lexer.peekChar() == '{') {
+ lexer.skip() catch unreachable;
+ lexer.context.template_nesting += 1;
+ token.kind.literal.template = .start;
+ lexer.context.regex_possible = true;
+ break;
+ },
+ else => {},
+ }
+ }
+ },
+ '}' => if (lexer.context.template_nesting > 0) {
+ lexer.context.template_nesting -= 1;
+ token.kind.literal = .{ .template = undefined };
+
+ while (true) {
+ switch (try lexer.consume()) {
+ '`' => {
+ token.kind.literal.template = .end;
+ break;
+ },
+ '\\' => try lexer.skip(),
+ '$' => if (lexer.peekChar() == '{') {
+ lexer.skip() catch unreachable;
+ lexer.context.template_nesting += 1;
+ token.kind.literal.template = .middle;
+ lexer.context.regex_possible = true;
+ break;
+ },
+ else => {},
+ }
+ }
+ } else return Lexer.Error.UnexpectedToken,
+ '/' => if (regex_possible) {
+ token.kind.literal = .regex;
+ while (true) {
+ switch (try lexer.consume()) {
+ '/' => break,
+ '\\' => try lexer.skip(),
+ '\n' => return Lexer.Error.UnexpectedToken,
+ else => {},
+ }
+ }
+ } else return Lexer.Error.UnexpectedToken,
+ else => return Lexer.Error.UnexpectedToken,
+ }
+
+ lexer.commit(token);
+ }
+ }
+};
+
+test "numeric" {
+ const Simple = struct {
+ pub fn run(comptime buffer: []const u8, n: usize) !void {
+ comptime var lexer: Lexer = .init(buffer);
+ try Literal.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(n, lexer.tokens[0].slice.len);
+ }
+ };
+
+ try Simple.run("1234", 4);
+ try Simple.run("1.234", 5);
+ try Simple.run("1_2_3_4", 7);
+ try Simple.run("0b101011", 8);
+ try Simple.run("0xabcf01", 8);
+ try Simple.run("0o731734", 8);
+ try Simple.run("0o731734n", 9);
+}
+
+test "string" {
+ const Simple = struct {
+ pub fn run(comptime buffer: []const u8, n: usize) !void {
+ comptime var lexer: Lexer = .init(buffer);
+ try Literal.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(n, lexer.tokens[0].slice.len);
+ }
+ };
+
+ try Simple.run("'hello'", 7);
+ try Simple.run("\"hello\"", 7);
+ try std.testing.expectError(
+ Lexer.Error.EndOfBuffer,
+ Simple.run("'hello", 0),
+ );
+}
+
+test "regex" {
+ const Simple = struct {
+ pub fn run(comptime buffer: []const u8, n: usize) !void {
+ comptime var lexer: Lexer = .init(buffer);
+ try Literal.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(n, lexer.tokens[0].slice.len);
+ }
+ };
+
+ try Simple.run("/hello/", 7);
+ try Simple.run("/some string\\/ here/", 20);
+}
diff --git a/src/z/parser/js/grammar/punctuator.zig b/src/z/parser/js/grammar/punctuator.zig
new file mode 100644
index 0000000..b085ce1
--- /dev/null
+++ b/src/z/parser/js/grammar/punctuator.zig
@@ -0,0 +1,122 @@
+const std = @import("std");
+
+const root = @import("root.zig");
+const Lexer = root.Lexer;
+const Token = root.Token;
+
+pub const Punctuator = enum {
+ @"+",
+ @"-",
+ @"*",
+ @"/",
+ @"%",
+ @"**",
+ @"++",
+ @"--",
+ @"=",
+ @"+=",
+ @"-=",
+ @"*=",
+ @"/=",
+ @"%=",
+ @"**=",
+ @"&=",
+ @"|=",
+ @"^=",
+ @"<<=",
+ @">>=",
+ @">>>=",
+ @"&&=",
+ @"||=",
+ @"??=",
+ @"==",
+ @"===",
+ @"!=",
+ @"!==",
+ @"<",
+ @"<=",
+ @">",
+ @">=",
+ @"&&",
+ @"||",
+ @"??",
+ @"&",
+ @"|",
+ @"^",
+ @"~",
+ @"<<",
+ @">>",
+ @">>>",
+ @"!",
+ @"?",
+ @":",
+ @";",
+ @",",
+ @".",
+ @"...",
+ @"=>",
+ @"(",
+ @")",
+ @"[",
+ @"]",
+ @"{",
+ @"}",
+ @"?.",
+ @"#",
+ @"@",
+
+ const Self = @This();
+
+ const punctuatormap: std.StaticStringMap(Self) = .initComptime(kws: {
+ var punctuators: []const struct { []const u8, Self } = &.{};
+
+ for (@typeInfo(Self).@"enum".fields) |field| {
+ const name: []const u8 = field.name;
+ const value: Self = @field(Self, field.name);
+
+ punctuators = punctuators ++ .{.{ name, value }};
+ }
+
+ break :kws punctuators;
+ });
+
+ pub inline fn tokenize(comptime lexer: *Lexer) Lexer.Error!void {
+ comptime {
+ @setEvalBranchQuota(10_000);
+ defer lexer.revert();
+
+ lexer.context.regex_possible = true;
+
+ if (punctuatormap.getLongestPrefix(lexer.buffer)) |pair| {
+
+ if (std.mem.eql(u8, pair.key, "}") and lexer.context.template_nesting > 0) {
+ return Lexer.Error.UnexpectedToken;
+ }
+
+ const token = lexer.start(.{ .punctuator = pair.value });
+ try lexer.skipTo(pair.key.len);
+ lexer.commit(token);
+ } else {
+ return Lexer.Error.UnexpectedToken;
+ }
+ }
+ }
+};
+
+test "simple" {
+ inline for (comptime Punctuator.punctuatormap.keys()) |key| {
+ comptime var lexer: Lexer = .init(key);
+ try Punctuator.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(key.len, lexer.tokens[0].slice.len);
+ }
+}
+
+test "ending" {
+ inline for (comptime Punctuator.punctuatormap.keys()) |key| {
+ comptime var lexer: Lexer = .init(key ++ "a");
+ try Punctuator.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(key.len, lexer.tokens[0].slice.len);
+ }
+}
diff --git a/src/z/parser/js/grammar/root.zig b/src/z/parser/js/grammar/root.zig
new file mode 100644
index 0000000..f732b7f
--- /dev/null
+++ b/src/z/parser/js/grammar/root.zig
@@ -0,0 +1,66 @@
+//! This is a Javascript parser and tokenizer.
+//! It is and probably will never be complete,
+//! since js is a very strange language and I want
+//! to move forward and code on more interesting things
+//! I started something called mujs (micro-js). It's a
+//! subset of the javascript language. It does not support
+//! the regex literal and currently also not template strings.
+//! It is chosen like that to make parsing easier and also less
+//! context-dependent.
+
+const std = @import("std");
+
+const parser = @import("../../root.zig");
+
+pub const Lexer = parser.Lexer(Grammar);
+pub const Token = parser.Token(Grammar);
+
+pub const Whitespace = @import("whitespace.zig").Whitespace;
+pub const Keyword = @import("keyword.zig").Keyword;
+pub const Literal = @import("literal.zig").Literal;
+pub const Punctuator = @import("punctuator.zig").Punctuator;
+pub const Identifier = @import("identifier.zig").Identifier;
+
+pub const Grammar = union(enum) {
+ whitespace: Whitespace,
+ keyword: Keyword,
+ literal: Literal,
+ punctuator: Punctuator,
+ identifier: Identifier,
+ end_of_file: void,
+
+ pub const Context = struct {
+ template_nesting: usize = 0,
+ regex_possible: bool = true,
+ };
+
+ pub inline fn tokenize(comptime lexer: *Lexer) Lexer.Error!void {
+ @setEvalBranchQuota(lexer.buffer.len * 1000);
+ comptime {
+ while (lexer.peekChar()) |_| {
+ Whitespace.tokenize(lexer) catch {
+ Keyword.tokenize(lexer) catch {
+ Literal.tokenize(lexer) catch {
+ Punctuator.tokenize(lexer) catch {
+ try Identifier.tokenize(lexer);
+ };
+ };
+ };
+ };
+ }
+
+ lexer.commit(lexer.start(.end_of_file));
+ }
+ }
+};
+
+test {
+ _ = std.testing.refAllDecls(@This());
+}
+
+test "z.js" {
+ comptime var lexer: Lexer = .init(@embedFile("../../../z.js"));
+ Grammar.tokenize(&lexer) catch |err| {
+ std.debug.print("error: {} at {s}", .{ err, lexer.buffer });
+ };
+}
diff --git a/src/z/parser/js/grammar/whitespace.zig b/src/z/parser/js/grammar/whitespace.zig
new file mode 100644
index 0000000..08d031f
--- /dev/null
+++ b/src/z/parser/js/grammar/whitespace.zig
@@ -0,0 +1,102 @@
+const std = @import("std");
+const Lexer = @import("root.zig").Lexer;
+const Token = @import("root.zig").Token;
+
+pub const Whitespace = enum {
+ space,
+ line_comment,
+ block_comment,
+
+ pub inline fn tokenize(comptime lexer: *Lexer) Lexer.Error!void {
+ comptime {
+ errdefer lexer.revert();
+
+ var token = lexer.start(.{ .whitespace = undefined });
+
+ switch (try lexer.consume()) {
+ ' ', '\t', '\r', '\n' => {
+ token.kind.whitespace = .space;
+
+ while (lexer.peekChar()) |ch| {
+ switch (ch) {
+ ' ', '\t', '\r', '\n' => try lexer.skip(),
+ else => break,
+ }
+ }
+ },
+ '/' => {
+ switch (lexer.peekChar() orelse 0) {
+ '*' => {
+ token.kind.whitespace = .block_comment;
+ try lexer.skipUntil("*/");
+ },
+ '/' => {
+ token.kind.whitespace = .line_comment;
+ lexer.skipUntil("\n") catch lexer.skipToEnd();
+ },
+ else => return Lexer.Error.UnexpectedToken,
+ }
+ },
+ else => return Lexer.Error.UnexpectedToken,
+ }
+
+ lexer.commit(token);
+ }
+ }
+};
+
+test "space" {
+ {
+ comptime var lexer: Lexer = .init(" ");
+ try Whitespace.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(1, lexer.tokens[0].slice.len);
+ }
+
+ {
+ comptime var lexer: Lexer = .init(" \t\t \n\r ");
+ try Whitespace.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqual(10, lexer.tokens[0].slice.len);
+ }
+}
+
+test "line-comment" {
+ {
+ comptime var lexer: Lexer = .init("// some comment");
+ try Whitespace.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqualStrings("// some comment", lexer.tokens[0].slice);
+ }
+
+ {
+ comptime var lexer: Lexer = .init("// some comment\n");
+ try Whitespace.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqualStrings("// some comment\n", lexer.tokens[0].slice);
+ }
+}
+
+test "block-comment" {
+ {
+ comptime var lexer: Lexer = .init("/* some comment */");
+ try Whitespace.tokenize(&lexer);
+ try std.testing.expectEqual(1, lexer.tokens.len);
+ try std.testing.expectEqualStrings("/* some comment */", lexer.tokens[0].slice);
+ }
+}
+
+test "mixed" {
+ comptime var lexer: Lexer = .init(
+ \\ // some comment
+ \\/* other comment here */ // comment or so
+ \\ /* multi
+ \\ * line
+ \\ * comment
+ \\ */
+ );
+ inline while (Whitespace.tokenize(&lexer)) {
+ } else |_| {}
+
+ try std.testing.expectEqual(7, lexer.tokens.len);
+}
diff --git a/src/z/parser/js/root.zig b/src/z/parser/js/root.zig
new file mode 100644
index 0000000..e604855
--- /dev/null
+++ b/src/z/parser/js/root.zig
@@ -0,0 +1,7 @@
+const std = @import("std");
+
+pub const grammar = @import("./grammar/root.zig");
+
+test {
+ _ = std.testing.refAllDecls(@This());
+}
diff --git a/src/z/parser/lexer.zig b/src/z/parser/lexer.zig
new file mode 100644
index 0000000..5beefde
--- /dev/null
+++ b/src/z/parser/lexer.zig
@@ -0,0 +1,127 @@
+const std = @import("std");
+
+pub fn Token(TokenKind: type) type {
+ return struct {
+ const Self = @This();
+
+ kind: TokenKind,
+ slice: []const u8,
+
+ pub fn start(comptime lexer: *const Lexer(TokenKind), kind: TokenKind) Self {
+ return .{
+ .kind = kind,
+ .slice = lexer.buffer,
+ };
+ }
+
+ pub inline fn stop(comptime self: Self, comptime lexer: *const Lexer(TokenKind)) Self {
+ comptime {
+ const length = self.slice.len - lexer.buffer.len;
+ return .{
+ .kind = self.kind,
+ .slice = self.slice[0..length],
+ };
+ }
+ }
+
+ pub fn format(self: *const Self, writer: *std.Io.Writer) !void {
+ try writer.print("Token{}(\"{s}\")", .{self.kind, self.slice});
+ }
+ };
+}
+
+pub fn Lexer(TokenKind: type) type {
+ return struct {
+ const Self = @This();
+
+ pub const Error = error{
+ UnexpectedToken,
+ EndOfBuffer,
+ };
+
+ buffer: []const u8,
+ last_buffer: []const u8,
+ tokens: []const Token(TokenKind) = &.{},
+ context: TokenKind.Context = .{},
+ last_context: TokenKind.Context = .{},
+
+ pub fn init(buffer: []const u8) Self {
+ return .{
+ .buffer = buffer,
+ .last_buffer = buffer,
+ };
+ }
+
+ pub inline fn peekChar(self: *Self) ?u8 {
+ return self.peekCharAt(0);
+ }
+
+ pub inline fn peekCharAt(self: *Self, n: usize) ?u8 {
+ return if (n < self.buffer.len) self.buffer[n] else null;
+ }
+
+ pub fn peekSlice(self: *Self, length: usize) ?[]const u8 {
+ return self.peekSliceAt(length, 0);
+ }
+
+ pub fn peekSliceAt(self: *Self, length: usize, offset: usize) ?[]const u8 {
+ if (length + offset <= self.buffer.len) {
+ return self.buffer[offset .. offset + length];
+ }
+ return null;
+ }
+
+ pub fn consume(self: *Self) !u8 {
+ return (try self.consumeTo(1))[0];
+ }
+
+ pub fn consumeTo(self: *Self, n: usize) ![]const u8 {
+ if (n <= self.buffer.len) {
+ const slice = self.buffer[0..n];
+ self.buffer = self.buffer[n..];
+ return slice;
+ } else {
+ return Error.EndOfBuffer;
+ }
+ }
+
+ pub fn skip(self: *Self) !void {
+ return self.skipTo(1);
+ }
+
+ pub fn skipTo(self: *Self, n: usize) Error!void {
+ if (n <= self.buffer.len) {
+ self.buffer = self.buffer[n..];
+ } else {
+ return Error.EndOfBuffer;
+ }
+ }
+
+ pub fn skipUntil(self: *Self, literal: []const u8) Error!void {
+ if (std.mem.find(u8, self.buffer, literal)) |index| {
+ self.buffer = self.buffer[index + literal.len..];
+ } else {
+ return Error.EndOfBuffer;
+ }
+ }
+
+ pub fn skipToEnd(self: *Self) void {
+ self.buffer = self.buffer[self.buffer.len..];
+ }
+
+ pub fn start(self: *Self, kind: TokenKind) Token(TokenKind) {
+ return .start(self, kind);
+ }
+
+ pub fn revert(self: *Self) void {
+ self.buffer = self.last_buffer;
+ self.context = self.last_context;
+ }
+
+ pub fn commit(self: *Self, tok: Token(TokenKind)) void {
+ self.tokens = self.tokens ++ [_]Token(TokenKind){tok.stop(self)};
+ self.last_buffer = self.buffer;
+ self.last_context = self.context;
+ }
+ };
+}
diff --git a/src/z/parser/root.zig b/src/z/parser/root.zig
new file mode 100644
index 0000000..e0b67fb
--- /dev/null
+++ b/src/z/parser/root.zig
@@ -0,0 +1,17 @@
+const std = @import("std");
+
+const lexer = @import("lexer.zig");
+pub const Lexer = lexer.Lexer;
+pub const Token = lexer.Token;
+
+pub const js = @import("js/root.zig");
+
+pub fn Parser(TokenType: type) type {
+ return struct {
+ buffer: []const TokenType,
+ };
+}
+
+test {
+ _ = std.testing.refAllDecls(@This());
+}
diff --git a/src/z/rest.js b/src/z/rest.js
new file mode 100644
index 0000000..c14944d
--- /dev/null
+++ b/src/z/rest.js
@@ -0,0 +1,20 @@
+
+const Rest = {
+ Request: class {
+ constructor(base) {
+ this.base = base;
+ }
+
+ get(url) { return fetch(url); }
+
+ post(url, body) {
+ return fetch(
+ url,
+ {
+ method: 'POST',
+ body: JSON.stringify(body),
+ }
+ );
+ }
+ },
+};
diff --git a/src/z/root.zig b/src/z/root.zig
index 26bbffc..1fa8d99 100644
--- a/src/z/root.zig
+++ b/src/z/root.zig
@@ -3,6 +3,7 @@ const std = @import("std");
pub const Minifier = @import("Minifier.zig");
pub const Component = @import("Component.zig");
pub const File = @import("File.zig");
+pub const parser = @import("parser/root.zig");
const Element = @import("html").Element;
@@ -25,7 +26,10 @@ pub const html: File = .{
};
pub fn env(comptime components: []const Component) Element {
- var children: []const Element = &.{script.load(@embedFile("z.js"))};
+ var children: []const Element = &.{
+ script.load(@embedFile("z.js")),
+ script.load(@embedFile("rest.js")),
+ };
inline for (components) |c| {
const child: []const Element = &.{c.toElement()};
diff --git a/src/z/z.js b/src/z/z.js
index ba07b9e..e279f42 100644
--- a/src/z/z.js
+++ b/src/z/z.js
@@ -1,10 +1,14 @@
class ZComponent extends HTMLElement {
+ static observedAttributes = [ 'type' ];
+
constructor() {
super();
this.attachShadow({ mode: 'open' });
+ this.onconnected = () => {};
+ this.ondisconnected = () => {};
}
- connectedCallback() {
+ build() {
const type = this.getAttribute('type');
if (type == null) {
@@ -22,6 +26,21 @@ class ZComponent extends HTMLElement {
Zenv.constructors[`component__${type}`](new Zenv(this));
}
+
+ attributeChangedCallback(name, previous, current) {
+ if (name == 'type') {
+ this.shadowRoot.innerHTML = '';
+ this.build()
+ }
+ }
+
+ connectedCallback() {
+ this.onconnected();
+ }
+
+ disconnectedCallback() {
+ this.ondisconnected();
+ }
}
class Zenv {