fix: clone

This commit is contained in:
Lucas F. 2026-01-17 17:37:36 -03:00
parent f8e7b01e9c
commit f09689d539

View file

@ -435,310 +435,502 @@ pub const Node = struct {
}; };
}, },
.tag => { .tag => {
switch (self.tag.?.kind) {
.debug => return Node{ .type = .tag, .tag = .{
.kind = .debug,
.args = "",
.body = .{ .debug = true },
.raw = self.tag.?.raw,
} },
.firstof => {
const values_copy = try allocator.alloc([]const u8, self.tag.?.body.firstof.values.len);
errdefer allocator.free(values_copy);
for (self.tag.?.body.firstof.values, 0..) |f, i| {
values_copy[i] = try allocator.dupe(u8, f);
}
return Node{ .type = .tag, .tag = .{
.kind = .firstof,
.args = try allocator.dupe(u8, self.tag.?.args),
.body = .{ .firstof = .{ .values = values_copy } },
.raw = try allocator.dupe(u8, self.tag.?.raw),
} };
},
.if_block => {
const true_body_copy = try allocator.alloc(Node, self.tag.?.body.@"if".true_body.len);
errdefer allocator.free(true_body_copy);
for (self.tag.?.body.@"if".true_body, 0..) |child, j| {
true_body_copy[j] = try child.clone(allocator);
}
const false_body_copy = try allocator.alloc(Node, self.tag.?.body.@"if".false_body.len);
errdefer allocator.free(false_body_copy);
for (self.tag.?.body.@"if".false_body, 0..) |child, j| {
false_body_copy[j] = try child.clone(allocator);
}
return Node{ return Node{
.type = .tag, .type = .tag,
.tag = .{ .tag = .{
.kind = .if_block,
.args = try allocator.dupe(u8, self.tag.?.args), .args = try allocator.dupe(u8, self.tag.?.args),
.body = .{
.@"if" = .{
.condition = try allocator.dupe(u8, self.tag.?.body.@"if".condition),
.true_body = true_body_copy,
.false_body = false_body_copy,
.raw_open = try allocator.dupe(u8, self.tag.?.body.@"if".raw_open),
.raw_close = try allocator.dupe(u8, self.tag.?.body.@"if".raw_close),
},
},
.raw = try allocator.dupe(u8, self.tag.?.raw), .raw = try allocator.dupe(u8, self.tag.?.raw),
.body = try allocator.dupe(u8, self.tag.?.body),
}, },
}; };
}, },
.debug => return Node{ .type = .tag, .tag = .{ .debug = true } }, .for_block => {
// .firstof => { const body_copy = try allocator.alloc(Node, self.tag.?.body.@"for".body.len);
// const values_copy = try allocator.alloc([]const u8, self.firstof.?.values.len); errdefer allocator.free(body_copy);
// errdefer allocator.free(values_copy);
//
// for (self.firstof.?.values, 0..) |f, i| {
// values_copy[i] = try allocator.dupe(u8, f);
// }
//
// return Node{ .type = .firstof, .firstof = .{ .values = values_copy } };
// },
// .if_block => { for (self.tag.?.body.@"for".body, 0..) |child, j| {
// const true_body_copy = try allocator.alloc(Node, self.@"if".?.true_body.len); body_copy[j] = try child.clone(allocator);
// errdefer allocator.free(true_body_copy); }
//
// for (self.@"if".?.true_body, 0..) |child, j| { const empty_body_copy = try allocator.alloc(Node, self.tag.?.body.@"for".empty_body.len);
// true_body_copy[j] = try child.clone(allocator); errdefer allocator.free(empty_body_copy);
// }
// for (self.tag.?.body.@"for".empty_body, 0..) |child, j| {
// const false_body_copy = try allocator.alloc(Node, self.@"if".?.false_body.len); empty_body_copy[j] = try child.clone(allocator);
// errdefer allocator.free(false_body_copy); }
//
// for (self.@"if".?.false_body, 0..) |child, j| { return Node{
// false_body_copy[j] = try child.clone(allocator); .type = .tag,
// } .tag = .{
// .kind = .for_block,
// return Node{ .args = try allocator.dupe(u8, self.tag.?.args),
// .type = .if_block, .body = .{
// .@"if" = .{ .@"for" = .{
// .condition = try allocator.dupe(u8, self.@"if".?.condition), .loop_var = try allocator.dupe(u8, self.tag.?.body.@"for".loop_var),
// .true_body = true_body_copy, .iterable = try allocator.dupe(u8, self.tag.?.body.@"for".iterable),
// .false_body = false_body_copy, .body = body_copy,
// .raw_open = try allocator.dupe(u8, self.@"if".?.raw_open), .empty_body = empty_body_copy,
// .raw_close = try allocator.dupe(u8, self.@"if".?.raw_close), .raw_open = try allocator.dupe(u8, self.tag.?.body.@"for".raw_open),
// }, .raw_close = try allocator.dupe(u8, self.tag.?.body.@"for".raw_close),
// }; },
// }, },
// .raw = try allocator.dupe(u8, self.tag.?.raw),
// .for_block => { },
// const body_copy = try allocator.alloc(Node, self.@"for".?.body.len); };
// errdefer allocator.free(body_copy); },
// .block => {
// for (self.@"for".?.body, 0..) |child, j| { const body_copy = try allocator.alloc(Node, self.tag.?.body.block.body.len);
// body_copy[j] = try child.clone(allocator); errdefer allocator.free(body_copy);
// }
// for (self.tag.?.body.block.body, 0..) |child, j| {
// const empty_body_copy = try allocator.alloc(Node, self.@"for".?.empty_body.len); body_copy[j] = try child.clone(allocator);
// errdefer allocator.free(empty_body_copy); }
//
// for (self.@"for".?.empty_body, 0..) |child, j| { return Node{
// empty_body_copy[j] = try child.clone(allocator); .type = .tag,
// } .tag = .{
// .kind = .block,
// return Node{ .args = try allocator.dupe(u8, self.tag.?.args),
// .type = .for_block, .body = .{
// .@"for" = .{ .block = .{
// .loop_var = try allocator.dupe(u8, self.@"for".?.loop_var), .name = try allocator.dupe(u8, self.tag.?.body.block.name),
// .iterable = try allocator.dupe(u8, self.@"for".?.iterable), .body = body_copy,
// .body = body_copy, .raw_open = try allocator.dupe(u8, self.tag.?.body.block.raw_open),
// .empty_body = empty_body_copy, .raw_close = try allocator.dupe(u8, self.tag.?.body.block.raw_close),
// .raw_open = try allocator.dupe(u8, self.@"for".?.raw_open), },
// .raw_close = try allocator.dupe(u8, self.@"for".?.raw_close), },
// }, .raw = try allocator.dupe(u8, self.tag.?.raw),
// }; },
// }, };
// },
// .block => { .extends => {
// const body_copy = try allocator.alloc(Node, self.block.?.body.len); return Node{
// errdefer allocator.free(body_copy); .type = .tag,
// .tag = .{
// for (self.block.?.body, 0..) |child, j| { .kind = .extends,
// body_copy[j] = try child.clone(allocator); .args = try allocator.dupe(u8, self.tag.?.args),
// } .body = .{
// .extends = .{
// return Node{ .parent_name = try allocator.dupe(u8, self.tag.?.body.extends.parent_name),
// .type = .block, },
// .block = .{ },
// .name = try allocator.dupe(u8, self.block.?.name), .raw = try allocator.dupe(u8, self.tag.?.raw),
// .body = body_copy, },
// .raw_open = try allocator.dupe(u8, self.block.?.raw_open), };
// .raw_close = try allocator.dupe(u8, self.block.?.raw_close), },
// }, .super => {
// }; return Node{
// }, .type = .tag,
// .tag = .{
// .extends => { .kind = .super,
// return Node{ .args = try allocator.dupe(u8, self.tag.?.args),
// .type = .extends, .body = .{ .super = true },
// .extends = .{ .raw = try allocator.dupe(u8, self.tag.?.raw),
// .parent_name = try allocator.dupe(u8, self.extends.?.parent_name), },
// }, };
// }; },
// }, .include => {
// return Node{
// .super => { .type = .tag,
// return Node{ .tag = .{
// .type = .super, .kind = .include,
// }; .args = try allocator.dupe(u8, self.tag.?.args),
// }, .raw = try allocator.dupe(u8, self.tag.?.raw),
// .include => { .body = .{
// return Node{ .include = .{
// .type = .include, .template_path = try allocator.dupe(u8, self.tag.?.body.include.template_path),
// .include = .{ },
// .template_name = try allocator.dupe(u8, self.include.?.template_name), },
// }, },
// }; };
// }, },
// .with_block => { .with_block => {
// const body_copy = try allocator.alloc(Node, self.with.?.body.len); const body_copy = try allocator.alloc(Node, self.tag.?.body.with.body.len);
// errdefer allocator.free(body_copy); errdefer allocator.free(body_copy);
//
// for (self.with.?.body, 0..) |child, j| { for (self.tag.?.body.with.body, 0..) |child, j| {
// body_copy[j] = try child.clone(allocator); body_copy[j] = try child.clone(allocator);
// } }
//
// const assignments_copy = try allocator.alloc(Assignment, self.with.?.assignments.len); const assignments_copy = try allocator.alloc(Assignment, self.tag.?.body.with.assignments.len);
// errdefer allocator.free(assignments_copy); errdefer allocator.free(assignments_copy);
//
// for (self.with.?.assignments, 0..) |f, i| { for (self.tag.?.body.with.assignments, 0..) |f, i| {
// assignments_copy[i] = .{ assignments_copy[i] = .{
// .key = try allocator.dupe(u8, f.key), .key = try allocator.dupe(u8, f.key),
// .value_expr = try allocator.dupe(u8, f.value_expr), .value_expr = try allocator.dupe(u8, f.value_expr),
// .is_literal = f.is_literal, .is_literal = f.is_literal,
// }; };
// } }
//
// return Node{ return Node{
// .type = .with_block, .type = .tag,
// .with = .{ .tag = .{
// .assignments = assignments_copy, .kind = .with_block,
// .body = body_copy, .args = try allocator.dupe(u8, self.tag.?.args),
// .raw_open = try allocator.dupe(u8, self.with.?.raw_open), .raw = try allocator.dupe(u8, self.tag.?.raw),
// .raw_close = try allocator.dupe(u8, self.with.?.raw_close),
// }, .body = .{
// }; .with = .{
// }, .assignments = assignments_copy,
// .now => { .body = body_copy,
// return Node{ .type = .now, .now = .{ .format = try allocator.dupe(u8, self.now.?.format) } }; .raw_open = try allocator.dupe(u8, self.tag.?.body.with.raw_open),
// }, .raw_close = try allocator.dupe(u8, self.tag.?.body.with.raw_close),
// .filter_block => { },
// const body_copy = try allocator.alloc(Node, self.filter_block.?.body.len); },
// errdefer allocator.free(body_copy); },
// };
// for (self.filter_block.?.body, 0..) |child, j| { },
// body_copy[j] = try child.clone(allocator); .now => {
// } return Node{
// .type = .tag,
// return Node{ .tag = .{
// .type = .filter_block, .kind = .now,
// .filter_block = .{ .args = try allocator.dupe(u8, self.tag.?.args),
// .filters = try allocator.dupe(u8, self.filter_block.?.filters), .raw = try allocator.dupe(u8, self.tag.?.raw),
// .body = body_copy,
// .raw_open = try allocator.dupe(u8, self.filter_block.?.raw_open), .body = .{
// .raw_close = try allocator.dupe(u8, self.filter_block.?.raw_close), .now = .{
// }, .format = try allocator.dupe(u8, self.tag.?.body.now.format),
// }; },
// }, },
// .autoescape => { },
// const body_copy = try allocator.alloc(Node, self.autoescape.?.body.len); };
// errdefer allocator.free(body_copy); },
// .autoescape => {
// for (self.autoescape.?.body, 0..) |child, j| { const body_copy = try allocator.alloc(Node, self.tag.?.body.autoescape.body.len);
// body_copy[j] = try child.clone(allocator); errdefer allocator.free(body_copy);
// }
// for (self.tag.?.body.autoescape.body, 0..) |child, j| {
// return Node{ body_copy[j] = try child.clone(allocator);
// .type = .autoescape, }
// .autoescape = .{
// .body = body_copy, return Node{
// .raw_open = try allocator.dupe(u8, self.autoescape.?.raw_open), .type = .tag,
// .raw_close = try allocator.dupe(u8, self.autoescape.?.raw_close), .tag = .{
// .enabled = self.autoescape.?.enabled, .kind = .autoescape,
// }, .args = try allocator.dupe(u8, self.tag.?.args),
// }; .raw = try allocator.dupe(u8, self.tag.?.raw),
// },
// .spaceless => { .body = .{
// const body_copy = try allocator.alloc(Node, self.spaceless.?.body.len); .autoescape = .{
// errdefer allocator.free(body_copy); .body = body_copy,
// .raw_open = try allocator.dupe(u8, self.tag.?.body.autoescape.raw_open),
// for (self.spaceless.?.body, 0..) |child, j| { .raw_close = try allocator.dupe(u8, self.tag.?.body.autoescape.raw_close),
// body_copy[j] = try child.clone(allocator); .enabled = self.tag.?.body.autoescape.enabled,
// } },
// },
// return Node{ },
// .type = .spaceless, };
// .spaceless = .{ },
// .body = body_copy, .spaceless => {
// .raw_open = try allocator.dupe(u8, self.spaceless.?.raw_open), const body_copy = try allocator.alloc(Node, self.tag.?.body.spaceless.body.len);
// .raw_close = try allocator.dupe(u8, self.spaceless.?.raw_close), errdefer allocator.free(body_copy);
// },
// }; for (self.tag.?.body.spaceless.body, 0..) |child, j| {
// }, body_copy[j] = try child.clone(allocator);
// .url => { }
// const args_copy = try allocator.alloc([]const u8, self.url.?.args.len);
// errdefer allocator.free(args_copy); return Node{
// .type = .tag,
// for (self.url.?.args, 0..) |f, i| { .tag = .{
// args_copy[i] = try allocator.dupe(u8, f); .kind = .spaceless,
// } .args = try allocator.dupe(u8, self.tag.?.args),
// .raw = try allocator.dupe(u8, self.tag.?.raw),
// return Node{ .type = .url, .url = .{
// .name = try allocator.dupe(u8, self.url.?.name), .body = .{
// .args = args_copy, .spaceless = .{
// } }; .body = body_copy,
// }, .raw_open = try allocator.dupe(u8, self.tag.?.body.spaceless.raw_open),
// .cycle => { .raw_close = try allocator.dupe(u8, self.tag.?.body.spaceless.raw_close),
// const values_copy = try allocator.alloc([]const u8, self.cycle.?.values.len); },
// errdefer allocator.free(values_copy); },
// },
// for (self.cycle.?.values, 0..) |f, i| { };
// values_copy[i] = try allocator.dupe(u8, f); },
// } .url => {
// const args_copy = try allocator.alloc([]const u8, self.tag.?.body.url.args.len);
// return Node{ .type = .cycle, .cycle = .{ .values = values_copy } }; errdefer allocator.free(args_copy);
// },
// .load => { for (self.tag.?.body.url.args, 0..) |f, i| {
// const libraries_copy = try allocator.alloc([]const u8, self.load.?.libraries.len); args_copy[i] = try allocator.dupe(u8, f);
// errdefer allocator.free(libraries_copy); }
//
// for (self.load.?.libraries, 0..) |f, i| { return Node{
// libraries_copy[i] = try allocator.dupe(u8, f); .type = .tag,
// } .tag = .{
// .kind = .url,
// return Node{ .type = .load, .load = .{ .libraries = libraries_copy } }; .args = try allocator.dupe(u8, self.tag.?.args),
// }, .raw = try allocator.dupe(u8, self.tag.?.raw),
// .csrf_token => {
// return Node{ .body = .{
// .type = .csrf_token, .url = .{
// }; .name = try allocator.dupe(u8, self.tag.?.body.url.name),
// }, .args = args_copy,
// .lorem => { },
// return Node{ },
// .type = .lorem, },
// .lorem = .{ };
// .count = if (self.lorem.?.count) |c| try allocator.dupe(u8, c) else null, },
// .method = if (self.lorem.?.method) |m| try allocator.dupe(u8, m) else null, .cycle => {
// .format = if (self.lorem.?.format) |f| try allocator.dupe(u8, f) else null, const values_copy = try allocator.alloc([]const u8, self.tag.?.body.cycle.values.len);
// }, errdefer allocator.free(values_copy);
// };
// }, for (self.tag.?.body.cycle.values, 0..) |f, i| {
// .partialdef => { values_copy[i] = try allocator.dupe(u8, f);
// const body_copy = try allocator.alloc(Node, self.partialdef.?.body.len); }
// errdefer allocator.free(body_copy);
// return Node{
// for (self.partialdef.?.body, 0..) |child, j| { .type = .tag,
// body_copy[j] = try child.clone(allocator); .tag = .{
// } .kind = .cycle,
// .args = try allocator.dupe(u8, self.tag.?.args),
// return Node{ .type = .partialdef, .partialdef = .{ .raw = try allocator.dupe(u8, self.tag.?.raw),
// .name = try allocator.dupe(u8, self.partialdef.?.name),
// .body = body_copy, .body = .{
// .raw_open = try allocator.dupe(u8, self.partialdef.?.raw_open), .cycle = .{
// .raw_close = try allocator.dupe(u8, self.partialdef.?.raw_close), .values = values_copy,
// } }; },
// }, },
// .partial => { },
// return Node{ .type = .partial, .partial = .{ };
// .name = try allocator.dupe(u8, self.partial.?.name), },
// } }; .load => {
// }, const libraries_copy = try allocator.alloc([]const u8, self.tag.?.body.load.libraries.len);
// .querystring => { errdefer allocator.free(libraries_copy);
// const modifications = try allocator.alloc([]const u8, self.querystring.?.modifications.len);
// errdefer allocator.free(modifications); for (self.tag.?.body.load.libraries, 0..) |f, i| {
// libraries_copy[i] = try allocator.dupe(u8, f);
// for (self.querystring.?.modifications, 0..) |f, i| { }
// modifications[i] = try allocator.dupe(u8, f);
// } return Node{
// .type = .tag,
// return Node{ .type = .querystring, .querystring = .{ .tag = .{
// .modifications = modifications, .kind = .load,
// } }; .args = try allocator.dupe(u8, self.tag.?.args),
// }, .raw = try allocator.dupe(u8, self.tag.?.raw),
// .regroup => {
// return Node{ .type = .regroup, .regroup = .{ .body = .{
// .source = try allocator.dupe(u8, self.regroup.?.source), .load = .{
// .by = try allocator.dupe(u8, self.regroup.?.by), .libraries = libraries_copy,
// .as_var = try allocator.dupe(u8, self.regroup.?.as_var), },
// } }; },
// }, },
// .resetcycle => { };
// return Node{ .type = .resetcycle, .resetcycle = .{ },
// .cycle_name = if (self.resetcycle.?.cycle_name) |n| try allocator.dupe(u8, n) else null, .lorem => {
// } }; return Node{
// }, .type = .tag,
// .widthratio => { .tag = .{
// return Node{ .type = .widthratio, .widthratio = .{ .kind = .lorem,
// .value = try allocator.dupe(u8, self.widthratio.?.value), .args = try allocator.dupe(u8, self.tag.?.args),
// .max_value = try allocator.dupe(u8, self.widthratio.?.max_value), .raw = try allocator.dupe(u8, self.tag.?.raw),
// .divisor = if (self.widthratio.?.divisor) |d| try allocator.dupe(u8, d) else null,
// } }; .body = .{
// }, .lorem = .{
// .templatetag => { .count = if (self.tag.?.body.lorem.count) |c| try allocator.dupe(u8, c) else null,
// return Node{ .type = .templatetag, .templatetag = .{ .kind = self.templatetag.?.kind } }; .method = if (self.tag.?.body.lorem.method) |m| try allocator.dupe(u8, m) else null,
// }, .format = if (self.tag.?.body.lorem.format) |f| try allocator.dupe(u8, f) else null,
},
},
},
};
},
.partialdef => {
const body_copy = try allocator.alloc(Node, self.tag.?.body.partialdef.body.len);
errdefer allocator.free(body_copy);
for (self.tag.?.body.partialdef.body, 0..) |child, j| {
body_copy[j] = try child.clone(allocator);
}
return Node{
.type = .tag,
.tag = .{
.kind = .partialdef,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.partialdef = .{
.name = try allocator.dupe(u8, self.tag.?.body.partialdef.name),
.body = body_copy,
.raw_open = try allocator.dupe(u8, self.tag.?.body.partialdef.raw_open),
.raw_close = try allocator.dupe(u8, self.tag.?.body.partialdef.raw_close),
},
},
},
};
},
.partial => {
return Node{
.type = .tag,
.tag = .{
.kind = .partial,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.partial = .{
.name = try allocator.dupe(u8, self.tag.?.body.partial.name),
},
},
},
};
},
.querystring => {
const modifications_copy = try allocator.alloc([]const u8, self.tag.?.body.querystring.modifications.len);
errdefer allocator.free(modifications_copy);
for (self.tag.?.body.querystring.modifications, 0..) |f, i| {
modifications_copy[i] = try allocator.dupe(u8, f);
}
return Node{
.type = .tag,
.tag = .{
.kind = .querystring,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.querystring = .{
.modifications = modifications_copy,
},
},
},
};
},
.regroup => {
return Node{
.type = .tag,
.tag = .{
.kind = .regroup,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.regroup = .{
.source = try allocator.dupe(u8, self.tag.?.body.regroup.source),
.by = try allocator.dupe(u8, self.tag.?.body.regroup.by),
.as_var = try allocator.dupe(u8, self.tag.?.body.regroup.as_var),
},
},
},
};
},
.resetcycle => {
return Node{
.type = .tag,
.tag = .{
.kind = .resetcycle,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.resetcycle = .{
.cycle_name = if (self.tag.?.body.resetcycle.cycle_name) |n| try allocator.dupe(u8, n) else null,
},
},
},
};
},
.widthratio => {
return Node{
.type = .tag,
.tag = .{
.kind = .widthratio,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.widthratio = .{
.value = try allocator.dupe(u8, self.tag.?.body.widthratio.value),
.max_value = try allocator.dupe(u8, self.tag.?.body.widthratio.max_value),
.divisor = if (self.tag.?.body.widthratio.divisor) |d| try allocator.dupe(u8, d) else null,
},
},
},
};
},
.templatetag => {
return Node{
.type = .tag,
.tag = .{
.kind = .templatetag,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.templatetag = .{
.kind = self.tag.?.body.templatetag.kind,
},
},
},
};
},
.csrf_token => {
return Node{
.type = .tag,
.tag = .{
.kind = .csrf_token,
.args = try allocator.dupe(u8, self.tag.?.args),
.raw = try allocator.dupe(u8, self.tag.?.raw),
.body = .{
.csrf_token = true,
},
},
};
},
else => unreachable,
}
},
} }
} }
}; };