Fix infinite loop in lexer when parsing attribute expressions with operators

The lexer would hang when encountering operators like + in attribute values
(e.g., class="btn btn-" + type). Added scanAttrValuePart to handle
expression continuation with operators (+, -, *, /).

Also cleaned up debug prints from view_engine.zig and demo/main.zig.
This commit is contained in:
2026-01-17 19:26:24 +05:30
parent 4538b17f0a
commit 8878b630cb
13 changed files with 149 additions and 95 deletions

View File

@@ -19,104 +19,34 @@ const Allocator = std.mem.Allocator;
/// Application state shared across all requests
const App = struct {
allocator: Allocator,
engine: pugz.ViewEngine,
view: pugz.ViewEngine,
pub fn init(allocator: Allocator) !App {
return .{
.allocator = allocator,
.engine = try pugz.ViewEngine.init(allocator, .{
.views_dir = "src/examples/app_01/views",
.view = try pugz.ViewEngine.init(allocator, .{
.views_dir = "src/examples/demo/views",
}),
};
}
pub fn deinit(self: *App) void {
self.engine.deinit();
self.view.deinit();
}
};
/// Handler for GET /
fn index(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.engine.render(app.allocator, "layout", .{
.title = "Home",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /page-a - demonstrates extends and block override
fn pageA(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.engine.render(app.allocator, "page-a", .{
.title = "Page A - Pets",
.items = &[_][]const u8{ "A", "B", "C" },
.n = 0,
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /page-b - demonstrates sub-layout inheritance
fn pageB(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.engine.render(app.allocator, "page-b", .{
.title = "Page B - Sub Layout",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /append - demonstrates block append
fn pageAppend(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.engine.render(app.allocator, "page-append", .{
.title = "Page Append",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /append-opt - demonstrates optional block keyword
fn pageAppendOptional(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.engine.render(app.allocator, "page-appen-optional-blk", .{
.title = "Page Append Optional",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
defer if (gpa.deinit() == .leak) @panic("leak");
const allocator = gpa.allocator();
// Initialize view engine once at startup
var app = try App.init(allocator);
defer app.deinit();
var server = try httpz.Server(*App).init(allocator, .{ .port = 8080 }, &app);
const port = 8080;
var server = try httpz.Server(*App).init(allocator, .{ .port = port }, &app);
defer server.deinit();
var router = try server.router(.{});
@@ -132,7 +62,7 @@ pub fn main() !void {
\\
\\Pugz Template Inheritance Demo
\\==============================
\\Server running at http://localhost:8080
\\Server running at http://localhost:{d}
\\
\\Routes:
\\ GET / - Home page (base layout)
@@ -143,7 +73,79 @@ pub fn main() !void {
\\
\\Press Ctrl+C to stop.
\\
, .{});
, .{port});
try server.listen();
}
/// Handler for GET /
fn index(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(app.allocator, "layout", .{
.title = "Home",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /page-a - demonstrates extends and block override
fn pageA(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(app.allocator, "page-a", .{
.title = "Page A - Pets",
.items = &[_][]const u8{ "A", "B", "C" },
.n = 0,
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /page-b - demonstrates sub-layout inheritance
fn pageB(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(app.allocator, "page-b", .{
.title = "Page B - Sub Layout",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /append - demonstrates block append
fn pageAppend(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(app.allocator, "page-append", .{
.title = "Page Append",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}
/// Handler for GET /append-opt - demonstrates optional block keyword
fn pageAppendOptional(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
const html = app.view.render(app.allocator, "page-appen-optional-blk", .{
.title = "Page Append Optional",
}) catch |err| {
res.status = 500;
res.body = @errorName(err);
return;
};
res.content_type = .HTML;
res.body = html;
}