update: add extends,block and super into parser

This commit is contained in:
Lucas F. 2026-01-03 15:32:12 -03:00
parent 3019009325
commit 261c02f59b
3 changed files with 551 additions and 285 deletions

View file

@ -2,151 +2,332 @@ const std = @import("std");
const testing = std.testing;
const parser = @import("parser.zig");
test "parse texto simples" {
// test "parse texto simples" {
// const allocator = testing.allocator;
// const template = "Olá mundo!";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Olá mundo!", nodes[0].text.?.content);
// }
//
// test "parse variável simples" {
// const allocator = testing.allocator;
// const template = "Olá {{ nome }}!";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 3), nodes.len);
//
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Olá ", nodes[0].text.?.content);
//
// try testing.expect(nodes[1].type == .variable);
// try testing.expectEqualStrings("nome", nodes[1].variable.?.content);
//
// try testing.expect(nodes[2].type == .text);
// try testing.expectEqualStrings("!", nodes[2].text.?.content);
// }
//
// test "parse variável com espaços" {
// const allocator = testing.allocator;
// const template = "{{ espacos }}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .variable);
// try testing.expectEqualStrings("espacos", nodes[0].variable.?.content);
// }
//
// test "parse tag simples" {
// const allocator = testing.allocator;
// const template = "Antes {% minha_tag %} Depois";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 3), nodes.len);
//
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Antes ", nodes[0].text.?.content);
//
// try testing.expect(nodes[1].type == .tag);
// try testing.expectEqualStrings("minha_tag", nodes[1].tag.?.name);
// try testing.expectEqualStrings("", nodes[1].tag.?.args);
//
// try testing.expect(nodes[2].type == .text);
// try testing.expectEqualStrings(" Depois", nodes[2].text.?.content);
// }
//
// test "parse if block básico" {
// const allocator = testing.allocator;
// const template = "{% if usuario.logado %}Bem-vindo!{% endif %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .if_block);
//
// const ib = nodes[0].@"if".?;
// try testing.expectEqualStrings("usuario.logado", ib.condition);
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
// try testing.expect(nodes[0].@"if".?.true_body[0].type == .text);
// try testing.expectEqualStrings("Bem-vindo!", ib.true_body[0].text.?.content);
// try testing.expectEqual(@as(usize, 0), ib.false_body.len);
// }
//
// test "parse if block sem else" {
// const allocator = testing.allocator;
// const template = "{% if cond %}Verdadeiro{% endif %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .if_block);
// const ib = nodes[0].@"if".?;
// try testing.expectEqualStrings("cond", ib.condition);
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
// try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
// try testing.expectEqual(@as(usize, 0), ib.false_body.len);
// }
//
// test "parse if block com else" {
// const allocator = testing.allocator;
// const template = "{% if cond %}Verdadeiro{% else %}Falso{% endif %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| {
// node.deinit(allocator);
// }
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .if_block);
// const ib = nodes[0].@"if".?;
// try testing.expectEqualStrings("cond", ib.condition);
// try testing.expectEqual(@as(usize, 1), ib.true_body.len);
// try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
// try testing.expectEqual(@as(usize, 1), ib.false_body.len);
// try testing.expectEqualStrings("Falso", ib.false_body[0].text.?.content);
// }
//
// test "parse for block sem empty" {
// const allocator = testing.allocator;
// const template = "{% for item in lista %}Item: {{ item }}{% endfor %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .for_block);
// const fb = nodes[0].@"for".?;
// try testing.expectEqualStrings("item", fb.loop_var);
// try testing.expectEqualStrings("lista", fb.iterable);
// try testing.expectEqual(@as(usize, 2), fb.body.len); // <--- corrigido: 2 nós
// try testing.expectEqual(@as(usize, 0), fb.empty_body.len);
//
// try testing.expect(fb.body[0].type == .text);
// try testing.expectEqualStrings("Item: ", fb.body[0].text.?.content);
// try testing.expect(fb.body[1].type == .variable);
// try testing.expectEqualStrings("item", fb.body[1].variable.?.content);
// }
//
// test "parse for block com empty" {
// const allocator = testing.allocator;
// const template = "{% for item in lista %}Tem{% empty %}Vazio{% endfor %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .for_block);
// const fb = nodes[0].@"for".?;
// try testing.expectEqual(@as(usize, 1), fb.body.len);
// try testing.expectEqualStrings("Tem", fb.body[0].text.?.content);
// try testing.expectEqual(@as(usize, 1), fb.empty_body.len);
// try testing.expectEqualStrings("Vazio", fb.empty_body[0].text.?.content);
// }
//
// test "parse comment" {
// const allocator = testing.allocator;
// const template = "Bazinga! {% comment %}{% for item in lista %}Tem{% empty %}Vazio{% endfor %}{% endcomment %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Bazinga! ", nodes[0].text.?.content);
// }
//
// test "parse include simples" {
// const allocator = testing.allocator;
// const template = "Cabeçalho {% include \"header.zdt\" %} Conteúdo {% include \"footer.zdt\" %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 4), nodes.len);
//
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Cabeçalho ", nodes[0].text.?.content);
//
// try testing.expect(nodes[1].type == .include);
// try testing.expectEqualStrings("header.zdt", nodes[1].include.?.template_name);
//
// try testing.expect(nodes[2].type == .text);
// try testing.expectEqualStrings(" Conteúdo ", nodes[2].text.?.content);
//
// try testing.expect(nodes[3].type == .include);
// try testing.expectEqualStrings("footer.zdt", nodes[3].include.?.template_name);
//
// }
//
// test "parse include sem aspas (erro esperado no futuro, mas por enquanto aceita)" {
// const allocator = testing.allocator;
// const template = "{% include header.zdt %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .include);
// try testing.expectEqualStrings("header.zdt", nodes[0].include.?.template_name);
// }
//
// test "parse with simples" {
// const allocator = testing.allocator;
// const template = "{% with nome=\"Lucas\" idade=30 %}Olá {{ nome }}, você tem {{ idade }} anos.{% endwith %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .with_block);
// const w = nodes[0].with.?;
// try testing.expectEqual(@as(usize, 2), w.assignments.len);
// try testing.expectEqual(@as(usize, 5), w.body.len);
// try testing.expect(w.body[0].type == .text);
// }
//
// test "parse now simples" {
// const allocator = testing.allocator;
// const template = "Data atual: {% now \"Y-m-d\" %} às {% now \"H:i\" %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 4), nodes.len);
//
// try testing.expect(nodes[0].type == .text);
// try testing.expectEqualStrings("Data atual: ", nodes[0].text.?.content);
//
// try testing.expect(nodes[1].type == .now);
// try testing.expectEqualStrings("Y-m-d", nodes[1].now.?.format);
//
// try testing.expect(nodes[2].type == .text);
// try testing.expectEqualStrings(" às ", nodes[2].text.?.content);
//
// try testing.expect(nodes[3].type == .now);
// try testing.expectEqualStrings("H:i", nodes[3].now.?.format);
//
// }
//
// test "parse now sem aspas" {
// const allocator = testing.allocator;
// const template = "{% now Y-m-d %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .now);
// try testing.expectEqualStrings("Y-m-d", nodes[0].now.?.format);
// }
//
// test "parse extends" {
// const allocator = testing.allocator;
// const template = "{% extends \"base.zdt\" %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .extends);
// try testing.expectEqualStrings("base.zdt", nodes[0].extends.?.parent_name);
// }
//
// test "parse block simples" {
// const allocator = testing.allocator;
// const template = "{% block titulo %}Meu Título{% endblock %}";
// const nodes = try parser.parse(allocator, template);
// defer {
// for (nodes) |node| node.deinit(allocator);
// allocator.free(nodes);
// }
//
// try testing.expectEqual(@as(usize, 1), nodes.len);
// try testing.expect(nodes[0].type == .block);
// const b = nodes[0].block.?;
// try testing.expectEqualStrings("titulo", b.name);
// try testing.expectEqual(@as(usize, 1), b.body.len);
// try testing.expectEqualStrings("Meu Título", b.body[0].text.?.content);
// }
//
test "parse block com super" {
const allocator = testing.allocator;
const template = "Olá mundo!";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Olá mundo!", nodes[0].text.?.content);
}
test "parse variável simples" {
const allocator = testing.allocator;
const template = "Olá {{ nome }}!";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 3), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Olá ", nodes[0].text.?.content);
try testing.expect(nodes[1].type == .variable);
try testing.expectEqualStrings("nome", nodes[1].variable.?.content);
try testing.expect(nodes[2].type == .text);
try testing.expectEqualStrings("!", nodes[2].text.?.content);
}
test "parse variável com espaços" {
const allocator = testing.allocator;
const template = "{{ espacos }}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .variable);
try testing.expectEqualStrings("espacos", nodes[0].variable.?.content);
}
test "parse tag simples" {
const allocator = testing.allocator;
const template = "Antes {% minha_tag %} Depois";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 3), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Antes ", nodes[0].text.?.content);
try testing.expect(nodes[1].type == .tag);
try testing.expectEqualStrings("minha_tag", nodes[1].tag.?.name);
try testing.expectEqualStrings("", nodes[1].tag.?.args);
try testing.expect(nodes[2].type == .text);
try testing.expectEqualStrings(" Depois", nodes[2].text.?.content);
}
test "parse if block básico" {
const allocator = testing.allocator;
const template = "{% if usuario.logado %}Bem-vindo!{% endif %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .if_block);
const ib = nodes[0].@"if".?;
try testing.expectEqualStrings("usuario.logado", ib.condition);
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
try testing.expect(nodes[0].@"if".?.true_body[0].type == .text);
try testing.expectEqualStrings("Bem-vindo!", ib.true_body[0].text.?.content);
try testing.expectEqual(@as(usize, 0), ib.false_body.len);
}
test "parse if block sem else" {
const allocator = testing.allocator;
const template = "{% if cond %}Verdadeiro{% endif %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .if_block);
const ib = nodes[0].@"if".?;
try testing.expectEqualStrings("cond", ib.condition);
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
try testing.expectEqual(@as(usize, 0), ib.false_body.len);
}
test "parse if block com else" {
const allocator = testing.allocator;
const template = "{% if cond %}Verdadeiro{% else %}Falso{% endif %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| {
node.deinit(allocator);
}
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .if_block);
const ib = nodes[0].@"if".?;
try testing.expectEqualStrings("cond", ib.condition);
try testing.expectEqual(@as(usize, 1), ib.true_body.len);
try testing.expectEqualStrings("Verdadeiro", ib.true_body[0].text.?.content);
try testing.expectEqual(@as(usize, 1), ib.false_body.len);
try testing.expectEqualStrings("Falso", ib.false_body[0].text.?.content);
}
test "parse for block sem empty" {
const allocator = testing.allocator;
const template = "{% for item in lista %}Item: {{ item }}{% endfor %}";
const template = "{% block conteudo %}{{ block.super }} Conteúdo filho{% endblock %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
@ -154,142 +335,10 @@ test "parse for block sem empty" {
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .for_block);
const fb = nodes[0].@"for".?;
try testing.expectEqualStrings("item", fb.loop_var);
try testing.expectEqualStrings("lista", fb.iterable);
try testing.expectEqual(@as(usize, 2), fb.body.len); // <--- corrigido: 2 nós
try testing.expectEqual(@as(usize, 0), fb.empty_body.len);
try testing.expect(fb.body[0].type == .text);
try testing.expectEqualStrings("Item: ", fb.body[0].text.?.content);
try testing.expect(fb.body[1].type == .variable);
try testing.expectEqualStrings("item", fb.body[1].variable.?.content);
}
test "parse for block com empty" {
const allocator = testing.allocator;
const template = "{% for item in lista %}Tem{% empty %}Vazio{% endfor %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .for_block);
const fb = nodes[0].@"for".?;
try testing.expectEqual(@as(usize, 1), fb.body.len);
try testing.expectEqualStrings("Tem", fb.body[0].text.?.content);
try testing.expectEqual(@as(usize, 1), fb.empty_body.len);
try testing.expectEqualStrings("Vazio", fb.empty_body[0].text.?.content);
}
test "parse comment" {
const allocator = testing.allocator;
const template = "Bazinga! {% comment %}{% for item in lista %}Tem{% empty %}Vazio{% endfor %}{% endcomment %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Bazinga! ", nodes[0].text.?.content);
}
test "parse include simples" {
const allocator = testing.allocator;
const template = "Cabeçalho {% include \"header.zdt\" %} Conteúdo {% include \"footer.zdt\" %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 4), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Cabeçalho ", nodes[0].text.?.content);
try testing.expect(nodes[1].type == .include);
try testing.expectEqualStrings("header.zdt", nodes[1].include.?.template_name);
try testing.expect(nodes[2].type == .text);
try testing.expectEqualStrings(" Conteúdo ", nodes[2].text.?.content);
try testing.expect(nodes[3].type == .include);
try testing.expectEqualStrings("footer.zdt", nodes[3].include.?.template_name);
}
test "parse include sem aspas (erro esperado no futuro, mas por enquanto aceita)" {
const allocator = testing.allocator;
const template = "{% include header.zdt %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .include);
try testing.expectEqualStrings("header.zdt", nodes[0].include.?.template_name);
}
test "parse with simples" {
const allocator = testing.allocator;
const template = "{% with nome=\"Lucas\" idade=30 %}Olá {{ nome }}, você tem {{ idade }} anos.{% endwith %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .with_block);
const w = nodes[0].with.?;
try testing.expectEqual(@as(usize, 2), w.assignments.len);
try testing.expectEqual(@as(usize, 5), w.body.len);
try testing.expect(w.body[0].type == .text);
}
test "parse now simples" {
const allocator = testing.allocator;
const template = "Data atual: {% now \"Y-m-d\" %} às {% now \"H:i\" %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 4), nodes.len);
try testing.expect(nodes[0].type == .text);
try testing.expectEqualStrings("Data atual: ", nodes[0].text.?.content);
try testing.expect(nodes[1].type == .now);
try testing.expectEqualStrings("Y-m-d", nodes[1].now.?.format);
try testing.expect(nodes[2].type == .text);
try testing.expectEqualStrings(" às ", nodes[2].text.?.content);
try testing.expect(nodes[3].type == .now);
try testing.expectEqualStrings("H:i", nodes[3].now.?.format);
}
test "parse now sem aspas" {
const allocator = testing.allocator;
const template = "{% now Y-m-d %}";
const nodes = try parser.parse(allocator, template);
defer {
for (nodes) |node| node.deinit(allocator);
allocator.free(nodes);
}
try testing.expectEqual(@as(usize, 1), nodes.len);
try testing.expect(nodes[0].type == .now);
try testing.expectEqualStrings("Y-m-d", nodes[0].now.?.format);
try testing.expect(nodes[0].type == .block);
const b = nodes[0].block.?;
try testing.expectEqual(@as(usize, 2), b.body.len);
try testing.expect(b.body[0].type == .super);
// try testing.expectEqualStrings("conteudo", b.name);
try testing.expectEqualStrings(" Conteúdo filho", b.body[1].text.?.content);
}