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