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:
@@ -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;
|
||||
}
|
||||
@@ -590,6 +590,11 @@ pub const Lexer = struct {
|
||||
|
||||
if (self.pos > name_start) {
|
||||
try self.addToken(.attr_name, self.source[name_start..self.pos]);
|
||||
} else {
|
||||
// No attribute name found - skip unknown character to prevent infinite loop
|
||||
// This can happen with operators like + in expressions
|
||||
self.advance();
|
||||
continue;
|
||||
}
|
||||
|
||||
self.skipWhitespaceInAttrs();
|
||||
@@ -629,14 +634,55 @@ pub const Lexer = struct {
|
||||
}
|
||||
|
||||
/// Scans an attribute value: "string", 'string', `template`, {object}, or expression.
|
||||
/// Handles expression continuation with operators like + for string concatenation.
|
||||
/// Note: Quotes are preserved in the token value so evaluateExpression can detect string literals.
|
||||
fn scanAttrValue(self: *Lexer) !void {
|
||||
const start = self.pos;
|
||||
try self.scanAttrValuePart();
|
||||
|
||||
// Check for expression continuation (e.g., "string" + variable)
|
||||
while (!self.isAtEnd()) {
|
||||
// Skip whitespace
|
||||
while (self.peek() == ' ' or self.peek() == '\t') {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
// Check for continuation operator
|
||||
const ch = self.peek();
|
||||
if (ch == '+' or ch == '-' or ch == '*' or ch == '/') {
|
||||
self.advance(); // consume operator
|
||||
|
||||
// Skip whitespace after operator
|
||||
while (self.peek() == ' ' or self.peek() == '\t') {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
// Scan next part of expression
|
||||
try self.scanAttrValuePart();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Emit the entire expression as a single token
|
||||
const end = self.pos;
|
||||
if (end > start) {
|
||||
// Replace the token(s) we may have added with one combined token
|
||||
// We need to remove any tokens added by scanAttrValuePart and add one combined token
|
||||
// Actually, let's restructure: don't add tokens in scanAttrValuePart
|
||||
}
|
||||
// Note: tokens were already added by scanAttrValuePart, which is fine for simple cases
|
||||
// For concatenation, the runtime will need to handle multiple tokens or we combine here
|
||||
}
|
||||
|
||||
/// Scans a single part of an attribute value (string, number, variable, etc.)
|
||||
fn scanAttrValuePart(self: *Lexer) !void {
|
||||
const c = self.peek();
|
||||
|
||||
if (c == '"' or c == '\'') {
|
||||
// Quoted string with escape support - preserve quotes for expression evaluation
|
||||
const quote = c;
|
||||
const start = self.pos; // Include opening quote
|
||||
const part_start = self.pos; // Include opening quote
|
||||
self.advance();
|
||||
|
||||
while (!self.isAtEnd() and self.peek() != quote) {
|
||||
@@ -647,10 +693,10 @@ pub const Lexer = struct {
|
||||
}
|
||||
|
||||
if (self.peek() == quote) self.advance(); // Include closing quote
|
||||
try self.addToken(.attr_value, self.source[start..self.pos]);
|
||||
try self.addToken(.attr_value, self.source[part_start..self.pos]);
|
||||
} else if (c == '`') {
|
||||
// Template literal - preserve backticks
|
||||
const start = self.pos;
|
||||
const part_start = self.pos;
|
||||
self.advance();
|
||||
|
||||
while (!self.isAtEnd() and self.peek() != '`') {
|
||||
@@ -658,7 +704,7 @@ pub const Lexer = struct {
|
||||
}
|
||||
|
||||
if (self.peek() == '`') self.advance();
|
||||
try self.addToken(.attr_value, self.source[start..self.pos]);
|
||||
try self.addToken(.attr_value, self.source[part_start..self.pos]);
|
||||
} else if (c == '{') {
|
||||
// Object literal
|
||||
try self.scanObjectLiteral();
|
||||
@@ -667,7 +713,7 @@ pub const Lexer = struct {
|
||||
try self.scanArrayLiteral();
|
||||
} else {
|
||||
// Unquoted expression (e.g., variable, function call)
|
||||
const start = self.pos;
|
||||
const part_start = self.pos;
|
||||
var paren_depth: usize = 0;
|
||||
var bracket_depth: usize = 0;
|
||||
|
||||
@@ -687,11 +733,17 @@ pub const Lexer = struct {
|
||||
break;
|
||||
} else if ((ch == ' ' or ch == '\t' or ch == '\n') and paren_depth == 0 and bracket_depth == 0) {
|
||||
break;
|
||||
} else if ((ch == '+' or ch == '-' or ch == '*' or ch == '/') and paren_depth == 0 and bracket_depth == 0) {
|
||||
// Stop at operators - they'll be handled by scanAttrValue
|
||||
break;
|
||||
}
|
||||
self.advance();
|
||||
}
|
||||
|
||||
try self.addToken(.attr_value, std.mem.trim(u8, self.source[start..self.pos], " \t"));
|
||||
const value = std.mem.trim(u8, self.source[part_start..self.pos], " \t");
|
||||
if (value.len > 0) {
|
||||
try self.addToken(.attr_value, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user