Skip to content

Commit

Permalink
wip implementation of ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-it committed Aug 17, 2024
1 parent 0fef38e commit b601603
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 80 deletions.
117 changes: 117 additions & 0 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Error = struct {
missing_template_value,
unexpected_extend,
unscripted_var,
unscripted_ctx,
duplicate_block: Span,
},

Expand Down Expand Up @@ -131,6 +132,14 @@ pub const Node = struct {
return node.varAttr().attr.value.?;
}

pub fn ctxAttr(node: Node) ScriptedAttr {
std.debug.assert(node.kind.output() == .ctx);
return node.var_ctx;
}
pub fn ctxValue(node: Node) html.Tokenizer.Attr.Value {
return node.ctxAttr().attr.value.?;
}

pub fn debugName(node: Node, src: []const u8) []const u8 {
return node.elem.startTag().name().string(src);
}
Expand Down Expand Up @@ -1129,6 +1138,114 @@ const Parser = struct {
continue;
}

//ctx
if (is(name_string, "ctx")) {
tmp_result.kind = switch (tmp_result.kind) {
.block => .block_ctx,
.block_if => .block_if_ctx,
.block_loop => .block_loop_ctx,
.element => .element_ctx,
.element_if => .element_if_ctx,
.element_else => .element_else_ctx,
.element_loop => .element_loop_ctx,
.element_inloop => .element_inloop_ctx,
.element_id => .element_id_ctx,
.element_id_if => .element_id_if_ctx,
.element_id_else => .element_id_else_ctx,
.element_id_loop => .element_id_loop_ctx,
.element_id_inloop => .element_id_inloop_ctx,

.block_var,
.block_if_var,
.block_loop_var,
.element_var,
.element_if_var,
.element_else_var,
.element_loop_var,
.element_inloop_var,
.element_id_var,
.element_id_if_var,
.element_id_else_var,
.element_id_loop_var,
.element_id_inloop_var,
=> {
@panic("TODO: explain that a tag combination is wrong");
},

.root,
.extend,
.super,
// never discorvered yet
.super_block,
.super_block_ctx,
=> unreachable,

// duplicate attr
.block_ctx,
.block_if_ctx,
.block_loop_ctx,
.element_ctx,
.element_if_ctx,
.element_else_ctx,
.element_loop_ctx,
.element_inloop_ctx,
.element_id_ctx,
.element_id_if_ctx,
.element_id_else_ctx,
.element_id_loop_ctx,
.element_id_inloop_ctx,
=> tmp_result.kind, // do nothing
};

//TODO: implement unescape
// const code = try value.unescape(gpa, p.src);
const code = if (attr.value) |v|
v.span.slice(p.src)
else blk: {

// return p.reportError(
// name,
// "var_no_value",
// "VAR MISSING VALUE",
// \\A `var` attribute requires a value that scripts what
// \\to put in the relative element's body.
// ,
// );
try p.errors.append(gpa, .{
.kind = .missing_attribute_value,
.main_location = name,
});
break :blk "";
};

// TODO: typecheck the expression
if (code.len > 0 and
std.mem.indexOfScalar(u8, code, '$') == null)
{
try p.errors.append(gpa, .{
.kind = .unscripted_ctx,
.main_location = name,
});

// return p.reportError(
// name,
// "unscripted_var",
// "UNSCRIPTED VAR",
// \\A `var` attribute requires a value that scripts what
// \\to put in the relative element's body.
// ,
// );
}
tmp_result.var_ctx = .{
.attr = attr,
.code = .{
.slice = code,
},
};

continue;
}

// if
if (is(name_string, "if")) {
tmp_result.kind = switch (tmp_result.kind) {
Expand Down
2 changes: 1 addition & 1 deletion src/errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn diagnostic(
const start_trim_left = line_off.start + line_off.line.len - line_trim_left.len;

const caret_len = span.end - span.start;
const caret_spaces_len = span.start - start_trim_left;
const caret_spaces_len = span.start -| start_trim_left;

const line_trim = std.mem.trimRight(u8, line_trim_left, &std.ascii.whitespace);

Expand Down
27 changes: 12 additions & 15 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@ pub const VM = vm.VM;
pub const Exception = vm.Exception;

pub const utils = struct {
pub fn ResourceDescriptor(comptime UserResourceDescriptor: type) type {
return union(enum) {
@"if": u32,
loop: u32,
user: UserResourceDescriptor,
};
}
pub fn loopUpFunction(comptime Value: type, comptime ResourceDesc: type) fn (
// TODO: iter element should be defined by us
pub fn loopUpFunction(comptime Value: type, comptime Template: type) fn (
Value.IterElement,
std.mem.Allocator,
[]const Value,
*ResourceDesc,
) error{ OutOfMemory, WantResource }!Value {
) error{ OutOfMemory, Interrupt }!Value {
return struct {
pub fn up(
v: Value.IterElement,
_: std.mem.Allocator,
args: []const Value,
ext: *ResourceDesc,
) error{ OutOfMemory, WantResource }!Value {
if (args.len != 0) return .{ .err = "'up' wants 0 arguments" };
ext.* = .{ .loop = v._up_idx };
return error.WantResource;
) error{ OutOfMemory, Interrupt }!Value {
if (args.len != 0) return .{
.err = "expected 0 arguments",
};

return Template.loopUp(
v._up_tpl,
v._up_idx,
);
}
}.up;
}
Expand Down
Loading

0 comments on commit b601603

Please sign in to comment.